blob: 79f7162aab9c87a1248594666954a66d3dabe163 [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#include <stdio.h>
23#include <ctype.h>
24#include <stdlib.h>
Robert Bieberd1659d62010-06-01 07:11:23 +000025#include <string.h>
Robert Bieberd5b24dd2010-05-25 15:19:52 +000026
27#include "skin_scan.h"
28#include "skin_debug.h"
29#include "symbols.h"
30#include "skin_parser.h"
31
32/* Scanning Functions */
33
34/* Simple function to advance a char* past a comment */
35void skip_comment(char** document)
36{
Robert Bieber06ea93d2010-06-07 23:49:06 +000037 while(**document != '\n' && **document != '\0')
38 (*document)++;
Robert Bieberd5b24dd2010-05-25 15:19:52 +000039 if(**document == '\n')
40 (*document)++;
41}
42
43void skip_whitespace(char** document)
44{
Robert Bieber06ea93d2010-06-07 23:49:06 +000045 while(**document == ' ' || **document == '\t')
46 (*document)++;
47}
48
49void skip_arglist(char** document)
50{
51 if(**document == ARGLISTOPENSYM)
52 (*document)++;
53 while(**document && **document != ARGLISTCLOSESYM)
54 {
55 if(**document == TAGSYM)
56 {
57 (*document)++;
58 if(**document == '\0')
59 break;
60 (*document)++;
61 }
62 else if(**document == ARGLISTOPENSYM)
63 skip_arglist(document);
64 else if(**document == ENUMLISTOPENSYM)
65 skip_enumlist(document);
66 else if(**document == COMMENTSYM)
67 skip_comment(document);
68 else
69 (*document)++;
70 }
71 if(**document == ARGLISTCLOSESYM)
72 (*document)++;
73}
74
75void skip_enumlist(char** document)
76{
77 if(**document == ENUMLISTOPENSYM)
78 (*document)++;
79 while(**document && **document != ENUMLISTCLOSESYM)
80 {
81 if(**document == TAGSYM)
82 {
83 (*document)++;
84 if(**document == '\0')
85 break;
86 (*document)++;
87 }
88 else if(**document == ARGLISTOPENSYM)
89 skip_arglist(document);
90 else if(**document == ENUMLISTOPENSYM)
91 skip_enumlist(document);
92 else if(**document == COMMENTSYM)
93 skip_comment(document);
94 else
95 (*document)++;
96 }
97
98 if(**document == ENUMLISTCLOSESYM)
99 (*document)++;
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000100}
101
102char* scan_string(char** document)
103{
104
105 char* cursor = *document;
106 int length = 0;
107 char* buffer = NULL;
108 int i;
109
110 while(*cursor != ARGLISTSEPERATESYM && *cursor != ARGLISTCLOSESYM &&
111 *cursor != '\0')
112 {
113 if(*cursor == COMMENTSYM)
114 {
115 skip_comment(&cursor);
116 continue;
117 }
118
Robert Bieber999990c2010-06-02 05:45:34 +0000119 if(*cursor == TAGSYM)
120 cursor++;
121
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000122 if(*cursor == '\n')
123 {
124 skin_error(UNEXPECTED_NEWLINE);
125 return NULL;
126 }
127
128 length++;
129 cursor++;
130 }
131
132 /* Copying the string */
133 cursor = *document;
134 buffer = skin_alloc_string(length);
Robert Bieber1937b1b2010-05-25 17:22:39 +0000135 buffer[length] = '\0';
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000136 for(i = 0; i < length; i++)
137 {
Robert Bieber999990c2010-06-02 05:45:34 +0000138 if(*cursor == TAGSYM)
139 cursor++;
140
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000141 if(*cursor == COMMENTSYM)
142 {
143 skip_comment(&cursor);
144 i--;
145 continue;
146 }
147
148 buffer[i] = *cursor;
149 cursor++;
150 }
151
152 *document = cursor;
153 return buffer;
154}
155
156int scan_int(char** document)
157{
158
Jonathan Gordone8a66242010-06-02 10:35:19 +0000159 char* cursor = *document, *end;
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000160 int length = 0;
Jonathan Gordone8a66242010-06-02 10:35:19 +0000161 char buffer[16];
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000162 int retval;
163 int i;
164
Robert Bieberea864be2010-06-02 06:52:17 +0000165 while(isdigit(*cursor) || *cursor == COMMENTSYM || *cursor == '-')
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000166 {
167 if(*cursor == COMMENTSYM)
168 {
169 skip_comment(&cursor);
170 continue;
171 }
172
173 length++;
174 cursor++;
175 }
Jonathan Gordone8a66242010-06-02 10:35:19 +0000176 if (length > 15)
177 length = 15;
178 end = cursor;
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000179 /* Copying to the buffer while avoiding comments */
180 cursor = *document;
181 buffer[length] = '\0';
182 for(i = 0; i < length; i++)
183 {
184 if(*cursor == COMMENTSYM)
185 {
186 skip_comment(&cursor);
187 i--;
188 continue;
189 }
190
191 buffer[i] = *cursor;
192 cursor++;
193
194 }
195 retval = atoi(buffer);
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000196
Jonathan Gordone8a66242010-06-02 10:35:19 +0000197 *document = end;
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000198 return retval;
199}
Robert Bieberd1659d62010-06-01 07:11:23 +0000200
201int check_viewport(char* document)
202{
203 if(strlen(document) < 3)
204 return 0;
205
206 if(document[0] != TAGSYM)
207 return 0;
208
209 if(document[1] != 'V')
210 return 0;
211
212 if(document[2] != ARGLISTOPENSYM
213 && document[2] != 'l'
214 && document[2] != 'i')
215 return 0;
216
217 return 1;
218}