Debian Codegraph @

Utilities/scr2xml/resource_tokenizer.cpp

raw
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2005 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** (if your name is missing here, please add it)
27 */
28
29 #include "resource_tokenizer.h"
30 #include <ClanLib/Core/System/clanstring.h>
31
32 ResourceTokenizer::ResourceTokenizer(std::vector<char>& buffer)
33 : config_file_data(buffer)
34 {
35 cur_pos = 0;
36 cur_line = 1;
37 in_remark = false;
38 total_size = buffer.size();
39 }
40
41 ResourceTokenizer::~ResourceTokenizer()
42 {
43 }
44
45 bool ResourceTokenizer::is_operator(char c)
46 {
47 return
48 c == ',' ||
49 c == '=' ||
50 c == '(' ||
51 c == ')' ||
52 c == ';' ||
53 c == '{' ||
54 c == '}';
55 }
56
57 bool ResourceTokenizer::is_space(char c)
58 {
59 return
60 c == ' ' ||
61 c == '\t' ||
62 c == 13 ||
63 c == 10;
64 }
65
66 bool ResourceTokenizer::is_remark(char c, int position)
67 {
68 if (in_remark)
69 {
70 if (c == '\n')
71 {
72 in_remark = false;
73 }
74 return true;
75 }
76 else
77 {
78 if (c == '/')
79 {
80 if (
81 position+1 < total_size &&
82 config_file_data[position+1] == '/')
83 {
84 in_remark = true;
85 return true;
86 }
87 }
88 else if (c == '#')
89 {
90 in_remark = true;
91 return true;
92 }
93 }
94 return false;
95 }
96
97 std::string ResourceTokenizer::get_next_token()
98 {
99 while (
100 cur_pos < total_size &&
101 (
102 is_remark(config_file_data[cur_pos], cur_pos) ||
103 is_space(config_file_data[cur_pos])
104 ))
105 {
106 if (config_file_data[cur_pos] == '\n') cur_line++;
107 cur_pos++;
108 }
109 if (cur_pos == total_size) return "";
110
111 if (is_operator(config_file_data[cur_pos]))
112 {
113 char temp[2];
114 temp[0] = config_file_data[cur_pos];
115 temp[1] = 0;
116 std::string ret(temp);
117 cur_pos++;
118
119 return ret;
120 }
121 else
122 {
123 int temp_pos = cur_pos;
124 if (config_file_data[temp_pos] == '"')
125 {
126 int num_chars = 0;
127 temp_pos++;
128 while (temp_pos < total_size)
129 {
130 if (config_file_data[temp_pos] == '"')
131 {
132 if (temp_pos+1>=total_size || config_file_data[temp_pos+1] != '"') break;
133 else
134 {
135 temp_pos++;
136 }
137 }
138 temp_pos++;
139 num_chars++;
140 }
141 if (temp_pos == total_size)
142 {
143 write_error("Missing '""' following '""'-begin");
144 return "";
145 }
146
147 char *temp = new char[num_chars+1];
148 temp[num_chars] = 0;
149 int ofs = 1;
150 for (int i=0;i<num_chars;i++)
151 {
152 temp[i] = config_file_data[cur_pos+ofs+i];
153 if (config_file_data[cur_pos+ofs+i] == '"')
154 {
155 ofs++;
156 }
157 }
158 cur_pos = temp_pos+1;
159
160 std::string ret(temp);
161 delete [] temp;
162
163 return ret;
164 }
165 else
166 {
167 while (temp_pos < total_size &&
168 !is_space(config_file_data[temp_pos]) &&
169 !is_operator(config_file_data[temp_pos]) &&
170 !is_remark(config_file_data[temp_pos], cur_pos))
171 {
172 temp_pos++;
173 }
174 int tok_size = temp_pos-cur_pos;
175 char *temp = new char[tok_size+1];
176 temp[tok_size] = 0;
177 memcpy(temp, &config_file_data[cur_pos], tok_size);
178 cur_pos += tok_size;
179
180 std::string ret(temp);
181 delete [] temp;
182
183 return ret;
184 }
185 }
186 }
187
188 std::string ResourceTokenizer::write_error(std::string err_msg)
189 {
190 std::string err;
191
192 err = CL_String::format("Error occured during read of config file (line %1): %2", cur_line, err_msg.c_str());
193
194 return err;
195 }