123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- /*
- * Seven Kingdoms: Ancient Adversaries
- *
- * Copyright 1997,1998 Enlight Software Ltd.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- //Filename : OFILE.CPP
- //Description : Object File
- #include <windows.h>
- #include <stdio.h>
- #include <OBOX.h>
- #include <ALL.h>
- #include <OFILE.h>
- //--------- Define static variables -----------//
- static char *path_array[] = { "" }; // multiple search path
- //-------- Begin of function File::file_open ----------//
- //
- // Open an existing file for reading
- // If the file is not found, call err.run()
- //
- // <char*> fileName = name of the file
- // [int] handleError = handle error
- // ( if 1, when error happen, err.run() will be called
- // immediately )
- // ( default : 1 )
- // [int] allowVarySize = allow the writing size and the read size to be different
- // ( default : 0 )
- //
- // return : 1-success, 0-fail
- //
- int File::file_open(char* fileName, int handleError, int allowVarySize)
- {
- if( strlen(fileName) > MAX_PATH )
- err.run( "File : file name is too long." );
- if( file_handle != INVALID_HANDLE_VALUE )
- file_close();
- strcpy( file_name, fileName );
- handle_error = handleError;
- allow_vary_size = allowVarySize;
- //--------- search all path to open the file ------//
- for( int i=0 ; i<sizeof(path_array)/sizeof(path_array[0]) ; i++ )
- {
- char filePath[MAX_PATH];
- strcpy( filePath, path_array[i] );
- strcat( filePath, fileName );
- file_handle = CreateFile(filePath,
- GENERIC_READ,
- FILE_SHARE_READ,
- (LPSECURITY_ATTRIBUTES) NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- (HANDLE) NULL);
- if( file_handle != INVALID_HANDLE_VALUE)
- return TRUE;
- }
- err.run( "Error opening file %s.", file_name );
- return FALSE;
- }
- //---------- End of function File::file_open ----------//
- //-------- Begin of function File::file_create ----------//
- //
- // Create a new file for writing (reading is also permitted)
- //
- // <char*> fileName = name of the file
- // [int] handleError = handle error
- // ( if 1, when error happen, err.run() will be called
- // immediately )
- // ( default : 1 )
- // [int] allowVarySize = allow the writing size and the read size to be different
- // ( default : 0 )
- //
- //
- // return : 1-success, 0-fail
- //
- int File::file_create(char* fileName, int handleError, int allowVarySize)
- {
- if( strlen(fileName) > MAX_PATH )
- err.run( "File : file name is too long." );
- strcpy( file_name, fileName );
- handle_error = handleError;
- allow_vary_size = allowVarySize;
- // cannot use creat() because it can't specify Binary file type
- file_handle = CreateFile(fileName,
- GENERIC_WRITE,
- 0,
- (LPSECURITY_ATTRIBUTES) NULL,
- CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL,
- (HANDLE) NULL);
- if( file_handle == INVALID_HANDLE_VALUE)
- err.run( "Error creating file %s", file_name );
- return TRUE;
- }
- //---------- End of function File::file_create ----------//
- //### begin alex 24/7 ###//
- //-------- Begin of function File::file_append ----------//
- //
- // Open an existing file for reading
- // If the file is not found, call err.run()
- //
- // <char*> fileName = name of the file
- // [int] handleError = handle error
- // ( if 1, when error happen, err.run() will be called
- // immediately )
- // ( default : 1 )
- // [int] allowVarySize = allow the writing size and the read size to be different
- // ( default : 0 )
- //
- // return : 1-success, 0-fail
- //
- int File::file_append(char* fileName, int handleError, int allowVarySize)
- {
- if( strlen(fileName) > MAX_PATH )
- err.run( "File : file name is too long." );
- if( file_handle != INVALID_HANDLE_VALUE )
- file_close();
- strcpy( file_name, fileName );
- handle_error = handleError;
- allow_vary_size = allowVarySize;
- //--------- search all path to open the file ------//
- for( int i=0 ; i<sizeof(path_array)/sizeof(path_array[0]) ; i++ )
- {
- char filePath[MAX_PATH];
- strcpy( filePath, path_array[i] );
- strcat( filePath, fileName );
- file_handle = CreateFile(filePath,
- GENERIC_WRITE,
- FILE_SHARE_WRITE,
- (LPSECURITY_ATTRIBUTES) NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- (HANDLE) NULL);
- if( file_handle != INVALID_HANDLE_VALUE)
- return TRUE;
- }
- err.run( "Error opening file %s.", file_name );
- return FALSE;
- }
- //---------- End of function File::file_append ----------//
- //#### end alex 24/7 ####//
- //-------- Begin of function File::file_close ----------//
- //
- void File::file_close()
- {
- if( file_handle != INVALID_HANDLE_VALUE )
- {
- CloseHandle(file_handle);
- file_handle = INVALID_HANDLE_VALUE;
- }
- }
- //---------- End of function File::file_close ----------//
- //-------- Begin of function File::~File ----------//
- //
- File::~File()
- {
- file_close();
- }
- //---------- End of function File::~File ----------//
- //-------- Begin of function File::file_write ----------//
- //
- // Write a block of data to the file
- //
- // <void*> dataBuf = pointer to data buffer to be written to the file
- // <unsigned> dataSize = length of the data (must < 64K)
- //
- // return : 1-success, 0-fail
- //
- int File::file_write(void* dataBuf, unsigned dataSize)
- {
- err_when( file_handle == INVALID_HANDLE_VALUE ); // not initialized
- if( allow_vary_size ) // allow the writing size and the read size to be different
- {
- if( dataSize > 0xFFFF )
- file_put_unsigned_short(0); // if exceed the unsigned short limit, don't write it
- else
- file_put_unsigned_short( dataSize );
- }
- DWORD actualWritten;
- int rc = WriteFile( file_handle, dataBuf, dataSize, &actualWritten, NULL);
-
- if( !rc && handle_error )
- err.run( "Error writing file %s", file_name );
- return rc;
- }
- //---------- End of function File::file_write ----------//
- //-------- Begin of function File::file_read ----------//
- //
- // Read a block of data from the file
- //
- // <void*> dataBuf = pointer to data buffer to be written to the file
- // <unsigned> dataSize = length of the data (must < 64K)
- //
- // return : 1-success, 0-fail
- //
- int File::file_read(void* dataBuf, unsigned dataSize)
- {
- #define MAX_READ_SIZE 0xFFF0
- err_when( file_handle == INVALID_HANDLE_VALUE ); // not initialized
- int curPos=file_pos();
- start_read:
- unsigned readSize=dataSize, writtenSize=dataSize;
- if( allow_vary_size ) // allow the writing size and the read size to be different
- {
- writtenSize = file_get_unsigned_short();
- if( writtenSize ) // writtenSize==0, if the size > 0xFFFF
- readSize = min(dataSize, writtenSize); // the read size is the minimum of the written size and the supposed read size
- }
- DWORD actualRead;
- int rc=ReadFile( file_handle, dataBuf, dataSize, &actualRead, NULL);
-
- //-------- if the data size has been reduced ----------//
- if( readSize < writtenSize )
- file_seek( writtenSize-readSize, FILE_CURRENT );
- //---- if the data size has been increased, reset the unread area ---//
- if( readSize < dataSize )
- memset( (char*)dataBuf+readSize, 0, dataSize-readSize );
- //----- if reading error, popup box and ask for retry -----//
- if( !rc && handle_error )
- {
- char msgStr[100];
- sprintf( msgStr, "Error reading file %s, Retry ?", file_name );
- if( box.ask(msgStr) )
- {
- file_seek( curPos );
- goto start_read;
- }
- }
- return rc;
- }
- //---------- End of function File::file_read ----------//
- //-------- Begin of function File::file_put_short ----------//
- //
- // Put a short integer to the file
- //
- // <short int> = the short integer
- //
- // return : 1-success, 0-fail
- //
- int File::file_put_short(short value)
- {
- err_when( file_handle == INVALID_HANDLE_VALUE ); // not initialized
- DWORD actualWritten;
- if( WriteFile( file_handle, &value, sizeof(short), &actualWritten, NULL ) )
- return TRUE;
- else
- {
- if( handle_error )
- err.run( "Error writing file %s", file_name );
- return FALSE;
- }
- }
- //---------- End of function File::file_put_short ----------//
- //-------- Begin of function File::file_get_short ----------//
- //
- // Get a short integer from the file
- //
- // return : the short integer
- //
- short File::file_get_short()
- {
- err_when( file_handle == INVALID_HANDLE_VALUE ); // not initialized
- DWORD actualRead;
- short value;
- if( ReadFile( file_handle, &value, sizeof(short), &actualRead, NULL ) )
- return value;
- else
- {
- if( handle_error )
- err.run( "Error reading file %s", file_name );
- return FALSE;
- }
- }
- //---------- End of function File::file_get_short ----------//
- //-------- Begin of function File::file_put_unsigned_short ----------//
- //
- // Put a unsigned short integer to the file
- //
- // <unsigned short> = the short integer
- //
- // return : 1-success, 0-fail
- //
- int File::file_put_unsigned_short(unsigned short value)
- {
- err_when( file_handle == INVALID_HANDLE_VALUE ); // not initialized
- DWORD actualWritten;
- if( WriteFile( file_handle, &value, sizeof(unsigned short), &actualWritten, NULL ) )
- return TRUE;
- else
- {
- if( handle_error )
- err.run( "Error writing file %s", file_name );
- return FALSE;
- }
- }
- //---------- End of function File::file_put_unsigned_short ----------//
- //-------- Begin of function File::file_get_unsigned_short ----------//
- //
- // Get a short integer from the file
- //
- // return : the short integer
- //
- unsigned short File::file_get_unsigned_short()
- {
- err_when( file_handle == INVALID_HANDLE_VALUE ); // not initialized
- DWORD actualRead;
- unsigned short value;
- if( ReadFile( file_handle, &value, sizeof(unsigned short), &actualRead, NULL ) )
- return value;
- else
- {
- if( handle_error )
- err.run( "Error reading file %s", file_name );
-
- return 0;
- }
- }
- //---------- End of function File::file_get_unsigned_short ----------//
- //-------- Begin of function File::file_put_long ----------//
- //
- // Put a long integer to the file
- //
- // <long int> = the long integer
- //
- // return : 1-success, 0-fail
- //
- int File::file_put_long(long value)
- {
- err_when( file_handle == INVALID_HANDLE_VALUE ); // not initialized
- DWORD actualWritten;
- if( WriteFile( file_handle, &value, sizeof(long), &actualWritten, NULL ) )
- return TRUE;
- else
- {
- if( handle_error )
- err.run( "Error writing file %s", file_name );
- return FALSE;
- }
- }
- //---------- End of function File::file_put_long ----------//
- //-------- Begin of function File::file_get_long ----------//
- //
- // Get a long integer from the file
- //
- // return : the long integer
- //
- long File::file_get_long()
- {
- err_when( file_handle == INVALID_HANDLE_VALUE ); // not initialized
- DWORD actualRead;
- long value;
- if( ReadFile( file_handle, &value, sizeof(long), &actualRead, NULL ) )
- return value;
- else
- {
- if( handle_error )
- err.run( "Error reading file %s", file_name );
- return FALSE;
- }
- }
- //---------- End of function File::file_get_long ----------//
- //---------- Start of function File::file_seek ---------//
- //
- // <long> offset = seek offset
- // [int] whence = FILE_BEGIN, FILE_CURRENT, FILE_END
- // (default : FILE_BEGIN)
- //
- // return : the offset of the pointer's new position, measured in
- // bytes from the file beginning.
- //
- long File::file_seek(long offset, int whence)
- {
- if( whence == -1 )
- whence = FILE_BEGIN;
- return SetFilePointer( file_handle, offset, NULL, whence );
- }
- //------------ End of function File::file_seek ----------//
- //---------- Start of function File::file_pos ---------//
- //
- // Get the position of current file pointer
- //
- // return : the position of current file pointer
- //
- long File::file_pos()
- {
- return SetFilePointer( file_handle, 0, NULL, FILE_CURRENT );
- }
- //------------ End of function File::file_pos ----------//
- //---------- Start of function File::file_size ---------//
- long File::file_size()
- {
- return GetFileSize(file_handle, NULL);
- }
- //------------ End of function File::file_size ----------//
|