1
0
Fork 0
forked from len0rd/rockbox

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
This commit is contained in:
Magnus Holmgren 2007-02-15 22:13:14 +00:00
parent 1f1da8e4ae
commit 375b7e0f85

View file

@ -68,8 +68,13 @@ static void display_bookmark(const char* bookmark,
int bookmark_count); int bookmark_count);
static void say_bookmark(const char* bookmark, static void say_bookmark(const char* bookmark,
int bookmark_id); int bookmark_id);
static bool play_bookmark(const char* bookmark);
static bool generate_bookmark_file_name(const char *in); static bool generate_bookmark_file_name(const char *in);
static char* get_bookmark(const char* bookmark_file, int bookmark_count); static char* get_bookmark(const char* bookmark_file, int bookmark_count);
static const char* skip_token(const char* s);
static const char* int_token(const char* s, int* dest);
static const char* long_token(const char* s, long* dest);
static const char* bool_token(const char* s, bool* dest);
static bool parse_bookmark(const char *bookmark, static bool parse_bookmark(const char *bookmark,
int *resume_index, int *resume_index,
int *resume_offset, int *resume_offset,
@ -109,48 +114,22 @@ bool bookmark_create_menu(void)
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */
bool bookmark_load_menu(void) bool bookmark_load_menu(void)
{ {
bool success = true; if (system_check())
int offset;
int seed;
int index;
char* bookmark;
if(!system_check())
return false;
else
{ {
char* name = playlist_get_name(NULL, global_temp_buffer, char* name = playlist_get_name(NULL, global_temp_buffer,
sizeof(global_temp_buffer)); sizeof(global_temp_buffer));
if (generate_bookmark_file_name(name)) if (generate_bookmark_file_name(name))
{ {
bookmark = select_bookmark(global_bookmark_file_name); char* bookmark = select_bookmark(global_bookmark_file_name);
if (!bookmark)
return false; /* User exited without selecting a bookmark */
success = parse_bookmark(bookmark, if (bookmark != NULL)
&index, {
&offset, return play_bookmark(bookmark);
&seed, }
NULL,
global_temp_buffer,
sizeof(global_temp_buffer),
NULL,
&global_settings.repeat_mode,
&global_settings.playlist_shuffle,
global_filename);
} }
else
{
/* something bad happened while creating bookmark name*/
success = false;
}
if (success)
bookmark_play(global_temp_buffer, index, offset, seed,
global_filename);
} }
return success; return false;
} }
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */
@ -159,36 +138,16 @@ bool bookmark_load_menu(void)
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */
bool bookmark_mrb_load() bool bookmark_mrb_load()
{ {
bool success = true; char* bookmark = select_bookmark(RECENT_BOOKMARK_FILE);
int offset;
int seed;
int index;
char* bookmark;
bookmark = select_bookmark(RECENT_BOOKMARK_FILE); if (bookmark != NULL)
if (!bookmark) {
return false; /* User exited without selecting a bookmark */ return play_bookmark(bookmark);
}
success = parse_bookmark(bookmark, return false;
&index,
&offset,
&seed,
NULL,
global_temp_buffer,
sizeof(global_temp_buffer),
NULL,
&global_settings.repeat_mode,
&global_settings.playlist_shuffle,
global_filename);
if (success)
bookmark_play(global_temp_buffer, index, offset, seed,
global_filename);
return success;
} }
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */
/* This function handles an autobookmark creation. This is an interface */ /* This function handles an autobookmark creation. This is an interface */
/* function. */ /* function. */
@ -483,10 +442,6 @@ bool bookmark_autoload(const char* file)
bool bookmark_load(const char* file, bool autoload) bool bookmark_load(const char* file, bool autoload)
{ {
int fd; int fd;
bool success = true;
int offset;
int seed;
int index;
char* bookmark = NULL;; char* bookmark = NULL;;
if(autoload) if(autoload)
@ -502,32 +457,15 @@ bool bookmark_load(const char* file, bool autoload)
else else
{ {
/* This is not an auto-load, so list the bookmarks */ /* This is not an auto-load, so list the bookmarks */
bookmark=select_bookmark(file); bookmark = select_bookmark(file);
if(!bookmark)
return true; /* User exited without selecting a bookmark */
} }
if(bookmark) if (bookmark != NULL)
{ {
success = parse_bookmark(bookmark, return play_bookmark(bookmark);
&index,
&offset,
&seed,
NULL,
global_temp_buffer,
sizeof(global_temp_buffer),
NULL,
&global_settings.repeat_mode,
&global_settings.playlist_shuffle,
global_filename);
} }
if(success) return true;
bookmark_play(global_temp_buffer, index, offset, seed,
global_filename);
return success;
} }
@ -914,6 +852,34 @@ static void say_bookmark(const char* bookmark,
talk_value((ms % 60000) / 1000, UNIT_SEC, true); talk_value((ms % 60000) / 1000, UNIT_SEC, true);
} }
/* ----------------------------------------------------------------------- */
/* This function parses a bookmark and then plays it. */
/* ------------------------------------------------------------------------*/
static bool play_bookmark(const char* bookmark)
{
int index;
int offset;
int seed;
if (parse_bookmark(bookmark,
&index,
&offset,
&seed,
NULL,
global_temp_buffer,
sizeof(global_temp_buffer),
NULL,
&global_settings.repeat_mode,
&global_settings.playlist_shuffle,
global_filename))
{
bookmark_play(global_temp_buffer, index, offset, seed,
global_filename);
return true;
}
return false;
}
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */
/* This function retrieves a given bookmark from a file. */ /* This function retrieves a given bookmark from a file. */
@ -956,6 +922,51 @@ static char* get_bookmark(const char* bookmark_file, int bookmark_count)
return NULL; return NULL;
} }
static const char* skip_token(const char* s)
{
while (*s && *s != ';')
{
s++;
}
if (*s)
{
s++;
}
return s;
}
static const char* int_token(const char* s, int* dest)
{
if (dest != NULL)
{
*dest = atoi(s);
}
return skip_token(s);
}
static const char* long_token(const char* s, long* dest)
{
if (dest != NULL)
{
*dest = atoi(s); /* Should be atol, but we don't have it. */
}
return skip_token(s);
}
static const char* bool_token(const char* s, bool* dest)
{
if (dest != NULL)
{
*dest = atoi(s) != 0;
}
return skip_token(s);
}
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */
/* This function takes a bookmark and parses it. This function also */ /* This function takes a bookmark and parses it. This function also */
/* validates the bookmark. Passing in NULL for an output variable */ /* validates the bookmark. Passing in NULL for an output variable */
@ -972,115 +983,38 @@ static bool parse_bookmark(const char *bookmark,
int * repeat_mode, bool *shuffle, int * repeat_mode, bool *shuffle,
char* file_name) char* file_name)
{ {
/* First check to see if a valid line was passed in. */ const char* s = bookmark;
int bookmark_len = strlen(bookmark); const char* end;
int local_resume_index = 0;
int local_resume_offset = 0;
int local_resume_seed = 0;
int local_resume_first_index = 0;
int local_mS = 0;
int local_shuffle = 0;
int local_repeat_mode = 0;
char* local_resume_file = NULL;
char* local_file_name = NULL;
char* field;
char* end;
static char bookmarkcopy[MAX_BOOKMARK_SIZE];
/* Don't do anything if the bookmark length is 0 */ s = int_token(s, resume_index);
if (bookmark_len <= 0) s = int_token(s, resume_offset);
return false; s = int_token(s, resume_seed);
s = int_token(s, resume_first_index);
s = long_token(s, ms);
s = int_token(s, repeat_mode);
s = bool_token(s, shuffle);
/* Making a dup of the bookmark to use with strtok_r */ if (*s == 0)
strncpy(bookmarkcopy, bookmark, sizeof(bookmarkcopy));
bookmarkcopy[sizeof(bookmarkcopy) - 1] = 0;
/* resume_index */
if ((field = strtok_r(bookmarkcopy, ";", &end)))
local_resume_index = atoi(field);
else
return false;
/* resume_offset */
if ((field = strtok_r(NULL, ";", &end)))
local_resume_offset = atoi(field);
else
return false;
/* resume_seed */
if ((field = strtok_r(NULL, ";", &end)))
local_resume_seed = atoi(field);
else
return false;
/* resume_first_index */
if ((field = strtok_r(NULL, ";", &end)))
local_resume_first_index = atoi(field);
else
return false;
/* Milliseconds into MP3. Used for the bookmark select menu */
if ((field = strtok_r(NULL, ";", &end)))
local_mS = atoi(field);
else
return false;
/* repeat_mode */
if ((field = strtok_r(NULL, ";", &end)))
local_repeat_mode = atoi(field);
else
return false;
/* shuffle mode */
if ((field = strtok_r(NULL, ";", &end)))
local_shuffle = atoi(field);
else
return false;
/* resume_file & file_name (for the bookmark select menu)*/
if (end)
{ {
local_resume_file = strtok_r(NULL, ";", &end);
if (end)
local_file_name = strtok_r(NULL, ";", &end);
}
else
return false; return false;
/* Only return the values the calling function wants */
if (resume_index)
*resume_index = local_resume_index;
if (resume_offset)
*resume_offset = local_resume_offset;
if (resume_seed)
*resume_seed = local_resume_seed;
if (resume_first_index)
*resume_first_index = local_resume_first_index;
if (resume_file && local_resume_file)
{
strncpy(resume_file, local_resume_file,
MIN(strlen(local_resume_file), resume_file_size-1));
resume_file[MIN(strlen(local_resume_file), resume_file_size-1)]=0;
} }
if (ms) end = strchr(s, ';');
*ms = local_mS;
if (shuffle) if (resume_file != NULL)
*shuffle = local_shuffle;
if (repeat_mode)
*repeat_mode = local_repeat_mode;
if (file_name && local_file_name)
{ {
strncpy(file_name, local_file_name,MAX_PATH-1); size_t len = (end == NULL) ? strlen(s) : (size_t) (end - s);
file_name[MAX_PATH-1] = 0;
len = MIN(resume_file_size - 1, len);
strncpy(resume_file, s, len);
resume_file[len] = 0;
}
if (end != NULL && file_name != NULL)
{
end++;
strncpy(file_name, end, MAX_PATH - 1);
file_name[MAX_PATH - 1] = 0;
} }
return true; return true;