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 | #include <stdio.h> |
| 23 | #include <ctype.h> |
| 24 | #include <stdlib.h> |
Robert Bieber | d1659d6 | 2010-06-01 07:11:23 +0000 | [diff] [blame] | 25 | #include <string.h> |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 26 | |
| 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 */ |
| 35 | void skip_comment(char** document) |
| 36 | { |
Robert Bieber | 06ea93d | 2010-06-07 23:49:06 +0000 | [diff] [blame] | 37 | while(**document != '\n' && **document != '\0') |
| 38 | (*document)++; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 39 | if(**document == '\n') |
| 40 | (*document)++; |
| 41 | } |
| 42 | |
| 43 | void skip_whitespace(char** document) |
| 44 | { |
Robert Bieber | 06ea93d | 2010-06-07 23:49:06 +0000 | [diff] [blame] | 45 | while(**document == ' ' || **document == '\t') |
| 46 | (*document)++; |
| 47 | } |
| 48 | |
| 49 | void 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 | |
| 75 | void 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 Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | char* 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 Bieber | 999990c | 2010-06-02 05:45:34 +0000 | [diff] [blame] | 119 | if(*cursor == TAGSYM) |
| 120 | cursor++; |
| 121 | |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 122 | 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 Bieber | 1937b1b | 2010-05-25 17:22:39 +0000 | [diff] [blame] | 135 | buffer[length] = '\0'; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 136 | for(i = 0; i < length; i++) |
| 137 | { |
Robert Bieber | 999990c | 2010-06-02 05:45:34 +0000 | [diff] [blame] | 138 | if(*cursor == TAGSYM) |
| 139 | cursor++; |
| 140 | |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 141 | 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 | |
| 156 | int scan_int(char** document) |
| 157 | { |
| 158 | |
Jonathan Gordon | e8a6624 | 2010-06-02 10:35:19 +0000 | [diff] [blame] | 159 | char* cursor = *document, *end; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 160 | int length = 0; |
Jonathan Gordon | e8a6624 | 2010-06-02 10:35:19 +0000 | [diff] [blame] | 161 | char buffer[16]; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 162 | int retval; |
| 163 | int i; |
| 164 | |
Robert Bieber | ea864be | 2010-06-02 06:52:17 +0000 | [diff] [blame] | 165 | while(isdigit(*cursor) || *cursor == COMMENTSYM || *cursor == '-') |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 166 | { |
| 167 | if(*cursor == COMMENTSYM) |
| 168 | { |
| 169 | skip_comment(&cursor); |
| 170 | continue; |
| 171 | } |
| 172 | |
| 173 | length++; |
| 174 | cursor++; |
| 175 | } |
Jonathan Gordon | e8a6624 | 2010-06-02 10:35:19 +0000 | [diff] [blame] | 176 | if (length > 15) |
| 177 | length = 15; |
| 178 | end = cursor; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 179 | /* 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 Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 196 | |
Jonathan Gordon | e8a6624 | 2010-06-02 10:35:19 +0000 | [diff] [blame] | 197 | *document = end; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 198 | return retval; |
| 199 | } |
Robert Bieber | d1659d6 | 2010-06-01 07:11:23 +0000 | [diff] [blame] | 200 | |
| 201 | int 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 | } |