NLMISC::CFile Struct Reference

#include <path.h>


Detailed Description

Utility class for file manipulation
Author:
Vianney Lecroart

Nevrax France

Date:
2001

Definition at line 317 of file path.h.

Static Public Member Functions

void addFileChangeCallback (const std::string &filename, void(*)(const std::string &filename))
void checkFileChange (TTime frequency=1000)
bool copyFile (const char *dest, const char *src, bool failIfExists=false)
bool createDirectory (const std::string &dirname)
bool deleteFile (const std::string &filename)
bool fileExists (const std::string &filename)
std::string findNewFile (const std::string &filename)
std::string getExtension (const std::string &filename)
uint32 getFileCreationDate (const std::string &filename)
uint32 getFileModificationDate (const std::string &filename)
std::string getFilename (const std::string &filename)
std::string getFilenameWithoutExtension (const std::string &filename)
uint32 getFileSize (FILE *f)
uint32 getFileSize (const std::string &filename)
int getLastSeparator (const std::string &filename)
std::string getPath (const std::string &filename)
void getTemporaryOutputFilename (const std::string &originalFilename, std::string &tempFilename)
bool isDirectory (const std::string &filename)
bool isExists (const std::string &filename)
bool moveFile (const char *dest, const char *src)
void removeFileChangeCallback (const std::string &filename)
bool setRWAccess (const std::string &filename)


Member Function Documentation

void NLMISC::CFile::addFileChangeCallback const std::string &  filename,
void(*  )(const std::string &filename)
[static]
 

Add a callback that will be call when the content file, named filename, changed. The system use the file modification date. To work, you need to call evenly the function checkFileChange(), this function only checks every 1s by default (you can change the default time)

ie: void cb (const std::string &filename) { nlinfo ("the file %s changed", filename.c_str()); } CFile::addFileChangeCallback ("myfile.txt", cb);

Definition at line 1567 of file path.cpp.

Referenced by NLMISC::CEntityIdTranslator::load().

01568 {
01569         string fn = CPath::lookup(filename, false, false);
01570         if (fn.empty())
01571         {
01572                 fn = filename;
01573         }
01574         nlinfo ("PATH: CFile::addFileChangeCallback: I'll check the modification date for this file '%s'", fn.c_str());
01575         FileToCheck.push_back(CFileEntry(fn, cb));
01576 }

void NLMISC::CFile::checkFileChange TTime  frequency = 1000  )  [static]
 

You have to call this function evenly (each frame for example) to enable the file change callback system. If the file not exists and is created in the run time, the callback will be called. If the file exists and is removed in the run time, the callback will be called.

Parameters:
frequency the time in millisecond that we wait before check another time (1s by default).

Definition at line 1578 of file path.cpp.

References buffer, nlwarning, and s.

01579 {
01580         static TTime lastChecked = CTime::getLocalTime();
01581 
01582         if (CTime::getLocalTime() > lastChecked + frequency)
01583         {
01584                 for (uint i = 0; i < FileToCheck.size(); i++)
01585                 {
01586                         if(CFile::getFileModificationDate(FileToCheck[i].FileName) != FileToCheck[i].LastModified)
01587                         {
01588                                 // need to reload it
01589                                 if(FileToCheck[i].Callback != NULL)
01590                                         FileToCheck[i].Callback(FileToCheck[i].FileName);
01591 
01592                                 FileToCheck[i].LastModified = CFile::getFileModificationDate(FileToCheck[i].FileName);
01593                         }
01594                 }
01595 
01596                 lastChecked = CTime::getLocalTime();
01597         }
01598 }

bool NLMISC::CFile::copyFile const char *  dest,
const char *  src,
bool  failIfExists = false
[static]
 

Copy a file NB this keeps file attributes

Parameters:
failIfExists If the destination file exists, nothing is done, and it returns false.
Returns:
true if the copy succeeded

Definition at line 1653 of file path.cpp.

References size, and src.

01654 {
01655         return CopyMoveFile(dest, src, true, failIfExists);
01656 }

bool NLMISC::CFile::createDirectory const std::string &  dirname  )  [static]
 

Create a directory

Returns:
true if succes

Definition at line 1664 of file path.cpp.

References uint.

01665 {
01666 #ifdef NL_OS_WINDOWS
01667         return _mkdir(filename.c_str())==0;
01668 #else
01669         // Set full permissions....
01670         return mkdir(filename.c_str(), 0xFFFF)==0;
01671 #endif
01672 }

