# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

old_path.cpp

Go to the documentation of this file.
00001 #if 0
00002 
00009 /* Copyright, 2000 Nevrax Ltd.
00010  *
00011  * This file is part of NEVRAX NEL.
00012  * NEVRAX NEL is free software; you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation; either version 2, or (at your option)
00015  * any later version.
00016 
00017  * NEVRAX NEL is distributed in the hope that it will be useful, but
00018  * WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00020  * General Public License for more details.
00021 
00022  * You should have received a copy of the GNU General Public License
00023  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00024  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00025  * MA 02111-1307, USA.
00026  */
00027 
00028 #include "nel/misc/path.h"
00029 #include "nel/misc/debug.h"
00030 //#include <stdio.h>
00031 #include <fstream>
00032 
00033 using namespace std;
00034 
00035 
00036 // Use this define if you want to display the absolute paths in the console.
00037 //#define       NL_DEBUG_PATH
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  * Adds a search path
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         // Add an ending slash if necessary
00066         if ( path[path.size()-1] != slash )
00067         {
00068                 s += slash;
00069         }
00070 
00071         // Add path to the search paths
00072         _SearchPaths.push_back( s );
00073 }
00074 
00075 
00076 /* Returns the long name (path and filename) for the specified file, using search paths
00077  * stored by addSearchPath.
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 //********************************* CFile
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         /*FILE *f;
00146         if ( (f = fopen( filename.c_str(), "r" )) == NULL )
00147         {
00148                 return false;
00149         }
00150         else
00151         {
00152                 fclose( f );
00153                 return true;
00154         }*/
00155         return ! ! fstream( filename.c_str(), ios::in ); // = ! fstream(...).fail()
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 } // NLMISC
00183 #endif