00001
00002
00003
00004 #include <windows.h>
00005 #include <stdio.h>
00006
00007 #include <ALL.H>
00008 #include <OFILE.H>
00009
00010
00011
00012 static char *path_array[] = {
00013 ""
00014 };
00015
00016
00031
00032 if( strlen(fileName) > MAX_PATH )
00033 err.run( "File : file name is too long." );
00034
00035 if( file_handle != INVALID_HANDLE_VALUE )
00036 file_close();
00037
00038 strcpy( file_name, fileName );
00039
00040 handle_error = handleError;
00041 allow_vary_size = allowVarySize;
00042
00043
00044
00045 for( int i=0 ; i<sizeof(path_array)/sizeof(path_array[0]) ; i++ ) {
00046 char filePath[MAX_PATH];
00047 strcpy( filePath, path_array[i] );
00048 strcat( filePath, fileName );
00049
00050 file_handle = CreateFile(filePath,
00051 GENERIC_READ,
00052 FILE_SHARE_READ,
00053 (LPSECURITY_ATTRIBUTES) NULL,
00054 OPEN_EXISTING,
00055 FILE_ATTRIBUTE_NORMAL,
00056 (HANDLE) NULL);
00057
00058 if( file_handle != INVALID_HANDLE_VALUE)
00059 return TRUE;
00060 }
00061
00062 err.run( "Error opening file %s.", file_name );
00063
00064 return FALSE;
00065 }
00066
00067
00068
00069
00084
00085 if( strlen(fileName) > MAX_PATH )
00086 err.run( "File : file name is too long." );
00087
00088 strcpy( file_name, fileName );
00089
00090 handle_error = handleError;
00091 allow_vary_size = allowVarySize;
00092
00093
00094
00095 file_handle = CreateFile(fileName,
00096 GENERIC_WRITE,
00097 0,
00098 (LPSECURITY_ATTRIBUTES) NULL,
00099 CREATE_ALWAYS,
00100 FILE_ATTRIBUTE_NORMAL,
00101 (HANDLE) NULL);
00102
00103 if( file_handle == INVALID_HANDLE_VALUE)
00104 err.run( "Error creating file %s", file_name );
00105
00106 return TRUE;
00107 }
00108
00109
00110
00111
00112
00127
00128 if( strlen(fileName) > MAX_PATH )
00129 err.run( "File : file name is too long." );
00130
00131 if( file_handle != INVALID_HANDLE_VALUE )
00132 file_close();
00133
00134 strcpy( file_name, fileName );
00135
00136 handle_error = handleError;
00137 allow_vary_size = allowVarySize;
00138
00139
00140
00141 for( int i=0 ; i<sizeof(path_array)/sizeof(path_array[0]) ; i++ ) {
00142 char filePath[MAX_PATH];
00143 strcpy( filePath, path_array[i] );
00144 strcat( filePath, fileName );
00145
00146 file_handle = CreateFile(filePath,
00147 GENERIC_WRITE,
00148 FILE_SHARE_WRITE,
00149 (LPSECURITY_ATTRIBUTES) NULL,
00150 OPEN_EXISTING,
00151 FILE_ATTRIBUTE_NORMAL,
00152 (HANDLE) NULL);
00153
00154 if( file_handle != INVALID_HANDLE_VALUE)
00155 return TRUE;
00156 }
00157
00158 err.run( "Error opening file %s.", file_name );
00159
00160 return FALSE;
00161 }
00162
00163
00164
00165
00166
00168
00169 if( file_handle != INVALID_HANDLE_VALUE ) {
00170 CloseHandle(file_handle);
00171 file_handle = INVALID_HANDLE_VALUE;
00172 }
00173 }
00174
00175
00176
00177
00179
00180 file_close();
00181 }
00182
00183
00184
00185
00194
00195 err_when( file_handle == INVALID_HANDLE_VALUE );
00196
00197 if( allow_vary_size ) {
00198 if( dataSize > 0xFFFF )
00199 file_put_unsigned_short(0);
00200 else
00201 file_put_unsigned_short( dataSize );
00202 }
00203
00204 DWORD actualWritten;
00205
00206 int rc = WriteFile( file_handle, dataBuf, dataSize, &actualWritten, NULL);
00207
00208 if( !rc && handle_error )
00209 err.run( "Error writing file %s", file_name );
00210
00211 return rc;
00212 }
00213
00214
00215
00216
00225
00226 #define MAX_READ_SIZE 0xFFF0
00227
00228 err_when( file_handle == INVALID_HANDLE_VALUE );
00229
00230 int curPos=file_pos();
00231
00232
00233
00234 unsigned readSize=dataSize, writtenSize=dataSize;
00235
00236 if( allow_vary_size ) {
00237 writtenSize = file_get_unsigned_short();
00238
00239 if( writtenSize )
00240 readSize = min(dataSize,writtenSize);
00241 }
00242
00243 DWORD actualRead;
00244
00245 int rc=ReadFile( file_handle, dataBuf, dataSize, &actualRead, NULL);
00246
00247
00248
00249 if( readSize < writtenSize )
00250 file_seek( writtenSize-readSize, FILE_CURRENT );
00251
00252
00253
00254 if( readSize < dataSize )
00255 memset( (char*)dataBuf+readSize, 0, dataSize-readSize );
00256
00257
00258
00259 if( !rc && handle_error ) {
00260 char msgStr[100];
00261
00262 sprintf( msgStr, "Error reading file %s, Retry ?", file_name );
00263
00264
00265
00266
00267
00268
00269 }
00270
00271 return rc;
00272 }
00273
00274
00275
00276
00284
00285 err_when( file_handle == INVALID_HANDLE_VALUE );
00286
00287 DWORD actualWritten;
00288
00289 if( WriteFile( file_handle, &value, sizeof(short), &actualWritten, NULL ) )
00290 return TRUE;
00291 else {
00292 if( handle_error )
00293 err.run( "Error writing file %s", file_name );
00294
00295 return FALSE;
00296 }
00297 }
00298
00299
00300
00301
00307
00308 err_when( file_handle == INVALID_HANDLE_VALUE );
00309
00310 DWORD actualRead;
00311 short value;
00312
00313 if( ReadFile( file_handle, &value, sizeof(short), &actualRead, NULL ) )
00314 return value;
00315 else {
00316 if( handle_error )
00317 err.run( "Error reading file %s", file_name );
00318
00319 return FALSE;
00320 }
00321 }
00322
00323
00324
00325
00333
00334 err_when( file_handle == INVALID_HANDLE_VALUE );
00335
00336 DWORD actualWritten;
00337
00338 if( WriteFile( file_handle, &value, sizeof(unsigned short), &actualWritten, NULL ) )
00339 return TRUE;
00340 else {
00341 if( handle_error )
00342 err.run( "Error writing file %s", file_name );
00343
00344 return FALSE;
00345 }
00346 }
00347
00348
00349
00350
00356
00357 err_when( file_handle == INVALID_HANDLE_VALUE );
00358
00359 DWORD actualRead;
00360 unsigned short value;
00361
00362 if( ReadFile( file_handle, &value, sizeof(unsigned short), &actualRead, NULL ) )
00363 return value;
00364 else {
00365 if( handle_error )
00366 err.run( "Error reading file %s", file_name );
00367
00368 return 0;
00369 }
00370 }
00371
00372
00373
00374
00382
00383 err_when( file_handle == INVALID_HANDLE_VALUE );
00384
00385 DWORD actualWritten;
00386
00387 if( WriteFile( file_handle, &value, sizeof(long), &actualWritten, NULL ) )
00388 return TRUE;
00389 else {
00390 if( handle_error )
00391 err.run( "Error writing file %s", file_name );
00392
00393 return FALSE;
00394 }
00395 }
00396
00397
00398
00399
00405
00406 err_when( file_handle == INVALID_HANDLE_VALUE );
00407
00408 DWORD actualRead;
00409 long value;
00410
00411 if( ReadFile( file_handle, &value, sizeof(long), &actualRead, NULL ) )
00412 return value;
00413 else {
00414 if( handle_error )
00415 err.run( "Error reading file %s", file_name );
00416
00417 return FALSE;
00418 }
00419 }
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432 long File::file_seek(long offset, int whence) {
00433 if( whence == -1 )
00434 whence = FILE_BEGIN;
00435
00436 return SetFilePointer( file_handle, offset, NULL, whence );
00437 }
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447 long File::file_pos() {
00448 return SetFilePointer( file_handle, 0, NULL, FILE_CURRENT );
00449 }
00450
00451
00452
00453
00454
00455 long File::file_size() {
00456 return GetFileSize(file_handle, NULL);
00457 }
00458
00459