summaryrefslogtreecommitdiffstats
path: root/usr.bin/dtc/string.hh
blob: 45bc4fda71c5bb1a7a1fb702e04163b2e3908475 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*-
 * Copyright (c) 2013 David Chisnall
 * All rights reserved.
 *
 * This software was developed by SRI International and the University of
 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
 * ("CTSRD"), as part of the DARPA CRASH research programme.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

#ifndef _STRING_HH_
#define _STRING_HH_
#include "input_buffer.hh"

namespace dtc
{

/**
 * String, referring to a place in the input file.  We don't bother copying
 * strings until we write them to the final output.  These strings should be
 * two words long: a start and a length.  They are intended to be cheap to copy
 * and store in collections.  Copying the string object does not copy the
 * underlying storage.
 *
 * Strings are not nul-terminated.
 */
class string
{
	/** Start address.  Contained within the mmap()'d input file and not
	 * owned by this object. */
	const char *start;
	/** length of the string.  DTS strings are allowed to contain nuls */
	int length;
	/** Generic function for parsing strings matching the character set
	 * defined by the template argument.  */
	template<class T>
	static string parse(input_buffer &s);
	public:
	/**
	 * Constructs a string referring into another buffer.
	 */
	string(const char *s, int l) : start(s), length(l) {}
	/** Constructs a string from a C string.  */
	string(const char *s) : start(s), length(strlen(s)) {}
	/** Default constructor, returns an empty string. */
	string() : start(0), length(0) {}
	/** Construct a from an input buffer, ending with a nul terminator. */
	string(input_buffer &s);
	/**
	 * Returns the longest string in the input buffer starting at the
	 * current cursor and composed entirely of characters that are valid in
	 * node names.
	 */
	static string parse_node_name(input_buffer &s);
	/**
	 * Returns the longest string in the input buffer starting at the
	 * current cursor and composed entirely of characters that are valid in
	 * property names.
	 */
	static string parse_property_name(input_buffer &s);
	/**
	 * Parses either a node or a property name.  If is_property is true on
	 * entry, then only property names are parsed.  If it is false, then it
	 * will be set, on return, to indicate whether the parsed name is only
	 * valid as a property.
	 */
	static string parse_node_or_property_name(input_buffer &s,
	                                          bool &is_property);
	/**
	 * Compares two strings for equality.  Strings are equal if they refer
	 * to identical byte sequences.
	 */
	bool operator==(const string& other) const;
	/**
	 * Compares a string against a C string.  The trailing nul in the C
	 * string is ignored for the purpose of comparison, so this will always
	 * fail if the string contains nul bytes.
	 */
	bool operator==(const char *other) const;
	/**
	 * Inequality operator, defined as the inverse of the equality
	 * operator.
	 */
	template <typename T>
	inline bool operator!=(T other)
	{
		return !(*this == other);
	}
	/**
	 * Comparison operator, defined to allow strings to be used as keys in
	 * maps.
	 */
	bool operator<(const string& other) const;
	/**
	 * Returns true if this is the empty string, false otherwise.
	 */
	inline bool empty() const
	{
		return length == 0;
	}
	/**
	 * Returns the size of the string, in bytes.
	 */
	inline size_t size()
	{
		return length;
	}
	/**
	 * Writes the string to the specified buffer.
	 */
	void push_to_buffer(byte_buffer &buffer, bool escapes=false);
	/**
	 * Prints the string to the specified output stream.
	 */
	void print(FILE *file);
	/**
	 * Dumps the string to the standard error stream.  Intended to be used
	 * for debugging.
	 */
	void dump();
};

} // namespace dtc

#endif // !_STRING_HH_
OpenPOWER on IntegriCloud