Virtual U.org
Get Personal Training on VU Today
    
Top shadow
 
 register/help
User Name:

Password:

OFILE.CPP Source File
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

OFILE.CPP

Go to the documentation of this file.
00001 //Filename    : OFILE.CPP
00002 //Description : Object File
00003 
00004 #include <windows.h>
00005 #include <stdio.h>
00006 //#include <OBOX.H>
00007 #include <ALL.H>
00008 #include <OFILE.H>
00009 
00010 //--------- Define static variables -----------//
00011 
00012 static char *path_array[] = {                     // multiple search path
00013     ""
00014 };
00015 
00016 //-------- Begin of function File::file_open ----------//
00031 int File::file_open(char* fileName, int handleError, int allowVarySize) {
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     //--------- search all path to open the file ------//
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 //---------- End of function File::file_open ----------//
00068 
00069 //-------- Begin of function File::file_create ----------//
00084 int File::file_create(char* fileName, int handleError, int allowVarySize) {
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     // cannot use creat() because it can't specify Binary file type
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 //---------- End of function File::file_create ----------//
00110 
00111 //### begin alex 24/7 ###//
00112 //-------- Begin of function File::file_append ----------//
00127 int File::file_append(char* fileName, int handleError, int allowVarySize) {
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     //--------- search all path to open the file ------//
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 //---------- End of function File::file_append ----------//
00164 //#### end alex 24/7 ####//
00165 
00166 //-------- Begin of function File::file_close ----------//
00168 void File::file_close() {
00169     if( file_handle != INVALID_HANDLE_VALUE ) {
00170         CloseHandle(file_handle);
00171         file_handle = INVALID_HANDLE_VALUE;
00172     }
00173 }
00174 
00175 //---------- End of function File::file_close ----------//
00176 
00177 //-------- Begin of function File::~File ----------//
00179 File::~File() {
00180     file_close();
00181 }
00182 
00183 //---------- End of function File::~File ----------//
00184 
00185 //-------- Begin of function File::file_write ----------//
00194 int File::file_write(void* dataBuf, unsigned dataSize) {
00195     err_when( file_handle == INVALID_HANDLE_VALUE );// not initialized
00196 
00197     if( allow_vary_size ) {                         // allow the writing size and the read size to be different
00198         if( dataSize > 0xFFFF )
00199             file_put_unsigned_short(0);                 // if exceed the unsigned short limit, don't write it
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 //---------- End of function File::file_write ----------//
00215 
00216 //-------- Begin of function File::file_read ----------//
00225 int File::file_read(void* dataBuf, unsigned dataSize) {
00226 #define MAX_READ_SIZE 0xFFF0
00227 
00228     err_when( file_handle == INVALID_HANDLE_VALUE );// not initialized
00229 
00230     int curPos=file_pos();
00231 
00232     //start_read:
00233 
00234     unsigned readSize=dataSize, writtenSize=dataSize;
00235 
00236     if( allow_vary_size ) {                         // allow the writing size and the read size to be different
00237         writtenSize = file_get_unsigned_short();
00238 
00239         if( writtenSize )                             // writtenSize==0, if the size > 0xFFFF
00240             readSize = min(dataSize,writtenSize);       // the read size is the minimum of the written size and the supposed read size
00241     }                                               //min & max bug chea
00242 
00243     DWORD actualRead;
00244 
00245     int rc=ReadFile( file_handle, dataBuf, dataSize, &actualRead, NULL);
00246 
00247     //-------- if the data size has been reduced ----------//
00248 
00249     if( readSize < writtenSize )
00250         file_seek( writtenSize-readSize, FILE_CURRENT );
00251 
00252     //---- if the data size has been increased, reset the unread area ---//
00253 
00254     if( readSize < dataSize )
00255         memset( (char*)dataBuf+readSize, 0, dataSize-readSize );
00256 
00257     //----- if reading error, popup box and ask for retry -----//
00258 
00259     if( !rc && handle_error ) {
00260         char msgStr[100];
00261 
00262         sprintf( msgStr, "Error reading file %s, Retry ?", file_name );
00263 
00264         /*if( box.ask(msgStr) )
00265           {
00266           file_seek( curPos );
00267           goto start_read;
00268           }*/
00269     }
00270 
00271     return rc;
00272 }
00273 
00274 //---------- End of function File::file_read ----------//
00275 
00276 //-------- Begin of function File::file_put_short ----------//
00284 int File::file_put_short(short value) {
00285     err_when( file_handle == INVALID_HANDLE_VALUE );// not initialized
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 //---------- End of function File::file_put_short ----------//
00300 
00301 //-------- Begin of function File::file_get_short ----------//
00307 short File::file_get_short() {
00308     err_when( file_handle == INVALID_HANDLE_VALUE );// not initialized
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 //---------- End of function File::file_get_short ----------//
00324 
00325 //-------- Begin of function File::file_put_unsigned_short ----------//
00333 int File::file_put_unsigned_short(unsigned short value) {
00334     err_when( file_handle == INVALID_HANDLE_VALUE );// not initialized
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 //---------- End of function File::file_put_unsigned_short ----------//
00349 
00350 //-------- Begin of function File::file_get_unsigned_short ----------//
00356 unsigned short File::file_get_unsigned_short() {
00357     err_when( file_handle == INVALID_HANDLE_VALUE );// not initialized
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 //---------- End of function File::file_get_unsigned_short ----------//
00373 
00374 //-------- Begin of function File::file_put_long ----------//
00382 int File::file_put_long(long value) {
00383     err_when( file_handle == INVALID_HANDLE_VALUE );// not initialized
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 //---------- End of function File::file_put_long ----------//
00398 
00399 //-------- Begin of function File::file_get_long ----------//
00405 long File::file_get_long() {
00406     err_when( file_handle == INVALID_HANDLE_VALUE );// not initialized
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 //---------- End of function File::file_get_long ----------//
00422 
00423 //---------- Start of function File::file_seek ---------//
00424 //
00425 // <long> offset = seek offset
00426 // [int]  whence = FILE_BEGIN, FILE_CURRENT, FILE_END
00427 //                 (default : FILE_BEGIN)
00428 //
00429 // return : the offset of the pointer's new position, measured in
00430 //          bytes from the file beginning.
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 //------------ End of function File::file_seek ----------//
00440 
00441 //---------- Start of function File::file_pos ---------//
00442 //
00443 // Get the position of current file pointer
00444 //
00445 // return : the position of current file pointer
00446 //
00447 long File::file_pos() {
00448     return SetFilePointer( file_handle, 0, NULL, FILE_CURRENT );
00449 }
00450 
00451 //------------ End of function File::file_pos ----------//
00452 
00453 //---------- Start of function File::file_size ---------//
00454 
00455 long File::file_size() {
00456     return GetFileSize(file_handle, NULL);
00457 }
00458 
00459 //------------ End of function File::file_size ----------//

Generated on Fri Aug 23 01:37:33 2002 for VirtualU by doxygen1.2.17