Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2010 Robert Bieber |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | |
| 22 | #ifndef GENERIC_PARSER_H |
| 23 | #define GENERIC_PARSER_H |
| 24 | |
Robert Bieber | c5e14b5 | 2010-05-31 17:39:58 +0000 | [diff] [blame] | 25 | #ifdef __cplusplus |
| 26 | extern "C" |
| 27 | { |
| 28 | #endif |
Robert Bieber | 64321ad | 2010-06-10 21:02:44 +0000 | [diff] [blame] | 29 | #include <stdlib.h> |
Jonathan Gordon | 1e0a010 | 2010-08-03 12:14:50 +0000 | [diff] [blame] | 30 | #include <stdbool.h> |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 31 | |
Jonathan Gordon | 9e07ef2 | 2011-11-15 14:11:08 +0000 | [diff] [blame] | 32 | #if defined(ROCKBOX) && !defined(__PCTOOL__) |
| 33 | /* Use this type and macro to convert a pointer from the |
| 34 | * skin buffer to a useable pointer */ |
| 35 | typedef long skinoffset_t; |
| 36 | #define SKINOFFSETTOPTR(base, offset) ((offset) < 0 ? NULL : ((void*)&base[offset])) |
| 37 | #define PTRTOSKINOFFSET(base, pointer) ((pointer) ? ((void*)pointer-(void*)base) : -1) |
| 38 | /* Use this macro when declaring a variable to self-document the code. |
| 39 | * type is the actual type being pointed to (i.e OFFSETTYPE(char*) foo ) |
| 40 | * |
| 41 | * WARNING: Don't use the PTRTOSKINOFFSET() around a function call as it wont |
| 42 | * do what you expect. |
| 43 | */ |
| 44 | #define OFFSETTYPE(type) skinoffset_t |
| 45 | #else |
| 46 | #define SKINOFFSETTOPTR(base, offset) offset |
| 47 | #define PTRTOSKINOFFSET(base, pointer) pointer |
| 48 | #define OFFSETTYPE(type) type |
| 49 | #endif |
| 50 | |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 51 | /******************************************************************** |
| 52 | ****** Data Structures ********************************************* |
| 53 | *******************************************************************/ |
| 54 | |
| 55 | /* Possible types of element in a WPS file */ |
| 56 | enum skin_element_type |
| 57 | { |
Jonathan Gordon | 35b09cb | 2010-06-13 03:13:01 +0000 | [diff] [blame] | 58 | UNKNOWN = -1, |
Robert Bieber | d1659d6 | 2010-06-01 07:11:23 +0000 | [diff] [blame] | 59 | VIEWPORT, |
Jonathan Gordon | dc34785 | 2010-07-04 02:04:14 +0000 | [diff] [blame] | 60 | LINE_ALTERNATOR, |
Jonathan Gordon | 78c9a19 | 2010-07-04 02:05:42 +0000 | [diff] [blame] | 61 | LINE, |
Robert Bieber | 6980c1e | 2010-05-29 00:04:04 +0000 | [diff] [blame] | 62 | CONDITIONAL, |
| 63 | TAG, |
Robert Bieber | 6980c1e | 2010-05-29 00:04:04 +0000 | [diff] [blame] | 64 | TEXT, |
| 65 | COMMENT, |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | enum skin_errorcode |
| 69 | { |
| 70 | MEMORY_LIMIT_EXCEEDED, |
| 71 | NEWLINE_EXPECTED, |
| 72 | ILLEGAL_TAG, |
| 73 | ARGLIST_EXPECTED, |
| 74 | TOO_MANY_ARGS, |
| 75 | DEFAULT_NOT_ALLOWED, |
| 76 | UNEXPECTED_NEWLINE, |
| 77 | INSUFFICIENT_ARGS, |
| 78 | INT_EXPECTED, |
Robert Bieber | 15488a0 | 2010-07-15 06:24:11 +0000 | [diff] [blame] | 79 | DECIMAL_EXPECTED, |
Bertrik Sikken | fffbdcc | 2010-11-05 09:51:19 +0000 | [diff] [blame] | 80 | SEPARATOR_EXPECTED, |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 81 | CLOSE_EXPECTED, |
| 82 | MULTILINE_EXPECTED |
| 83 | }; |
| 84 | |
| 85 | /* Holds a tag parameter, either numeric or text */ |
| 86 | struct skin_tag_parameter |
| 87 | { |
| 88 | enum |
| 89 | { |
Robert Bieber | 15488a0 | 2010-07-15 06:24:11 +0000 | [diff] [blame] | 90 | INTEGER, |
| 91 | DECIMAL, /* stored in data.number as (whole*10)+part */ |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 92 | STRING, |
| 93 | CODE, |
| 94 | DEFAULT |
| 95 | } type; |
| 96 | |
| 97 | union |
| 98 | { |
Robert Bieber | 15488a0 | 2010-07-15 06:24:11 +0000 | [diff] [blame] | 99 | int number; |
Jonathan Gordon | 9e07ef2 | 2011-11-15 14:11:08 +0000 | [diff] [blame] | 100 | OFFSETTYPE(char*) text; |
| 101 | OFFSETTYPE(struct skin_element*) code; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 102 | } data; |
Robert Bieber | 5943f4c | 2010-06-01 19:55:20 +0000 | [diff] [blame] | 103 | |
| 104 | char type_code; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 105 | |
| 106 | }; |
| 107 | |
Thomas Martitz | 4f3e1d6 | 2011-10-16 15:55:12 +0000 | [diff] [blame] | 108 | /* Defines an element of a SKIN file, |
| 109 | * |
| 110 | * This is allocated a lot, so it's optimized for size */ |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 111 | struct skin_element |
| 112 | { |
Thomas Martitz | 4f3e1d6 | 2011-10-16 15:55:12 +0000 | [diff] [blame] | 113 | /* Link to the next element */ |
Jonathan Gordon | 9e07ef2 | 2011-11-15 14:11:08 +0000 | [diff] [blame] | 114 | OFFSETTYPE(struct skin_element*) next; |
Thomas Martitz | 4f3e1d6 | 2011-10-16 15:55:12 +0000 | [diff] [blame] | 115 | /* Pointer to an array of children */ |
Jonathan Gordon | 9e07ef2 | 2011-11-15 14:11:08 +0000 | [diff] [blame] | 116 | OFFSETTYPE(struct skin_element**) children; |
Robert Bieber | 64321ad | 2010-06-10 21:02:44 +0000 | [diff] [blame] | 117 | /* Placeholder for element data |
| 118 | * TEXT and COMMENT uses it for the text string |
| 119 | * TAG, VIEWPORT, LINE, etc may use it for post parse extra storage |
| 120 | */ |
Jonathan Gordon | 9e07ef2 | 2011-11-15 14:11:08 +0000 | [diff] [blame] | 121 | OFFSETTYPE(void*) data; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 122 | |
| 123 | /* The tag or conditional name */ |
Nils Wallménius | c2529c3 | 2010-07-31 11:28:37 +0000 | [diff] [blame] | 124 | const struct tag_info *tag; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 125 | |
Thomas Martitz | 4f3e1d6 | 2011-10-16 15:55:12 +0000 | [diff] [blame] | 126 | /* Pointer to an array of parameters */ |
Jonathan Gordon | 9e07ef2 | 2011-11-15 14:11:08 +0000 | [diff] [blame] | 127 | OFFSETTYPE(struct skin_tag_parameter*) params; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 128 | |
Thomas Martitz | 4f3e1d6 | 2011-10-16 15:55:12 +0000 | [diff] [blame] | 129 | /* Number of elements in the children array */ |
| 130 | short children_count; |
| 131 | /* The line on which it's defined in the source file */ |
| 132 | short line; |
| 133 | /* Defines what type of element it is */ |
| 134 | enum skin_element_type type; |
| 135 | /* Number of elements in the params array */ |
| 136 | char params_count; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
Jonathan Gordon | 2d31d77 | 2010-07-29 12:37:48 +0000 | [diff] [blame] | 139 | enum skin_cb_returnvalue |
| 140 | { |
| 141 | CALLBACK_ERROR = -666, |
| 142 | FEATURE_NOT_AVAILABLE, |
| 143 | CALLBACK_OK = 0, |
| 144 | /* > 0 reserved for future use */ |
| 145 | }; |
| 146 | typedef int (*skin_callback)(struct skin_element* element, void* data); |
| 147 | |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 148 | /*********************************************************************** |
| 149 | ***** Functions ******************************************************* |
| 150 | **********************************************************************/ |
| 151 | |
| 152 | /* Parses a WPS document and returns a list of skin_element |
| 153 | structures. */ |
Jonathan Gordon | 2d31d77 | 2010-07-29 12:37:48 +0000 | [diff] [blame] | 154 | #ifdef ROCKBOX |
| 155 | struct skin_element* skin_parse(const char* document, |
| 156 | skin_callback callback, void* callback_data); |
| 157 | #else |
Robert Bieber | a9848ce | 2010-06-01 21:25:02 +0000 | [diff] [blame] | 158 | struct skin_element* skin_parse(const char* document); |
Jonathan Gordon | 2d31d77 | 2010-07-29 12:37:48 +0000 | [diff] [blame] | 159 | #endif |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 160 | /* Memory management functions */ |
Björn Stenberg | f1a144a | 2010-06-17 11:04:32 +0000 | [diff] [blame] | 161 | struct skin_element* skin_alloc_element(void); |
Jonathan Gordon | 9e07ef2 | 2011-11-15 14:11:08 +0000 | [diff] [blame] | 162 | OFFSETTYPE(struct skin_element*)* skin_alloc_children(int count); |
| 163 | struct skin_tag_parameter* skin_alloc_params(int count); |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 164 | char* skin_alloc_string(int length); |
| 165 | |
Robert Bieber | 0769fc5 | 2010-05-25 22:24:08 +0000 | [diff] [blame] | 166 | void skin_free_tree(struct skin_element* root); |
| 167 | |
Robert Bieber | 64321ad | 2010-06-10 21:02:44 +0000 | [diff] [blame] | 168 | |
Robert Bieber | c5e14b5 | 2010-05-31 17:39:58 +0000 | [diff] [blame] | 169 | #ifdef __cplusplus |
| 170 | } |
| 171 | #endif |
| 172 | |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 173 | #endif /* GENERIC_PARSER_H */ |