00001
00006 #include "gameIOManager.h"
00007
00008
00009 char **readfile( char namefile[],
00010 bool *schema_ok,
00011 unsigned int *mapCurrentRow,
00012 unsigned int *mapCurrentCol,
00013 unsigned int mapMaxRow,
00014 unsigned int mapMaxCol,
00015 bool differentColLen
00016 )
00017 {
00018 D1(printf("\nGAMEIOMANAGER.readfile() - START\n"));
00019 D2(printf("\nMax dimensions (rows,cols)=(%u,%u)\n",
00020 mapMaxRow,mapMaxCol ));
00021 D2(printf("\nDIFFerent columns CONTROL is turn %s\n",
00022 differentColLen?"ON":"OFF" ));
00023
00024 char **contentFile = allocResetVariable(mapMaxRow,mapMaxCol);
00025
00026
00027 *mapCurrentRow = 0;
00028 *mapCurrentCol = 0;
00029
00030
00031
00032 FILE *file2read = NULL;
00033 char fileChar;
00034 int checkchar;
00035 unsigned int checkCol = 0;
00036
00037 if ( (file2read=fopen(namefile,"r")) == NULL)
00038 {
00039 D1(printf("Impossible to open the file %s\n",namefile));
00040 *schema_ok = false;
00041 return contentFile;
00042 }
00043
00044 do
00045 {
00046 checkchar = fscanf(file2read,"%c",&fileChar);
00047 if(checkchar == EOF )
00048 fileChar = EOF;
00049
00050 if((fileChar == COMMENT) || (fileChar == EOF) || (fileChar == ENDLINE))
00051 skipCharacters( &fileChar, &checkchar, file2read );
00052 else
00053 {
00054 if( *mapCurrentRow >= mapMaxRow ){
00055 D1(printf("\nFile have more rows then maximum allowed (%u)\n",
00056 mapMaxRow ));
00057 D1(printf("\nGAMEIOMANAGER.readfile() - Error & END\n"));
00058 *schema_ok = false;
00059 return contentFile;
00060 }
00061
00062 contentFile[ *mapCurrentRow ] [ 0 ] = fileChar;
00063 *mapCurrentCol = 1;
00064
00065 if ( ! readCharacters( mapCurrentCol, mapMaxCol, mapCurrentRow,
00066 mapMaxRow, contentFile, file2read ) )
00067 {
00068 *schema_ok = false;
00069 D1(printf("\nGAMEIOMANAGER.readfile() - Error & END\n"));
00070 return contentFile;
00071 }
00072
00073
00074 if( differentColLen )
00075 {
00076 if ( ! checkDifferentColLen( mapCurrentRow, mapCurrentCol,
00077 &checkCol ) )
00078 {
00079 *schema_ok = false;
00080 D1(printf("\nGAMEIOMANAGER.readfile() - Error & END\n"));
00081 return contentFile;
00082 }
00083 }
00084 *mapCurrentRow = *mapCurrentRow + 1;
00085 }
00086 }
00087 while( fileChar != EOF );
00088
00089
00090 fclose(file2read);
00091 *schema_ok = true;
00092
00093 D1(printf("\nGAMEIOMANAGER.readfile() - END\n"));
00094 return contentFile;
00095 }
00096
00097
00098 char **allocResetVariable( unsigned int mapMaxRow,
00099 unsigned int mapMaxCol
00100 )
00101 {
00102 D1(printf("\nGAMEIOMANAGER.allocResetVariable() - START\n"));
00103
00104
00105 char **contentFile = (char **) malloc( sizeof(char *) * mapMaxRow );
00106 unsigned int i = 0, j = 0;
00107 for( i=0; i<mapMaxRow; i++)
00108 {
00109 assert( i < mapMaxRow );
00110 contentFile[i] = (char *) malloc( sizeof(char) * mapMaxCol );
00111
00112 for(j=0; j<mapMaxCol; j++){
00113 assert( j < mapMaxCol );
00114 contentFile[i][j] = NULLCHAR;
00115 }
00116 }
00117
00118 D1(printf("\nGAMEIOMANAGER.allocResetVariable() - END\n"));
00119
00120 return contentFile;
00121 }
00122
00123
00124 void skipCharacters( char *fileChar,
00125 int *checkchar,
00126 FILE *file2read
00127 )
00128 {
00129 D1(printf("\nGAMEIOMANAGER.skipCharacters() - START\n"));
00130
00131 while( ( *fileChar != EOF ) && ( *fileChar != ENDLINE ) )
00132 {
00133 *checkchar = fscanf(file2read,"%c",fileChar);
00134 if(*checkchar == EOF )
00135 *fileChar = EOF;
00136 }
00137
00138 D1(printf("\nGAMEIOMANAGER.skipCharacters() - END\n"));
00139 }
00140
00141
00142 bool readCharacters(unsigned int *mapCurrentCol,
00143 unsigned int mapMaxCol,
00144 const unsigned int *mapCurrentRow,
00145 unsigned int mapMaxRow,
00146 char **contentFile,
00147 FILE *file2read
00148 )
00149 {
00150 D1(printf("\nGAMEIOMANAGER.readCharacters() - START\n"));
00151 bool stopRead = false;
00152 char fileChar;
00153 int checkchar;
00154
00155 while( !stopRead )
00156 {
00157 assert( !stopRead );
00158 checkchar = fscanf(file2read,"%c",&fileChar);
00159 if(checkchar == EOF )
00160 fileChar = EOF;
00161
00162
00163 if( ( fileChar != ENDLINE ) && ( fileChar != EOF ) )
00164 {
00165
00166 if( *mapCurrentCol >= mapMaxCol )
00167 {
00168 D1(printf("\nGAMEIOMANAGER.readCharacters() - END\n"));
00169 D1(printf("\nFile have more columns then maximum allowed (%u)\n",
00170 mapMaxCol ));
00171 return false;
00172 }
00173
00174 else
00175 {
00176 contentFile[ *mapCurrentRow ] [ *mapCurrentCol ] = fileChar;
00177 *mapCurrentCol = *mapCurrentCol + 1;
00178 }
00179 }
00180 else
00181 stopRead = true;
00182 }
00183
00184 D1(printf("\nGAMEIOMANAGER.readCharacters() - END\n"));
00185 return true;
00186 }
00187
00188
00189 bool checkDifferentColLen( const unsigned int *mapCurrentRow,
00190 const unsigned int *mapCurrentCol,
00191 unsigned int *checkCol
00192 )
00193 {
00194 D1(printf("\nGAMEIOMANAGER.checkDifferentColLen() - START\n"));
00195
00196 if(*mapCurrentRow == 0)
00197 *checkCol = *mapCurrentCol;
00198 else
00199 if( *checkCol != *mapCurrentCol )
00200 {
00201 D1(printf("\nDifferent columns number in schema (at row %u)\n",
00202 (*mapCurrentRow+1)));
00203 return false;
00204 }
00205
00206 D1(printf("\nGAMEIOMANAGER.checkDifferentColLen() - END\n"));
00207 return true;
00208 }
00209
00210
00211 bool readSetGameRecord( unsigned int *readrows )
00212 {
00213
00214 D1(printf("\nGAMEIOMANAGER.readSetGameRecord() - START\n"));
00215
00216
00217 unsigned int numrow=0, numcol=0;
00218 const unsigned short int RECORD_ROW_NUMBER = 3;
00219 const unsigned short int ADDITIONAL_CHAR_NUMBER = 1;
00220 char **filecontent = NULL;
00221 bool record_ok = false;
00222 char *filenamecpy =
00223 (char *) malloc( sizeof(char) * ( ADDITIONAL_CHAR_NUMBER
00224 + strlen(RECORD_FILE_NAME) ) );
00225
00226 strcpy( filenamecpy , RECORD_FILE_NAME );
00227 filecontent = readfile( filenamecpy,
00228 &record_ok,
00229 &numrow,
00230 &numcol,
00231 (MAX_RECORD_SHOW * RECORD_ROW_NUMBER),
00232 MAX_RECORDNAME_LEN,
00233 false
00234 );
00235
00236 free( filenamecpy );
00237
00238
00239 if( ! record_ok )
00240 {
00241 D1(printf("\nRecord file is corrupted!\n"));
00242 D1(printf("\nGAMEIOMANAGER.readSetGameRecord() - Error & END\n"));
00243 return false;
00244 }
00245
00246
00247 D2(printf("\nReading file %s return %u record lines.\n",
00248 RECORD_FILE_NAME, numrow ));
00249
00250 bool foundErrors = false;
00251 foundErrors = setGameRecordStructValue(filecontent, numrow, numcol);
00252
00253 if (foundErrors)
00254 {
00255 D1(printf("\nGAMEIOMANAGER.readSetGameRecord() - Error & END\n"));
00256 return false;
00257 }
00258
00259 *readrows = (numrow/RECORD_ROW_NUMBER);
00260
00261 D1(printf("\nGAMEIOMANAGER.readSetGameRecord() - END\n"));
00262 return true;
00263 }
00264
00265
00266 bool setGameRecordStructValue( char **filecontent,
00267 unsigned int numrow,
00268 unsigned int numcol
00269 )
00270 {
00271 D1(printf("\nGAMEIOMANAGER.setGameRecordStructValue() - START\n"));
00272
00273 unsigned int structind = 0, i = 0, j = 0;
00274 for(i=0; i<numrow; i++)
00275 {
00276
00277 for(j=0; j<strlen(filecontent[i]); j++)
00278 if( ! isallowedchar(LOWERCASE+UPPERCASE+DIGITS, filecontent[i][j]))
00279 {
00280 D1(printf("\nName's character %u^ at row %u not allowed.\n"
00281 ,j ,i ));
00282 D1(printf(
00283 "\nGAMEIOMANAGER.setGameRecordStructValue() - END\n"));
00284 return true;
00285 }
00286
00287 strcpy( gameRecord[ structind ].name , filecontent[i] );
00288
00289
00290 i++;
00291
00292
00293 for(j=0; j<strlen(filecontent[i]); j++)
00294 if( ! isallowedchar( DIGITS , filecontent[i][j] ) )
00295 {
00296 D1(printf(
00297 "\nPoints' character %u^ at row %u not allowed.\n",j,i));
00298 D1(printf(
00299 "\nGAMEIOMANAGER.setGameRecordStructValue() - END\n"));
00300 return true;
00301 }
00302
00303 double check = atoi( filecontent[i] );
00304 if( check > UINT_MAX ){
00305 D1(printf(
00306 "\nPoints value (%.0lf) exceed max allowed (%u).\n",
00307 check, UINT_MAX ));
00308 D1(printf("\nGAMEIOMANAGER.setGameRecordStructValue() - END\n"));
00309 return true;
00310 }
00311
00312 gameRecord[ structind ].points = (unsigned int) check;
00313
00314
00315 i++;
00316
00317
00318 for(j=0; j<strlen(filecontent[i]); j++)
00319 if( ! isallowedchar( DIGITS , filecontent[i][j] ) )
00320 {
00321 D1(printf(
00322 "\nLevel's character %u^ at row %u not allowed.\n",j,i
00323 ));
00324 D1(printf(
00325 "\nGAMEIOMANAGER.setGameRecordStructValue() - END\n"));
00326 return true;
00327 }
00328
00329 check = atoi( filecontent[i] );
00330 if( check > NEW_GAME_ROUND ){
00331 D1(printf(
00332 "\nLevel value (%.0lf) exceed max allowed (%u).\n",
00333 check, NEW_GAME_ROUND ));
00334 D1(printf("\nGAMEIOMANAGER.setGameRecordStructValue() - END\n"));
00335 return true;
00336 }
00337
00338 gameRecord[ structind ].level = (unsigned int) check;
00339
00340
00341 structind++;
00342 }
00343
00344 D1(printf("\nGAMEIOMANAGER.setGameRecordStructValue() - END\n"));
00345 return false;
00346 }
00347
00348 void save_record( const char *name,
00349 unsigned int totalpoints,
00350 unsigned int currentlevel
00351 )
00352 {
00353
00354 D1(printf("\nGAMEIOMANAGERS.save_record() - START\n"));
00355
00356 unsigned int numrec = 0;
00357 bool readstat = false;
00358
00359 readstat = readSetGameRecord( &numrec );
00360
00361 if( ! readstat ){
00362 assert( !readstat );
00363 remove( RECORD_FILE_NAME );
00364 numrec = 0;
00365 }
00366
00367 unsigned int pos = 0;
00368
00369
00370 if( numrec > 0 ){
00371 assert( numrec > 0 );
00372
00373
00374 while( pos < numrec ){
00375 assert( pos < numrec );
00376
00377 if(gameRecord[pos].points <= totalpoints)
00378 break;
00379 else
00380 pos++;
00381 }
00382 }
00383
00384 if ( numrec < MAX_RECORD_SHOW )
00385 numrec++;
00386
00387 D2(printf("\nInsert position: %u\n",pos));
00388
00389 FILE * recordf = fopen ( RECORD_FILE_NAME , "w" );
00390
00391 if( recordf == NULL ){
00392 D1(printf("\nError in file %s when opening for writing!\n",
00393 RECORD_FILE_NAME ));
00394 return;
00395 }
00396
00397 D2(printf("\nWriting on %s number of record %u\n",
00398 RECORD_FILE_NAME, numrec ));
00399
00400 unsigned int i=0;
00401 unsigned int i_gameRecord=0;
00402 while( i<numrec )
00403 {
00404 assert( recordf != NULL );
00405
00406 if( pos == i )
00407 {
00408 fprintf( recordf, "%s\n", name );
00409 fprintf( recordf, "%u\n", totalpoints );
00410 fprintf( recordf, "%u\n", currentlevel );
00411 i_gameRecord--;
00412 }
00413 else
00414 {
00415 fprintf( recordf, "%s\n", gameRecord[i_gameRecord].name );
00416 fprintf( recordf, "%u\n", gameRecord[i_gameRecord].points );
00417 fprintf( recordf, "%u\n", gameRecord[i_gameRecord].level );
00418
00419 D3(printf("\nName: %s points: %u level: %u\n",
00420 gameRecord[i_gameRecord].name,
00421 gameRecord[i_gameRecord].points,
00422 gameRecord[i_gameRecord].level ));
00423 }
00424
00425 i++;
00426 i_gameRecord++;
00427
00428 }
00429
00430 assert( recordf != NULL );
00431 fclose(recordf);
00432
00433 D1(printf("\nGAMEIOMANAGER.save_record() - END\n"));
00434
00435 }
00436
00437
00438 void printschema()
00439 {
00440 D1(
00441
00442 unsigned int i = 0;
00443 unsigned int j = 0;
00444
00445 printf("\nSCHEMA:\n");
00446 for(i=0;i<schemaData.numRow;i++)
00447 {
00448 printf("\n");
00449 for(j=0;j<schemaData.numCol;j++)
00450 printf("%c",schemaData.gameMap[i][j]);
00451 }
00452 printf("\n\n");
00453
00454 );
00455 }
00456
00457
00458