Rewrite read_scores() with suggestions from caf. Failing to open the scores file no longer leaks memory, and a malformed record no longer adds an empty link to the global scores list.

git-svn-id: svn://svn.code.sf.net/p/bitchx/code/trunk@253 13b04d17-f746-0410-82c6-800466cd88b0
This commit is contained in:
Tim Cava
2013-07-02 14:42:36 +00:00
parent 4ff0b8d2d5
commit ed1ad22d70
2 changed files with 19 additions and 36 deletions

View File

@@ -11,7 +11,7 @@ int Acro_Init(IrcCommandDll **intp, Function_ptr *global_table)
add_module_proc(RAW_PROC, "acro", "PRIVMSG", NULL, 0, 0, acro_main, NULL);
add_module_proc(COMMAND_PROC, "scores", "scores", NULL, 0, 0, put_scores, NULL);
gscores = read_scores();
read_scores();
if (!game)
game = init_acro(game);
put_it("BitchX Acromania dll v0.9b by By-Tor loaded...");
@@ -338,43 +338,26 @@ vrec *take_vote(grec *acro, vrec *voters, prec *players, char *nick, char *host,
return voters;
}
srec *read_scores()
void read_scores(void)
{
srec *tmp, *tmp2;
char buff[100], *p;
FILE *sf;
tmp = tmp2 = (srec *)new_malloc(sizeof(srec));
memset(buff, 0, sizeof(buff));
sf = fopen(SCOREFILE, "r");
if (!sf)
return 0;
while (!feof(sf))
FILE *infile;
srec *record;
unsigned long score;
char nick[64];
infile = fopen(SCOREFILE, "r");
if (infile == NULL)
return;
while (fscanf(infile, " %63[^ ,] , %lu", nick, &score) == 2)
{
if (fgets(buff, 51, sf))
{
if (tmp->nick)
tmp = tmp->next = (srec *)new_malloc(sizeof(srec));
if (buff[strlen(buff)-1] == '\n')
buff[strlen(buff)-1] = 0;
if (!*buff)
break;
if ((p = (char *)strchr(buff, ',')))
*p++ = 0;
else if (!p)
{
fclose(sf);
return tmp2;
record = new_malloc(sizeof(srec));
record->nick = m_strdup(nick);
record->score = score;
add_to_list((List **)&gscores, (List *)record);
}
tmp->nick = (char *)new_malloc(strlen(buff+1));
strcpy(tmp->nick, buff);
if (p)
tmp->score = strtoul(p, NULL, 10);
}
else
break;
}
fclose(sf);
return tmp2;
fclose(infile);
}
int write_scores(srec *tmp)

View File

@@ -98,7 +98,7 @@ BUILT_IN_DLL(put_scores);
grec *init_acro(grec *);
void make_acro(grec *);
int valid_acro(grec *, char *);
srec *read_scores(void);
void read_scores(void);
int write_scores(srec *);
prec *take_acro(grec *, prec *, char *, char *, char *);
vrec *take_vote(grec *, vrec *, prec *, char *, char *, char *);