1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "highscore.h"
24 static const struct plugin_api
*rb
;
26 void highscore_init(const struct plugin_api
* newrb
)
31 int highscore_save(char *filename
, struct highscore
*scores
, int num_scores
)
38 fd
= rb
->open(filename
, O_WRONLY
|O_CREAT
);
42 for(i
= 0;i
< num_scores
;i
++)
44 rb
->snprintf(buf
, sizeof(buf
)-1, "%s:%d:%d\n",
45 scores
[i
].name
, scores
[i
].score
, scores
[i
].level
);
46 rc
= rb
->write(fd
, buf
, rb
->strlen(buf
));
57 int highscore_load(char *filename
, struct highscore
*scores
, int num_scores
)
62 char *name
, *score
, *level
;
65 fd
= rb
->open(filename
, O_RDONLY
);
67 rb
->memset(scores
, 0, sizeof(struct highscore
)*(num_scores
+1));
73 while(rb
->read_line(fd
, buf
, sizeof(buf
)-1) && i
< num_scores
)
79 ptr
= rb
->strchr(buf
, ':');
85 rb
->strncpy(scores
[i
].name
, name
, sizeof(scores
[i
].name
));
87 DEBUGF("%s\n", scores
[i
].name
);
90 ptr
= rb
->strchr(ptr
, ':');
96 scores
[i
].score
= rb
->atoi(score
);
99 scores
[i
].level
= rb
->atoi(level
);
104 int highscore_update(int score
, int level
, struct highscore
*scores
, int num_scores
)
109 /* look through the scores and see if this one is in the top ones */
110 for(i
= num_scores
-1;i
>= 0; i
--)
112 if ((score
> scores
[i
].score
))
114 /* Move the rest down one... */
119 rb
->memcpy((void *)&scores
[j
-1], (void *)&scores
[j
], sizeof(struct highscore
));
122 scores
[i
].score
= score
;
123 scores
[i
].level
= level
;
124 /* Need to sort out entering a name... maybe old three letter arcade style */