Path_UNIX.cpp
Go to the documentation of this file.
00001 //
00002 // Path_UNIX.cpp
00003 //
00004 // $Id: //poco/1.3/Foundation/src/Path_UNIX.cpp#2 $
00005 //
00006 // Library: Foundation
00007 // Package: Filesystem
00008 // Module:  Path
00009 //
00010 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
00011 // and Contributors.
00012 //
00013 // Permission is hereby granted, free of charge, to any person or organization
00014 // obtaining a copy of the software and accompanying documentation covered by
00015 // this license (the "Software") to use, reproduce, display, distribute,
00016 // execute, and transmit the Software, and to prepare derivative works of the
00017 // Software, and to permit third-parties to whom the Software is furnished to
00018 // do so, all subject to the following:
00019 // 
00020 // The copyright notices in the Software and this entire statement, including
00021 // the above license grant, this restriction and the following disclaimer,
00022 // must be included in all copies of the Software, in whole or in part, and
00023 // all derivative works of the Software, unless such copies or derivative
00024 // works are solely in the form of machine-executable object code generated by
00025 // a source language processor.
00026 // 
00027 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00028 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00029 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
00030 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
00031 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
00032 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00033 // DEALINGS IN THE SOFTWARE.
00034 //
00035 
00036 
00037 #include "Poco/Path_UNIX.h"
00038 #include "Poco/Exception.h"
00039 #include "Poco/Environment_UNIX.h"
00040 #include <unistd.h>
00041 #include <stdlib.h>
00042 #include <sys/types.h>
00043 #include <pwd.h>
00044 #include <cctype>
00045 #include <climits>
00046 
00047 
00048 #ifndef PATH_MAX
00049 #define PATH_MAX 1024 // fallback
00050 #endif
00051 
00052 
00053 namespace Poco {
00054 
00055 
00056 std::string PathImpl::currentImpl()
00057 {
00058         std::string path;
00059         char cwd[PATH_MAX];
00060         if (getcwd(cwd, sizeof(cwd)))
00061                 path = cwd;
00062         else
00063                 throw SystemException("cannot get current directory");
00064         std::string::size_type n = path.size();
00065         if (n > 0 && path[n - 1] != '/') path.append("/");
00066         return path;
00067 }
00068 
00069 
00070 std::string PathImpl::homeImpl()
00071 {
00072         std::string path;
00073         struct passwd* pwd = getpwuid(getuid());
00074         if (pwd)
00075                 path = pwd->pw_dir;
00076         else
00077         {
00078                 pwd = getpwuid(geteuid());
00079                 if (pwd)
00080                         path = pwd->pw_dir;
00081                 else
00082                         path = EnvironmentImpl::getImpl("HOME");
00083         }
00084         std::string::size_type n = path.size();
00085         if (n > 0 && path[n - 1] != '/') path.append("/");
00086         return path;
00087 }
00088 
00089 
00090 std::string PathImpl::tempImpl()
00091 {
00092         std::string path;
00093         char* tmp = getenv("TMPDIR");
00094         if (tmp)
00095         {
00096                 path = tmp;
00097                 std::string::size_type n = path.size();
00098                 if (n > 0 && path[n - 1] != '/') path.append("/");
00099         }
00100         else
00101         {
00102                 path = "/tmp/";
00103         }
00104         return path;
00105 }
00106 
00107 
00108 std::string PathImpl::nullImpl()
00109 {
00110         return "/dev/null";
00111 }
00112 
00113 
00114 std::string PathImpl::expandImpl(const std::string& path)
00115 {
00116         std::string result;
00117         std::string::const_iterator it  = path.begin();
00118         std::string::const_iterator end = path.end();
00119         if (it != end && *it == '~')
00120         {
00121                 ++it;
00122                 if (it != end && *it == '/')
00123                 {
00124                         result += homeImpl(); ++it;
00125                 }
00126                 else result += '~';
00127         }
00128         while (it != end)
00129         {
00130                 if (*it == '$')
00131                 {
00132                         std::string var;
00133                         ++it;
00134                         if (it != end && *it == '{')
00135                         {
00136                                 ++it;
00137                                 while (it != end && *it != '}') var += *it++;
00138                                 if (it != end) ++it;
00139                         }
00140                         else
00141                         {
00142                                 while (it != end && (std::isalnum(*it) || *it == '_')) var += *it++;
00143                         }
00144                         char* val = getenv(var.c_str());
00145                         if (val) result += val;
00146                 }
00147                 else result += *it++;
00148         }
00149         return result;
00150 }
00151 
00152 
00153 void PathImpl::listRootsImpl(std::vector<std::string>& roots)
00154 {
00155         roots.clear();
00156         roots.push_back("/");
00157 }
00158 
00159 
00160 } // namespace Poco


pluginlib
Author(s): Tully Foote and Eitan Marder-Eppstein
autogenerated on Sat Dec 28 2013 17:20:19