1 |
/* $MidnightBSD$ */ |
2 |
/*- |
3 |
* Copyright (c) 2013 David Chisnall |
4 |
* All rights reserved. |
5 |
* |
6 |
* This software was developed by SRI International and the University of |
7 |
* Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237) |
8 |
* ("CTSRD"), as part of the DARPA CRASH research programme. |
9 |
* |
10 |
* Redistribution and use in source and binary forms, with or without |
11 |
* modification, are permitted provided that the following conditions |
12 |
* are met: |
13 |
* 1. Redistributions of source code must retain the above copyright |
14 |
* notice, this list of conditions and the following disclaimer. |
15 |
* 2. Redistributions in binary form must reproduce the above copyright |
16 |
* notice, this list of conditions and the following disclaimer in the |
17 |
* documentation and/or other materials provided with the distribution. |
18 |
* |
19 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
20 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
23 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
24 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
25 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
26 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
27 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
28 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 |
* SUCH DAMAGE. |
30 |
* |
31 |
* $FreeBSD: stable/10/usr.bin/dtc/string.hh 245803 2013-01-22 17:49:51Z theraven $ |
32 |
*/ |
33 |
|
34 |
#ifndef _STRING_HH_ |
35 |
#define _STRING_HH_ |
36 |
#include "input_buffer.hh" |
37 |
|
38 |
namespace dtc |
39 |
{ |
40 |
|
41 |
/** |
42 |
* String, referring to a place in the input file. We don't bother copying |
43 |
* strings until we write them to the final output. These strings should be |
44 |
* two words long: a start and a length. They are intended to be cheap to copy |
45 |
* and store in collections. Copying the string object does not copy the |
46 |
* underlying storage. |
47 |
* |
48 |
* Strings are not nul-terminated. |
49 |
*/ |
50 |
class string |
51 |
{ |
52 |
/** Start address. Contained within the mmap()'d input file and not |
53 |
* owned by this object. */ |
54 |
const char *start; |
55 |
/** length of the string. DTS strings are allowed to contain nuls */ |
56 |
int length; |
57 |
/** Generic function for parsing strings matching the character set |
58 |
* defined by the template argument. */ |
59 |
template<class T> |
60 |
static string parse(input_buffer &s); |
61 |
public: |
62 |
/** |
63 |
* Constructs a string referring into another buffer. |
64 |
*/ |
65 |
string(const char *s, int l) : start(s), length(l) {} |
66 |
/** Constructs a string from a C string. */ |
67 |
string(const char *s) : start(s), length(strlen(s)) {} |
68 |
/** Default constructor, returns an empty string. */ |
69 |
string() : start(0), length(0) {} |
70 |
/** Construct a from an input buffer, ending with a nul terminator. */ |
71 |
string(input_buffer &s); |
72 |
/** |
73 |
* Returns the longest string in the input buffer starting at the |
74 |
* current cursor and composed entirely of characters that are valid in |
75 |
* node names. |
76 |
*/ |
77 |
static string parse_node_name(input_buffer &s); |
78 |
/** |
79 |
* Returns the longest string in the input buffer starting at the |
80 |
* current cursor and composed entirely of characters that are valid in |
81 |
* property names. |
82 |
*/ |
83 |
static string parse_property_name(input_buffer &s); |
84 |
/** |
85 |
* Parses either a node or a property name. If is_property is true on |
86 |
* entry, then only property names are parsed. If it is false, then it |
87 |
* will be set, on return, to indicate whether the parsed name is only |
88 |
* valid as a property. |
89 |
*/ |
90 |
static string parse_node_or_property_name(input_buffer &s, |
91 |
bool &is_property); |
92 |
/** |
93 |
* Compares two strings for equality. Strings are equal if they refer |
94 |
* to identical byte sequences. |
95 |
*/ |
96 |
bool operator==(const string& other) const; |
97 |
/** |
98 |
* Compares a string against a C string. The trailing nul in the C |
99 |
* string is ignored for the purpose of comparison, so this will always |
100 |
* fail if the string contains nul bytes. |
101 |
*/ |
102 |
bool operator==(const char *other) const; |
103 |
/** |
104 |
* Inequality operator, defined as the inverse of the equality |
105 |
* operator. |
106 |
*/ |
107 |
template <typename T> |
108 |
inline bool operator!=(T other) |
109 |
{ |
110 |
return !(*this == other); |
111 |
} |
112 |
/** |
113 |
* Comparison operator, defined to allow strings to be used as keys in |
114 |
* maps. |
115 |
*/ |
116 |
bool operator<(const string& other) const; |
117 |
/** |
118 |
* Returns true if this is the empty string, false otherwise. |
119 |
*/ |
120 |
inline bool empty() const |
121 |
{ |
122 |
return length == 0; |
123 |
} |
124 |
/** |
125 |
* Returns the size of the string, in bytes. |
126 |
*/ |
127 |
inline size_t size() |
128 |
{ |
129 |
return length; |
130 |
} |
131 |
/** |
132 |
* Writes the string to the specified buffer. |
133 |
*/ |
134 |
void push_to_buffer(byte_buffer &buffer, bool escapes=false); |
135 |
/** |
136 |
* Prints the string to the specified output stream. |
137 |
*/ |
138 |
void print(FILE *file); |
139 |
/** |
140 |
* Dumps the string to the standard error stream. Intended to be used |
141 |
* for debugging. |
142 |
*/ |
143 |
void dump(); |
144 |
}; |
145 |
|
146 |
} // namespace dtc |
147 |
|
148 |
#endif // !_STRING_HH_ |