blob: 50d58bc250f3e65859b0e637d9dfe7c4924a4d16 [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 */
Nils Wallménius597ccdd2010-07-31 16:25:41 +000035void skip_comment(const char** document)
Robert Bieberd5b24dd2010-05-25 15:19:52 +000036{
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
Nils Wallménius597ccdd2010-07-31 16:25:41 +000043void skip_arglist(const char** document)
Robert Bieber06ea93d2010-06-07 23:49:06 +000044{
45 if(**document == ARGLISTOPENSYM)
46 (*document)++;
47 while(**document && **document != ARGLISTCLOSESYM)
48 {
49 if(**document == TAGSYM)
50 {
51 (*document)++;
52 if(**document == '\0')
53 break;
54 (*document)++;
55 }
56 else if(**document == ARGLISTOPENSYM)
57 skip_arglist(document);
58 else if(**document == ENUMLISTOPENSYM)
59 skip_enumlist(document);
60 else if(**document == COMMENTSYM)
61 skip_comment(document);
62 else
63 (*document)++;
64 }
65 if(**document == ARGLISTCLOSESYM)
66 (*document)++;
67}
68
Nils Wallménius597ccdd2010-07-31 16:25:41 +000069void skip_enumlist(const char** document)
Robert Bieber06ea93d2010-06-07 23:49:06 +000070{
71 if(**document == ENUMLISTOPENSYM)
72 (*document)++;
73 while(**document && **document != ENUMLISTCLOSESYM)
74 {
75 if(**document == TAGSYM)
76 {
77 (*document)++;
78 if(**document == '\0')
79 break;
80 (*document)++;
81 }
82 else if(**document == ARGLISTOPENSYM)
83 skip_arglist(document);
84 else if(**document == ENUMLISTOPENSYM)
85 skip_enumlist(document);
86 else if(**document == COMMENTSYM)
87 skip_comment(document);
88 else
89 (*document)++;
90 }
91
92 if(**document == ENUMLISTCLOSESYM)
93 (*document)++;
Robert Bieberd5b24dd2010-05-25 15:19:52 +000094}
95
Nils Wallménius597ccdd2010-07-31 16:25:41 +000096char* scan_string(const char** document)
Robert Bieberd5b24dd2010-05-25 15:19:52 +000097{
98
Nils Wallménius597ccdd2010-07-31 16:25:41 +000099 const char* cursor = *document;
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000100 int length = 0;
101 char* buffer = NULL;
102 int i;
103
Bertrik Sikkenfffbdcc2010-11-05 09:51:19 +0000104 while(*cursor != ARGLISTSEPARATESYM && *cursor != ARGLISTCLOSESYM &&
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000105 *cursor != '\0')
106 {
107 if(*cursor == COMMENTSYM)
108 {
109 skip_comment(&cursor);
110 continue;
111 }
112
Robert Bieber999990c2010-06-02 05:45:34 +0000113 if(*cursor == TAGSYM)
114 cursor++;
115
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000116 if(*cursor == '\n')
117 {
Dominik Riebeling3cd19682010-07-18 08:58:04 +0000118 skin_error(UNEXPECTED_NEWLINE, cursor);
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000119 return NULL;
120 }
121
122 length++;
123 cursor++;
124 }
125
126 /* Copying the string */
127 cursor = *document;
128 buffer = skin_alloc_string(length);
Jonathan Gordon2d31d772010-07-29 12:37:48 +0000129 if (!buffer)
130 return NULL;
Robert Bieber1937b1b2010-05-25 17:22:39 +0000131 buffer[length] = '\0';
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000132 for(i = 0; i < length; i++)
133 {
Robert Bieber999990c2010-06-02 05:45:34 +0000134 if(*cursor == TAGSYM)
135 cursor++;
136
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000137 if(*cursor == COMMENTSYM)
138 {
139 skip_comment(&cursor);
140 i--;
141 continue;
142 }
143
144 buffer[i] = *cursor;
145 cursor++;
146 }
147
148 *document = cursor;
149 return buffer;
150}
151
Nils Wallménius597ccdd2010-07-31 16:25:41 +0000152int scan_int(const char** document)
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000153{
154
Nils Wallménius597ccdd2010-07-31 16:25:41 +0000155 const char *cursor = *document, *end;
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000156 int length = 0;
Jonathan Gordone8a66242010-06-02 10:35:19 +0000157 char buffer[16];
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000158 int retval;
159 int i;
160
Robert Bieberea864be2010-06-02 06:52:17 +0000161 while(isdigit(*cursor) || *cursor == COMMENTSYM || *cursor == '-')
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000162 {
163 if(*cursor == COMMENTSYM)
164 {
165 skip_comment(&cursor);
166 continue;
167 }
168
169 length++;
170 cursor++;
171 }
Jonathan Gordone8a66242010-06-02 10:35:19 +0000172 if (length > 15)
173 length = 15;
174 end = cursor;
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000175 /* Copying to the buffer while avoiding comments */
176 cursor = *document;
177 buffer[length] = '\0';
178 for(i = 0; i < length; i++)
179 {
180 if(*cursor == COMMENTSYM)
181 {
182 skip_comment(&cursor);
183 i--;
184 continue;
185 }
186
187 buffer[i] = *cursor;
188 cursor++;
189
190 }
191 retval = atoi(buffer);
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000192
Jonathan Gordone8a66242010-06-02 10:35:19 +0000193 *document = end;
Robert Bieberd5b24dd2010-05-25 15:19:52 +0000194 return retval;
195}
Robert Bieberd1659d62010-06-01 07:11:23 +0000196
Nils Wallménius597ccdd2010-07-31 16:25:41 +0000197int check_viewport(const char* document)
Robert Bieberd1659d62010-06-01 07:11:23 +0000198{
199 if(strlen(document) < 3)
200 return 0;
201
202 if(document[0] != TAGSYM)
203 return 0;
204
205 if(document[1] != 'V')
206 return 0;
207
208 if(document[2] != ARGLISTOPENSYM
209 && document[2] != 'l'
210 && document[2] != 'i')
211 return 0;
212
213 return 1;
214}