00001 #if 0
00002
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "nel/misc/path.h"
00029 #include "nel/misc/debug.h"
00030
00031 #include <fstream>
00032
00033 using namespace std;
00034
00035
00036
00037
00038
00039 #ifdef NL_DEBUG_PATH
00040 #define NL_DISPLAY_PATH(_x_) nlinfo("Path: %s", _x_.c_str())
00041 #else
00042 #define NL_DISPLAY_PATH(_x_) NULL
00043 #endif
00044
00045
00046 namespace NLMISC {
00047
00048
00049 CStringVector CPath::_SearchPaths;
00050
00051
00052
00053
00054
00055
00056 void CPath::addSearchPath( const string& path )
00057 {
00058 if ( path == "" )
00059 {
00060 return;
00061 }
00062 string s = path;
00063 const char slash = '/';
00064
00065
00066 if ( path[path.size()-1] != slash )
00067 {
00068 s += slash;
00069 }
00070
00071
00072 _SearchPaths.push_back( s );
00073 }
00074
00075
00076
00077
00078
00079 string CPath::lookup( const string& filename, bool throwException )
00080 {
00081 if(!filename.empty())
00082 {
00083 if ( CFile::fileExists(filename) )
00084 {
00085 NL_DISPLAY_PATH(filename);
00086 return filename;
00087 }
00088 CStringVector::iterator isv;
00089 string s;
00090 for ( isv=CPath::_SearchPaths.begin(); isv!=CPath::_SearchPaths.end(); ++isv )
00091 {
00092 s = *isv + filename;
00093 if ( CFile::fileExists(s) )
00094 {
00095 NL_DISPLAY_PATH(s);
00096 return s;
00097 }
00098 }
00099 }
00100
00101 if (throwException)
00102 throw EPathNotFound( filename );
00103
00104 return "";
00105 }
00106
00107
00108
00109 int CFile::getLastSeparator (const std::string &filename)
00110 {
00111 int pos = filename.find_last_of ('/');
00112 if (pos == string::npos)
00113 {
00114 pos = filename.find_last_of ('\\');
00115 }
00116 return pos;
00117 }
00118
00119 std::string CFile::getFilename (const std::string &filename)
00120 {
00121 int pos = CFile::getLastSeparator(filename);
00122 if (pos != string::npos)
00123 return filename.substr (pos + 1);
00124 else
00125 return filename;
00126 }
00127
00128 std::string CFile::getPath (const std::string &filename)
00129 {
00130 int pos = CFile::getLastSeparator(filename);
00131 if (pos != string::npos)
00132 return filename.substr (0, pos + 1);
00133 else
00134 return filename;
00135 }
00136
00137 bool CFile::isDirectory (const std::string &filename)
00138 {
00139 return (CFile::getLastSeparator(filename) == string::npos);
00140 }
00141
00142
00143 bool CFile::fileExists (const string& filename)
00144 {
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 return ! ! fstream( filename.c_str(), ios::in );
00156 }
00157
00158 string CFile::findNewFile (const string &filename)
00159 {
00160 int pos = filename.find_last_of ('.');
00161 if (pos == string::npos)
00162 return filename;
00163
00164 string start = filename.substr (0, pos);
00165 string end = filename.substr (pos);
00166
00167 uint num = 0;
00168 char numchar[4];
00169 string npath;
00170 do
00171 {
00172 npath = start;
00173 smprintf(numchar,4,"%03d",num++);
00174 npath += numchar;
00175 npath += end;
00176 if (!CFile::fileExists(npath)) break;
00177 }
00178 while (num<999);
00179 return npath;
00180 }
00181
00182 }
00183 #endif