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 int highscore_save(char *filename
, struct highscore
*scores
, int num_scores
)
31 fd
= rb
->open(filename
, O_WRONLY
|O_CREAT
);
35 for(i
= 0;i
< num_scores
;i
++)
37 rb
->snprintf(buf
, sizeof(buf
), "%d:%d:%s\n",
38 scores
[i
].score
, scores
[i
].level
, scores
[i
].name
);
39 rc
= rb
->write(fd
, buf
, rb
->strlen(buf
));
50 int highscore_load(char *filename
, struct highscore
*scores
, int num_scores
)
55 char *score
, *level
, *name
;
57 rb
->memset(scores
, 0, sizeof(struct highscore
)*num_scores
);
59 fd
= rb
->open(filename
, O_RDONLY
);
64 while(rb
->read_line(fd
, buf
, sizeof(buf
)) > 0 && i
< num_scores
)
68 if ( !rb
->settings_parseline(buf
, &score
, &level
) )
70 if ( !rb
->settings_parseline(level
, &level
, &name
) )
73 scores
[i
].score
= rb
->atoi(score
);
74 scores
[i
].level
= rb
->atoi(level
);
75 rb
->strncpy(scores
[i
].name
, name
, sizeof(scores
[i
].name
)-1);
82 int highscore_update(int score
, int level
, const char *name
,
83 struct highscore
*scores
, int num_scores
)
86 struct highscore
*entry
;
88 if (!highscore_would_update(score
, scores
, num_scores
))
92 while (pos
> 0 && score
> scores
[pos
-1].score
)
95 rb
->memcpy((void *)&scores
[pos
], (void *)&scores
[pos
-1],
96 sizeof(struct highscore
));
100 entry
= scores
+ pos
;
101 entry
->score
= score
;
102 entry
->level
= level
;
103 rb
->strncpy(entry
->name
, name
, sizeof(entry
->name
));
104 entry
->name
[sizeof(entry
->name
)-1] = '\0';
109 bool highscore_would_update(int score
, struct highscore
*scores
,
112 return (num_scores
> 0) && (score
> scores
[num_scores
-1].score
);