2 * Copyright (c) 2013 David Chisnall
5 * This software was developed by SRI International and the University of
6 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7 * ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * The source files are ASCII, so we provide a non-locale-aware version of
41 * isalpha. This is a class so that it can be used with a template function
42 * for parsing strings.
46 static inline bool check(const char c)
48 return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') &&
53 * Check whether a character is in the set allowed for node names. This is a
54 * class so that it can be used with a template function for parsing strings.
56 struct is_node_name_character
58 static inline bool check(const char c)
64 case 'a'...'z': case 'A'...'Z': case '0'...'9':
65 case ',': case '.': case '+': case '-':
72 * Check whether a character is in the set allowed for property names. This is
73 * a class so that it can be used with a template function for parsing strings.
75 struct is_property_name_character
77 static inline bool check(const char c)
83 case 'a'...'z': case 'A'...'Z': case '0'...'9':
84 case ',': case '.': case '+': case '-':
96 template<class T> string
97 string::parse(input_buffer &s)
99 const char *start = s;
101 while (T::check(*s)) { l++; ++s; }
102 return string(start, l);
105 string::string(input_buffer &s) : start((const char*)s), length(0)
107 while(s[length] != '\0')
114 string::parse_node_name(input_buffer &s)
116 return parse<is_node_name_character>(s);
120 string::parse_property_name(input_buffer &s)
122 return parse<is_property_name_character>(s);
125 string::parse_node_or_property_name(input_buffer &s, bool &is_property)
129 return parse_property_name(s);
131 const char *start = s;
133 while (is_node_name_character::check(*s))
138 while (is_property_name_character::check(*s))
144 return string(start, l);
148 string::operator==(const string& other) const
150 return (length == other.length) &&
151 (memcmp(start, other.start, length) == 0);
155 string::operator==(const char *other) const
157 return strncmp(other, start, length) == 0;
161 string::operator<(const string& other) const
163 if (length < other.length) { return true; }
164 if (length > other.length) { return false; }
165 return memcmp(start, other.start, length) < 0;
169 string::push_to_buffer(byte_buffer &buffer, bool escapes)
171 for (int i=0 ; i<length ; ++i)
173 uint8_t c = start[i];
174 if (escapes && c == '\\' && i+1 < length)
179 // For now, we just ignore invalid escape sequences.
208 int v = digittoint(c);
209 if (i+1 < length && start[i+1] <= '7' && start[i+1] >= '0')
212 v |= digittoint(start[i+1]);
214 if (i+1 < length && start[i+1] <= '7' && start[i+1] >= '0')
217 v |= digittoint(start[i+1]);
230 int v = digittoint(start[i]);
231 if (i+1 < length && ishexdigit(start[i+1]))
234 v |= digittoint(start[++i]);
246 string::print(FILE *file)
248 fwrite(start, length, 1, file);