]>
Commit | Line | Data |
---|---|---|
1 | // SciTE - Scintilla based Text Editor | |
2 | // PropSet.h - a java style properties file module | |
3 | // Copyright 1998-2000 by Neil Hodgson <neilh@scintilla.org> | |
4 | // The License.txt file describes the conditions under which this software may be distributed. | |
5 | ||
6 | #ifndef PROPSET_H | |
7 | #define PROPSET_H | |
8 | ||
9 | bool EqualCaseInsensitive(const char *a, const char *b); | |
10 | ||
11 | #if PLAT_WIN | |
12 | #define strcasecmp stricmp | |
13 | #define strncasecmp strnicmp | |
14 | #endif | |
15 | ||
16 | #ifdef __WXMSW__ | |
17 | #define strcasecmp stricmp | |
18 | #define strncasecmp strnicmp | |
19 | #endif | |
20 | ||
21 | // Define another string class. | |
22 | // While it would be 'better' to use std::string, that doubles the executable size. | |
23 | ||
24 | inline char *StringDup(const char *s, int len=-1) { | |
25 | if (!s) | |
26 | return 0; | |
27 | if (len == -1) | |
28 | len = strlen(s); | |
29 | char *sNew = new char[len + 1]; | |
30 | if (sNew) { | |
31 | strncpy(sNew, s, len); | |
32 | sNew[len] = '\0'; | |
33 | } | |
34 | return sNew; | |
35 | } | |
36 | ||
37 | class SString { | |
38 | char *s; | |
39 | int ssize; | |
40 | public: | |
41 | typedef const char* const_iterator; | |
42 | typedef int size_type; | |
43 | static size_type npos; | |
44 | const char* begin(void) const { | |
45 | return s; | |
46 | } | |
47 | const char* end(void) const { | |
48 | return &s[ssize]; | |
49 | } | |
50 | size_type size(void) const { | |
51 | if (s) | |
52 | return ssize; | |
53 | else | |
54 | return 0; | |
55 | } | |
56 | SString &assign(const char* sother, int size_ = -1) { | |
57 | char *t = s; | |
58 | s = StringDup(sother,size_); | |
59 | ssize = (s) ? strlen(s) : 0; | |
60 | delete []t; | |
61 | return *this; | |
62 | } | |
63 | SString &assign(const SString& sother, int size_ = -1) { | |
64 | return assign(sother.s,size_); | |
65 | } | |
66 | SString &assign(const_iterator ibeg, const_iterator iend) { | |
67 | return assign(ibeg,iend - ibeg); | |
68 | } | |
69 | SString() { | |
70 | s = 0; | |
71 | ssize = 0; | |
72 | } | |
73 | SString(const SString &source) { | |
74 | s = StringDup(source.s); | |
75 | ssize = (s) ? strlen(s) : 0; | |
76 | } | |
77 | SString(const char *s_) { | |
78 | s = StringDup(s_); | |
79 | ssize = (s) ? strlen(s) : 0; | |
80 | } | |
81 | SString(int i) { | |
82 | char number[100]; | |
83 | sprintf(number, "%0d", i); | |
84 | s = StringDup(number); | |
85 | ssize = (s) ? strlen(s) : 0; | |
86 | } | |
87 | ~SString() { | |
88 | delete []s; | |
89 | s = 0; | |
90 | ssize = 0; | |
91 | } | |
92 | SString &operator=(const SString &source) { | |
93 | if (this != &source) { | |
94 | delete []s; | |
95 | s = StringDup(source.s); | |
96 | ssize = (s) ? strlen(s) : 0; | |
97 | } | |
98 | return *this; | |
99 | } | |
100 | bool operator==(const SString &other) const { | |
101 | if ((s == 0) && (other.s == 0)) | |
102 | return true; | |
103 | if ((s == 0) || (other.s == 0)) | |
104 | return false; | |
105 | return strcmp(s, other.s) == 0; | |
106 | } | |
107 | bool operator!=(const SString &other) const { | |
108 | return !operator==(other); | |
109 | } | |
110 | bool operator==(const char *sother) const { | |
111 | if ((s == 0) && (sother == 0)) | |
112 | return true; | |
113 | if ((s == 0) || (sother == 0)) | |
114 | return false; | |
115 | return strcmp(s, sother) == 0; | |
116 | } | |
117 | bool operator!=(const char *sother) const { | |
118 | return !operator==(sother); | |
119 | } | |
120 | const char *c_str() const { | |
121 | if (s) | |
122 | return s; | |
123 | else | |
124 | return ""; | |
125 | } | |
126 | int length() const { | |
127 | if (s) | |
128 | return strlen(s); | |
129 | else | |
130 | return 0; | |
131 | } | |
132 | char operator[](int i) const { | |
133 | if (s) | |
134 | return s[i]; | |
135 | else | |
136 | return '\0'; | |
137 | } | |
138 | SString &operator +=(const char *sother) { | |
139 | return append(sother,-1); | |
140 | } | |
141 | SString &operator +=(const SString &sother) { | |
142 | return append(sother.s,sother.ssize); | |
143 | } | |
144 | SString &operator +=(char ch) { | |
145 | return append(&ch,1); | |
146 | } | |
147 | SString &append(const char* sother, int lenOther) { | |
148 | int len = length(); | |
149 | if(lenOther < 0) | |
150 | lenOther = strlen(sother); | |
151 | char *sNew = new char[len + lenOther + 1]; | |
152 | if (sNew) { | |
153 | if (s) | |
154 | memcpy(sNew, s, len); | |
155 | strncpy(&sNew[len], sother, lenOther); | |
156 | sNew[len + lenOther] = '\0'; | |
157 | delete []s; | |
158 | s = sNew; | |
159 | ssize = (s) ? strlen(s) : 0; | |
160 | } | |
161 | return *this; | |
162 | } | |
163 | int value() const { | |
164 | if (s) | |
165 | return atoi(s); | |
166 | else | |
167 | return 0; | |
168 | } | |
169 | void substitute(char find, char replace) { | |
170 | char *t = s; | |
171 | while (t) { | |
172 | t = strchr(t, find); | |
173 | if (t) | |
174 | *t = replace; | |
175 | } | |
176 | } | |
177 | // I don't think this really belongs here -- Neil | |
178 | void correctPath() { | |
179 | #ifdef unix | |
180 | substitute('\\', '/'); | |
181 | #else | |
182 | substitute('/', '\\'); | |
183 | #endif | |
184 | } | |
185 | }; | |
186 | ||
187 | struct Property { | |
188 | unsigned int hash; | |
189 | char *key; | |
190 | char *val; | |
191 | Property *next; | |
192 | Property() : hash(0), key(0), val(0), next(0) {} | |
193 | }; | |
194 | ||
195 | class PropSet { | |
196 | private: | |
197 | enum { hashRoots=31 }; | |
198 | Property *props[hashRoots]; | |
199 | public: | |
200 | PropSet *superPS; | |
201 | PropSet(); | |
202 | ~PropSet(); | |
203 | void Set(const char *key, const char *val); | |
204 | void Set(char *keyval); | |
205 | SString Get(const char *key); | |
206 | SString GetExpanded(const char *key); | |
207 | SString Expand(const char *withvars); | |
208 | int GetInt(const char *key, int defaultValue=0); | |
209 | SString GetWild(const char *keybase, const char *filename); | |
210 | SString GetNewExpand(const char *keybase, const char *filename); | |
211 | void Clear(); | |
212 | void ReadFromMemory(const char *data, int len, const char *directoryForImports=0); | |
213 | void Read(const char *filename, const char *directoryForImports); | |
214 | }; | |
215 | ||
216 | class WordList { | |
217 | public: | |
218 | // Each word contains at least one character - a empty word acts as sentinal at the end. | |
219 | char **words; | |
220 | char **wordsNoCase; | |
221 | char *list; | |
222 | int len; | |
223 | bool onlyLineEnds; // Delimited by any white space or only line ends | |
224 | bool sorted; | |
225 | int starts[256]; | |
226 | WordList(bool onlyLineEnds_ = false) : | |
227 | words(0), wordsNoCase(0), list(0), len(0), onlyLineEnds(onlyLineEnds_), sorted(false) {} | |
228 | ~WordList() { Clear(); } | |
229 | operator bool() { return words ? true : false; } | |
230 | const char *operator[](int ind) { return words[ind]; } | |
231 | void Clear(); | |
232 | void Set(const char *s); | |
233 | char *Allocate(int size); | |
234 | void SetFromAllocated(); | |
235 | bool InList(const char *s); | |
236 | const char *GetNearestWord(const char *wordStart, int searchLen = -1, bool ignoreCase = false); | |
237 | char *GetNearestWords(const char *wordStart, int searchLen = -1, bool ignoreCase = false); | |
238 | }; | |
239 | ||
240 | inline bool nonFuncChar(char ch) { | |
241 | return strchr("\t\n\r !\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~", ch) != NULL; | |
242 | } | |
243 | ||
244 | #endif |