summaryrefslogtreecommitdiff
path: root/apps/gui/wps_parser.c
diff options
context:
space:
mode:
authorNicolas Pennequin <nicolas.pennequin@free.fr>2007-04-30 14:08:58 +0000
committerNicolas Pennequin <nicolas.pennequin@free.fr>2007-04-30 14:08:58 +0000
commit7e6af1532b29c06da3f527e1dba84e7284d8a42f (patch)
treea00a2f912c5cba0f54969adcc4c63a0fbe08047b /apps/gui/wps_parser.c
parent3a7760c3e2691d0367f5c0b5585fd885b99c6693 (diff)
downloadrockbox-7e6af1532b29c06da3f527e1dba84e7284d8a42f.zip
rockbox-7e6af1532b29c06da3f527e1dba84e7284d8a42f.tar.gz
rockbox-7e6af1532b29c06da3f527e1dba84e7284d8a42f.tar.bz2
rockbox-7e6af1532b29c06da3f527e1dba84e7284d8a42f.tar.xz
Better handling of strings in the WPS parser by detecting duplicates to avoid having two copies of the same string in the string buffer.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@13295 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'apps/gui/wps_parser.c')
-rw-r--r--apps/gui/wps_parser.c45
1 files changed, 32 insertions, 13 deletions
diff --git a/apps/gui/wps_parser.c b/apps/gui/wps_parser.c
index 6d8078a..442fd35 100644
--- a/apps/gui/wps_parser.c
+++ b/apps/gui/wps_parser.c
@@ -653,7 +653,7 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr)
if (!data || !wps_bufptr || !*wps_bufptr)
return false;
- char *current_string = data->string_buffer;
+ char *stringbuf = data->string_buffer;
int stringbuf_used = 0;
int fail = 0;
line = 1;
@@ -770,13 +770,9 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr)
&& stringbuf_used < STRING_BUFFER_SIZE - 1)
{
data->tokens[data->num_tokens].type = WPS_TOKEN_STRING;
- data->strings[data->num_strings] = current_string;
- data->tokens[data->num_tokens].value.i = data->num_strings;
- data->num_tokens++;
- /* Copy the first byte */
- *current_string++ = *(wps_bufptr - 1);
- stringbuf_used++;
+ unsigned int len = 1;
+ const char *string_start = wps_bufptr - 1;
/* continue until we hit something that ends the string
or we run out of memory */
@@ -786,15 +782,38 @@ static bool wps_parse(struct wps_data *data, const char *wps_bufptr)
*wps_bufptr != '|' && *wps_bufptr != '\n' &&
stringbuf_used < STRING_BUFFER_SIZE - 1)
{
- *current_string++ = *wps_bufptr++;
- stringbuf_used++;
+ wps_bufptr++;
+ len++;
}
- /* null terminate the string */
- *current_string++ = '\0';
- stringbuf_used++;
+ /* look if we already have that string */
+ char **str;
+ int i;
+ bool found;
+ for (i = 0, str = data->strings, found = false;
+ i < data->num_strings
+ && !(found = (strlen(*str) == len && strncmp(string_start, *str, len) == 0));
+ i++, str++) ;
+ /* If a matching string is found, found is true and i is the
+ index of the string. If not, found is false */
+
+ if (!found)
+ {
+ /* new string */
+ strncpy(stringbuf, string_start, len);
+ data->strings[data->num_strings] = stringbuf;
+ stringbuf += len + 1;
+ stringbuf_used += len + 1;
+ data->tokens[data->num_tokens].value.i = data->num_strings;
+ data->num_strings++;
+ }
+ else
+ {
+ /* another ocurrence of an existing string */
+ data->tokens[data->num_tokens].value.i = i;
+ }
- data->num_strings++;
+ data->num_tokens++;
}
break;
}