bool NLMISC::CFile::deleteFile const std::string &  filename  )  [static]
 

Delete a file if possible (change the write access if possible)

Returns:
true if the delete occurs.

Definition at line 1757 of file path.cpp.

01758 {
01759         setRWAccess(filename);
01760         int res = unlink (filename.c_str());
01761         if (res == -1)
01762         {
01763                 nlwarning ("PATH: Can't delete file '%s': %d %s", filename.c_str(), errno, strerror(errno));
01764                 return false;
01765         }
01766         return true;
01767 }

bool NLMISC::CFile::fileExists const std::string &  filename  )  [static]
 

Return true if the file exists. Warning: this test will also tell that the file does not exist if you don't have the rights to read it (Unix).

Definition at line 1402 of file path.cpp.

Referenced by NLMISC::CConfigFile::reparse().

01403 {
01404         H_AUTO(FileExists);
01405         return ! ! fstream( filename.c_str(), ios::in );
01406 }

string NLMISC::CFile::findNewFile const std::string &  filename  )  [static]
 

Return a new filename that doesn't exists. It's used for screenshot filename for example. example: findNewFile("foobar.tga"); will try foobar001.tga, if the file exists, try foobar002.tga and so on until it finds an unexistant file.

Definition at line 1409 of file path.cpp.

References uint32.

01410 {
01411         uint32 pos = filename.find_last_of ('.');
01412         if (pos == string::npos)
01413                 return filename;
01414         
01415         string start = filename.substr (0, pos);
01416         string end = filename.substr (pos);
01417 
01418         uint num = 0;
01419         char numchar[4];
01420         string npath;
01421         do
01422         {
01423                 npath = start;
01424                 smprintf(numchar,4,"%03d",num++);
01425                 npath += numchar;
01426                 npath += end;
01427                 if (!CFile::fileExists(npath)) break;
01428         }
01429         while (num<999);
01430         return npath;
01431 }

string NLMISC::CFile::getExtension const std::string &  filename  )  [static]
 

Definition at line 1352 of file path.cpp.

Referenced by NL3D::CAnimationSet::loadFromFiles().

01353 {
01354         uint32 pos = filename.find_last_of ('.');
01355         if (pos == string::npos)
01356                 return "";
01357         else
01358                 return filename.substr (pos + 1);
01359 }

uint32 NLMISC::CFile::getFileCreationDate const std::string &  filename  )  [static]
 

Return creation Time of the file. 0 if not found.

You have to provide the full path of the file (the function doesn't lookup)

Definition at line 1511 of file path.cpp.

References NLMISC::FileToCheck, nlinfo, and uint.

01512 {
01513         uint pos;
01514         string fn;
01515         if ((pos=filename.find('@')) != string::npos)
01516         {
01517                 fn = CPath::lookup(filename.substr (0, pos));
01518         }
01519         else
01520         {
01521                 fn = filename;
01522         }
01523 
01524 #if defined (NL_OS_WINDOWS)
01525         struct _stat buf;
01526         int result = _stat (fn.c_str (), &buf);
01527 #elif defined (NL_OS_UNIX)
01528         struct stat buf;
01529         int result = stat (fn.c_str (), &buf);
01530 #endif
01531 
01532         if (result != 0) return 0;
01533         else return buf.st_ctime;
01534 }

uint32 NLMISC::CFile::getFileModificationDate const std::string &  filename  )  [static]
 

Return Time of last modification of file. 0 if not found.

You have to provide the full path of the file (the function doesn't lookup)

Definition at line 1485 of file path.cpp.

Referenced by loadForm().

01486 {
01487         uint pos;
01488         string fn;
01489         if ((pos=filename.find('@')) != string::npos)
01490         {
01491                 fn = CPath::lookup(filename.substr (0, pos));
01492         }
01493         else
01494         {
01495                 fn = filename;
01496         }
01497 
01498 #if defined (NL_OS_WINDOWS)
01499         struct _stat buf;
01500         int result = _stat (fn.c_str (), &buf);
01501 #elif defined (NL_OS_UNIX)
01502         struct stat buf;
01503         int result = stat (fn.c_str (), &buf);
01504 #endif
01505 
01506         if (result != 0) return 0;
01507         else return buf.st_mtime;
01508 }

string NLMISC::CFile::getFilename const std::string &  filename  )  [static]
 

