• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

FileOperations.cpp

Go to the documentation of this file.
00001 #include "RakMemoryOverride.h"
00002 #include "_FindFirst.h" // For linux
00003 #include "FileOperations.h"
00004 #include <stdio.h>
00005 #include <string.h>
00006 #ifdef _WIN32 
00007 // For mkdir
00008 #include <direct.h>
00009 #include <io.h>
00010 #else
00011 #include <sys/stat.h>
00012 #include <unistd.h>
00013 #include "_FindFirst.h"
00014 #endif
00015 #include "errno.h"
00016 
00017 #ifdef _MSC_VER
00018 #pragma warning( push )
00019 #endif
00020 
00021 #ifdef _MSC_VER
00022 #pragma warning( disable : 4966 ) // mkdir declared deprecated by Microsoft in order to make it harder to be cross platform.  I don't agree it's deprecated.
00023 #endif
00024 bool WriteFileWithDirectories( const char *path, char *data, unsigned dataLength )
00025 {
00026         int index;
00027         FILE *fp;
00028         char *pathCopy;
00029         int res;
00030 
00031         if ( path == 0 || path[ 0 ] == 0 )
00032                 return false;
00033 
00034         pathCopy = (char*) rakMalloc_Ex( strlen( path ) + 1, __FILE__, __LINE__ );
00035 
00036         strcpy( pathCopy, path );
00037 
00038         // Ignore first / if there is one
00039         if (pathCopy[0])
00040         {
00041                 index = 1;
00042                 while ( pathCopy[ index ] )
00043                 {
00044                         if ( pathCopy[ index ] == '/' || pathCopy[ index ] == '\\')
00045                         {
00046                                 pathCopy[ index ] = 0;
00047         
00048         #ifdef _WIN32
00049         #pragma warning( disable : 4966 ) // mkdir declared deprecated by Microsoft in order to make it harder to be cross platform.  I don't agree it's deprecated.
00050                                 res = mkdir( pathCopy );
00051         #else
00052         
00053                                 res = mkdir( pathCopy, 0744 );
00054         #endif
00055                                 if (res<0 && errno!=EEXIST && errno!=EACCES)
00056                                 {
00057                                         rakFree_Ex(pathCopy, __FILE__, __LINE__ );
00058                                         return false;
00059                                 }
00060         
00061                                 pathCopy[ index ] = '/';
00062                         }
00063         
00064                         index++;
00065                 }
00066         }
00067 
00068         if (data)
00069         {
00070                 fp = fopen( path, "wb" );
00071 
00072                 if ( fp == 0 )
00073                 {
00074                         rakFree_Ex(pathCopy, __FILE__, __LINE__ );
00075                         return false;
00076                 }
00077 
00078                 fwrite( data, 1, dataLength, fp );
00079 
00080                 fclose( fp );
00081         }
00082         else
00083         {
00084 #ifdef _WIN32
00085 #pragma warning( disable : 4966 ) // mkdir declared deprecated by Microsoft in order to make it harder to be cross platform.  I don't agree it's deprecated.
00086                 res = mkdir( pathCopy );
00087 #else
00088                 res = mkdir( pathCopy, 0744 );
00089 #endif
00090 
00091                 if (res<0 && errno!=EEXIST)
00092                 {
00093                         rakFree_Ex(pathCopy, __FILE__, __LINE__ );
00094                         return false;
00095                 }
00096         }
00097 
00098         rakFree_Ex(pathCopy, __FILE__, __LINE__ );
00099 
00100         return true;
00101 }
00102 bool IsSlash(unsigned char c)
00103 {
00104         return c=='/' || c=='\\';
00105 }
00106 
00107 void AddSlash( char *input )
00108 {
00109         if (input==0 || input[0]==0)
00110                 return;
00111 
00112         int lastCharIndex=(int) strlen(input)-1;
00113         if (input[lastCharIndex]=='\\')
00114                 input[lastCharIndex]='/';
00115         else if (input[lastCharIndex]!='/')
00116         {
00117                 input[lastCharIndex+1]='/';
00118                 input[lastCharIndex+2]=0;
00119         }
00120 }
00121 bool DirectoryExists(const char *directory)
00122 {
00123         _finddata_t fileInfo;
00124         intptr_t dir;
00125         char baseDirWithStars[560];
00126         strcpy(baseDirWithStars, directory);
00127         AddSlash(baseDirWithStars);
00128         strcat(baseDirWithStars, "*.*");
00129         dir=_findfirst(baseDirWithStars, &fileInfo );
00130         if (dir==-1)
00131                 return false;
00132         _findclose(dir);
00133         return true;
00134 }
00135 void QuoteIfSpaces(char *str)
00136 {
00137         unsigned i;
00138         bool hasSpace=false;
00139         for (i=0; str[i]; i++)
00140         {
00141                 if (str[i]==' ')
00142                 {
00143                         hasSpace=true;
00144                         break;
00145                 }
00146         }
00147         if (hasSpace)
00148         {
00149                 int len=(int)strlen(str);
00150                 memmove(str+1, str, len);
00151                 str[0]='\"';
00152                 str[len]='\"';
00153                 str[len+1]=0;
00154         }
00155 }
00156 unsigned int GetFileLength(const char *path)
00157 {
00158         FILE *fp = fopen(path, "rb");
00159         if (fp==0) return 0;
00160         fseek(fp, 0, SEEK_END);
00161         unsigned int fileLength = ftell(fp);
00162         fclose(fp);
00163         return fileLength;
00164 
00165 }
00166 
00167 #ifdef _MSC_VER
00168 #pragma warning( pop )
00169 #endif
00170 
00171 

Generated on Thu Sep 30 2010 01:27:23 for RakNet by  doxygen 1.7.1