diff options
author | Nicolas Pennequin <nicolas.pennequin@free.fr> | 2007-04-08 14:00:00 +0000 |
---|---|---|
committer | Nicolas Pennequin <nicolas.pennequin@free.fr> | 2007-04-08 14:00:00 +0000 |
commit | fd19b8dba443b24edf9a669b34f65066aba784cc (patch) | |
tree | 1057f77848492f9c26fef7d90e4db9b094130f6b | |
parent | d2688c860de8900e44ceb13f90f8986041f3eb6b (diff) | |
download | rockbox-fd19b8dba443b24edf9a669b34f65066aba784cc.tar.gz rockbox-fd19b8dba443b24edf9a669b34f65066aba784cc.zip |
* Improve conditional evaluation to allow using enums with only two cases (problem reported by roolku). This happens to simplify the code.
* Rename get_tag() to get_token_value() and make it get a token pointer instead of the index of a token. This also simplifies the code a bit.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@13073 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r-- | apps/gui/gwps-common.c | 81 |
1 files changed, 42 insertions, 39 deletions
diff --git a/apps/gui/gwps-common.c b/apps/gui/gwps-common.c index 078e20ba44..42ba17ad11 100644 --- a/apps/gui/gwps-common.c +++ b/apps/gui/gwps-common.c | |||
@@ -747,16 +747,16 @@ static char* get_dir(char* buf, int buf_size, const char* path, int level) | |||
747 | /* Return the tag found at index i and write its value in buf. | 747 | /* Return the tag found at index i and write its value in buf. |
748 | The return value is buf if the tag had a value, or NULL if not. | 748 | The return value is buf if the tag had a value, or NULL if not. |
749 | 749 | ||
750 | intval is used with enums: when this function is called, it should contain | 750 | intval is used with conditionals/enums: when this function is called, |
751 | the number of options in the enum. When this function returns, it will | 751 | intval should contain the number of options in the conditional/enum. |
752 | contain the enum case we are actually in. | 752 | When this function returns, intval is -1 if the tag is non numeric or, |
753 | When not treating an enum, intval should be NULL. | 753 | if the tag is numeric, intval is the enum case we want to go to. |
754 | When not treating a conditional/enum, intval should be NULL. | ||
754 | */ | 755 | */ |
755 | static char *get_tag(struct gui_wps *gwps, | 756 | static char *get_token_value(struct gui_wps *gwps, |
756 | int i, | 757 | struct wps_token *token, |
757 | char *buf, | 758 | char *buf, int buf_size, |
758 | int buf_size, | 759 | int *intval) |
759 | int *intval) | ||
760 | { | 760 | { |
761 | if (!gwps) | 761 | if (!gwps) |
762 | return NULL; | 762 | return NULL; |
@@ -769,7 +769,7 @@ static char *get_tag(struct gui_wps *gwps, | |||
769 | 769 | ||
770 | struct mp3entry *id3; | 770 | struct mp3entry *id3; |
771 | 771 | ||
772 | if (data->tokens[i].next) | 772 | if (token->next) |
773 | id3 = state->nid3; | 773 | id3 = state->nid3; |
774 | else | 774 | else |
775 | id3 = state->id3; | 775 | id3 = state->id3; |
@@ -779,19 +779,22 @@ static char *get_tag(struct gui_wps *gwps, | |||
779 | 779 | ||
780 | int limit = 1; | 780 | int limit = 1; |
781 | if (intval) | 781 | if (intval) |
782 | { | ||
782 | limit = *intval; | 783 | limit = *intval; |
784 | *intval = -1; | ||
785 | } | ||
783 | 786 | ||
784 | #if CONFIG_RTC | 787 | #if CONFIG_RTC |
785 | static struct tm* tm; | 788 | static struct tm* tm; |
786 | #endif | 789 | #endif |
787 | 790 | ||
788 | switch (data->tokens[i].type) | 791 | switch (token->type) |
789 | { | 792 | { |
790 | case WPS_TOKEN_CHARACTER: | 793 | case WPS_TOKEN_CHARACTER: |
791 | return &(data->tokens[i].value.c); | 794 | return &(token->value.c); |
792 | 795 | ||
793 | case WPS_TOKEN_STRING: | 796 | case WPS_TOKEN_STRING: |
794 | return data->strings[data->tokens[i].value.i]; | 797 | return data->strings[token->value.i]; |
795 | 798 | ||
796 | case WPS_TOKEN_TRACK_TIME_ELAPSED: | 799 | case WPS_TOKEN_TRACK_TIME_ELAPSED: |
797 | format_time(buf, buf_size, | 800 | format_time(buf, buf_size, |
@@ -949,7 +952,7 @@ static char *get_tag(struct gui_wps *gwps, | |||
949 | return id3->vbr ? "(avg)" : NULL; | 952 | return id3->vbr ? "(avg)" : NULL; |
950 | 953 | ||
951 | case WPS_TOKEN_FILE_DIRECTORY: | 954 | case WPS_TOKEN_FILE_DIRECTORY: |
952 | return get_dir(buf, buf_size, id3->path, data->tokens[i].value.i); | 955 | return get_dir(buf, buf_size, id3->path, token->value.i); |
953 | 956 | ||
954 | case WPS_TOKEN_BATTERY_PERCENT: | 957 | case WPS_TOKEN_BATTERY_PERCENT: |
955 | { | 958 | { |
@@ -1310,7 +1313,7 @@ static int evaluate_conditional(struct gui_wps *gwps, int cond_index) | |||
1310 | 1313 | ||
1311 | struct wps_data *data = gwps->data; | 1314 | struct wps_data *data = gwps->data; |
1312 | 1315 | ||
1313 | int ret; | 1316 | int ret, i; |
1314 | int num_options = data->tokens[cond_index].value.i; | 1317 | int num_options = data->tokens[cond_index].value.i; |
1315 | char result[128], *value; | 1318 | char result[128], *value; |
1316 | int cond_start = cond_index; | 1319 | int cond_start = cond_index; |
@@ -1320,34 +1323,33 @@ static int evaluate_conditional(struct gui_wps *gwps, int cond_index) | |||
1320 | && cond_start < data->num_tokens) | 1323 | && cond_start < data->num_tokens) |
1321 | cond_start++; | 1324 | cond_start++; |
1322 | 1325 | ||
1323 | if (num_options > 2) /* enum */ | 1326 | /* treat ?xx<true> constructs as if they had 2 options. */ |
1324 | { | 1327 | if (num_options < 2) |
1325 | int intval = num_options; | 1328 | num_options = 2; |
1326 | /* get_tag needs to know the number of options in the enum */ | 1329 | |
1327 | get_tag(gwps, cond_index + 1, result, sizeof(result), &intval); | 1330 | int intval = num_options; |
1328 | /* intval is now the number of the enum option we want to read, | 1331 | /* get_token_value needs to know the number of options in the enum */ |
1329 | starting from 1 */ | 1332 | value = get_token_value(gwps, &data->tokens[cond_index + 1], |
1330 | if (intval > num_options || intval < 1) | 1333 | result, sizeof(result), &intval); |
1331 | intval = num_options; | 1334 | |
1332 | 1335 | /* intval is now the number of the enum option we want to read, | |
1333 | int next = cond_start; | 1336 | starting from 1. If intval is -1, we check on the nullity of value. */ |
1334 | int i; | 1337 | if (intval == -1) |
1335 | for (i = 1; i < intval; i++) | 1338 | intval = value ? 1 : num_options; |
1336 | { | 1339 | else if (intval > num_options || intval < 1) |
1337 | next = data->tokens[next].value.i; | 1340 | intval = num_options; |
1338 | } | 1341 | |
1339 | ret = next; | 1342 | /* skip to the right enum case */ |
1340 | } | 1343 | int next = cond_start; |
1341 | else /* %?xx<true|false> or %?<true> */ | 1344 | for (i = 1; i < intval; i++) |
1342 | { | 1345 | { |
1343 | value = get_tag(gwps, cond_index + 1, result, sizeof(result), NULL); | 1346 | next = data->tokens[next].value.i; |
1344 | ret = value ? cond_start : data->tokens[cond_start].value.i; | ||
1345 | } | 1347 | } |
1348 | ret = next; | ||
1346 | 1349 | ||
1347 | #ifdef HAVE_LCD_BITMAP | 1350 | #ifdef HAVE_LCD_BITMAP |
1348 | /* clear all pictures in the conditional */ | 1351 | /* clear all pictures in the conditional */ |
1349 | int i; | 1352 | for (i = 0; i < MAX_IMAGES; i++) |
1350 | for (i=0; i < MAX_IMAGES; i++) | ||
1351 | { | 1353 | { |
1352 | if (data->img[i].cond_index == cond_index) | 1354 | if (data->img[i].cond_index == cond_index) |
1353 | clear_image_pos(gwps, i); | 1355 | clear_image_pos(gwps, i); |
@@ -1455,7 +1457,8 @@ static bool get_line(struct gui_wps *gwps, | |||
1455 | default: | 1457 | default: |
1456 | { | 1458 | { |
1457 | /* get the value of the tag and copy it to the buffer */ | 1459 | /* get the value of the tag and copy it to the buffer */ |
1458 | char *value = get_tag(gwps,i,temp_buf,sizeof(temp_buf),NULL); | 1460 | char *value = get_token_value(gwps, &data->tokens[i], |
1461 | temp_buf, sizeof(temp_buf), NULL); | ||
1459 | if (value) | 1462 | if (value) |
1460 | { | 1463 | { |
1461 | update = true; | 1464 | update = true; |