2010-05-25 15:19:52 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Robert Bieber
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2010-07-29 12:37:48 +00:00
|
|
|
#include <stdbool.h>
|
2010-05-25 15:19:52 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2010-06-17 07:56:51 +00:00
|
|
|
#include "skin_buffer.h"
|
2010-05-25 15:19:52 +00:00
|
|
|
#include "skin_parser.h"
|
|
|
|
#include "skin_debug.h"
|
|
|
|
#include "tag_table.h"
|
|
|
|
#include "symbols.h"
|
|
|
|
#include "skin_scan.h"
|
|
|
|
|
|
|
|
/* Global variables for the parser */
|
|
|
|
int skin_line = 0;
|
2010-07-18 00:59:02 +00:00
|
|
|
char* skin_start = 0;
|
2010-07-02 08:05:03 +00:00
|
|
|
int viewport_line = 0;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
2010-07-29 12:37:48 +00:00
|
|
|
#ifdef ROCKBOX
|
|
|
|
static skin_callback callback = NULL;
|
|
|
|
static void* callback_data;
|
|
|
|
#endif
|
|
|
|
|
2010-05-25 15:19:52 +00:00
|
|
|
/* Auxiliary parsing functions (not visible at global scope) */
|
2010-07-31 16:25:41 +00:00
|
|
|
static struct skin_element* skin_parse_viewport(const char** document);
|
|
|
|
static struct skin_element* skin_parse_line(const char** document);
|
|
|
|
static struct skin_element* skin_parse_line_optional(const char** document,
|
2010-06-10 21:02:44 +00:00
|
|
|
int conditional);
|
2010-07-31 16:25:41 +00:00
|
|
|
static struct skin_element* skin_parse_sublines(const char** document);
|
|
|
|
static struct skin_element* skin_parse_sublines_optional(const char** document,
|
2010-06-10 21:02:44 +00:00
|
|
|
int conditional);
|
|
|
|
|
2010-07-31 16:25:41 +00:00
|
|
|
static int skin_parse_tag(struct skin_element* element, const char** document);
|
|
|
|
static int skin_parse_text(struct skin_element* element, const char** document,
|
2010-06-10 21:02:44 +00:00
|
|
|
int conditional);
|
|
|
|
static int skin_parse_conditional(struct skin_element* element,
|
2010-07-31 16:25:41 +00:00
|
|
|
const char** document);
|
|
|
|
static int skin_parse_comment(struct skin_element* element, const char** document);
|
|
|
|
static struct skin_element* skin_parse_code_as_arg(const char** document);
|
2010-05-25 15:19:52 +00:00
|
|
|
|
2010-06-17 13:54:09 +00:00
|
|
|
|
2010-07-31 16:25:41 +00:00
|
|
|
static void skip_whitespace(const char** document)
|
2010-07-29 12:37:48 +00:00
|
|
|
{
|
|
|
|
while(**document == ' ' || **document == '\t')
|
|
|
|
(*document)++;
|
|
|
|
}
|
2010-06-17 13:54:09 +00:00
|
|
|
|
2010-07-29 12:37:48 +00:00
|
|
|
#ifdef ROCKBOX
|
|
|
|
struct skin_element* skin_parse(const char* document,
|
|
|
|
skin_callback cb, void* cb_data)
|
|
|
|
{
|
|
|
|
callback = cb;
|
|
|
|
callback_data = cb_data;
|
|
|
|
#else
|
2010-06-01 21:25:02 +00:00
|
|
|
struct skin_element* skin_parse(const char* document)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-07-29 12:37:48 +00:00
|
|
|
#endif
|
2010-05-25 15:19:52 +00:00
|
|
|
struct skin_element* root = NULL;
|
|
|
|
struct skin_element* last = NULL;
|
|
|
|
|
2010-07-31 16:25:41 +00:00
|
|
|
const char* cursor = document; /*Keeps track of location in the document*/
|
2010-06-17 07:56:51 +00:00
|
|
|
|
2010-05-25 15:19:52 +00:00
|
|
|
skin_line = 1;
|
2010-07-18 00:59:02 +00:00
|
|
|
skin_start = (char*)document;
|
2010-07-02 08:05:03 +00:00
|
|
|
viewport_line = 0;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
2010-06-07 21:09:13 +00:00
|
|
|
skin_clear_errors();
|
|
|
|
|
2010-05-25 15:19:52 +00:00
|
|
|
while(*cursor != '\0')
|
2010-06-01 07:11:23 +00:00
|
|
|
{
|
2011-11-15 14:11:08 +00:00
|
|
|
struct skin_element* tree = skin_parse_viewport(&cursor);
|
2010-06-01 07:11:23 +00:00
|
|
|
if(!root)
|
2011-11-15 14:11:08 +00:00
|
|
|
{
|
|
|
|
root = tree;
|
|
|
|
last = root;
|
|
|
|
}
|
2010-06-01 07:11:23 +00:00
|
|
|
else
|
2011-11-15 14:11:08 +00:00
|
|
|
{
|
|
|
|
last->next = skin_buffer_to_offset(tree);
|
|
|
|
last = tree;
|
|
|
|
}
|
2010-06-01 07:11:23 +00:00
|
|
|
|
|
|
|
if(!last)
|
2010-06-08 19:34:27 +00:00
|
|
|
{
|
|
|
|
skin_free_tree(root); /* Clearing any memory already used */
|
2010-06-01 07:11:23 +00:00
|
|
|
return NULL;
|
2010-06-08 19:34:27 +00:00
|
|
|
}
|
2010-06-01 07:11:23 +00:00
|
|
|
|
|
|
|
/* Making sure last is at the end */
|
2011-11-15 14:11:08 +00:00
|
|
|
while(IS_VALID_OFFSET(last->next))
|
|
|
|
last = skin_buffer_from_offset(last->next);
|
2010-06-01 07:11:23 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return root;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-07-31 16:25:41 +00:00
|
|
|
static struct skin_element* skin_parse_viewport(const char** document)
|
2010-06-01 07:11:23 +00:00
|
|
|
{
|
|
|
|
struct skin_element* root = NULL;
|
|
|
|
struct skin_element* last = NULL;
|
|
|
|
struct skin_element* retval = NULL;
|
|
|
|
|
|
|
|
retval = skin_alloc_element();
|
2010-07-29 12:37:48 +00:00
|
|
|
if (!retval)
|
|
|
|
return NULL;
|
2010-06-01 07:11:23 +00:00
|
|
|
retval->type = VIEWPORT;
|
|
|
|
retval->children_count = 1;
|
|
|
|
retval->line = skin_line;
|
2010-07-02 08:05:03 +00:00
|
|
|
viewport_line = skin_line;
|
2010-06-01 07:11:23 +00:00
|
|
|
|
2011-11-15 14:11:08 +00:00
|
|
|
OFFSETTYPE(struct skin_element*)* children;
|
2010-06-01 07:11:23 +00:00
|
|
|
|
2010-07-31 16:25:41 +00:00
|
|
|
const char* cursor = *document; /* Keeps track of location in the document */
|
|
|
|
const char* bookmark; /* Used when we need to look ahead */
|
2010-06-01 07:11:23 +00:00
|
|
|
|
|
|
|
int sublines = 0; /* Flag for parsing sublines */
|
|
|
|
|
2010-06-01 18:31:58 +00:00
|
|
|
/* Parsing out the viewport tag if there is one */
|
|
|
|
if(check_viewport(cursor))
|
|
|
|
{
|
2010-11-05 18:46:21 +00:00
|
|
|
if (!skin_parse_tag(retval, &cursor))
|
|
|
|
return NULL;
|
2010-06-02 07:04:33 +00:00
|
|
|
if(*cursor == '\n')
|
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
skin_line++;
|
|
|
|
}
|
2010-06-01 18:31:58 +00:00
|
|
|
}
|
2010-07-29 12:37:48 +00:00
|
|
|
#ifdef ROCKBOX
|
|
|
|
else if (callback)
|
|
|
|
{
|
|
|
|
if (callback(retval, callback_data) == CALLBACK_ERROR)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2010-06-11 08:03:32 +00:00
|
|
|
|
2010-10-05 13:02:34 +00:00
|
|
|
if (check_viewport(cursor))
|
|
|
|
{
|
|
|
|
retval->children_count = 0;
|
|
|
|
*document = cursor;
|
|
|
|
return retval;
|
|
|
|
}
|
2010-06-11 08:03:32 +00:00
|
|
|
retval->children_count = 1;
|
2011-11-15 14:11:08 +00:00
|
|
|
children = skin_alloc_children(1);
|
|
|
|
if (!children)
|
2010-07-29 12:37:48 +00:00
|
|
|
return NULL;
|
2010-06-11 08:03:32 +00:00
|
|
|
do
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
/* First, we check to see if this line will contain sublines */
|
|
|
|
bookmark = cursor;
|
|
|
|
sublines = 0;
|
2010-06-01 07:11:23 +00:00
|
|
|
while(*cursor != '\n' && *cursor != '\0'
|
|
|
|
&& !(check_viewport(cursor) && cursor != *document))
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
if(*cursor == MULTILINESYM)
|
|
|
|
{
|
|
|
|
sublines = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(*cursor == TAGSYM)
|
|
|
|
{
|
|
|
|
/* A ';' directly after a '%' doesn't count */
|
|
|
|
cursor ++;
|
|
|
|
|
|
|
|
if(*cursor == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
cursor++;
|
|
|
|
}
|
2010-05-30 01:56:50 +00:00
|
|
|
else if(*cursor == COMMENTSYM)
|
|
|
|
{
|
|
|
|
skip_comment(&cursor);
|
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
else if(*cursor == ARGLISTOPENSYM)
|
|
|
|
{
|
|
|
|
skip_arglist(&cursor);
|
|
|
|
}
|
|
|
|
else if(*cursor == ENUMLISTOPENSYM)
|
|
|
|
{
|
2010-06-10 21:22:16 +00:00
|
|
|
skip_enumlist(&cursor);
|
2010-06-07 23:49:06 +00:00
|
|
|
}
|
2010-05-25 15:19:52 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Advancing the cursor as normal */
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cursor = bookmark;
|
|
|
|
|
2010-06-02 06:41:41 +00:00
|
|
|
if(sublines)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2011-11-15 14:11:08 +00:00
|
|
|
struct skin_element* out = skin_parse_sublines(&cursor);
|
|
|
|
if (!root)
|
|
|
|
{
|
|
|
|
root = out;
|
|
|
|
last = root;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
last->next = skin_buffer_to_offset(out);
|
|
|
|
last = out;
|
|
|
|
}
|
2010-05-25 15:19:52 +00:00
|
|
|
if(!last)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-09-25 07:43:36 +00:00
|
|
|
#ifdef ROCKBOX
|
|
|
|
/* strip all leading comments */
|
|
|
|
while(*cursor == '#')
|
|
|
|
{
|
|
|
|
skip_comment(&cursor);
|
|
|
|
skin_line++;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (check_viewport(cursor))
|
|
|
|
break;
|
|
|
|
#endif
|
2011-11-15 14:11:08 +00:00
|
|
|
|
|
|
|
struct skin_element* out = skin_parse_line(&cursor);
|
|
|
|
if (!root)
|
|
|
|
{
|
|
|
|
root = out;
|
|
|
|
last = root;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
last->next = skin_buffer_to_offset(out);
|
|
|
|
last = out;
|
|
|
|
}
|
2010-05-25 15:19:52 +00:00
|
|
|
if(!last)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
/* Making sure last is at the end */
|
2011-11-15 14:11:08 +00:00
|
|
|
while(IS_VALID_OFFSET(last->next))
|
|
|
|
last = skin_buffer_from_offset(last->next);
|
2010-05-25 15:19:52 +00:00
|
|
|
|
2010-06-02 06:41:41 +00:00
|
|
|
if(*cursor == '\n')
|
2010-06-02 07:04:33 +00:00
|
|
|
{
|
2010-06-02 06:41:41 +00:00
|
|
|
cursor++;
|
2010-06-02 07:04:33 +00:00
|
|
|
skin_line++;
|
|
|
|
}
|
2011-09-25 07:43:36 +00:00
|
|
|
#ifdef ROCKBOX
|
|
|
|
/* strip all comments */
|
|
|
|
while(*cursor == '#')
|
|
|
|
{
|
|
|
|
skip_comment(&cursor);
|
|
|
|
skin_line++;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (check_viewport(cursor))
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
|
2010-06-01 07:11:23 +00:00
|
|
|
}
|
2010-06-11 08:03:32 +00:00
|
|
|
while(*cursor != '\0' && !(check_viewport(cursor) && cursor != *document));
|
2010-06-01 07:11:23 +00:00
|
|
|
|
|
|
|
*document = cursor;
|
|
|
|
|
2011-11-15 14:11:08 +00:00
|
|
|
children[0] = skin_buffer_to_offset(root);
|
|
|
|
retval->children = skin_buffer_to_offset(children);
|
2010-06-01 07:11:23 +00:00
|
|
|
return retval;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Auxiliary Parsing Functions */
|
|
|
|
|
2010-07-31 16:25:41 +00:00
|
|
|
static struct skin_element* skin_parse_line(const char**document)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
return skin_parse_line_optional(document, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If conditional is set to true, then this will break upon encountering
|
2010-11-05 09:51:19 +00:00
|
|
|
* SEPARATESYM. This should only be used when parsing a line inside a
|
2010-05-25 15:19:52 +00:00
|
|
|
* conditional, otherwise just use the wrapper function skin_parse_line()
|
|
|
|
*/
|
2010-07-31 16:25:41 +00:00
|
|
|
static struct skin_element* skin_parse_line_optional(const char** document,
|
2010-06-10 21:02:44 +00:00
|
|
|
int conditional)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-07-31 16:25:41 +00:00
|
|
|
const char* cursor = *document;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
struct skin_element* root = NULL;
|
|
|
|
struct skin_element* current = NULL;
|
2010-05-27 19:57:15 +00:00
|
|
|
struct skin_element* retval = NULL;
|
2011-11-15 14:11:08 +00:00
|
|
|
OFFSETTYPE(struct skin_element*)* children = NULL;
|
2010-05-27 19:57:15 +00:00
|
|
|
|
|
|
|
/* A wrapper for the line */
|
|
|
|
retval = skin_alloc_element();
|
2010-07-29 12:37:48 +00:00
|
|
|
if (!retval)
|
|
|
|
return NULL;
|
2010-05-27 19:57:15 +00:00
|
|
|
retval->type = LINE;
|
|
|
|
retval->line = skin_line;
|
2010-06-22 18:51:44 +00:00
|
|
|
if(*cursor != '\0' && *cursor != '\n' && *cursor != MULTILINESYM
|
2010-11-05 09:51:19 +00:00
|
|
|
&& !(conditional && (*cursor == ARGLISTSEPARATESYM
|
2010-06-11 19:51:34 +00:00
|
|
|
|| *cursor == ARGLISTCLOSESYM
|
2010-11-05 09:51:19 +00:00
|
|
|
|| *cursor == ENUMLISTSEPARATESYM
|
2010-06-11 19:51:34 +00:00
|
|
|
|| *cursor == ENUMLISTCLOSESYM)))
|
|
|
|
{
|
2010-06-11 08:03:32 +00:00
|
|
|
retval->children_count = 1;
|
2010-06-11 19:51:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
retval->children_count = 0;
|
|
|
|
}
|
|
|
|
|
2010-06-11 08:03:32 +00:00
|
|
|
if(retval->children_count > 0)
|
2010-07-29 12:37:48 +00:00
|
|
|
{
|
2011-11-15 14:11:08 +00:00
|
|
|
children = skin_alloc_children(1);
|
|
|
|
if (!children)
|
2010-07-29 12:37:48 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ROCKBOX
|
|
|
|
if (callback)
|
|
|
|
{
|
|
|
|
switch (callback(retval, callback_data))
|
|
|
|
{
|
|
|
|
case CALLBACK_ERROR:
|
|
|
|
return NULL;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
while(*cursor != '\n' && *cursor != '\0' && *cursor != MULTILINESYM
|
2010-11-05 09:51:19 +00:00
|
|
|
&& !((*cursor == ARGLISTSEPARATESYM
|
2010-05-25 17:22:39 +00:00
|
|
|
|| *cursor == ARGLISTCLOSESYM
|
2010-11-05 09:51:19 +00:00
|
|
|
|| *cursor == ENUMLISTSEPARATESYM
|
2010-05-25 17:22:39 +00:00
|
|
|
|| *cursor == ENUMLISTCLOSESYM)
|
2010-06-01 07:11:23 +00:00
|
|
|
&& conditional)
|
|
|
|
&& !(check_viewport(cursor) && cursor != *document))
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
/* Allocating memory if necessary */
|
|
|
|
if(root)
|
|
|
|
{
|
2011-11-15 14:11:08 +00:00
|
|
|
struct skin_element *next = skin_alloc_element();
|
|
|
|
if (!next)
|
2010-07-29 12:37:48 +00:00
|
|
|
return NULL;
|
2011-11-15 14:11:08 +00:00
|
|
|
current->next = skin_buffer_to_offset(next);
|
|
|
|
current = next;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
current = skin_alloc_element();
|
2010-07-29 12:37:48 +00:00
|
|
|
if (!current)
|
|
|
|
return NULL;
|
2010-05-25 15:19:52 +00:00
|
|
|
root = current;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parsing the current element */
|
|
|
|
if(*cursor == TAGSYM && cursor[1] == CONDITIONSYM)
|
|
|
|
{
|
|
|
|
if(!skin_parse_conditional(current, &cursor))
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-06-02 05:45:34 +00:00
|
|
|
else if(*cursor == TAGSYM && !find_escape_character(cursor[1]))
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
if(!skin_parse_tag(current, &cursor))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else if(*cursor == COMMENTSYM)
|
|
|
|
{
|
|
|
|
if(!skin_parse_comment(current, &cursor))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!skin_parse_text(current, &cursor, conditional))
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Moving up the calling function's pointer */
|
|
|
|
*document = cursor;
|
2010-07-29 12:37:48 +00:00
|
|
|
|
2010-06-11 08:03:32 +00:00
|
|
|
if(root)
|
2011-11-15 14:11:08 +00:00
|
|
|
{
|
|
|
|
children[0] = skin_buffer_to_offset(root);
|
|
|
|
retval->children = skin_buffer_to_offset(children);
|
|
|
|
}
|
2010-05-27 19:57:15 +00:00
|
|
|
return retval;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
|
|
|
|
2010-07-31 16:25:41 +00:00
|
|
|
static struct skin_element* skin_parse_sublines(const char** document)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
return skin_parse_sublines_optional(document, 0);
|
|
|
|
}
|
|
|
|
|
2010-07-31 16:25:41 +00:00
|
|
|
static struct skin_element* skin_parse_sublines_optional(const char** document,
|
2010-05-25 15:19:52 +00:00
|
|
|
int conditional)
|
|
|
|
{
|
|
|
|
struct skin_element* retval;
|
2011-11-15 14:11:08 +00:00
|
|
|
OFFSETTYPE(struct skin_element*)* children;
|
2010-07-31 16:25:41 +00:00
|
|
|
const char* cursor = *document;
|
2010-05-25 15:19:52 +00:00
|
|
|
int sublines = 1;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
retval = skin_alloc_element();
|
2010-07-29 12:37:48 +00:00
|
|
|
if (!retval)
|
|
|
|
return NULL;
|
2010-07-04 02:04:14 +00:00
|
|
|
retval->type = LINE_ALTERNATOR;
|
2011-11-15 14:11:08 +00:00
|
|
|
retval->next = skin_buffer_to_offset(NULL);
|
2010-05-27 19:57:15 +00:00
|
|
|
retval->line = skin_line;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
/* First we count the sublines */
|
|
|
|
while(*cursor != '\0' && *cursor != '\n'
|
2010-11-05 09:51:19 +00:00
|
|
|
&& !((*cursor == ARGLISTSEPARATESYM
|
2010-05-25 17:22:39 +00:00
|
|
|
|| *cursor == ARGLISTCLOSESYM
|
2010-11-05 09:51:19 +00:00
|
|
|
|| *cursor == ENUMLISTSEPARATESYM
|
2010-05-25 17:22:39 +00:00
|
|
|
|| *cursor == ENUMLISTCLOSESYM)
|
2010-06-01 07:11:23 +00:00
|
|
|
&& conditional)
|
|
|
|
&& !(check_viewport(cursor) && cursor != *document))
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
if(*cursor == COMMENTSYM)
|
2010-06-02 07:48:48 +00:00
|
|
|
{
|
2010-05-25 15:19:52 +00:00
|
|
|
skip_comment(&cursor);
|
2010-06-02 07:48:48 +00:00
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
else if(*cursor == ENUMLISTOPENSYM)
|
2010-06-02 07:48:48 +00:00
|
|
|
{
|
2010-06-07 23:49:06 +00:00
|
|
|
skip_enumlist(&cursor);
|
2010-06-02 07:48:48 +00:00
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
else if(*cursor == ARGLISTOPENSYM)
|
|
|
|
{
|
|
|
|
skip_arglist(&cursor);
|
|
|
|
}
|
|
|
|
else if(*cursor == TAGSYM)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
if(*cursor == '\0' || *cursor == '\n')
|
|
|
|
break;
|
2010-06-07 23:49:06 +00:00
|
|
|
cursor++;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
else if(*cursor == MULTILINESYM)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
sublines++;
|
2010-06-07 23:49:06 +00:00
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cursor++;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ...and then we parse them */
|
|
|
|
retval->children_count = sublines;
|
2011-11-15 14:11:08 +00:00
|
|
|
children = skin_alloc_children(sublines);
|
|
|
|
if (!children)
|
2010-07-29 12:37:48 +00:00
|
|
|
return NULL;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
cursor = *document;
|
|
|
|
for(i = 0; i < sublines; i++)
|
|
|
|
{
|
2011-11-15 14:11:08 +00:00
|
|
|
children[i] = skin_buffer_to_offset(skin_parse_line_optional(&cursor, conditional));
|
|
|
|
if (children[i] < 0)
|
|
|
|
return NULL;
|
2010-05-25 15:19:52 +00:00
|
|
|
skip_whitespace(&cursor);
|
|
|
|
|
|
|
|
if(*cursor != MULTILINESYM && i != sublines - 1)
|
|
|
|
{
|
2010-07-18 00:59:02 +00:00
|
|
|
skin_error(MULTILINE_EXPECTED, cursor);
|
2010-05-25 15:19:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-05-27 19:57:15 +00:00
|
|
|
else if(i != sublines - 1)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-29 12:37:48 +00:00
|
|
|
#ifdef ROCKBOX
|
|
|
|
if (callback)
|
|
|
|
{
|
|
|
|
if (callback(retval, callback_data) == CALLBACK_ERROR)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2010-05-25 15:19:52 +00:00
|
|
|
*document = cursor;
|
2011-11-15 14:11:08 +00:00
|
|
|
retval->children = skin_buffer_to_offset(children);
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2010-07-31 16:25:41 +00:00
|
|
|
static int skin_parse_tag(struct skin_element* element, const char** document)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-07-31 16:25:41 +00:00
|
|
|
const char* cursor = *document + 1;
|
|
|
|
const char* bookmark;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
char tag_name[3];
|
|
|
|
char* tag_args;
|
2010-07-31 11:28:37 +00:00
|
|
|
const struct tag_info *tag;
|
2011-11-15 14:11:08 +00:00
|
|
|
struct skin_tag_parameter* params = NULL;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
int num_args = 1;
|
|
|
|
int i;
|
2010-05-25 17:22:39 +00:00
|
|
|
int star = 0; /* Flag for the all-or-none option */
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
int optional = 0;
|
|
|
|
|
|
|
|
/* Checking the tag name */
|
|
|
|
tag_name[0] = cursor[0];
|
|
|
|
tag_name[1] = cursor[1];
|
|
|
|
tag_name[2] = '\0';
|
|
|
|
|
|
|
|
/* First we check the two characters after the '%', then a single char */
|
2010-06-01 16:44:52 +00:00
|
|
|
tag = find_tag(tag_name);
|
2010-05-25 15:19:52 +00:00
|
|
|
|
2010-06-01 16:44:52 +00:00
|
|
|
if(!tag)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
tag_name[1] = '\0';
|
2010-06-01 16:44:52 +00:00
|
|
|
tag = find_tag(tag_name);
|
2010-05-25 15:19:52 +00:00
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cursor += 2;
|
|
|
|
}
|
|
|
|
|
2010-06-01 16:44:52 +00:00
|
|
|
if(!tag)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-07-18 00:59:02 +00:00
|
|
|
skin_error(ILLEGAL_TAG, cursor);
|
2010-05-25 15:19:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copying basic tag info */
|
2010-06-11 08:03:32 +00:00
|
|
|
if(element->type != CONDITIONAL && element->type != VIEWPORT)
|
2010-06-10 21:02:44 +00:00
|
|
|
element->type = TAG;
|
2010-06-01 16:44:52 +00:00
|
|
|
element->tag = tag;
|
|
|
|
tag_args = tag->params;
|
2010-05-25 15:19:52 +00:00
|
|
|
element->line = skin_line;
|
|
|
|
|
2010-05-25 17:22:39 +00:00
|
|
|
/* Checking for the * flag */
|
|
|
|
if(tag_args[0] == '*')
|
|
|
|
{
|
|
|
|
star = 1;
|
|
|
|
tag_args++;
|
|
|
|
}
|
|
|
|
|
2010-05-25 15:19:52 +00:00
|
|
|
/* If this tag has no arguments, we can bail out now */
|
2010-05-25 17:22:39 +00:00
|
|
|
if(strlen(tag_args) == 0
|
2010-06-02 05:27:37 +00:00
|
|
|
|| (tag_args[0] == '|' && *cursor != ARGLISTOPENSYM)
|
|
|
|
|| (star && *cursor != ARGLISTOPENSYM))
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-07-29 12:37:48 +00:00
|
|
|
|
|
|
|
#ifdef ROCKBOX
|
|
|
|
if (callback)
|
|
|
|
{
|
|
|
|
if (callback(element, callback_data) == CALLBACK_ERROR)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2010-05-25 15:19:52 +00:00
|
|
|
*document = cursor;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Checking the number of arguments and allocating args */
|
2010-07-04 01:39:57 +00:00
|
|
|
if(*cursor != ARGLISTOPENSYM && tag_args[0] != '|')
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-07-18 00:59:02 +00:00
|
|
|
skin_error(ARGLIST_EXPECTED, cursor);
|
2010-05-25 15:19:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
|
2010-06-07 23:49:06 +00:00
|
|
|
bookmark = cursor;
|
|
|
|
while(*cursor != '\n' && *cursor != '\0' && *cursor != ARGLISTCLOSESYM)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
/* Skipping over escaped characters */
|
|
|
|
if(*cursor == TAGSYM)
|
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
if(*cursor == '\0')
|
|
|
|
break;
|
2010-06-07 23:49:06 +00:00
|
|
|
cursor++;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
else if(*cursor == COMMENTSYM)
|
|
|
|
{
|
2010-05-25 15:19:52 +00:00
|
|
|
skip_comment(&cursor);
|
2010-06-07 23:49:06 +00:00
|
|
|
}
|
|
|
|
else if(*cursor == ARGLISTOPENSYM)
|
|
|
|
{
|
|
|
|
skip_arglist(&cursor);
|
|
|
|
}
|
2010-11-05 09:51:19 +00:00
|
|
|
else if(*cursor == ARGLISTSEPARATESYM)
|
2010-06-07 23:49:06 +00:00
|
|
|
{
|
2010-05-25 15:19:52 +00:00
|
|
|
num_args++;
|
2010-06-07 23:49:06 +00:00
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
}
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
|
2010-05-25 15:19:52 +00:00
|
|
|
cursor = bookmark; /* Restoring the cursor */
|
|
|
|
element->params_count = num_args;
|
2011-11-15 14:11:08 +00:00
|
|
|
params = skin_alloc_params(num_args);
|
|
|
|
if (!params)
|
2010-07-29 12:37:48 +00:00
|
|
|
return 0;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
/* Now we have to actually parse each argument */
|
2010-05-25 17:22:39 +00:00
|
|
|
for(i = 0; i < num_args; i++)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-08-14 11:17:49 +00:00
|
|
|
char type_code;
|
2010-05-25 15:19:52 +00:00
|
|
|
/* Making sure we haven't run out of arguments */
|
2010-07-04 01:39:57 +00:00
|
|
|
if(*tag_args == '\0')
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-07-18 00:59:02 +00:00
|
|
|
skin_error(TOO_MANY_ARGS, cursor);
|
2010-05-25 15:19:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Checking for the optional bar */
|
|
|
|
if(*tag_args == '|')
|
|
|
|
{
|
|
|
|
optional = 1;
|
|
|
|
tag_args++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Scanning the arguments */
|
2010-05-25 17:22:39 +00:00
|
|
|
skip_whitespace(&cursor);
|
2010-05-25 15:19:52 +00:00
|
|
|
|
2010-05-25 17:22:39 +00:00
|
|
|
/* Checking for comments */
|
|
|
|
if(*cursor == COMMENTSYM)
|
|
|
|
skip_comment(&cursor);
|
2010-08-14 11:17:49 +00:00
|
|
|
|
|
|
|
if (*tag_args == '[')
|
|
|
|
{
|
|
|
|
/* we need to guess which type of param it is.
|
|
|
|
* guess using this priority:
|
|
|
|
* default > decimal/integer > single tag/code > string
|
|
|
|
*/
|
|
|
|
int j=0;
|
|
|
|
bool canbedefault = false;
|
|
|
|
bool haspercent = false, number = true, hasdecimal = false;
|
|
|
|
char temp_params[8];
|
|
|
|
tag_args++;
|
|
|
|
while (*tag_args != ']')
|
|
|
|
{
|
|
|
|
if (*tag_args >= 'a' && *tag_args <= 'z')
|
|
|
|
canbedefault = true;
|
|
|
|
temp_params[j++] = tolower(*tag_args++);
|
|
|
|
}
|
|
|
|
temp_params[j] = '\0';
|
|
|
|
j = 0;
|
New skin tag: %if(<tag>, <operator>, <operand> [,option count]) which lets you do very simple logical comparissons on other tags.
<tag> is the tag to check against
<operator> is the comparisson to do, any one of... =, !=, >, >=, <, <= (when comparring against a string tag like %ia only = and != work, and it is done NOT case sensitive)
<operand> is either another tag, a number, or text.
[option count] is an optinal number to use for the few tags which scale to the amount of options when used as a conditional (i.e %?pv<a|b|c|d> would have 4 options)
example: %?if(%pv, >=, 0)<Warning.. volume clipping|coool...>
That says "If the value from %pv (volume) is greater than or equal to 0 then display the warning line, otherwise the cool line."
%?if(%ia, =, %Ia)<same artist> <= this artist and next artist are the same.
some tags might need a touch of tweaking to work better with this. experiment and have fun
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27846 a1c6a512-1295-4272-9138-f99709370657
2010-08-19 13:49:32 +00:00
|
|
|
while (cursor[j] && cursor[j] != ',' && cursor[j] != ')')
|
2010-08-14 11:17:49 +00:00
|
|
|
{
|
|
|
|
haspercent = haspercent || (cursor[j] == '%');
|
|
|
|
hasdecimal = hasdecimal || (cursor[j] == '.');
|
New skin tag: %if(<tag>, <operator>, <operand> [,option count]) which lets you do very simple logical comparissons on other tags.
<tag> is the tag to check against
<operator> is the comparisson to do, any one of... =, !=, >, >=, <, <= (when comparring against a string tag like %ia only = and != work, and it is done NOT case sensitive)
<operand> is either another tag, a number, or text.
[option count] is an optinal number to use for the few tags which scale to the amount of options when used as a conditional (i.e %?pv<a|b|c|d> would have 4 options)
example: %?if(%pv, >=, 0)<Warning.. volume clipping|coool...>
That says "If the value from %pv (volume) is greater than or equal to 0 then display the warning line, otherwise the cool line."
%?if(%ia, =, %Ia)<same artist> <= this artist and next artist are the same.
some tags might need a touch of tweaking to work better with this. experiment and have fun
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@27846 a1c6a512-1295-4272-9138-f99709370657
2010-08-19 13:49:32 +00:00
|
|
|
number = number && (isdigit(cursor[j]) ||
|
|
|
|
(cursor[j] == '.') ||
|
|
|
|
(cursor[j] == '-'));
|
2010-08-14 11:17:49 +00:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
type_code = '*';
|
|
|
|
if (canbedefault && *cursor == DEFAULTSYM && !isdigit(cursor[1]))
|
|
|
|
{
|
|
|
|
type_code = 'i';
|
|
|
|
}
|
|
|
|
else if (number && hasdecimal && strchr(temp_params, 'd'))
|
|
|
|
{
|
|
|
|
type_code = 'd';
|
|
|
|
}
|
|
|
|
else if (number &&
|
|
|
|
(strchr(temp_params, 'i') || strchr(temp_params, 'd')))
|
|
|
|
{
|
|
|
|
type_code = strchr(temp_params, 'i') ? 'i' : 'd';
|
|
|
|
}
|
|
|
|
else if (haspercent &&
|
|
|
|
(strchr(temp_params, 't') || strchr(temp_params, 'c')))
|
|
|
|
{
|
|
|
|
type_code = strchr(temp_params, 't') ? 't' : 'c';
|
|
|
|
}
|
|
|
|
else if (strchr(temp_params, 's'))
|
|
|
|
{
|
|
|
|
type_code = 's';
|
|
|
|
}
|
|
|
|
if (type_code == '*')
|
|
|
|
{
|
|
|
|
skin_error(INSUFFICIENT_ARGS, cursor);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
type_code = *tag_args;
|
2010-06-01 19:55:20 +00:00
|
|
|
/* Storing the type code */
|
2011-11-15 14:11:08 +00:00
|
|
|
params[i].type_code = type_code;
|
2010-06-01 19:55:20 +00:00
|
|
|
|
2010-07-15 06:24:11 +00:00
|
|
|
/* Checking a nullable argument for null. */
|
2010-06-02 06:52:17 +00:00
|
|
|
if(*cursor == DEFAULTSYM && !isdigit(cursor[1]))
|
2010-05-25 17:22:39 +00:00
|
|
|
{
|
2010-08-14 11:17:49 +00:00
|
|
|
if(islower(type_code))
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2011-11-15 14:11:08 +00:00
|
|
|
params[i].type = DEFAULT;
|
2010-05-25 17:22:39 +00:00
|
|
|
cursor++;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
2010-05-25 17:22:39 +00:00
|
|
|
else
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-07-18 00:59:02 +00:00
|
|
|
skin_error(DEFAULT_NOT_ALLOWED, cursor);
|
2010-05-25 17:22:39 +00:00
|
|
|
return 0;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
2010-05-25 17:22:39 +00:00
|
|
|
}
|
2010-08-14 11:17:49 +00:00
|
|
|
else if(tolower(type_code) == 'i')
|
2010-05-25 17:22:39 +00:00
|
|
|
{
|
|
|
|
/* Scanning an int argument */
|
2010-06-02 06:52:17 +00:00
|
|
|
if(!isdigit(*cursor) && *cursor != '-')
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-07-18 00:59:02 +00:00
|
|
|
skin_error(INT_EXPECTED, cursor);
|
2010-05-25 17:22:39 +00:00
|
|
|
return 0;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
|
|
|
|
2011-11-15 14:11:08 +00:00
|
|
|
params[i].type = INTEGER;
|
|
|
|
params[i].data.number = scan_int(&cursor);
|
2010-07-15 06:24:11 +00:00
|
|
|
}
|
2010-08-14 11:17:49 +00:00
|
|
|
else if(tolower(type_code) == 'd')
|
2010-07-15 06:24:11 +00:00
|
|
|
{
|
|
|
|
int val = 0;
|
|
|
|
bool have_point = false;
|
|
|
|
bool have_tenth = false;
|
|
|
|
while ( isdigit(*cursor) || *cursor == '.' )
|
|
|
|
{
|
|
|
|
if (*cursor != '.')
|
|
|
|
{
|
|
|
|
val *= 10;
|
|
|
|
val += *cursor - '0';
|
|
|
|
if (have_point)
|
|
|
|
{
|
|
|
|
have_tenth = true;
|
|
|
|
cursor++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
have_point = true;
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
if (have_tenth == false)
|
|
|
|
val *= 10;
|
2011-11-15 14:11:08 +00:00
|
|
|
params[i].type = DECIMAL;
|
|
|
|
params[i].data.number = val;
|
2010-05-25 17:22:39 +00:00
|
|
|
}
|
2010-08-14 11:17:49 +00:00
|
|
|
else if(tolower(type_code) == 'n' ||
|
|
|
|
tolower(type_code) == 's' || tolower(type_code) == 'f')
|
2010-05-25 17:22:39 +00:00
|
|
|
{
|
|
|
|
/* Scanning a string argument */
|
2011-11-15 14:11:08 +00:00
|
|
|
params[i].type = STRING;
|
|
|
|
params[i].data.text = skin_buffer_to_offset(scan_string(&cursor));
|
2010-05-25 15:19:52 +00:00
|
|
|
|
2010-05-25 17:22:39 +00:00
|
|
|
}
|
2010-08-14 11:17:49 +00:00
|
|
|
else if(tolower(type_code) == 'c')
|
2010-05-25 17:22:39 +00:00
|
|
|
{
|
|
|
|
/* Recursively parsing a code argument */
|
2011-11-15 14:11:08 +00:00
|
|
|
params[i].type = CODE;
|
|
|
|
params[i].data.code = skin_buffer_to_offset(skin_parse_code_as_arg(&cursor));
|
|
|
|
if(params[i].data.code < 0)
|
2010-05-25 15:19:52 +00:00
|
|
|
return 0;
|
2010-05-25 17:22:39 +00:00
|
|
|
}
|
2010-08-14 11:17:49 +00:00
|
|
|
else if (tolower(type_code) == 't')
|
2010-08-05 11:28:35 +00:00
|
|
|
{
|
|
|
|
struct skin_element* child = skin_alloc_element();
|
|
|
|
child->type = TAG;
|
|
|
|
if (!skin_parse_tag(child, &cursor))
|
|
|
|
return 0;
|
2011-11-15 14:11:08 +00:00
|
|
|
child->next = skin_buffer_to_offset(NULL);
|
|
|
|
params[i].type = CODE;
|
|
|
|
params[i].data.code = skin_buffer_to_offset(child);
|
2010-08-05 11:28:35 +00:00
|
|
|
}
|
|
|
|
|
2010-05-25 17:22:39 +00:00
|
|
|
|
|
|
|
skip_whitespace(&cursor);
|
|
|
|
|
2010-11-05 09:51:19 +00:00
|
|
|
if(*cursor != ARGLISTSEPARATESYM && i < num_args - 1)
|
2010-05-25 17:22:39 +00:00
|
|
|
{
|
2010-11-05 09:51:19 +00:00
|
|
|
skin_error(SEPARATOR_EXPECTED, cursor);
|
2010-05-25 17:22:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if(*cursor != ARGLISTCLOSESYM && i == num_args - 1)
|
|
|
|
{
|
2010-07-18 00:59:02 +00:00
|
|
|
skin_error(CLOSE_EXPECTED, cursor);
|
2010-05-25 17:22:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cursor++;
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
|
|
|
|
2010-06-09 16:15:01 +00:00
|
|
|
if (*tag_args != 'N')
|
|
|
|
tag_args++;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
2010-06-01 07:11:23 +00:00
|
|
|
/* Checking for the optional bar */
|
|
|
|
if(*tag_args == '|')
|
|
|
|
{
|
|
|
|
optional = 1;
|
|
|
|
tag_args++;
|
|
|
|
}
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
2011-11-15 14:11:08 +00:00
|
|
|
element->params = skin_buffer_to_offset(params);
|
2010-05-25 15:19:52 +00:00
|
|
|
|
2010-05-25 17:22:39 +00:00
|
|
|
/* Checking for a premature end */
|
2010-07-04 01:39:57 +00:00
|
|
|
if(*tag_args != '\0' && !optional)
|
2010-05-25 17:22:39 +00:00
|
|
|
{
|
2010-07-18 00:59:02 +00:00
|
|
|
skin_error(INSUFFICIENT_ARGS, cursor);
|
2010-05-25 17:22:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2010-07-29 12:37:48 +00:00
|
|
|
#ifdef ROCKBOX
|
|
|
|
if (callback)
|
|
|
|
{
|
|
|
|
if (callback(element, callback_data) == CALLBACK_ERROR)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2010-05-25 15:19:52 +00:00
|
|
|
*document = cursor;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the conditional flag is set true, then parsing text will stop at an
|
2010-11-05 09:51:19 +00:00
|
|
|
* ARGLISTSEPARATESYM. Only set that flag when parsing within a conditional
|
2010-05-25 15:19:52 +00:00
|
|
|
*/
|
2010-07-31 16:25:41 +00:00
|
|
|
static int skin_parse_text(struct skin_element* element, const char** document,
|
2010-06-10 21:02:44 +00:00
|
|
|
int conditional)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-07-31 16:25:41 +00:00
|
|
|
const char* cursor = *document;
|
2010-05-25 15:19:52 +00:00
|
|
|
int length = 0;
|
|
|
|
int dest;
|
2010-06-10 21:02:44 +00:00
|
|
|
char *text = NULL;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
/* First figure out how much text we're copying */
|
|
|
|
while(*cursor != '\0' && *cursor != '\n' && *cursor != MULTILINESYM
|
|
|
|
&& *cursor != COMMENTSYM
|
2010-11-05 09:51:19 +00:00
|
|
|
&& !((*cursor == ARGLISTSEPARATESYM
|
2010-05-25 17:22:39 +00:00
|
|
|
|| *cursor == ARGLISTCLOSESYM
|
2010-11-05 09:51:19 +00:00
|
|
|
|| *cursor == ENUMLISTSEPARATESYM
|
2010-05-25 17:22:39 +00:00
|
|
|
|| *cursor == ENUMLISTCLOSESYM)
|
|
|
|
&& conditional))
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
/* Dealing with possibility of escaped characters */
|
|
|
|
if(*cursor == TAGSYM)
|
|
|
|
{
|
|
|
|
if(find_escape_character(cursor[1]))
|
|
|
|
cursor++;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
length++;
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor = *document;
|
|
|
|
|
|
|
|
/* Copying the text into the element struct */
|
|
|
|
element->type = TEXT;
|
|
|
|
element->line = skin_line;
|
2011-11-15 14:11:08 +00:00
|
|
|
element->next = skin_buffer_to_offset(NULL);
|
|
|
|
text = skin_alloc_string(length);
|
|
|
|
element->data = skin_buffer_to_offset(text);
|
|
|
|
if (element->data < 0)
|
2010-07-29 12:37:48 +00:00
|
|
|
return 0;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
for(dest = 0; dest < length; dest++)
|
|
|
|
{
|
|
|
|
/* Advancing cursor if we've encountered an escaped character */
|
|
|
|
if(*cursor == TAGSYM)
|
|
|
|
cursor++;
|
|
|
|
|
2010-06-10 21:02:44 +00:00
|
|
|
text[dest] = *cursor;
|
2010-05-25 15:19:52 +00:00
|
|
|
cursor++;
|
|
|
|
}
|
2010-06-10 21:02:44 +00:00
|
|
|
text[length] = '\0';
|
2010-07-29 12:37:48 +00:00
|
|
|
|
|
|
|
#ifdef ROCKBOX
|
|
|
|
if (callback)
|
|
|
|
{
|
|
|
|
if (callback(element, callback_data) == CALLBACK_ERROR)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
*document = cursor;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-07-31 16:25:41 +00:00
|
|
|
static int skin_parse_conditional(struct skin_element* element, const char** document)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-07-31 16:25:41 +00:00
|
|
|
const char* cursor = *document + 1; /* Starting past the "%" */
|
|
|
|
const char* bookmark;
|
2010-05-25 15:19:52 +00:00
|
|
|
int children = 1;
|
|
|
|
int i;
|
2010-07-29 12:37:48 +00:00
|
|
|
|
|
|
|
#ifdef ROCKBOX
|
|
|
|
bool feature_available = true;
|
2010-07-31 16:25:41 +00:00
|
|
|
const char *false_branch = NULL;
|
2010-10-28 11:00:36 +00:00
|
|
|
const char *conditional_end = NULL;
|
2010-07-29 12:37:48 +00:00
|
|
|
#endif
|
2011-11-15 14:11:08 +00:00
|
|
|
OFFSETTYPE(struct skin_element*)* children_array = NULL;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
2010-07-29 12:37:48 +00:00
|
|
|
/* Some conditional tags allow for target feature checking,
|
|
|
|
* so to handle that call the callback as usual with type == TAG
|
|
|
|
* then call it a second time with type == CONDITIONAL and check the return
|
|
|
|
* value */
|
|
|
|
element->type = TAG;
|
2010-05-25 15:19:52 +00:00
|
|
|
element->line = skin_line;
|
|
|
|
|
|
|
|
/* Parsing the tag first */
|
2010-06-10 21:02:44 +00:00
|
|
|
if(!skin_parse_tag(element, &cursor))
|
2010-05-25 15:19:52 +00:00
|
|
|
return 0;
|
|
|
|
|
2010-07-29 12:37:48 +00:00
|
|
|
element->type = CONDITIONAL;
|
|
|
|
#ifdef ROCKBOX
|
|
|
|
if (callback)
|
|
|
|
{
|
|
|
|
switch (callback(element, callback_data))
|
|
|
|
{
|
|
|
|
case FEATURE_NOT_AVAILABLE:
|
|
|
|
feature_available = false;
|
|
|
|
break;
|
|
|
|
case CALLBACK_ERROR:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-05-25 15:19:52 +00:00
|
|
|
/* Counting the children */
|
2010-05-25 17:22:39 +00:00
|
|
|
if(*(cursor++) != ENUMLISTOPENSYM)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-07-18 00:59:02 +00:00
|
|
|
skin_error(ARGLIST_EXPECTED, cursor);
|
2010-05-25 15:19:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
bookmark = cursor;
|
2010-05-25 17:22:39 +00:00
|
|
|
while(*cursor != ENUMLISTCLOSESYM && *cursor != '\n' && *cursor != '\0')
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
if(*cursor == COMMENTSYM)
|
|
|
|
{
|
|
|
|
skip_comment(&cursor);
|
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
else if(*cursor == ENUMLISTOPENSYM)
|
2010-06-02 07:48:48 +00:00
|
|
|
{
|
2010-06-07 23:49:06 +00:00
|
|
|
skip_enumlist(&cursor);
|
2010-06-02 07:48:48 +00:00
|
|
|
}
|
2010-06-07 23:49:06 +00:00
|
|
|
else if(*cursor == TAGSYM)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
if(*cursor == '\0' || *cursor == '\n')
|
|
|
|
break;
|
|
|
|
cursor++;
|
|
|
|
}
|
2010-11-05 09:51:19 +00:00
|
|
|
else if(*cursor == ENUMLISTSEPARATESYM)
|
2010-06-07 23:49:06 +00:00
|
|
|
{
|
2010-05-25 15:19:52 +00:00
|
|
|
children++;
|
2010-06-07 23:49:06 +00:00
|
|
|
cursor++;
|
2010-07-29 12:37:48 +00:00
|
|
|
#ifdef ROCKBOX
|
|
|
|
if (false_branch == NULL && !feature_available)
|
|
|
|
{
|
|
|
|
false_branch = cursor;
|
|
|
|
children--;
|
|
|
|
}
|
|
|
|
#endif
|
2010-06-07 23:49:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
}
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
2010-07-29 12:37:48 +00:00
|
|
|
#ifdef ROCKBOX
|
|
|
|
if (*cursor == ENUMLISTCLOSESYM &&
|
|
|
|
false_branch == NULL && !feature_available)
|
|
|
|
{
|
|
|
|
false_branch = cursor+1;
|
|
|
|
children--;
|
|
|
|
}
|
2010-10-28 11:00:36 +00:00
|
|
|
if (element->tag->flags&FEATURE_TAG)
|
|
|
|
{
|
2010-11-03 11:08:55 +00:00
|
|
|
if (feature_available && children > 1)
|
2010-10-28 11:00:36 +00:00
|
|
|
children--;
|
|
|
|
}
|
|
|
|
conditional_end = cursor;
|
2010-07-29 12:37:48 +00:00
|
|
|
/* if we are skipping the true branch fix that up */
|
|
|
|
cursor = false_branch ? false_branch : bookmark;
|
|
|
|
#else
|
2010-05-25 15:19:52 +00:00
|
|
|
cursor = bookmark;
|
2010-07-29 12:37:48 +00:00
|
|
|
#endif
|
2010-05-25 15:19:52 +00:00
|
|
|
/* Parsing the children */
|
2010-12-24 07:58:26 +00:00
|
|
|
|
|
|
|
/* Feature tags could end up having 0 children which breaks
|
|
|
|
* the render in dangerous ways. Minor hack, but insert an empty
|
|
|
|
* child. (e.g %?xx<foo> when xx isnt available ) */
|
|
|
|
|
|
|
|
if (children == 0)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2011-10-08 13:54:55 +00:00
|
|
|
const char* emptyline= "";
|
|
|
|
children = 1;
|
2011-11-15 14:11:08 +00:00
|
|
|
children_array = skin_alloc_children(children);
|
|
|
|
if (!children_array)
|
2011-10-08 13:54:55 +00:00
|
|
|
return 0;
|
|
|
|
element->children_count = children;
|
2011-11-15 14:11:08 +00:00
|
|
|
children_array[0] = skin_buffer_to_offset(skin_parse_code_as_arg(&emptyline));
|
2011-10-08 13:54:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-11-15 14:11:08 +00:00
|
|
|
children_array = skin_alloc_children(children);
|
|
|
|
if (!children_array)
|
2011-10-08 13:54:55 +00:00
|
|
|
return 0;
|
|
|
|
element->children_count = children;
|
|
|
|
|
|
|
|
for(i = 0; i < children; i++)
|
|
|
|
{
|
2011-11-15 14:11:08 +00:00
|
|
|
children_array[i] = skin_buffer_to_offset(skin_parse_code_as_arg(&cursor));
|
|
|
|
if (children_array[i] < 0)
|
2011-10-08 13:54:55 +00:00
|
|
|
return 0;
|
|
|
|
skip_whitespace(&cursor);
|
2010-10-28 11:00:36 +00:00
|
|
|
#ifdef ROCKBOX
|
2011-10-08 13:54:55 +00:00
|
|
|
if ((element->tag->flags&FEATURE_TAG) && feature_available)
|
|
|
|
cursor = conditional_end;
|
2010-10-28 11:00:36 +00:00
|
|
|
#endif
|
2010-05-25 15:19:52 +00:00
|
|
|
|
2011-10-08 13:54:55 +00:00
|
|
|
if(i < children - 1 && *cursor != ENUMLISTSEPARATESYM)
|
|
|
|
{
|
|
|
|
skin_error(SEPARATOR_EXPECTED, cursor);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if(i == children - 1 && *cursor != ENUMLISTCLOSESYM)
|
|
|
|
{
|
|
|
|
skin_error(CLOSE_EXPECTED, cursor);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-25 15:19:52 +00:00
|
|
|
*document = cursor;
|
2011-11-15 14:11:08 +00:00
|
|
|
element->children = skin_buffer_to_offset(children_array);
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-07-31 16:25:41 +00:00
|
|
|
static int skin_parse_comment(struct skin_element* element, const char** document)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-07-31 16:25:41 +00:00
|
|
|
const char* cursor = *document;
|
2010-06-17 07:56:51 +00:00
|
|
|
#ifndef ROCKBOX
|
2010-06-10 21:02:44 +00:00
|
|
|
char* text = NULL;
|
2010-06-17 07:56:51 +00:00
|
|
|
#endif
|
2010-05-25 15:19:52 +00:00
|
|
|
int length;
|
|
|
|
/*
|
|
|
|
* Finding the index of the ending newline or null-terminator
|
|
|
|
* The length of the string of interest doesn't include the leading #, the
|
|
|
|
* length we need to reserve is the same as the index of the last character
|
|
|
|
*/
|
|
|
|
for(length = 0; cursor[length] != '\n' && cursor[length] != '\0'; length++);
|
|
|
|
|
|
|
|
element->type = COMMENT;
|
|
|
|
element->line = skin_line;
|
2010-06-10 21:02:44 +00:00
|
|
|
#ifdef ROCKBOX
|
2011-11-15 14:11:08 +00:00
|
|
|
element->data = INVALID_OFFSET;
|
2010-06-10 21:02:44 +00:00
|
|
|
#else
|
|
|
|
element->data = text = skin_alloc_string(length);
|
2010-07-29 12:37:48 +00:00
|
|
|
if (!element->data)
|
|
|
|
return 0;
|
2010-05-25 15:19:52 +00:00
|
|
|
/* We copy from one char past cursor to leave out the # */
|
2010-06-10 21:02:44 +00:00
|
|
|
memcpy((void*)text, (void*)(cursor + 1),
|
2010-06-01 21:25:02 +00:00
|
|
|
sizeof(char) * (length-1));
|
2010-06-10 21:02:44 +00:00
|
|
|
text[length - 1] = '\0';
|
|
|
|
#endif
|
2010-06-01 21:25:02 +00:00
|
|
|
if(cursor[length] == '\n')
|
2010-05-25 15:19:52 +00:00
|
|
|
skin_line++;
|
|
|
|
|
2010-06-01 21:25:02 +00:00
|
|
|
*document += (length); /* Move cursor up past # and all text */
|
|
|
|
if(**document == '\n')
|
|
|
|
(*document)++;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-07-31 16:25:41 +00:00
|
|
|
static struct skin_element* skin_parse_code_as_arg(const char** document)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
int sublines = 0;
|
2010-07-31 16:25:41 +00:00
|
|
|
const char* cursor = *document;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
/* Checking for sublines */
|
2010-06-10 21:22:16 +00:00
|
|
|
while(*cursor != '\n' && *cursor != '\0'
|
2010-11-05 09:51:19 +00:00
|
|
|
&& *cursor != ENUMLISTSEPARATESYM && *cursor != ARGLISTSEPARATESYM
|
2010-06-10 21:22:16 +00:00
|
|
|
&& *cursor != ENUMLISTCLOSESYM && *cursor != ARGLISTCLOSESYM)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
|
|
|
if(*cursor == MULTILINESYM)
|
|
|
|
{
|
|
|
|
sublines = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(*cursor == TAGSYM)
|
|
|
|
{
|
|
|
|
/* A ';' directly after a '%' doesn't count */
|
|
|
|
cursor ++;
|
|
|
|
|
|
|
|
if(*cursor == '\0')
|
|
|
|
break;
|
|
|
|
|
|
|
|
cursor++;
|
|
|
|
}
|
2010-06-10 21:22:16 +00:00
|
|
|
else if(*cursor == ARGLISTOPENSYM)
|
|
|
|
{
|
|
|
|
skip_arglist(&cursor);
|
|
|
|
}
|
|
|
|
else if(*cursor == ENUMLISTOPENSYM)
|
|
|
|
{
|
|
|
|
skip_enumlist(&cursor);
|
|
|
|
}
|
2010-05-25 15:19:52 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Advancing the cursor as normal */
|
|
|
|
cursor++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sublines)
|
|
|
|
return skin_parse_sublines_optional(document, 1);
|
|
|
|
else
|
|
|
|
return skin_parse_line_optional(document, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Memory management */
|
2010-06-10 21:02:44 +00:00
|
|
|
struct skin_element* skin_alloc_element()
|
|
|
|
{
|
2010-05-25 15:19:52 +00:00
|
|
|
struct skin_element* retval = (struct skin_element*)
|
2010-06-17 13:54:09 +00:00
|
|
|
skin_buffer_alloc(sizeof(struct skin_element));
|
2010-07-29 12:37:48 +00:00
|
|
|
if (!retval)
|
|
|
|
return NULL;
|
2010-06-13 03:13:01 +00:00
|
|
|
retval->type = UNKNOWN;
|
2011-11-15 14:11:08 +00:00
|
|
|
retval->next = skin_buffer_to_offset(NULL);
|
|
|
|
retval->params = skin_buffer_to_offset(NULL);
|
2010-06-11 08:03:32 +00:00
|
|
|
retval->tag = NULL;
|
2010-05-25 15:19:52 +00:00
|
|
|
retval->params_count = 0;
|
|
|
|
retval->children_count = 0;
|
2011-11-15 14:11:08 +00:00
|
|
|
retval->data = INVALID_OFFSET;
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
}
|
2010-07-29 12:37:48 +00:00
|
|
|
/* On a ROCKBOX build we try to save space as much as possible
|
|
|
|
* so if we can, use a shared param pool which should be more then large
|
|
|
|
* enough for any tag. params should be used straight away by the callback
|
|
|
|
* so this is safe.
|
|
|
|
*/
|
2011-11-15 14:11:08 +00:00
|
|
|
struct skin_tag_parameter* skin_alloc_params(int count)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2010-06-10 21:02:44 +00:00
|
|
|
size_t size = sizeof(struct skin_tag_parameter) * count;
|
2010-06-17 13:54:09 +00:00
|
|
|
return (struct skin_tag_parameter*)skin_buffer_alloc(size);
|
2010-05-25 15:19:52 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
char* skin_alloc_string(int length)
|
|
|
|
{
|
2010-06-17 13:54:09 +00:00
|
|
|
return (char*)skin_buffer_alloc(sizeof(char) * (length + 1));
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
|
|
|
|
2011-11-15 14:11:08 +00:00
|
|
|
OFFSETTYPE(struct skin_element*)* skin_alloc_children(int count)
|
2010-05-25 15:19:52 +00:00
|
|
|
{
|
2011-11-15 14:11:08 +00:00
|
|
|
return (OFFSETTYPE(struct skin_element*)*)
|
2010-06-17 13:54:09 +00:00
|
|
|
skin_buffer_alloc(sizeof(struct skin_element*) * count);
|
2010-05-25 15:19:52 +00:00
|
|
|
}
|
2010-05-25 22:24:08 +00:00
|
|
|
|
|
|
|
void skin_free_tree(struct skin_element* root)
|
|
|
|
{
|
2010-06-10 21:02:44 +00:00
|
|
|
#ifndef ROCKBOX
|
2010-05-25 22:24:08 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* First make the recursive call */
|
|
|
|
if(!root)
|
|
|
|
return;
|
|
|
|
skin_free_tree(root->next);
|
|
|
|
|
|
|
|
/* Free any text */
|
2010-06-10 21:02:44 +00:00
|
|
|
if(root->type == TEXT || root->type == COMMENT)
|
|
|
|
free(root->data);
|
2010-05-25 22:24:08 +00:00
|
|
|
|
|
|
|
/* Then recursively free any children, before freeing their pointers */
|
|
|
|
for(i = 0; i < root->children_count; i++)
|
|
|
|
skin_free_tree(root->children[i]);
|
|
|
|
if(root->children_count > 0)
|
|
|
|
free(root->children);
|
|
|
|
|
|
|
|
/* Free any parameters, making sure to deallocate strings */
|
|
|
|
for(i = 0; i < root->params_count; i++)
|
|
|
|
if(root->params[i].type == STRING)
|
|
|
|
free(root->params[i].data.text);
|
|
|
|
if(root->params_count > 0)
|
|
|
|
free(root->params);
|
|
|
|
|
|
|
|
/* Finally, delete root's memory */
|
|
|
|
free(root);
|
2010-06-10 21:02:44 +00:00
|
|
|
#else
|
|
|
|
(void)root;
|
|
|
|
#endif
|
2010-05-25 22:24:08 +00:00
|
|
|
}
|