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

Password:

Ogfilea.cpp Source File
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

Ogfilea.cpp

Go to the documentation of this file.
00001 //Filename    : OGFILEA.CPP
00002 //Description : Game File Array
00003 
00004 #include <IO.H>
00005 #include <ODIR.H>
00006 #include <OSYS.H>
00007 #include <ODATE.H>
00008 #include <OMOUSE.H>
00009 #include <OMOUSECR.H>
00010 #include <OPOWER.H>
00011 
00012 #include <OBOX.H>
00013 #include <OFONT.H>
00014 #include <OINFO.H>
00015 #include <OGAME.H>
00016 #include <OGAMESET.H>
00017 #include <OGFILE.H>
00018 
00019 //--------- Define constant ---------//
00020 //----- File name of the game file array --------//
00021 
00022 #define MAX_BROWSE_DISP_REC    5                  // max. no. of records can be displayed in the saved game browser
00023 
00024 #define HALL_OF_FAME_FILE_NAME  "HALLFAME.DAT"
00025 
00026 //------- Declare static vars & functions ----------//
00027 
00028 static int     sort_game_file_function( const void *a, const void *b );
00029 //static int     last_game_recno();
00030 
00031 //------ Begin of function GameFileArray constuctor ------//
00032 
00033 GameFileArray::GameFileArray() : DynArray( sizeof(GameFile), 10 ) {
00034     demo_format = 0;
00035 
00036 #ifdef DEMO
00037     demo_format = 1;
00038 #endif
00039 
00040 #ifdef DEMO_DESIGN
00041     demo_format = 1;
00042 #endif
00043 
00044     last_file_name[0] = NULL;
00045     has_read_hall_of_fame = 0;
00046 
00047     memset( hall_fame_array, 0, sizeof(HallFame)*HALL_FAME_NUM );
00048 }
00049 
00050 //-------- End of function GameFileArray constuctor ------//
00051 
00052 //------ Begin of function GameFileArray::init ------//
00053 
00054 void GameFileArray::init(char *extStr) {
00055     //------------- Read Hall of Fame ------------//
00056 
00057     if( !has_read_hall_of_fame ) {                  // only read once, GameFileArray::init() is called every time the load/save game menu is brought up.
00058         read_hall_of_fame();
00059         has_read_hall_of_fame = 1;
00060     }
00061 
00062     //-- Load all headers of all saved game files in current directory --//
00063 
00064     load_all_game_header(extStr,
00065 #if(GAME_VERSION>=200)
00066                          stricmp(extStr,"*.SAV")==0                    // load auto save as well if extension is numeric
00067 #else
00068                          0
00069 #endif
00070         );
00071 }
00072 
00073 //-------- End of function GameFileArray::init ------//
00074 
00075 //------ Begin of function GameFileArray::deinit ------//
00076 
00077 void GameFileArray::deinit() {
00078     has_read_hall_of_fame = 0;                      // 0228
00079 }
00080 
00081 //-------- End of function GameFileArray::deinit ------//
00082 
00083 //-------- Begin of function GameFileArray::save_new_game -----//
00092 void GameFileArray::save_new_game(char* fileName) {
00093     GameFile  gameFile;
00094     GameFile* gameFilePtr;
00095     int       addFlag=1;
00096     int       gameFileRecno;
00097 
00098     memset( &gameFile, 0, sizeof(GameFile) );
00099 
00100     if( fileName ) {
00101         //----- check for overwriting an existing file ----//
00102 
00103         for( gameFileRecno=1 ; gameFileRecno<=game_file_array.size() ; gameFileRecno++ ) {
00104             gameFilePtr = game_file_array[gameFileRecno];
00105 
00106             // if this file name already exist
00107             if( strcmp(gameFilePtr->file_name, fileName)==0 ) {
00108                 addFlag=0;
00109                 break;
00110             }
00111         }
00112 
00113         strcpy( gameFile.file_name, fileName );
00114     }
00115     else {
00116         gameFile.set_file_name();                     // give it a new game_file_name based on current group name
00117     }
00118 
00119     //----------- save game now ------------//
00120 
00121     if( gameFile.save_game(fileName) ) {
00122         strcpy( last_file_name, gameFile.file_name );
00123 
00124         if( addFlag ) {
00125             linkin(&gameFile);
00126 
00127             quick_sort( sort_game_file_function );
00128         }
00129         else {
00130             // dynarr's func
00131             game_file_array.update(&gameFile, gameFileRecno);
00132         }
00133     }
00134 }
00135 
00136 //-------- End of function GameFileArray::save_new_game -------//
00137 
00138 //------- Begin of function GameFileArray::del_game ------//
00140 void GameFileArray::del_game(int recNo) {
00141     if( !box.ask( "This saved game will be deleted, proceed ?" ) )
00142         return;
00143 
00144     //---------------------------------------------------//
00145 
00146     unlink(game_file_array[recNo]->file_name);      // unlink() is a C standard function in io.h, for delete file
00147 
00148     go(recNo);
00149     linkout();
00150 
00151     go(recNo-1);                                    // skip back one record
00152 }
00153 
00154 //--------- End of function GameFileArray::del_game ------//
00155 
00156 //------- Begin of function GameFileArray::del_express_game ------//
00158 void GameFileArray::del_express_game(int recNo) {
00159     if( !box.ask( "This express game will be deleted, proceed ?" ) )
00160         return;
00161 
00162     //---------------------------------------------------//
00163 
00164     unlink(game_file_array[recNo]->file_name);      // unlink() is a C standard function in io.h, for delete file
00165 
00166     go(recNo);
00167     linkout();
00168 
00169     go(recNo-1);                                    // skip back one record
00170 }
00171 
00172 //--------- End of function GameFileArray::del_express_game ------//
00173 
00174 //-------- Begin of function GameFileArray::load_all_game_header --------//
00180 
00181 void GameFileArray::load_all_game_header(char *extStr, int readAutoSave) {
00182     int       i;
00183     Directory gameDir;
00184     GameFile  gameFile;
00185     File      file;
00186 
00187 #if(GAME_VERSION>=200)
00188     if( readAutoSave ) {
00189         // see config.auto_save, config.auto_save_file_name
00190         // auto save file name has numeric extension
00191 
00192         // BUGHERE : need to check extStr is not numeric extension
00193         gameDir.read( "*.*", 1 );
00194         for( i = gameDir.size(); i > 0; --i ) {
00195             char ext[_MAX_EXT];
00196             _splitpath( gameDir[i]->name, NULL, NULL, NULL, ext );
00197             // check for numeric extension
00198             if( ext[0] == '.'
00199                 && ext[1] >= '0' && ext[1] <= '9'
00200                 && ext[2] >= '0' && ext[2] <= '9'
00201                 && ext[3] >= '0' && ext[3] <= '9'
00202                 && ext[4] == '\0' ) {
00203                 continue;                                 // keep this file name
00204             }
00205             gameDir.linkout(i);                         // filter out this file
00206         }
00207     }
00208 
00209     // next gameDir.read will append other file name
00210     if( extStr )
00211         gameDir.read( extStr, 1 );                    // 1-Sort file names
00212 #else
00213 
00214     gameDir.read( extStr, 1 );                      // 1-Sort file names
00215 #endif
00216 
00217     //-------- read in the headers of all game sets -------//
00218 
00219     zap();
00220 
00221     for( i=1 ; i<=gameDir.size() ; i++ ) {
00222         if( file.file_open( gameDir[i]->name, 1, 1 )  // last 1=allow varying read & write size
00223             && file.file_read(&gameFile, sizeof(GameFile))
00224             && gameFile.validate_header() ) {
00225             // in case that the name may be different
00226             strcpy( gameFile.file_name, gameDir[i]->name );
00227             gameFile.file_date = gameDir[i]->time;
00228             linkin(&gameFile);
00229         }
00230         file.file_close();
00231     }
00232 }
00233 
00234 //------ End of function GameFileArray::load_all_game_header --------//
00235 
00236 //------ Begin of function sort_game_file_function ------//
00238 static int sort_game_file_function( const void *a, const void *b ) {
00239     return strcmp( ((GameFile*)a)->file_name, ((GameFile*)b)->file_name );
00240 }
00241 
00242 //------- End of function sort_game_file_function ------//
00243 
00244 //------ Begin of function last_game_recno ------//
00246 static int last_game_recno() {
00247     int i;
00248 
00249     for( i=game_file_array.size() ; i>0 ; i-- ) {
00250         if( strcmp(game_file_array[i]->file_name, game_file_array.last_file_name)==0 )
00251             return i;
00252     }
00253 
00254     return 0;                                       // if none of them match, return 1 as the first record
00255 }
00256 
00257 //------- End of function last_game_recno ------//
00258 
00259 //------- Begin of function GameFileArray::operator[] -----//
00260 
00261 GameFile* GameFileArray::operator[](int recNo) {
00262     GameFile* gameFile = (GameFile*) get(recNo);
00263 
00264     //  err_when(gameFile == NULL);  Let it return NULL value
00265 
00266     return gameFile;
00267 }
00268 
00269 //--------- End of function GameFileArray::operator[] ----//
00270 
00271 //--------------------------------------------------------------------//
00272 //--------------------------------------------------------------------//
00273 //--------------------------------------------------------------------//
00274 //--------------------------------------------------------------------//
00275 //--------------------------------------------------------------------//
00276 
00277 //------- Begin of function GameFileArray::write_hall_of_fame ------//
00279 int GameFileArray::write_hall_of_fame() {
00280     int  rc;
00281     File file;
00282 
00283     // 0=don't handle error itself
00284     rc = file.file_create( HALL_OF_FAME_FILE_NAME, 0, 1 );
00285 
00286     if( !rc )
00287         return 0;
00288     // 1=allow the writing size and the read size to be different
00289     //--------- Write Hall of Fame ----------//
00290 
00291     if( rc )
00292         rc = file.file_write( hall_fame_array, sizeof(HallFame) * HALL_FAME_NUM );
00293 
00294     //------ write last saved game file name ------//
00295 
00296     if( rc )
00297         rc = file.file_write( last_file_name, MAX_PATH+1 );
00298 
00299     file.file_close();
00300 
00301     return rc;
00302 }
00303 
00304 //--------- End of function GameFileArray::write_hall_of_fame ------//
00305 
00306 //------- Begin of function GameFileArray::read_hall_of_fame ------//
00308 int GameFileArray::read_hall_of_fame() {
00309     if( !m.is_file_exist(HALL_OF_FAME_FILE_NAME) )
00310         return 0;
00311 
00312     int  rc;
00313     File file;
00314 
00315     // 0=don't handle error itself
00316     rc = file.file_open( HALL_OF_FAME_FILE_NAME, 0, 1 );
00317     // 1=allow the writing size and the read size to be different
00318     if( !rc )
00319         return 0;
00320     // 1=allow the writing size and the read size to be different
00321     //--------- Read Hall of Fame ----------//
00322 
00323     if( rc )
00324         rc = file.file_read( hall_fame_array, sizeof(HallFame) * HALL_FAME_NUM );
00325 
00326     //------ read last saved game file name ------//
00327 
00328     if( rc )
00329         rc = file.file_read( last_file_name, MAX_PATH+1);
00330 
00331     file.file_close();
00332 
00333     return rc;
00334 }
00335 
00336 //--------- End of function GameFileArray::read_hall_of_fame ------//

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