aboutsummaryrefslogtreecommitdiffhomepage
path: root/include/PropSet.h
blob: 9766aa8612de5203ee18b40d555fb34cde572698 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// SciTE - Scintilla based Text Editor
// PropSet.h - a java style properties file module
// Copyright 1998-2000 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.

#ifndef PROPSET_H
#define PROPSET_H

bool EqualCaseInsensitive(const char *a, const char *b);

#if PLAT_WIN
#define strcasecmp  stricmp
#define strncasecmp strnicmp
#endif

// Define another string class.
// While it would be 'better' to use std::string, that doubles the executable size.

inline char *StringDup(const char *s, int len=-1) {
	if (!s)
		return 0;
	if (len == -1)
		len = strlen(s);
	char *sNew = new char[len + 1];
	if (sNew) {
		strncpy(sNew, s, len);
		sNew[len] = '\0';
	}
	return sNew;
}

class SString {
	char *s;
	int ssize;
public:
	typedef const char* const_iterator;
	typedef int size_type;
	static size_type npos;
	const char* begin(void) const {
		return s;
	}
	const char* end(void) const {
		return &s[ssize];
	}
	size_type size(void) const {
		if (s)
			return ssize;
		else
			return 0;
	}
	SString &assign(const char* sother, int size_ = -1) {
		char *t = s;
		s = StringDup(sother,size_);
		ssize = (s) ? strlen(s) : 0;
		delete []t;
		return *this;
	}
	SString &assign(const SString& sother, int size_ = -1) {
		return assign(sother.s,size_);
	}
	SString &assign(const_iterator ibeg, const_iterator iend) {
		return assign(ibeg,iend - ibeg);
	}
	SString() {
		s = 0;
		ssize = 0;
	}
	SString(const SString &source) {
		s = StringDup(source.s);
		ssize = (s) ? strlen(s) : 0;
	}
	SString(const char *s_) {
		s = StringDup(s_);
		ssize = (s) ? strlen(s) : 0;
	}
	SString(int i) {
		char number[100];
		sprintf(number, "%0d", i);
		s = StringDup(number);
		ssize = (s) ? strlen(s) : 0;
	}
	~SString() {
		delete []s;
		s = 0;
		ssize = 0;
	}
	SString &operator=(const SString &source) {
		if (this != &source) {
			delete []s;
			s = StringDup(source.s);
			ssize = (s) ? strlen(s) : 0;
		}
		return *this;
	}
	bool operator==(const SString &other) const {
		if ((s == 0) && (other.s == 0))
			return true;
		if ((s == 0) || (other.s == 0))
			return false;
		return strcmp(s, other.s) == 0;
	}
	bool operator!=(const SString &other) const {
		return !operator==(other);
	}
	bool operator==(const char *sother) const {
		if ((s == 0) && (sother == 0))
			return true;
		if ((s == 0) || (sother == 0))
			return false;
		return strcmp(s, sother) == 0;
	}
	bool operator!=(const char *sother) const {
		return !operator==(sother);
	}
	const char *c_str() const {
		if (s)
			return s;
		else
			return "";
	}
	int length() const {
		if (s)
			return strlen(s);
		else
			return 0;
	}
	char operator[](int i) const {
		if (s)
			return s[i];
		else
			return '\0';
	}
	SString &operator +=(const char *sother) {
		return append(sother,-1);
	}
	SString &operator +=(const SString &sother) {
		return append(sother.s,sother.ssize);
	}
	SString &operator +=(char ch) {
		return append(&ch,1);
	}
	SString &append(const char* sother, int lenOther) {
		int len = length();
		if(lenOther < 0) 
			lenOther = strlen(sother);
		char *sNew = new char[len + lenOther + 1];
		if (sNew) {
			if (s)
				memcpy(sNew, s, len);
			strncpy(&sNew[len], sother, lenOther);
			sNew[len + lenOther] = '\0';
			delete []s;
			s = sNew;
			ssize = (s) ? strlen(s) : 0;
		}
		return *this;
	}
	int value() const {
		if (s)
			return atoi(s);
		else 
			return 0;
	}
	void substitute(char find, char replace) {
		char *t = s;
		while (t) {
			t = strchr(t, find);
			if (t)
				*t = replace;
		}
	}
	// I don't think this really belongs here -- Neil
	void correctPath() {
#ifdef unix
		substitute('\\', '/');
#else
		substitute('/', '\\');
#endif
	}
};

struct Property {
	unsigned int hash;
	char *key;
	char *val;
	Property *next;
	Property() : hash(0), key(0), val(0), next(0) {}
};

class PropSet {
private:
	enum { hashRoots=31 };
	Property *props[hashRoots];
public:
	PropSet *superPS;
	PropSet();
	~PropSet();
	void Set(const char *key, const char *val);
	void Set(char *keyval);
	SString Get(const char *key);
	SString GetExpanded(const char *key);
	SString Expand(const char *withvars);
	int GetInt(const char *key, int defaultValue=0);
	SString GetWild(const char *keybase, const char *filename);
	SString GetNewExpand(const char *keybase, const char *filename);
	void Clear();
	void ReadFromMemory(const char *data, int len, const char *directoryForImports=0);
	void Read(const char *filename, const char *directoryForImports);
};

class WordList {
public:
	// Each word contains at least one character - a empty word acts as sentinal at the end.
	char **words;
	char **wordsNoCase;
	char *list;
	int len;
	bool onlyLineEnds;	// Delimited by any white space or only line ends
	bool sorted;
	int starts[256];
	WordList(bool onlyLineEnds_ = false) : 
		words(0), wordsNoCase(0), list(0), len(0), onlyLineEnds(onlyLineEnds_), sorted(false) {}
	~WordList() { Clear(); }
	operator bool() { return words ? true : false; }
	const char *operator[](int ind) { return words[ind]; }
	void Clear();
	void Set(const char *s);
	char *Allocate(int size);
	void SetFromAllocated();
	bool InList(const char *s);
	const char *GetNearestWord(const char *wordStart, int searchLen = -1, bool ignoreCase = false);
	char *GetNearestWords(const char *wordStart, int searchLen = -1, bool ignoreCase = false);
};

inline bool nonFuncChar(char ch) {
	return strchr("\t\n\r !\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", ch) != NULL;
}

#endif