blob: 6f1af25a051aab8e5e3d5bcb0734561d3a478a17 [file] [log] [blame]
Robert Bieberd5b24dd2010-05-25 15:19:52 +00001/***************************************************************************
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 Bieberc5e14b52010-05-31 17:39:58 +000025#ifdef __cplusplus
26extern "C"
27{
28#endif
Robert Bieber64321ad2010-06-10 21:02:44 +000029#include <stdlib.h>
Jonathan Gordon1e0a0102010-08-03 12:14:50 +000030#include <stdbool.h>
Robert Bieberd5b24dd2010-05-25 15:19:52 +000031
32/********************************************************************
33 ****** Data Structures *********************************************
34 *******************************************************************/
35
36/* Possible types of element in a WPS file */
37enum skin_element_type
38{
Jonathan Gordon35b09cb2010-06-13 03:13:01 +000039 UNKNOWN = -1,
Robert Bieberd1659d62010-06-01 07:11:23 +000040 VIEWPORT,
Jonathan Gordondc347852010-07-04 02:04:14 +000041 LINE_ALTERNATOR,
Jonathan Gordon78c9a192010-07-04 02:05:42 +000042 LINE,
Robert Bieber6980c1e2010-05-29 00:04:04 +000043 CONDITIONAL,
44 TAG,
Robert Bieber6980c1e2010-05-29 00:04:04 +000045 TEXT,
46 COMMENT,
Robert Bieberd5b24dd2010-05-25 15:19:52 +000047};
48
49enum skin_errorcode
50{
51 MEMORY_LIMIT_EXCEEDED,
52 NEWLINE_EXPECTED,
53 ILLEGAL_TAG,
54 ARGLIST_EXPECTED,
55 TOO_MANY_ARGS,
56 DEFAULT_NOT_ALLOWED,
57 UNEXPECTED_NEWLINE,
58 INSUFFICIENT_ARGS,
59 INT_EXPECTED,
Robert Bieber15488a02010-07-15 06:24:11 +000060 DECIMAL_EXPECTED,
Bertrik Sikkenfffbdcc2010-11-05 09:51:19 +000061 SEPARATOR_EXPECTED,
Robert Bieberd5b24dd2010-05-25 15:19:52 +000062 CLOSE_EXPECTED,
63 MULTILINE_EXPECTED
64};
65
66/* Holds a tag parameter, either numeric or text */
67struct skin_tag_parameter
68{
69 enum
70 {
Robert Bieber15488a02010-07-15 06:24:11 +000071 INTEGER,
72 DECIMAL, /* stored in data.number as (whole*10)+part */
Robert Bieberd5b24dd2010-05-25 15:19:52 +000073 STRING,
74 CODE,
75 DEFAULT
76 } type;
77
78 union
79 {
Robert Bieber15488a02010-07-15 06:24:11 +000080 int number;
Robert Bieberd5b24dd2010-05-25 15:19:52 +000081 char* text;
82 struct skin_element* code;
83 } data;
Robert Bieber5943f4c2010-06-01 19:55:20 +000084
85 char type_code;
Robert Bieberd5b24dd2010-05-25 15:19:52 +000086
87};
88
89/* Defines an element of a SKIN file */
90struct skin_element
91{
92 /* Defines what type of element it is */
93 enum skin_element_type type;
94
95 /* The line on which it's defined in the source file */
96 int line;
97
Robert Bieber64321ad2010-06-10 21:02:44 +000098 /* Placeholder for element data
99 * TEXT and COMMENT uses it for the text string
100 * TAG, VIEWPORT, LINE, etc may use it for post parse extra storage
101 */
102 void* data;
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000103
104 /* The tag or conditional name */
Nils Wallméniusc2529c32010-07-31 11:28:37 +0000105 const struct tag_info *tag;
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000106
107 /* Pointer to and size of an array of parameters */
108 int params_count;
109 struct skin_tag_parameter* params;
110
111 /* Pointer to and size of an array of children */
112 int children_count;
113 struct skin_element** children;
114
115 /* Link to the next element */
116 struct skin_element* next;
117};
118
Jonathan Gordon2d31d772010-07-29 12:37:48 +0000119enum skin_cb_returnvalue
120{
121 CALLBACK_ERROR = -666,
122 FEATURE_NOT_AVAILABLE,
123 CALLBACK_OK = 0,
124 /* > 0 reserved for future use */
125};
126typedef int (*skin_callback)(struct skin_element* element, void* data);
127
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000128/***********************************************************************
129 ***** Functions *******************************************************
130 **********************************************************************/
131
132/* Parses a WPS document and returns a list of skin_element
133 structures. */
Jonathan Gordon2d31d772010-07-29 12:37:48 +0000134#ifdef ROCKBOX
135struct skin_element* skin_parse(const char* document,
136 skin_callback callback, void* callback_data);
137#else
Robert Biebera9848ce2010-06-01 21:25:02 +0000138struct skin_element* skin_parse(const char* document);
Jonathan Gordon2d31d772010-07-29 12:37:48 +0000139#endif
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000140/* Memory management functions */
Björn Stenbergf1a144a2010-06-17 11:04:32 +0000141struct skin_element* skin_alloc_element(void);
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000142struct skin_element** skin_alloc_children(int count);
Jonathan Gordon1e0a0102010-08-03 12:14:50 +0000143struct skin_tag_parameter* skin_alloc_params(int count, bool use_shared_params);
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000144char* skin_alloc_string(int length);
145
Robert Bieber0769fc52010-05-25 22:24:08 +0000146void skin_free_tree(struct skin_element* root);
147
Robert Bieber64321ad2010-06-10 21:02:44 +0000148
Robert Bieberc5e14b52010-05-31 17:39:58 +0000149#ifdef __cplusplus
150}
151#endif
152
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000153#endif /* GENERIC_PARSER_H */