home *** CD-ROM | disk | FTP | other *** search
/ ST-Computer Leser 2002 January / STC_CD_01_2002.iso / GAMES / BOINKO21 / SRC / SRC / FILEIO.C < prev    next >
C/C++ Source or Header  |  2000-11-27  |  4KB  |  251 lines

  1. /*  fileio.c
  2.  * v. .04 Nov 2000
  3.  *
  4.  * Creating files
  5.  * Saving files
  6.  * Reading files
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. #include "boink.h"
  14.  
  15. extern int objcolors[];
  16.  
  17. long
  18. read_cfg_file(char *file_to_read)
  19. {
  20.     FILE *fp;    
  21.     char tempstr[14];
  22.     int i;
  23.  
  24.     fp = fopen(file_to_read, "r");
  25.  
  26.     if (fp != (FILE *)NULL) 
  27.     {
  28.         fgets(tempstr, 10,  fp);
  29.         timer = atoi(tempstr);
  30.  
  31.         for (i = 0; i < 6; i++)
  32.         {
  33.             if(fgets(tempstr, 10,  fp)!=(char *)NULL)
  34.                 objcolors[i] = atoi(tempstr);
  35.             else
  36.                 objcolors[i] = 1;
  37.         }
  38.         
  39.         fclose(fp);
  40.     } else
  41.         return -1;
  42.  
  43.     return 1;
  44. }
  45.  
  46. long
  47. write_cfg_file(char *file_to_write)
  48. {
  49.     FILE *fd;
  50.     char tempstr[16];
  51.  
  52.     fd = fopen(file_to_write,"w");
  53.  
  54.     if (fd == NULL)
  55.         return 0;
  56.  
  57.     sprintf(tempstr,"%d\n\0",timer);        
  58.     fwrite(tempstr, sizeof(char), strlen(tempstr), fd);
  59.  
  60.     sprintf(tempstr,"%d\n\0",objcolors[0]);
  61.     fwrite(tempstr, sizeof(char), strlen(tempstr), fd);
  62.             
  63.     sprintf(tempstr,"%d\n\0",objcolors[1]);
  64.     fwrite(tempstr, sizeof(char), strlen(tempstr), fd);
  65.  
  66.     sprintf(tempstr,"%d\n\0",objcolors[2]);
  67.     fwrite(tempstr, sizeof(char), strlen(tempstr), fd);
  68.  
  69.     sprintf(tempstr,"%d\n\0",objcolors[3]);
  70.     fwrite(tempstr, sizeof(char), strlen(tempstr), fd);
  71.  
  72.     sprintf(tempstr,"%d\n\0",objcolors[4]);
  73.     fwrite(tempstr, sizeof(char), strlen(tempstr), fd);
  74.  
  75.     sprintf(tempstr,"%d\n\0",objcolors[5]);
  76.     fwrite(tempstr, sizeof(char), strlen(tempstr), fd);
  77.  
  78.     fclose(fd);
  79.  
  80.     return 1;
  81. }
  82.  
  83. /* This will write out the highscore file
  84.     This routine could probably use some 
  85.     error checking
  86. */
  87.  
  88. long
  89. write_score(void)
  90. {
  91.     FILE *fd;
  92. /*    char tempstr[24];*/
  93.     int i;
  94.  
  95.     fd = fopen(score_file,"w");
  96.     if (fd==NULL)  return 0;
  97.  
  98.     for (i = 0; i < 10; i++)
  99.         {
  100.             fprintf(fd,"%s\n",scores[i].name);
  101.             fprintf(fd,"%d\n",scores[i].level);
  102.             fprintf(fd,"%ld\n",scores[i].score);
  103.         }
  104.  
  105.     fclose(fd);
  106.  
  107.     return 1;
  108. }
  109.  
  110.  
  111. long
  112. read_highscore(void)
  113. {
  114.     int formdo_ret;
  115.     long fileio_return;
  116.     int i;
  117.     FILE *fp;    
  118.     char tempstr[30];
  119.  
  120. reopen_score:
  121.  
  122.     fp = fopen(score_file, "r");
  123.  
  124.     if (fp != (FILE *)NULL) 
  125.     {
  126.         for (i = 0; i < 10; i++)
  127.             {
  128.                 /* This is just to clear out the name */
  129.                 strcpy(scores[i].name,"              \0\0");
  130.  
  131.                 fgets(tempstr, 16,  fp);
  132.                 if(tempstr[strlen(tempstr)-1]=='\n')
  133.                   tempstr[strlen(tempstr)-1]=0;
  134.                 if(tempstr[strlen(tempstr)-1]=='\r')
  135.                   tempstr[strlen(tempstr)-1]=0;
  136.                 strcpy(scores[i].name,tempstr);
  137.  
  138.                 fgets(tempstr, 10,  fp);
  139.                 scores[i].level = atoi(tempstr);
  140.  
  141.                 fgets(tempstr, 10,  fp);
  142.                 scores[i].score = atol(tempstr);
  143.             }
  144.  
  145.         fclose(fp);
  146.  
  147.         /* Init RSC form for High Score */
  148.  
  149.         for (i = 0; i < 10; i++)
  150.         {
  151.             sprintf(tempstr,"%2d %-14s %2d %6ld",i+1,scores[i].name,
  152.                 scores[i].level,scores[i].score);
  153.  
  154.             set_tedinfo(scorelist_dial,SCORE1 + i,tempstr);
  155.         }
  156.  
  157.     }
  158.     else
  159.     {
  160.         /*we need to tell them the file doesn't exist*/
  161.  
  162.         formdo_ret = form_alert(1,alert_noscore);
  163.  
  164.         if (formdo_ret == 1)
  165.         {
  166.             /* we will have to create one now*/
  167.  
  168.             if ( write_score() == 0 )
  169.                 form_alert(1,alert_cantcreate);
  170.             else
  171.                 goto reopen_score;                    
  172.         }
  173.         else
  174.         {
  175.              form_alert(1,alert_cantfind);
  176.         }
  177.     }
  178.     
  179.     return 1;
  180. }
  181.  
  182. long
  183. read_level_file(char *file_to_read)
  184. {
  185.     FILE *fp;    
  186.     char tempstr[200];
  187.     char *temp;
  188.     int i = 0;
  189.     int ii = 0;
  190.     
  191.     extern char brickmask[99][63];
  192.  
  193.     fp = fopen(file_to_read, "r");
  194.  
  195.     if (fp != (FILE *)NULL) 
  196.     {
  197.         while(fgets(tempstr, 200,  fp)!=NULL)
  198.         {
  199.             if (strncmp(tempstr,"#",1)!=0)
  200.             {
  201.                 temp = (strtok(tempstr,","));
  202.  
  203.                 brickmask[ii][i] = atoi(temp);
  204.                 i++;
  205.                 
  206.                 if (i == 63)
  207.                 {
  208.                     ii++;
  209.                     i = 0;
  210.                 }
  211.                 
  212.                 while((temp = strtok(NULL,",")) != NULL)
  213.                     {
  214.                         if ((temp[0] == ' ')||
  215.                             (temp[0] == '\r')||
  216.                             (temp[0] == '\n'))
  217.                             ;
  218.                         else
  219.                         {
  220.                             brickmask[ii][i] = atoi(temp);
  221.  
  222.                             i++;
  223.                 
  224.                             if (i == 63)
  225.                             {
  226.                                 ii++;
  227.                                 i = 0;
  228.                             }
  229.                         }    
  230.                     }                    
  231.             }
  232.             else
  233.             {
  234.                 /* This line is a remark */
  235.                 /* maybe do something with them sometime */
  236.                 ;            
  237.             }            
  238.         }        
  239.         
  240.         fclose(fp);
  241.     } else
  242.     {
  243.         /* Can't read file */
  244.         printf("Can't read file\n");
  245.         return -1;
  246.     }
  247.         
  248.     file_levels = ii;
  249.     return 1;
  250. }
  251.