diff options
author | Linus Nielsen Feltzing <linus@haxx.se> | 2005-06-08 13:47:46 +0000 |
---|---|---|
committer | Linus Nielsen Feltzing <linus@haxx.se> | 2005-06-08 13:47:46 +0000 |
commit | fa148bb81260844a77ef2f988f46fb8584bfb3a2 (patch) | |
tree | 97221c629e47f8bc45a56e19654d1f5c6a1b965f /apps/plugins/lib/highscore.c | |
parent | 9415629f4c3cf7e81aa89e37bcd0b26c24103ed9 (diff) | |
download | rockbox-fa148bb81260844a77ef2f988f46fb8584bfb3a2.tar.gz rockbox-fa148bb81260844a77ef2f988f46fb8584bfb3a2.zip |
A new API for saving highscores in game plugins
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@6611 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/plugins/lib/highscore.c')
-rw-r--r-- | apps/plugins/lib/highscore.c | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/apps/plugins/lib/highscore.c b/apps/plugins/lib/highscore.c new file mode 100644 index 0000000000..9fbcdf25a3 --- /dev/null +++ b/apps/plugins/lib/highscore.c | |||
@@ -0,0 +1,99 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2005 Linus Nielsen Feltzing | ||
11 | * | ||
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. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | #include "plugin.h" | ||
20 | #include "highscore.h" | ||
21 | |||
22 | static struct plugin_api *rb; | ||
23 | |||
24 | void highscore_init(struct plugin_api* newrb) | ||
25 | { | ||
26 | rb = newrb; | ||
27 | } | ||
28 | |||
29 | int highscore_save(char *filename, struct highscore *scores, int num_scores) | ||
30 | { | ||
31 | int i; | ||
32 | int fd; | ||
33 | int rc; | ||
34 | char buf[80]; | ||
35 | |||
36 | fd = rb->open(filename, O_WRONLY|O_CREAT); | ||
37 | if(fd < 0) | ||
38 | return -1; | ||
39 | |||
40 | for(i = 0;i < num_scores;i++) | ||
41 | { | ||
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)); | ||
45 | if(rc < 0) | ||
46 | { | ||
47 | rb->close(fd); | ||
48 | return -2; | ||
49 | } | ||
50 | } | ||
51 | rb->close(fd); | ||
52 | return 0; | ||
53 | } | ||
54 | |||
55 | int highscore_load(char *filename, struct highscore *scores, int num_scores) | ||
56 | { | ||
57 | int i; | ||
58 | int fd; | ||
59 | char buf[80]; | ||
60 | char *name, *score, *level; | ||
61 | char *ptr; | ||
62 | |||
63 | fd = rb->open(filename, O_RDONLY); | ||
64 | if(fd < 0) | ||
65 | return -1; | ||
66 | |||
67 | rb->memset(scores, 0, sizeof(struct highscore)*num_scores); | ||
68 | |||
69 | i = -1; | ||
70 | while(rb->read_line(fd, buf, sizeof(buf)-1) && i < num_scores) | ||
71 | { | ||
72 | i++; | ||
73 | |||
74 | DEBUGF("%s\n", buf); | ||
75 | name = buf; | ||
76 | ptr = rb->strchr(buf, ':'); | ||
77 | if ( !ptr ) | ||
78 | continue; | ||
79 | *ptr = 0; | ||
80 | ptr++; | ||
81 | |||
82 | rb->strncpy(scores[i].name, name, sizeof(scores[i].name)); | ||
83 | |||
84 | DEBUGF("%s\n", scores[i].name); | ||
85 | score = ptr; | ||
86 | |||
87 | ptr = rb->strchr(ptr, ':'); | ||
88 | if ( !ptr ) | ||
89 | continue; | ||
90 | *ptr = 0; | ||
91 | ptr++; | ||
92 | |||
93 | scores[i].score = rb->atoi(score); | ||
94 | |||
95 | level = ptr; | ||
96 | scores[i].level = rb->atoi(level); | ||
97 | } | ||
98 | return 0; | ||
99 | } | ||