Platform_Win32_FileSystem_File.cpp
Go to the documentation of this file.00001 //*** Platform_Win32_FileSystem_File.cpp *** 00002 00003 #include "Platform_OS.h" 00004 00005 #define WIN32_LEAN_AND_MEAN 00006 #define VC_EXTRALEAN 00007 #include <windows.h> 00008 #include <malloc.h> 00009 00010 #include "Platform_Win32_FileSystem_File.h" 00011 00012 //*** Constructor *** 00013 00014 Platform_Win32_FileSystem_File::Platform_Win32_FileSystem_File(const char* path): 00015 path_(0), 00016 fileHandle_(INVALID_HANDLE_VALUE) 00017 { 00018 if (!path) 00019 { 00020 Platform::GetPlatform_OS()->OutputDebugText("No path specified!\n"); 00021 } 00022 00023 if (path) 00024 { 00025 path_=strdup(path); 00026 } 00027 00028 } 00029 00030 00031 //*** Destructor *** 00032 00033 Platform_Win32_FileSystem_File::~Platform_Win32_FileSystem_File() 00034 { 00035 if (path_) 00036 { 00037 free(path_); 00038 } 00039 00040 if (fileHandle_!=INVALID_HANDLE_VALUE) 00041 { 00042 CloseHandle(fileHandle_); 00043 } 00044 } 00045 00046 00047 //*** GetPath *** 00048 00049 const char* Platform_Win32_FileSystem_File::GetPath() 00050 { 00051 return path_; 00052 } 00053 00054 00055 //*** Exists *** 00056 00057 bool Platform_Win32_FileSystem_File::Exists() 00058 { 00059 if (!path_) 00060 { 00061 return false; 00062 } 00063 00064 WIN32_FIND_DATA findFileData; 00065 HANDLE hFind = FindFirstFile(GetPath(), &findFileData); 00066 if (hFind!=INVALID_HANDLE_VALUE && !(findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) 00067 { 00068 FindClose(hFind); 00069 return true; 00070 } 00071 00072 FindClose(hFind); 00073 return false; 00074 } 00075 00076 00077 //*** Create *** 00078 00079 bool Platform_Win32_FileSystem_File::Create() 00080 { 00081 if (fileHandle_!=INVALID_HANDLE_VALUE) 00082 { 00083 Platform::GetPlatform_OS()->OutputDebugText("File already open\n"); 00084 return false; 00085 } 00086 00087 fileHandle_=CreateFile(path_,GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,0); 00088 return fileHandle_!=INVALID_HANDLE_VALUE; 00089 } 00090 00091 00092 //*** Open *** 00093 00094 bool Platform_Win32_FileSystem_File::Open() 00095 { 00096 if (fileHandle_!=INVALID_HANDLE_VALUE) 00097 { 00098 Platform::GetPlatform_OS()->OutputDebugText("File already open\n"); 00099 return false; 00100 } 00101 00102 fileHandle_=CreateFile(path_,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); 00103 return fileHandle_!=INVALID_HANDLE_VALUE; 00104 } 00105 00106 00107 //*** Close *** 00108 00109 bool Platform_Win32_FileSystem_File::Close() 00110 { 00111 if (fileHandle_==INVALID_HANDLE_VALUE) 00112 { 00113 Platform::GetPlatform_OS()->OutputDebugText("File not open\n"); 00114 return false; 00115 } 00116 00117 int result=CloseHandle(fileHandle_); 00118 fileHandle_=INVALID_HANDLE_VALUE; 00119 return result!=0; 00120 } 00121 00122 00123 //*** Delete *** 00124 00125 void Platform_Win32_FileSystem_File::Delete() 00126 { 00127 if (path_) 00128 { 00129 DeleteFile(path_); 00130 } 00131 } 00132 00133 00134 //*** GetPosition *** 00135 00136 int Platform_Win32_FileSystem_File::GetPosition() 00137 { 00138 if (fileHandle_==INVALID_HANDLE_VALUE) 00139 { 00140 Platform::GetPlatform_OS()->OutputDebugText("File not open\n"); 00141 return 0; 00142 } 00143 return SetFilePointer(fileHandle_,0,0,FILE_CURRENT); 00144 } 00145 00146 00147 //*** SetPosition *** 00148 00149 int Platform_Win32_FileSystem_File::SetPosition(int position,SetPositionOrigin origin) 00150 { 00151 if (fileHandle_==INVALID_HANDLE_VALUE) 00152 { 00153 Platform::GetPlatform_OS()->OutputDebugText("File not open\n"); 00154 return 0; 00155 } 00156 00157 DWORD startingPoint=0; 00158 switch (origin) 00159 { 00160 case ORIGIN_START: 00161 { 00162 startingPoint=FILE_BEGIN; 00163 }break; 00164 case ORIGIN_CURRENT: 00165 { 00166 startingPoint=FILE_CURRENT; 00167 }break; 00168 case ORIGIN_END: 00169 { 00170 startingPoint=FILE_END; 00171 }break; 00172 } 00173 int result=SetFilePointer(fileHandle_,position,0,startingPoint); 00174 if (result==INVALID_SET_FILE_POINTER) 00175 { 00176 return -1; 00177 } 00178 00179 return result; 00180 } 00181 00182 00183 //*** WRITEMACRO *** 00184 00185 // This macro defines the code that is to be executed for all of the overloaded Write methods 00186 // The code is identical for all the different Write methods, only the input parameters change, 00187 // so it makes more sense to use a macro rather than copy-paste. This macro is undefined as 00188 // soon as it is not needed anymore 00189 00190 #define WRITEMACRO() \ 00191 if (fileHandle_==INVALID_HANDLE_VALUE) \ 00192 { \ 00193 Platform::GetPlatform_OS()->OutputDebugText("Tried to write to file that is not open for writing\n"); \ 00194 return 0; \ 00195 } \ 00196 \ 00197 unsigned long numberOfBytesWritten=0; /* Stores the number of bytes read */ \ 00198 /* Writes the data to the file */ \ 00199 BOOL result=WriteFile(fileHandle_,value,sizeof(*value)*count,&numberOfBytesWritten,0); \ 00200 if (!result) \ 00201 { \ 00202 Platform::GetPlatform_OS()->OutputDebugText("Write operation failed\n"); \ 00203 return 0; \ 00204 } \ 00205 \ 00206 /* Return number of elements written, rather than the number of bytes */ \ 00207 return numberOfBytesWritten/sizeof(*value); \ 00208 00209 00210 //** Write methods */ 00211 00212 int Platform_Win32_FileSystem_File::Write(const char* value, int count) 00213 { 00214 WRITEMACRO(); // The code for all the write methods are the same, so a macro is used instead of copy-paste 00215 } 00216 00217 int Platform_Win32_FileSystem_File::Write(const short* value, int count) 00218 { 00219 WRITEMACRO(); // The code for all the write methods are the same, so a macro is used instead of copy-paste 00220 } 00221 00222 int Platform_Win32_FileSystem_File::Write(const int* value, int count) 00223 { 00224 WRITEMACRO(); // The code for all the write methods are the same, so a macro is used instead of copy-paste 00225 } 00226 00227 int Platform_Win32_FileSystem_File::Write(const long* value, int count) 00228 { 00229 WRITEMACRO(); // The code for all the write methods are the same, so a macro is used instead of copy-paste 00230 } 00231 00232 int Platform_Win32_FileSystem_File::Write(const unsigned char* value, int count) 00233 { 00234 WRITEMACRO(); // The code for all the write methods are the same, so a macro is used instead of copy-paste 00235 } 00236 00237 int Platform_Win32_FileSystem_File::Write(const unsigned short* value, int count) 00238 { 00239 WRITEMACRO(); // The code for all the write methods are the same, so a macro is used instead of copy-paste 00240 } 00241 00242 int Platform_Win32_FileSystem_File::Write(const unsigned int* value, int count) 00243 { 00244 WRITEMACRO(); // The code for all the write methods are the same, so a macro is used instead of copy-paste 00245 } 00246 00247 int Platform_Win32_FileSystem_File::Write(const unsigned long* value, int count) 00248 { 00249 WRITEMACRO(); // The code for all the write methods are the same, so a macro is used instead of copy-paste 00250 } 00251 00252 int Platform_Win32_FileSystem_File::Write(const float* value, int count) 00253 { 00254 WRITEMACRO(); // The code for all the write methods are the same, so a macro is used instead of copy-paste 00255 } 00256 00257 int Platform_Win32_FileSystem_File::Write(const double* value, int count) 00258 { 00259 WRITEMACRO(); // The code for all the write methods are the same, so a macro is used instead of copy-paste 00260 } 00261 00262 int Platform_Win32_FileSystem_File::Write(const bool* value, int count) 00263 { 00264 WRITEMACRO(); // The code for all the write methods are the same, so a macro is used instead of copy-paste 00265 } 00266 00267 // No need for the write macro anymore, so undefine it 00268 #undef WRITEMACRO 00269 00270 //*** READMACRO *** 00271 00272 // This macro defines the code that is to be executed for all of the overloaded Read methods 00273 // The code is identical for all the different Read methods, only the input parameters change, 00274 // so it makes more sense to use a macro rather than copy-paste. This macro is undefined as 00275 // soon as it is not needed anymore 00276 00277 #define READMACRO() \ 00278 if (fileHandle_==INVALID_HANDLE_VALUE) \ 00279 { \ 00280 Platform::GetPlatform_OS()->OutputDebugText("Tried to read from file that is not open for reading\n"); \ 00281 return 0; \ 00282 } \ 00283 \ 00284 unsigned long numberOfBytesRead=0; /* Stores the number of bytes read */ \ 00285 /* Reads the data from the file */ \ 00286 BOOL result=ReadFile(fileHandle_,value,sizeof(*value)*count,&numberOfBytesRead,0); \ 00287 if (!result) \ 00288 { \ 00289 Platform::GetPlatform_OS()->OutputDebugText("Read operation failed\n"); \ 00290 return 0; \ 00291 } \ 00292 \ 00293 /* Return number of elements read, rather than the number of bytes */ \ 00294 return numberOfBytesRead/sizeof(*value); \ 00295 00296 00297 //** Read methods */ 00298 00299 int Platform_Win32_FileSystem_File::Read(char* value, int count) 00300 { 00301 READMACRO(); 00302 } 00303 00304 int Platform_Win32_FileSystem_File::Read(short* value, int count) 00305 { 00306 READMACRO(); 00307 } 00308 00309 int Platform_Win32_FileSystem_File::Read(int* value, int count) 00310 { 00311 READMACRO(); 00312 } 00313 00314 int Platform_Win32_FileSystem_File::Read(long* value, int count) 00315 { 00316 READMACRO(); 00317 } 00318 00319 int Platform_Win32_FileSystem_File::Read(unsigned char* value, int count) 00320 { 00321 READMACRO(); 00322 } 00323 00324 int Platform_Win32_FileSystem_File::Read(unsigned short* value, int count) 00325 { 00326 READMACRO(); 00327 } 00328 00329 int Platform_Win32_FileSystem_File::Read(unsigned int* value, int count) 00330 { 00331 READMACRO(); 00332 } 00333 00334 int Platform_Win32_FileSystem_File::Read(unsigned long* value, int count) 00335 { 00336 READMACRO(); 00337 } 00338 00339 int Platform_Win32_FileSystem_File::Read(float* value, int count) 00340 { 00341 READMACRO(); 00342 } 00343 00344 int Platform_Win32_FileSystem_File::Read(double* value, int count) 00345 { 00346 READMACRO(); 00347 } 00348 00349 int Platform_Win32_FileSystem_File::Read(bool* value, int count) 00350 { 00351 READMACRO(); 00352 } 00353 00354 // No need for the read macro anymore, so undefine it 00355 #undef READMACRO 00356
Reproduction/republishing of any material on this site without permission is strictly prohibited.
