scoreboard.c/h: consistent indentation

This commit is contained in:
wdlkmpx 2021-01-14 23:23:00 +08:00
parent 565348506b
commit a43f2eb39b
2 changed files with 195 additions and 181 deletions

View File

@ -1,7 +1,7 @@
/* scoreboard.c - save/load scores
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* modif (y it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*/
@ -23,204 +23,218 @@
int _score_fd = -1;
gint score_setup(void) {
_score_fd = child_setup(SCORE_FILE);
return _score_fd;
gint score_setup(void)
{
_score_fd = child_setup(SCORE_FILE);
return _score_fd;
}
void free_score_board_full(struct score_board_full *bf, gint nbf) {
gint i;
for(i = 0; i < nbf; i++) {
g_free(bf[i].rules);
}
g_free(bf);
void free_score_board_full(struct score_board_full *bf, gint nbf)
{
gint i;
for (i = 0; i < nbf; i++) {
g_free(bf[i].rules);
}
g_free(bf);
}
gint write_score(struct score_board *b, struct score_board_full *bf, gint nbf) {
gint i;
gchar *buf, *tname, *tdate, *rules;
size_t sz;
if(child_writer_alive() == 0) {
return FALSE;
}
gint write_score(struct score_board *b, struct score_board_full *bf, gint nbf)
{
gint i;
gchar *buf, *tname, *tdate, *rules;
size_t sz;
for(i = 0; i < 10; i++) {
if(strlen(b[i].name)) {
tname = g_strdup(b[i].name);
if(!tname) {
tname = g_strdup(_("Unknown"));
}
tdate = g_strdup(b[i].date);
if(!tdate) {
tdate = g_strdup(_("Unknown"));
}
rules = rules_get_as_str();
buf = g_strdup_printf("%s\t%i\t%s\t%s\n", tname, b[i].score, tdate, rules);
sz = strlen(buf);
write(_score_fd, &sz, sizeof(sz));
write(_score_fd, buf, strlen(buf));
g_free(rules);
g_free(tdate);
g_free(tname);
g_free(buf);
}
}
for(i = 0; i < nbf; i++) {
if(strlen(bf[i].name)) {
buf = g_strdup_printf("%s\t%i\t%s\t%s\n", bf[i].name, bf[i].score, bf[i].date, bf[i].rules);
sz = strlen(buf);
write(_score_fd, &sz, sizeof(sz));
write(_score_fd, buf, strlen(buf));
g_free(buf);
}
}
sz = 0;
write(_score_fd, &sz, sizeof(sz));
if (child_writer_alive() == 0) {
return FALSE;
}
return TRUE;
for (i = 0; i < 10; i++)
{
if (strlen(b[i].name)) {
tname = g_strdup(b[i].name);
if (!tname) {
tname = g_strdup(_("Unknown"));
}
tdate = g_strdup(b[i].date);
if (!tdate) {
tdate = g_strdup(_("Unknown"));
}
rules = rules_get_as_str();
buf = g_strdup_printf("%s\t%i\t%s\t%s\n", tname, b[i].score, tdate, rules);
sz = strlen(buf);
write(_score_fd, &sz, sizeof(sz));
write(_score_fd, buf, strlen(buf));
g_free(rules);
g_free(tdate);
g_free(tname);
g_free(buf);
}
}
for (i = 0; i < nbf; i++) {
if (strlen(bf[i].name)) {
buf = g_strdup_printf("%s\t%i\t%s\t%s\n", bf[i].name, bf[i].score, bf[i].date, bf[i].rules);
sz = strlen(buf);
write(_score_fd, &sz, sizeof(sz));
write(_score_fd, buf, strlen(buf));
g_free(buf);
}
}
sz = 0;
write(_score_fd, &sz, sizeof(sz));
return TRUE;
}
int score_sort(const void *a, const void *b) {
if(((const struct score_board *)a)->score == ((const struct score_board *)b)->score) return 0;
if(((const struct score_board *)a)->score < ((const struct score_board *)b)->score) return 1;
return -1;
int score_sort(const void *a, const void *b)
{
if (((const struct score_board *)a)->score == ((const struct score_board *)b)->score) return 0;
if (((const struct score_board *)a)->score < ((const struct score_board *)b)->score) return 1;
return -1;
}
gint read_score(struct score_board *b, struct score_board_full **bf, gint *nbf) {
FILE *fp;
gchar buffer[BUFFER_SIZE];
gchar **str_val, *tstr, **tstr_val;
gint valid, sc, fsc;
gsize br, bw;
struct flock lockinfo;
gchar *g_rules = NULL;
gint read_score(struct score_board *b, struct score_board_full **bf, gint *nbf)
{
FILE *fp;
gchar buffer[BUFFER_SIZE];
gchar **str_val, *tstr, **tstr_val;
gint valid, sc, fsc;
gsize br, bw;
struct flock lockinfo;
memset(b, 0, sizeof(struct score_board) * 10);
gchar *g_rules = NULL;
if(!(fp = fopen(LOCALSTATEDIR SCORE_FILE, "r"))) {
return FALSE;
}
memset(b, 0, sizeof(struct score_board) * 10);
do {
lockinfo.l_whence = SEEK_SET;
lockinfo.l_start = 0;
lockinfo.l_len = 0;
lockinfo.l_type = F_WRLCK;
fcntl(fileno(fp), F_GETLK, &lockinfo);
} while(lockinfo.l_type != F_UNLCK);
if (!(fp = fopen(LOCALSTATEDIR SCORE_FILE, "r"))) {
return FALSE;
}
sc = 0;
fsc = 0;
while(fgets(buffer, BUFFER_SIZE, fp)) {
g_strchomp(buffer);
str_val = g_strsplit(buffer, "\t", 4);
if(str_val[0] && str_val[0][0] &&
str_val[1] && str_val[1][0] &&
str_val[2] && str_val[2][0]) {
valid = 0;
if(!str_val[3]) { /* < 2.2.0 file format */
g_rules = rules_get_classic_as_str();
valid = 1;
} else {
if((valid = rules_check_str(str_val[3]))) {
g_rules = g_strdup(str_val[3]);
} else { /* > 2.2.0 && < 3.0.2 file format ? */
tstr_val = g_strsplit(str_val[3], "\t", 5);
if(tstr_val[0] && tstr_val[1] && tstr_val[2] &&
tstr_val[3] && tstr_val[4]) {
g_rules = rules_conv_3_0_to_str(tstr_val[0], tstr_val[1], tstr_val[2], tstr_val[3], tstr_val[4]);
if(g_rules) { /* yes. its < 3.0.2 format */
valid = 1;
} else { /* nope. just piece of shit. */
g_rules = g_strdup("");
}
}
g_strfreev(tstr_val);
}
}
if(valid && g_ascii_strcasecmp(str_val[0], "<none>")) {
if(rules_is_current_str(g_rules)) {
if(g_utf8_validate(str_val[0], -1, NULL)) {
tstr = g_strdup(str_val[0]);
} else {
tstr = g_locale_to_utf8(str_val[0], -1, &br, &bw, NULL);
}
if(tstr) {
strncpy(b[sc].name, tstr, sizeof(b[sc].name));
g_free(tstr);
} else {
strncpy(b[sc].name, _("Unknown"), sizeof(b[sc].name));
}
b[sc].score = strtol(str_val[1], NULL, 10);
if((b[sc].score == LONG_MIN) || (b[sc].score == LONG_MAX)) {
b[sc].score = 0;
}
if(g_utf8_validate(str_val[2], -1, NULL)) {
tstr = g_strdup(str_val[2]);
} else {
tstr = g_locale_to_utf8(str_val[2], -1, &br, &bw, NULL);
}
if(tstr) {
strncpy(b[sc].date, tstr, sizeof(b[sc].date));
g_free(tstr);
} else {
strncpy(b[sc].date, _("Unknown"), sizeof(b[sc].date));
}
sc++;
} else if(bf) {
*bf = g_realloc(*bf, sizeof(struct score_board_full) * (fsc + 1));
strncpy((*bf)[fsc].name, str_val[0], sizeof((*bf)[fsc].name));
(*bf)[fsc].score = strtol(str_val[1], NULL, 10);
if(((*bf)[fsc].score == LONG_MIN) || ((*bf)[fsc].score == LONG_MAX)) {
(*bf)[fsc].score = 0;
}
strncpy((*bf)[fsc].date, str_val[2], sizeof((*bf)[fsc].date));
(*bf)[fsc].rules = g_strdup(g_rules);
fsc++;
}
}
g_free(g_rules);
}
g_strfreev(str_val);
}
fclose(fp);
do {
lockinfo.l_whence = SEEK_SET;
lockinfo.l_start = 0;
lockinfo.l_len = 0;
lockinfo.l_type = F_WRLCK;
fcntl(fileno(fp), F_GETLK, &lockinfo);
} while (lockinfo.l_type != F_UNLCK);
qsort(b, 10, sizeof(struct score_board), score_sort);
sc = 0;
fsc = 0;
while (fgets(buffer, BUFFER_SIZE, fp))
{
g_strchomp(buffer);
str_val = g_strsplit(buffer, "\t", 4);
if (str_val[0] && str_val[0][0] &&
str_val[1] && str_val[1][0] &&
str_val[2] && str_val[2][0])
{
valid = 0;
if (!str_val[3]) { /* < 2.2.0 file for mat */
g_rules = rules_get_classic_as_str();
valid = 1;
} else {
if ((valid = rules_check_str(str_val[3]))) {
g_rules = g_strdup(str_val[3]);
} else { /* > 2.2.0 && < 3.0.2 file for mat ? */
tstr_val = g_strsplit(str_val[3], "\t", 5);
if (tstr_val[0] && tstr_val[1] && tstr_val[2] &&
tstr_val[3] && tstr_val[4]) {
g_rules = rules_conv_3_0_to_str(tstr_val[0], tstr_val[1], tstr_val[2], tstr_val[3], tstr_val[4]);
if (g_rules) { /* yes. its < 3.0.2 for mat */
valid = 1;
} else { /* nope. just piece of shit. */
g_rules = g_strdup("");
}
}
g_strfreev(tstr_val);
}
}
if (valid && g_ascii_strcasecmp(str_val[0], "<none>")) {
if (rules_is_current_str(g_rules)) {
if (g_utf8_validate(str_val[0], -1, NULL)) {
tstr = g_strdup(str_val[0]);
} else {
tstr = g_locale_to_utf8(str_val[0], -1, &br, &bw, NULL);
}
if (tstr) {
strncpy(b[sc].name, tstr, sizeof(b[sc].name));
g_free(tstr);
} else {
strncpy(b[sc].name, _("Unknown"), sizeof(b[sc].name));
}
b[sc].score = strtol(str_val[1], NULL, 10);
if ((b[sc].score == LONG_MIN) || (b[sc].score == LONG_MAX)) {
b[sc].score = 0;
}
if (g_utf8_validate(str_val[2], -1, NULL)) {
tstr = g_strdup(str_val[2]);
} else {
tstr = g_locale_to_utf8(str_val[2], -1, &br, &bw, NULL);
}
if (tstr) {
strncpy(b[sc].date, tstr, sizeof(b[sc].date));
g_free(tstr);
} else {
strncpy(b[sc].date, _("Unknown"), sizeof(b[sc].date));
}
sc++;
} else if (bf) {
*bf = g_realloc(*bf, sizeof(struct score_board_full) * (fsc + 1));
strncpy((*bf)[fsc].name, str_val[0], sizeof((*bf)[fsc].name));
(*bf)[fsc].score = strtol(str_val[1], NULL, 10);
if (((*bf)[fsc].score == LONG_MIN) || ((*bf)[fsc].score == LONG_MAX)) {
(*bf)[fsc].score = 0;
}
strncpy((*bf)[fsc].date, str_val[2], sizeof((*bf)[fsc].date));
(*bf)[fsc].rules = g_strdup(g_rules);
fsc++;
}
}
g_free(g_rules);
}
g_strfreev(str_val);
}
fclose(fp);
if(nbf) {
*nbf = fsc;
}
qsort(b, 10, sizeof(struct score_board), score_sort);
return TRUE;
if (nbf) {
*nbf = fsc;
}
return TRUE;
}
gint insert_entry_in_score_board(struct score_board *board, struct score_board entry) {
gint i=0,j;
if(entry.score <= 0) {
return -1;
}
gint insert_entry_in_score_board(struct score_board *board, struct score_board entry)
{
gint i=0,j;
while(i < 10 && board[i].score > entry.score) {
i++;
}
if (entry.score <= 0) {
return -1;
}
if(i > 9) {
return -1;
}
while (i < 10 && board[i].score > entry.score) {
i++;
}
for(j = 8;j >= i; j--) {
strncpy(board[j + 1].name, board[j].name, sizeof(board[j + 1].name));
strncpy(board[j + 1].date, board[j].date, sizeof(board[j + 1].date));
board[j + 1].score = board[j].score;
}
if (i > 9) {
return -1;
}
strncpy(board[i].name, entry.name, sizeof(board[i].name));
strncpy(board[i].date, entry.date, sizeof(board[i].date));
board[i].score = entry.score;
for (j = 8;j >= i; j--) {
strncpy(board[j + 1].name, board[j].name, sizeof(board[j + 1].name));
strncpy(board[j + 1].date, board[j].date, sizeof(board[j + 1].date));
board[j + 1].score = board[j].score;
}
return i;
strncpy(board[i].name, entry.name, sizeof(board[i].name));
strncpy(board[i].date, entry.date, sizeof(board[i].date));
board[i].score = entry.score;
return i;
}

View File

@ -4,16 +4,16 @@
#define SCORE_FILE "/gtkballs-scores"
struct score_board {
gchar name[30];
gint score;
gchar date[60];
gchar name[30];
gint score;
gchar date[60];
};
struct score_board_full {
gchar name[30];
gint score;
gchar date[60];
gchar *rules;
gchar name[30];
gint score;
gchar date[60];
gchar *rules;
};
gint score_setup(void);
@ -22,6 +22,6 @@ void free_score_board_full(struct score_board_full *bf, gint nbf);
gint write_score(struct score_board *, struct score_board_full *, gint);
gint read_score(struct score_board *, struct score_board_full **, gint *);
gint insert_entry_in_score_board(struct score_board *board,
struct score_board entry);
struct score_board entry);
#endif /* __SCOREBOARD_H__ */