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
35 #include "input_buffer.hh"
41 * String, referring to a place in the input file. We don't bother copying
42 * strings until we write them to the final output. These strings should be
43 * two words long: a start and a length. They are intended to be cheap to copy
44 * and store in collections. Copying the string object does not copy the
47 * Strings are not nul-terminated.
51 /** Start address. Contained within the mmap()'d input file and not
52 * owned by this object. */
54 /** length of the string. DTS strings are allowed to contain nuls */
56 /** Generic function for parsing strings matching the character set
57 * defined by the template argument. */
59 static string parse(input_buffer &s);
62 * Constructs a string referring into another buffer.
64 string(const char *s, int l) : start(s), length(l) {}
65 /** Constructs a string from a C string. */
66 string(const char *s) : start(s), length(strlen(s)) {}
67 /** Default constructor, returns an empty string. */
68 string() : start(0), length(0) {}
69 /** Construct a from an input buffer, ending with a nul terminator. */
70 string(input_buffer &s);
72 * Returns the longest string in the input buffer starting at the
73 * current cursor and composed entirely of characters that are valid in
76 static string parse_node_name(input_buffer &s);
78 * Returns the longest string in the input buffer starting at the
79 * current cursor and composed entirely of characters that are valid in
82 static string parse_property_name(input_buffer &s);
84 * Parses either a node or a property name. If is_property is true on
85 * entry, then only property names are parsed. If it is false, then it
86 * will be set, on return, to indicate whether the parsed name is only
87 * valid as a property.
89 static string parse_node_or_property_name(input_buffer &s,
92 * Compares two strings for equality. Strings are equal if they refer
93 * to identical byte sequences.
95 bool operator==(const string& other) const;
97 * Compares a string against a C string. The trailing nul in the C
98 * string is ignored for the purpose of comparison, so this will always
99 * fail if the string contains nul bytes.
101 bool operator==(const char *other) const;
103 * Inequality operator, defined as the inverse of the equality
106 template <typename T>
107 inline bool operator!=(T other)
109 return !(*this == other);
112 * Comparison operator, defined to allow strings to be used as keys in
115 bool operator<(const string& other) const;
117 * Returns true if this is the empty string, false otherwise.
119 inline bool empty() const
124 * Returns the size of the string, in bytes.
131 * Writes the string to the specified buffer.
133 void push_to_buffer(byte_buffer &buffer, bool escapes=false);
135 * Prints the string to the specified output stream.
137 void print(FILE *file);
139 * Dumps the string to the standard error stream. Intended to be used
147 #endif // !_STRING_HH_