NewNet/nnpath.cpp

00001 /*  NewNet - A networking framework in C++
00002     Copyright (C) 2006 Ingmar K. Steen (iksteen@gmail.com)
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation; either version 2 of the License, or
00007     (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018  */
00019 
00020 #include "nnpath.h"
00021 #include <assert.h>
00022 #include "platform.h"
00023 
00024 NewNet::Path::Path(const std::vector<std::string> & path)
00025 {
00026   if(path.empty())
00027     return;
00028 
00029   std::vector<std::string>::const_iterator it, begin = path.begin(), end = path.end();
00030   for(it = begin; it != end; ++it)
00031   {
00032     if(it != begin)
00033       m_Path += seperator();
00034     m_Path += (*it);
00035   }
00036 }
00037 
00038 bool
00039 NewNet::Path::isAbsolute() const
00040 {
00041 #ifndef WIN32
00042   return ((! m_Path.empty()) && (m_Path[0] == '/'));
00043 #else
00044   return ((m_Path.length() >= 3) && (m_Path.substr(1, 2) == ":\\"));
00045 #endif // ! WIN32
00046 }
00047 
00048 std::vector<std::string>
00049 NewNet::Path::split() const
00050 {
00051   std::vector<std::string> ret;
00052   if(m_Path.empty())
00053     return ret;
00054 
00055   std::string path(m_Path);
00056   std::string::size_type ix = path.find(seperator());
00057   while(ix != std::string::npos)
00058   {
00059     ret.push_back(path.substr(0, ix));
00060     path = path.substr(ix + 1);
00061     ix = path.find(seperator());
00062   }
00063   ret.push_back(path);
00064   return ret;
00065 }
00066 
00067 NewNet::Path
00068 NewNet::Path::simplified() const
00069 {
00070   std::string::size_type minElements = isAbsolute() ? 1 : 0;
00071   std::vector<std::string> s(split()), r;
00072   std::vector<std::string>::iterator it, begin = s.begin(), end = s.end();
00073   for(it = begin; it != end; ++it)
00074   {
00075     if(((*it) == ".") || ((it != begin) && (*it).empty()))
00076       continue;
00077     else if((*it) == "..")
00078     {
00079       if(r.size() > minElements)
00080         r.pop_back();
00081       else if(minElements == 0)
00082         r.push_back(*it);
00083     }
00084     else
00085       r.push_back(*it);
00086   }
00087   return Path(r);
00088 }
00089 
00090 NewNet::Path
00091 NewNet::Path::absolute(const std::string & base_) const
00092 {
00093   if(isAbsolute())
00094     return *this;
00095 
00096   Path base(base_);
00097   if(base.path().empty())
00098     base = currentDir();
00099   else if(! base.isAbsolute())
00100     base = base.absolute();
00101 
00102   if(base.path().empty() || (base.path()[base.path().length() - 1] != seperator()))
00103     return Path(base.path() + seperator() + m_Path);
00104   else
00105     return Path(base.path() + m_Path);
00106 }
00107 
00108 NewNet::Path
00109 NewNet::Path::currentDir()
00110 {
00111   std::string path;
00112   char * buf = new char[PATH_MAX];
00113   assert(getcwd(buf, PATH_MAX) != 0);
00114   path = buf;
00115   delete [] buf;
00116   return Path(path);
00117 }

Generated on Sun Jan 7 14:00:01 2007 for NewNet by  doxygen 1.5.1