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 */ |
Nils Wallménius | 597ccdd | 2010-07-31 16:25:41 +0000 | [diff] [blame] | 35 | void skip_comment(const char** document) |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 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 | |
Nils Wallménius | 597ccdd | 2010-07-31 16:25:41 +0000 | [diff] [blame] | 43 | void skip_arglist(const char** document) |
Robert Bieber | 06ea93d | 2010-06-07 23:49:06 +0000 | [diff] [blame] | 44 | { |
| 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énius | 597ccdd | 2010-07-31 16:25:41 +0000 | [diff] [blame] | 69 | void skip_enumlist(const char** document) |
Robert Bieber | 06ea93d | 2010-06-07 23:49:06 +0000 | [diff] [blame] | 70 | { |
| 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 Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Nils Wallménius | 597ccdd | 2010-07-31 16:25:41 +0000 | [diff] [blame] | 96 | char* scan_string(const char** document) |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 97 | { |
| 98 | |
Nils Wallménius | 597ccdd | 2010-07-31 16:25:41 +0000 | [diff] [blame] | 99 | const char* cursor = *document; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 100 | int length = 0; |
| 101 | char* buffer = NULL; |
| 102 | int i; |
| 103 | |
Bertrik Sikken | fffbdcc | 2010-11-05 09:51:19 +0000 | [diff] [blame] | 104 | while(*cursor != ARGLISTSEPARATESYM && *cursor != ARGLISTCLOSESYM && |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 105 | *cursor != '\0') |
| 106 | { |
| 107 | if(*cursor == COMMENTSYM) |
| 108 | { |
| 109 | skip_comment(&cursor); |
| 110 | continue; |
| 111 | } |
| 112 | |
Robert Bieber | 999990c | 2010-06-02 05:45:34 +0000 | [diff] [blame] | 113 | if(*cursor == TAGSYM) |
| 114 | cursor++; |
| 115 | |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 116 | if(*cursor == '\n') |
| 117 | { |
Dominik Riebeling | 3cd1968 | 2010-07-18 08:58:04 +0000 | [diff] [blame] | 118 | skin_error(UNEXPECTED_NEWLINE, cursor); |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 119 | return NULL; |
| 120 | } |
| 121 | |
| 122 | length++; |
| 123 | cursor++; |
| 124 | } |
| 125 | |
| 126 | /* Copying the string */ |
| 127 | cursor = *document; |
| 128 | buffer = skin_alloc_string(length); |
Jonathan Gordon | 2d31d77 | 2010-07-29 12:37:48 +0000 | [diff] [blame] | 129 | if (!buffer) |
| 130 | return NULL; |
Robert Bieber | 1937b1b | 2010-05-25 17:22:39 +0000 | [diff] [blame] | 131 | buffer[length] = '\0'; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 132 | for(i = 0; i < length; i++) |
| 133 | { |
Robert Bieber | 999990c | 2010-06-02 05:45:34 +0000 | [diff] [blame] | 134 | if(*cursor == TAGSYM) |
| 135 | cursor++; |
| 136 | |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 137 | 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énius | 597ccdd | 2010-07-31 16:25:41 +0000 | [diff] [blame] | 152 | int scan_int(const char** document) |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 153 | { |
| 154 | |
Nils Wallménius | 597ccdd | 2010-07-31 16:25:41 +0000 | [diff] [blame] | 155 | const char *cursor = *document, *end; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 156 | int length = 0; |
Jonathan Gordon | e8a6624 | 2010-06-02 10:35:19 +0000 | [diff] [blame] | 157 | char buffer[16]; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 158 | int retval; |
| 159 | int i; |
| 160 | |
Robert Bieber | ea864be | 2010-06-02 06:52:17 +0000 | [diff] [blame] | 161 | while(isdigit(*cursor) || *cursor == COMMENTSYM || *cursor == '-') |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 162 | { |
| 163 | if(*cursor == COMMENTSYM) |
| 164 | { |
| 165 | skip_comment(&cursor); |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | length++; |
| 170 | cursor++; |
| 171 | } |
Jonathan Gordon | e8a6624 | 2010-06-02 10:35:19 +0000 | [diff] [blame] | 172 | if (length > 15) |
| 173 | length = 15; |
| 174 | end = cursor; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 175 | /* 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 Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 192 | |
Jonathan Gordon | e8a6624 | 2010-06-02 10:35:19 +0000 | [diff] [blame] | 193 | *document = end; |
Robert Bieber | d5b24dd | 2010-05-25 15:19:52 +0000 | [diff] [blame] | 194 | return retval; |
| 195 | } |
Robert Bieber | d1659d6 | 2010-06-01 07:11:23 +0000 | [diff] [blame] | 196 | |
Nils Wallménius | 597ccdd | 2010-07-31 16:25:41 +0000 | [diff] [blame] | 197 | int check_viewport(const char* document) |
Robert Bieber | d1659d6 | 2010-06-01 07:11:23 +0000 | [diff] [blame] | 198 | { |
| 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 | } |