Debian Codegraph @

src/scoreboard.c

raw symbols SCIP
1 /* scoreboard.c - save/load scores
2 *
3 * This program is free software; you can redistribute it and/or
4 * modif (y it under the terms of the GNU General Public
5 * License as published by the Free Software Foundation; either
6 * version 2 of the License, or (at your option) any later version.
7 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <gtk/gtk.h>
14 #include <glib.h>
15
16 #include "gtkballs.h"
17 #include "scoreboard.h"
18
19 #include "game.h"
20
21 #define SCORE_FILE "gtkballs-scores"
22 #define BUFFER_SIZE 1024
23
24
25 void free_score_board_full(struct score_board_full *bf, gint nbf)
26 {
27 gint i;
28
29 for (i = 0; i < nbf; i++) {
30 g_free(bf[i].rules);
31 }
32 g_free(bf);
33 }
34
35
36 gint write_score(struct score_board *b, struct score_board_full *bf, gint nbf)
37 {
38 gint i;
39 gchar *tname, *tdate, *rules;
40 char * score_file = get_config_dir_file (SCORE_FILE);
41 FILE * fd;
42
43 fd = fopen (score_file, "w");
44 if (!fd) {
45 g_free (score_file);
46 return FALSE;
47 }
48
49 for (i = 0; i < 10; i++)
50 {
51 if (strlen(b[i].name)) {
52 tname = g_strdup(b[i].name);
53 if (!tname) {
54 tname = g_strdup(_("Unknown"));
55 }
56 tdate = g_strdup(b[i].date);
57 if (!tdate) {
58 tdate = g_strdup(_("Unknown"));
59 }
60 rules = rules_get_as_str();
61 fprintf (fd, "%s\t%i\t%s\t%s\n", tname, b[i].score, tdate, rules);
62 g_free(rules);
63 g_free(tdate);
64 g_free(tname);
65 }
66 }
67 for (i = 0; i < nbf; i++) {
68 if (strlen(bf[i].name)) {
69 fprintf (fd, "%s\t%i\t%s\t%s\n", bf[i].name, bf[i].score, bf[i].date, bf[i].rules);
70 }
71 }
72 g_free (score_file);
73 fclose (fd);
74
75 return TRUE;
76 }
77
78
79 int score_sort(const void *a, const void *b)
80 {
81 if (((const struct score_board *)a)->score == ((const struct score_board *)b)->score) return 0;
82 if (((const struct score_board *)a)->score < ((const struct score_board *)b)->score) return 1;
83 return -1;
84 }
85
86
87 gint read_score(struct score_board *b, struct score_board_full **bf, gint *nbf)
88 {
89 FILE *fp;
90 gchar buffer[BUFFER_SIZE];
91 gchar **str_val, *tstr, **tstr_val;
92 gint valid, sc, fsc;
93 gsize br, bw;
94 char * score_file = get_config_dir_file (SCORE_FILE);
95
96 gchar *g_rules = NULL;
97
98 memset(b, 0, sizeof(struct score_board) * 10);
99
100 if (!(fp = fopen(score_file, "r"))) {
101 if (nbf) {
102 *nbf = 0;
103 }
104 g_free (score_file);
105 return TRUE;
106 }
107
108 sc = 0;
109 fsc = 0;
110 while (fgets(buffer, BUFFER_SIZE, fp))
111 {
112 g_strchomp(buffer);
113 str_val = g_strsplit(buffer, "\t", 4);
114 if (str_val[0] && str_val[0][0] &&
115 str_val[1] && str_val[1][0] &&
116 str_val[2] && str_val[2][0])
117 {
118 valid = 0;
119 if (!str_val[3]) { /* < 2.2.0 file for mat */
120 g_rules = rules_get_classic_as_str();
121 valid = 1;
122 } else {
123 if ((valid = rules_check_str(str_val[3]))) {
124 g_rules = g_strdup(str_val[3]);
125 } else { /* > 2.2.0 && < 3.0.2 file for mat ? */
126 tstr_val = g_strsplit(str_val[3], "\t", 5);
127 if (tstr_val[0] && tstr_val[1] && tstr_val[2] &&
128 tstr_val[3] && tstr_val[4]) {
129 g_rules = rules_conv_3_0_to_str(tstr_val[0], tstr_val[1], tstr_val[2], tstr_val[3], tstr_val[4]);
130 if (g_rules) { /* yes. its < 3.0.2 for mat */
131 valid = 1;
132 } else { /* nope. just piece of shit. */
133 g_rules = g_strdup("");
134 }
135 }
136 g_strfreev(tstr_val);
137 }
138 }
139 if (valid && g_ascii_strcasecmp(str_val[0], "<none>")) {
140 if (rules_is_current_str(g_rules)) {
141 if (g_utf8_validate(str_val[0], -1, NULL)) {
142 tstr = g_strdup(str_val[0]);
143 } else {
144 tstr = g_locale_to_utf8(str_val[0], -1, &br, &bw, NULL);
145 }
146 if (tstr) {
147 strncpy(b[sc].name, tstr, sizeof(b[sc].name)-1);
148 g_free(tstr);
149 } else {
150 strncpy(b[sc].name, _("Unknown"), sizeof(b[sc].name)-1);
151 }
152 b[sc].score = strtol(str_val[1], NULL, 10);
153 if ((b[sc].score == INT_MIN) || (b[sc].score == INT_MAX)) {
154 b[sc].score = 0;
155 }
156 if (g_utf8_validate(str_val[2], -1, NULL)) {
157 tstr = g_strdup(str_val[2]);
158 } else {
159 tstr = g_locale_to_utf8(str_val[2], -1, &br, &bw, NULL);
160 }
161 if (tstr) {
162 strncpy(b[sc].date, tstr, sizeof(b[sc].date)-1);
163 g_free(tstr);
164 } else {
165 strncpy(b[sc].date, _("Unknown"), sizeof(b[sc].date)-1);
166 }
167 sc++;
168 } else if (bf) {
169 *bf = g_realloc(*bf, sizeof(struct score_board_full) * (fsc + 1));
170 strncpy((*bf)[fsc].name, str_val[0], sizeof((*bf)[fsc].name)-1);
171 (*bf)[fsc].score = strtol(str_val[1], NULL, 10);
172 if (((*bf)[fsc].score == INT_MIN) || ((*bf)[fsc].score == INT_MAX)) {
173 (*bf)[fsc].score = 0;
174 }
175 strncpy((*bf)[fsc].date, str_val[2], sizeof((*bf)[fsc].date)-1);
176 (*bf)[fsc].rules = g_strdup(g_rules);
177 fsc++;
178 }
179 }
180 g_free(g_rules);
181 }
182 g_strfreev(str_val);
183 }
184
185 g_free (score_file);
186 fclose(fp);
187
188 qsort(b, 10, sizeof(struct score_board), score_sort);
189
190 if (nbf) {
191 *nbf = fsc;
192 }
193
194 return TRUE;
195 }
196
197
198 gint insert_entry_in_score_board(struct score_board *board, struct score_board entry)
199 {
200 gint i=0,j;
201
202 if (entry.score <= 0) {
203 return -1;
204 }
205
206 while (i < 10 && board[i].score > entry.score) {
207 i++;
208 }
209
210 if (i > 9) {
211 return -1;
212 }
213
214 for (j = 8;j >= i; j--) {
215 strncpy(board[j + 1].name, board[j].name, sizeof(board[j + 1].name)-1);
216 strncpy(board[j + 1].date, board[j].date, sizeof(board[j + 1].date)-1);
217 board[j + 1].score = board[j].score;
218 }
219
220 strncpy(board[i].name, entry.name, sizeof(board[i].name));
221 strncpy(board[i].date, entry.date, sizeof(board[i].date));
222 board[i].score = entry.score;
223
224 return i;
225 }