summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMagnus Holmgren <magnushol@gmail.com>2007-02-15 22:13:14 +0000
committerMagnus Holmgren <magnushol@gmail.com>2007-02-15 22:13:14 +0000
commit375b7e0f85a1cfee58e6d359d83df6f3e9e44cb5 (patch)
treec22d197e99da7317d5b3cfe0d5746f6aaeeeb7ab
parent1f1da8e4aecc8595546cea5b5bbd545d6685be57 (diff)
downloadrockbox-375b7e0f85a1cfee58e6d359d83df6f3e9e44cb5.tar.gz
rockbox-375b7e0f85a1cfee58e6d359d83df6f3e9e44cb5.zip
Size-optimize the bookmarking code a bit (~450 bytes code, 350 bytes BSS on ColdFire).
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@12320 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--apps/bookmark.c310
1 files changed, 122 insertions, 188 deletions
diff --git a/apps/bookmark.c b/apps/bookmark.c
index e6c6261856..0bcdb131ca 100644
--- a/apps/bookmark.c
+++ b/apps/bookmark.c
@@ -68,8 +68,13 @@ static void display_bookmark(const char* bookmark,
68 int bookmark_count); 68 int bookmark_count);
69static void say_bookmark(const char* bookmark, 69static void say_bookmark(const char* bookmark,
70 int bookmark_id); 70 int bookmark_id);
71static bool play_bookmark(const char* bookmark);
71static bool generate_bookmark_file_name(const char *in); 72static bool generate_bookmark_file_name(const char *in);
72static char* get_bookmark(const char* bookmark_file, int bookmark_count); 73static char* get_bookmark(const char* bookmark_file, int bookmark_count);
74static const char* skip_token(const char* s);
75static const char* int_token(const char* s, int* dest);
76static const char* long_token(const char* s, long* dest);
77static const char* bool_token(const char* s, bool* dest);
73static bool parse_bookmark(const char *bookmark, 78static bool parse_bookmark(const char *bookmark,
74 int *resume_index, 79 int *resume_index,
75 int *resume_offset, 80 int *resume_offset,
@@ -109,48 +114,22 @@ bool bookmark_create_menu(void)
109/* ----------------------------------------------------------------------- */ 114/* ----------------------------------------------------------------------- */
110bool bookmark_load_menu(void) 115bool bookmark_load_menu(void)
111{ 116{
112 bool success = true; 117 if (system_check())
113 int offset;
114 int seed;
115 int index;
116 char* bookmark;
117
118 if(!system_check())
119 return false;
120 else
121 { 118 {
122 char* name = playlist_get_name(NULL, global_temp_buffer, 119 char* name = playlist_get_name(NULL, global_temp_buffer,
123 sizeof(global_temp_buffer)); 120 sizeof(global_temp_buffer));
124 if (generate_bookmark_file_name(name)) 121 if (generate_bookmark_file_name(name))
125 { 122 {
126 bookmark = select_bookmark(global_bookmark_file_name); 123 char* bookmark = select_bookmark(global_bookmark_file_name);
127 if (!bookmark) 124
128 return false; /* User exited without selecting a bookmark */ 125 if (bookmark != NULL)
129 126 {
130 success = parse_bookmark(bookmark, 127 return play_bookmark(bookmark);
131 &index, 128 }
132 &offset,
133 &seed,
134 NULL,
135 global_temp_buffer,
136 sizeof(global_temp_buffer),
137 NULL,
138 &global_settings.repeat_mode,
139 &global_settings.playlist_shuffle,
140 global_filename);
141 }
142 else
143 {
144 /* something bad happened while creating bookmark name*/
145 success = false;
146 } 129 }
147
148 if (success)
149 bookmark_play(global_temp_buffer, index, offset, seed,
150 global_filename);
151 } 130 }
152 131
153 return success; 132 return false;
154} 133}
155 134
156/* ----------------------------------------------------------------------- */ 135/* ----------------------------------------------------------------------- */
@@ -159,36 +138,16 @@ bool bookmark_load_menu(void)
159/* ----------------------------------------------------------------------- */ 138/* ----------------------------------------------------------------------- */
160bool bookmark_mrb_load() 139bool bookmark_mrb_load()
161{ 140{
162 bool success = true; 141 char* bookmark = select_bookmark(RECENT_BOOKMARK_FILE);
163 int offset;
164 int seed;
165 int index;
166 char* bookmark;
167 142
168 bookmark = select_bookmark(RECENT_BOOKMARK_FILE); 143 if (bookmark != NULL)
169 if (!bookmark) 144 {
170 return false; /* User exited without selecting a bookmark */ 145 return play_bookmark(bookmark);
171 146 }
172 success = parse_bookmark(bookmark,
173 &index,
174 &offset,
175 &seed,
176 NULL,
177 global_temp_buffer,
178 sizeof(global_temp_buffer),
179 NULL,
180 &global_settings.repeat_mode,
181 &global_settings.playlist_shuffle,
182 global_filename);
183
184 if (success)
185 bookmark_play(global_temp_buffer, index, offset, seed,
186 global_filename);
187 147
188 return success; 148 return false;
189} 149}
190 150
191
192/* ----------------------------------------------------------------------- */ 151/* ----------------------------------------------------------------------- */
193/* This function handles an autobookmark creation. This is an interface */ 152/* This function handles an autobookmark creation. This is an interface */
194/* function. */ 153/* function. */
@@ -483,10 +442,6 @@ bool bookmark_autoload(const char* file)
483bool bookmark_load(const char* file, bool autoload) 442bool bookmark_load(const char* file, bool autoload)
484{ 443{
485 int fd; 444 int fd;
486 bool success = true;
487 int offset;
488 int seed;
489 int index;
490 char* bookmark = NULL;; 445 char* bookmark = NULL;;
491 446
492 if(autoload) 447 if(autoload)
@@ -502,32 +457,15 @@ bool bookmark_load(const char* file, bool autoload)
502 else 457 else
503 { 458 {
504 /* This is not an auto-load, so list the bookmarks */ 459 /* This is not an auto-load, so list the bookmarks */
505 bookmark=select_bookmark(file); 460 bookmark = select_bookmark(file);
506 if(!bookmark)
507 return true; /* User exited without selecting a bookmark */
508 } 461 }
509 462
510 if(bookmark) 463 if (bookmark != NULL)
511 { 464 {
512 success = parse_bookmark(bookmark, 465 return play_bookmark(bookmark);
513 &index,
514 &offset,
515 &seed,
516 NULL,
517 global_temp_buffer,
518 sizeof(global_temp_buffer),
519 NULL,
520 &global_settings.repeat_mode,
521 &global_settings.playlist_shuffle,
522 global_filename);
523
524 } 466 }
525 467
526 if(success) 468 return true;
527 bookmark_play(global_temp_buffer, index, offset, seed,
528 global_filename);
529
530 return success;
531} 469}
532 470
533 471
@@ -914,6 +852,34 @@ static void say_bookmark(const char* bookmark,
914 talk_value((ms % 60000) / 1000, UNIT_SEC, true); 852 talk_value((ms % 60000) / 1000, UNIT_SEC, true);
915} 853}
916 854
855/* ----------------------------------------------------------------------- */
856/* This function parses a bookmark and then plays it. */
857/* ------------------------------------------------------------------------*/
858static bool play_bookmark(const char* bookmark)
859{
860 int index;
861 int offset;
862 int seed;
863
864 if (parse_bookmark(bookmark,
865 &index,
866 &offset,
867 &seed,
868 NULL,
869 global_temp_buffer,
870 sizeof(global_temp_buffer),
871 NULL,
872 &global_settings.repeat_mode,
873 &global_settings.playlist_shuffle,
874 global_filename))
875 {
876 bookmark_play(global_temp_buffer, index, offset, seed,
877 global_filename);
878 return true;
879 }
880
881 return false;
882}
917 883
918/* ----------------------------------------------------------------------- */ 884/* ----------------------------------------------------------------------- */
919/* This function retrieves a given bookmark from a file. */ 885/* This function retrieves a given bookmark from a file. */
@@ -956,6 +922,51 @@ static char* get_bookmark(const char* bookmark_file, int bookmark_count)
956 return NULL; 922 return NULL;
957} 923}
958 924
925static const char* skip_token(const char* s)
926{
927 while (*s && *s != ';')
928 {
929 s++;
930 }
931
932 if (*s)
933 {
934 s++;
935 }
936
937 return s;
938}
939
940static const char* int_token(const char* s, int* dest)
941{
942 if (dest != NULL)
943 {
944 *dest = atoi(s);
945 }
946
947 return skip_token(s);
948}
949
950static const char* long_token(const char* s, long* dest)
951{
952 if (dest != NULL)
953 {
954 *dest = atoi(s); /* Should be atol, but we don't have it. */
955 }
956
957 return skip_token(s);
958}
959
960static const char* bool_token(const char* s, bool* dest)
961{
962 if (dest != NULL)
963 {
964 *dest = atoi(s) != 0;
965 }
966
967 return skip_token(s);
968}
969
959/* ----------------------------------------------------------------------- */ 970/* ----------------------------------------------------------------------- */
960/* This function takes a bookmark and parses it. This function also */ 971/* This function takes a bookmark and parses it. This function also */
961/* validates the bookmark. Passing in NULL for an output variable */ 972/* validates the bookmark. Passing in NULL for an output variable */
@@ -972,117 +983,40 @@ static bool parse_bookmark(const char *bookmark,
972 int * repeat_mode, bool *shuffle, 983 int * repeat_mode, bool *shuffle,
973 char* file_name) 984 char* file_name)
974{ 985{
975 /* First check to see if a valid line was passed in. */ 986 const char* s = bookmark;
976 int bookmark_len = strlen(bookmark); 987 const char* end;
977 int local_resume_index = 0; 988
978 int local_resume_offset = 0; 989 s = int_token(s, resume_index);
979 int local_resume_seed = 0; 990 s = int_token(s, resume_offset);
980 int local_resume_first_index = 0; 991 s = int_token(s, resume_seed);
981 int local_mS = 0; 992 s = int_token(s, resume_first_index);
982 int local_shuffle = 0; 993 s = long_token(s, ms);
983 int local_repeat_mode = 0; 994 s = int_token(s, repeat_mode);
984 char* local_resume_file = NULL; 995 s = bool_token(s, shuffle);
985 char* local_file_name = NULL; 996
986 char* field; 997 if (*s == 0)
987 char* end;
988 static char bookmarkcopy[MAX_BOOKMARK_SIZE];
989
990 /* Don't do anything if the bookmark length is 0 */
991 if (bookmark_len <= 0)
992 return false;
993
994 /* Making a dup of the bookmark to use with strtok_r */
995 strncpy(bookmarkcopy, bookmark, sizeof(bookmarkcopy));
996 bookmarkcopy[sizeof(bookmarkcopy) - 1] = 0;
997
998 /* resume_index */
999 if ((field = strtok_r(bookmarkcopy, ";", &end)))
1000 local_resume_index = atoi(field);
1001 else
1002 return false;
1003
1004 /* resume_offset */
1005 if ((field = strtok_r(NULL, ";", &end)))
1006 local_resume_offset = atoi(field);
1007 else
1008 return false;
1009
1010 /* resume_seed */
1011 if ((field = strtok_r(NULL, ";", &end)))
1012 local_resume_seed = atoi(field);
1013 else
1014 return false;
1015
1016 /* resume_first_index */
1017 if ((field = strtok_r(NULL, ";", &end)))
1018 local_resume_first_index = atoi(field);
1019 else
1020 return false;
1021
1022 /* Milliseconds into MP3. Used for the bookmark select menu */
1023 if ((field = strtok_r(NULL, ";", &end)))
1024 local_mS = atoi(field);
1025 else
1026 return false;
1027
1028 /* repeat_mode */
1029 if ((field = strtok_r(NULL, ";", &end)))
1030 local_repeat_mode = atoi(field);
1031 else
1032 return false;
1033
1034 /* shuffle mode */
1035 if ((field = strtok_r(NULL, ";", &end)))
1036 local_shuffle = atoi(field);
1037 else
1038 return false;
1039
1040 /* resume_file & file_name (for the bookmark select menu)*/
1041 if (end)
1042 { 998 {
1043 local_resume_file = strtok_r(NULL, ";", &end);
1044
1045 if (end)
1046 local_file_name = strtok_r(NULL, ";", &end);
1047 }
1048 else
1049 return false; 999 return false;
1050
1051 /* Only return the values the calling function wants */
1052 if (resume_index)
1053 *resume_index = local_resume_index;
1054
1055 if (resume_offset)
1056 *resume_offset = local_resume_offset;
1057
1058 if (resume_seed)
1059 *resume_seed = local_resume_seed;
1060
1061 if (resume_first_index)
1062 *resume_first_index = local_resume_first_index;
1063
1064 if (resume_file && local_resume_file)
1065 {
1066 strncpy(resume_file, local_resume_file,
1067 MIN(strlen(local_resume_file), resume_file_size-1));
1068 resume_file[MIN(strlen(local_resume_file), resume_file_size-1)]=0;
1069 } 1000 }
1001
1002 end = strchr(s, ';');
1070 1003
1071 if (ms) 1004 if (resume_file != NULL)
1072 *ms = local_mS; 1005 {
1073 1006 size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
1074 if (shuffle)
1075 *shuffle = local_shuffle;
1076 1007
1077 if (repeat_mode) 1008 len = MIN(resume_file_size - 1, len);
1078 *repeat_mode = local_repeat_mode; 1009 strncpy(resume_file, s, len);
1010 resume_file[len] = 0;
1011 }
1079 1012
1080 if (file_name && local_file_name) 1013 if (end != NULL && file_name != NULL)
1081 { 1014 {
1082 strncpy(file_name, local_file_name,MAX_PATH-1); 1015 end++;
1083 file_name[MAX_PATH-1] = 0; 1016 strncpy(file_name, end, MAX_PATH - 1);
1017 file_name[MAX_PATH - 1] = 0;
1084 } 1018 }
1085 1019
1086 return true; 1020 return true;
1087} 1021}
1088 1022