Retrieve the associated file name. An empty string is returned if the path is invalid

Definition at line 1333 of file path.cpp.

References nlwarning, and res.

Referenced by loadForm().

01334 {
01335         uint32 pos = CFile::getLastSeparator(filename);
01336         if (pos != string::npos)
01337                 return filename.substr (pos + 1);
01338         else
01339                 return filename;
01340 }

string NLMISC::CFile::getFilenameWithoutExtension const std::string &  filename  )  [static]
 

Definition at line 1342 of file path.cpp.

References res.

01343 {
01344         string filename2 = getFilename (filename);
01345         uint32 pos = filename2.find_last_of ('.');
01346         if (pos == string::npos)
01347                 return filename2;
01348         else
01349                 return filename2.substr (0, pos);
01350 }

uint32 NLMISC::CFile::getFileSize FILE *  f  )  [static]
 

Return the size of the file (in bytes).

Definition at line 1472 of file path.cpp.

References uint, and uint32.

01473 {
01474 #if defined (NL_OS_WINDOWS)
01475         struct _stat buf;
01476         int result = _fstat (fileno(f), &buf);
01477 #elif defined (NL_OS_UNIX)
01478         struct stat buf;
01479         int result = fstat (fileno(f), &buf);
01480 #endif
01481         if (result != 0) return 0;
01482         else return buf.st_size;
01483 }

uint32 NLMISC::CFile::getFileSize const std::string &  filename  )  [static]
 

Return the size of the file (in bytes).

You have to provide the full path of the file (the function doesn't lookup)

Definition at line 1434 of file path.cpp.

References uint32.

01435 {
01436 /*      FILE *fp = fopen (filename.c_str(), "rb");
01437         if (fp == NULL) return 0;
01438         nlfseek64 (fp, 0, SEEK_END);
01439         uint32 size = ftell (fp);
01440         fclose (fp);
01441         return size;*/
01442 
01443 /*      const char *s = filename.c_str();
01444         int h = _open (s, _O_RDONLY | _O_BINARY);
01445         _lseek (h, 0, SEEK_END);
01446         uint32 size = _tell (h);
01447         _close (h);
01448         return size;
01449 */
01450 
01451         if (filename.find('@') != string::npos)
01452         {
01453                 uint32 fs = 0, bfo;
01454                 bool c, d;
01455                 CBigFile::getInstance().getFile (filename, fs, bfo, c, d);
01456                 return fs;
01457         }
01458         else
01459         {
01460 #if defined (NL_OS_WINDOWS)
01461                 struct _stat buf;
01462                 int result = _stat (filename.c_str (), &buf);
01463 #elif defined (NL_OS_UNIX)
01464                 struct stat buf;
01465                 int result = stat (filename.c_str (), &buf);
01466 #endif
01467                 if (result != 0) return 0;
01468                 else return buf.st_size;
01469         }
01470 }

int NLMISC::CFile::getLastSeparator const std::string &  filename  )  [static]
 

Return the position between [begin,end[ of the last separator between path and filename ('/' or '\'). If there's no separator, it returns string::npos.

Definition at line 1319 of file path.cpp.

01320 {
01321         uint32 pos = filename.find_last_of ('/');
01322         if (pos == string::npos)
01323         {
01324                 pos = filename.find_last_of ('\\');
01325                 if (pos == string::npos)
01326                 {
01327                         pos = filename.find_last_of ('@');
01328                 }
01329         }
01330         return pos;
01331 }

string NLMISC::CFile::getPath const std::string &  filename  )  [static]
 

Retrieve the associated file path with the trailing slash. Returns an empty string if the path is invalid

Definition at line 1361 of file path.cpp.

01362 {
01363         uint32 pos = CFile::getLastSeparator(filename);
01364         if (pos != string::npos)
01365                 return filename.substr (0, pos + 1);
01366         else
01367                 return "";
01368 }

void NLMISC::CFile::getTemporaryOutputFilename const std::string &  originalFilename,
std::string &  tempFilename
[static]
 

Get temporary output filename. Call this method to get a temporary output filename. If you have successfuly saved your data, delete the old filename and move the new one.

Definition at line 1769 of file path.cpp.

01770 {
01771         uint i = 0;
01772         do
01773                 tempFilename = originalFilename+".tmp"+toString (i++);
01774         while (CFile::isExists(tempFilename));
01775 }

bool NLMISC::CFile::isDirectory const std::string &  filename  )  [static]
 

Just to know if it is a directory. _FileName empty and path not !!!

Definition at line 1370 of file path.cpp.

References num, NLMISC::smprintf(), uint, and uint32.

01371 {
01372 #ifdef NL_OS_WINDOWS
01373         DWORD res = GetFileAttributes(filename.c_str());
01374         if (res == ~0U)
01375         {
01376                 nlwarning ("PATH: %s is not a valid file / directory name", filename.c_str ());
01377                 return false;
01378         }
01379         return (res & FILE_ATTRIBUTE_DIRECTORY) != 0;
01380 #else // NL_OS_WINDOWS
01381         struct stat buf;
01382         int res = stat (filename.c_str (), &buf);
01383         if (res == -1)
01384         {
01385                 nlwarning ("PATH: can't stat '%s' error %d '%s'", filename.c_str(), errno, strerror(errno));
01386                 return false;
01387         }
01388         return (buf.st_mode & S_IFDIR) != 0;
01389 #endif // NL_OS_WINDOWS
01390 }

bool NLMISC::CFile::isExists const std::string &  filename  )  [static]
 

