ye another sound patch from Duong-Khang NGUYEN <neoneurone@users.sf.net>.
[supertux.git] / src / high_scores.c
1 /*
2  
3   by Adam Czachorowski
4   gislan@o2.pl
5  
6 */
7
8 /* Open the highscore file: */
9
10 #include <string.h>
11 #include <stdlib.h>
12
13 #include "globals.h"
14 #include "high_scores.h"
15
16 FILE * opendata(char * mode)
17 {
18   char * filename;
19   FILE * fi;
20
21
22 #ifdef LINUX
23
24   /* Create the buffer for the filename: */
25
26   filename = (char *) malloc(sizeof(char) * (strlen(st_dir) +
27                              strlen("/highscore") + 1));
28
29   strcpy(filename, st_dir);
30   /* Open the high score file: */
31
32   strcat(filename, "/highscore");
33 #else
34
35   filename = "st_highscore.dat";
36 #endif
37
38
39   /* Try opening the file: */
40
41   fi = fopen(filename, mode);
42
43   if (fi == NULL)
44     {
45       fprintf(stderr, "Warning: I could not open the high score file ");
46
47       if (strcmp(mode, "r") == 0)
48         fprintf(stderr, "for read!!!\n");
49       else if (strcmp(mode, "w") == 0)
50         fprintf(stderr, "for write!!!\n");
51
52     }
53
54   return(fi);
55 }
56
57 /* Load data from high score file: */
58
59 int load_hs(void)
60 {
61   FILE * fi;
62   char temp[128];
63   int score = 100;
64
65   /* Try to open file: */
66
67   fi = opendata("r");
68   if (fi != NULL)
69     {
70       do
71         {
72           fgets(temp, sizeof(temp), fi);
73
74           if (!feof(fi))
75             {
76               temp[strlen(temp) - 1] = '\0';
77
78
79               /* Parse each line: */
80
81               if (strstr(temp, "highscore=") == temp)
82                 {
83                   score = atoi(temp + 10);
84
85                   if (score == 0)
86                     score = 100;
87                 }
88             }
89         }
90       while (!feof(fi));
91
92       fclose(fi);
93     }
94   return score;
95 }
96
97 void save_hs(int score)
98 {
99   FILE * fi;
100
101
102   /* Try to open file: */
103
104   fi = opendata("w");
105   if (fi != NULL)
106     {
107       fprintf(fi, "# Supertux highscore file\n\n");
108
109       fprintf(fi, "highscore=%d\n", score);
110
111       fprintf(fi, "# (File automatically created.)\n");
112
113       fclose(fi);
114     }
115 }