imxtools/sbtools: fix file type detection
[maemo-rb.git] / lib / skin_parser / skin_parser.h
blobec51b64c8b7d6c68fa8b2bffabde00b57e10c95c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2010 Robert Bieber
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #ifndef GENERIC_PARSER_H
23 #define GENERIC_PARSER_H
25 #ifdef __cplusplus
26 extern "C"
28 #endif
29 #include <stdlib.h>
30 #include <stdbool.h>
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 )
41 * WARNING: Don't use the PTRTOSKINOFFSET() around a function call as it wont
42 * do what you expect.
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
51 /********************************************************************
52 ****** Data Structures *********************************************
53 *******************************************************************/
55 /* Possible types of element in a WPS file */
56 enum skin_element_type
58 UNKNOWN = -1,
59 VIEWPORT,
60 LINE_ALTERNATOR,
61 LINE,
62 CONDITIONAL,
63 TAG,
64 TEXT,
65 COMMENT,
68 enum skin_errorcode
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,
79 DECIMAL_EXPECTED,
80 SEPARATOR_EXPECTED,
81 CLOSE_EXPECTED,
82 MULTILINE_EXPECTED
85 /* Holds a tag parameter, either numeric or text */
86 struct skin_tag_parameter
88 enum
90 INTEGER,
91 DECIMAL, /* stored in data.number as (whole*10)+part */
92 PERCENT, /* stored in data.number as (whole*10)+part */
93 STRING,
94 CODE,
95 DEFAULT
96 } type;
98 union
100 int number;
101 OFFSETTYPE(char*) text;
102 OFFSETTYPE(struct skin_element*) code;
103 } data;
105 char type_code;
109 /* Defines an element of a SKIN file,
111 * This is allocated a lot, so it's optimized for size */
112 struct skin_element
114 /* Link to the next element */
115 OFFSETTYPE(struct skin_element*) next;
116 /* Pointer to an array of children */
117 OFFSETTYPE(struct skin_element**) children;
118 /* Placeholder for element data
119 * TEXT and COMMENT uses it for the text string
120 * TAG, VIEWPORT, LINE, etc may use it for post parse extra storage
122 OFFSETTYPE(void*) data;
124 /* The tag or conditional name */
125 const struct tag_info *tag;
127 /* Pointer to an array of parameters */
128 OFFSETTYPE(struct skin_tag_parameter*) params;
130 /* Number of elements in the children array */
131 short children_count;
132 /* The line on which it's defined in the source file */
133 short line;
134 /* Defines what type of element it is */
135 enum skin_element_type type;
136 /* Number of elements in the params array */
137 char params_count;
140 enum skin_cb_returnvalue
142 CALLBACK_ERROR = -666,
143 FEATURE_NOT_AVAILABLE,
144 CALLBACK_OK = 0,
145 /* > 0 reserved for future use */
147 typedef int (*skin_callback)(struct skin_element* element, void* data);
149 /***********************************************************************
150 ***** Functions *******************************************************
151 **********************************************************************/
153 /* Parses a WPS document and returns a list of skin_element
154 structures. */
155 #ifdef ROCKBOX
156 struct skin_element* skin_parse(const char* document,
157 skin_callback callback, void* callback_data);
158 #else
159 struct skin_element* skin_parse(const char* document);
160 #endif
161 /* Memory management functions */
162 struct skin_element* skin_alloc_element(void);
163 OFFSETTYPE(struct skin_element*)* skin_alloc_children(int count);
164 struct skin_tag_parameter* skin_alloc_params(int count);
165 char* skin_alloc_string(int length);
167 void skin_free_tree(struct skin_element* root);
170 #ifdef __cplusplus
172 #endif
174 #endif /* GENERIC_PARSER_H */