1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "highscore.h"
22 static struct plugin_api
*rb
;
24 void highscore_init(struct plugin_api
* newrb
)
29 int highscore_save(char *filename
, struct highscore
*scores
, int num_scores
)
36 fd
= rb
->open(filename
, O_WRONLY
|O_CREAT
);
40 for(i
= 0;i
< num_scores
;i
++)
42 rb
->snprintf(buf
, sizeof(buf
)-1, "%s:%d:%d\n",
43 scores
[i
].name
, scores
[i
].score
, scores
[i
].level
);
44 rc
= rb
->write(fd
, buf
, rb
->strlen(buf
));
55 int highscore_load(char *filename
, struct highscore
*scores
, int num_scores
)
60 char *name
, *score
, *level
;
63 fd
= rb
->open(filename
, O_RDONLY
);
65 rb
->memset(scores
, 0, sizeof(struct highscore
)*(num_scores
+1));
71 while(rb
->read_line(fd
, buf
, sizeof(buf
)-1) && i
< num_scores
)
77 ptr
= rb
->strchr(buf
, ':');
83 rb
->strncpy(scores
[i
].name
, name
, sizeof(scores
[i
].name
));
85 DEBUGF("%s\n", scores
[i
].name
);
88 ptr
= rb
->strchr(ptr
, ':');
94 scores
[i
].score
= rb
->atoi(score
);
97 scores
[i
].level
= rb
->atoi(level
);
102 int highscore_update(int score
, int level
, struct highscore
*scores
, int num_scores
)
107 /* look through the scores and see if this one is in the top ones */
108 for(i
= num_scores
-1;i
>= 0; i
--)
110 if ((score
> scores
[i
].score
))
112 /* Move the rest down one... */
117 rb
->memcpy((void *)&scores
[j
-1], (void *)&scores
[j
], sizeof(struct highscore
));
120 scores
[i
].score
= score
;
121 scores
[i
].level
= level
;
122 /* Need to sort out entering a name... maybe old three letter arcade style */