Return true if the file OR directory exists. Warning: this test will also tell that the file does not exist if you don't have the rights to read it (Unix).

Definition at line 1392 of file path.cpp.

References num.

01393 {
01394 #ifdef NL_OS_WINDOWS
01395         return (GetFileAttributes(filename.c_str()) != ~0U);
01396 #else // NL_OS_WINDOWS
01397         struct stat buf;
01398         return stat (filename.c_str (), &buf) == 0;
01399 #endif // NL_OS_WINDOWS
01400 }

bool NLMISC::CFile::moveFile const char *  dest,
const char *  src
[static]
 

Move a file NB this keeps file attributes

Definition at line 1658 of file path.cpp.

References size.

01659 {
01660         return CopyMoveFile(dest, src, false);
01661 }

void NLMISC::CFile::removeFileChangeCallback const std::string &  filename  )  [static]
 

Remove a file that was previously added by addFileChangeCallback

Definition at line 1549 of file path.cpp.

References NLMISC::FileToCheck.

01550 {
01551         string fn = CPath::lookup(filename, false, false);
01552         if (fn.empty())
01553         {
01554                 fn = filename;
01555         }
01556         for (uint i = 0; i < FileToCheck.size(); i++)
01557         {
01558                 if(FileToCheck[i].FileName == fn)
01559                 {
01560                         nlinfo ("PATH: CFile::removeFileChangeCallback: '%s' is removed from checked files modification", fn.c_str());
01561                         FileToCheck.erase(FileToCheck.begin()+i);
01562                         return;
01563                 }
01564         }
01565 }

bool NLMISC::CFile::setRWAccess const std::string &  filename  )  [static]
 

Try to set the file access to read/write if not already set. return true if the file doesn't exists or if the file already have RW access. Work actually only on Windows and returns always true on other platforms.

Returns:
true if RW access is granted

Definition at line 1720 of file path.cpp.

References nlwarning, and res.

01721 {
01722 #ifdef NL_OS_WINDOWS
01723         // if the file exists and there's no write access
01724         if (_access (filename.c_str(), 00) == 0 && _access (filename.c_str(), 06) == -1)
01725         {
01726                 // try to set the read/write access
01727                 if (_chmod (filename.c_str(), _S_IREAD | _S_IWRITE) == -1)
01728                 {
01729                         nlwarning ("PATH: Can't set RW access to file '%s': %d %s", filename.c_str(), errno, strerror(errno));
01730                         return false;
01731                 }
01732         }
01733 #else
01734         // if the file exists and there's no write access
01735         if (access (filename.c_str(), F_OK) == 0)
01736         {
01737                 // try to set the read/write access
01738                 if (chmod (filename.c_str(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH) == -1)
01739                 {
01740                         nlwarning ("PATH: Can't set RW access to file '%s': %d %s", filename.c_str(), errno, strerror(errno));
01741                         return false;
01742                 }
01743         }
01744         else
01745         {
01746                 nlwarning("PATH: Can't access to file '%s'", filename.c_str());
01747         }
01748 #endif
01749         return true;
01750 }


The documentation for this struct was generated from the following files:
Generated on Tue Mar 16 13:12:52 2004 for NeL by doxygen 1.3.6