]>
Commit | Line | Data |
---|---|---|
9ce192d4 RD |
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 | // Define another string class. | |
12 | // While it would be 'better' to use std::string, that doubles the executable size. | |
13 | ||
14 | inline char *StringDup(const char *s) { | |
15 | if (!s) | |
16 | return 0; | |
17 | char *sNew = new char[strlen(s) + 1]; | |
18 | if (sNew) | |
19 | strcpy(sNew, s); | |
20 | return sNew; | |
21 | } | |
22 | ||
23 | class SString { | |
24 | char *s; | |
25 | public: | |
26 | SString() { | |
27 | s = 0; | |
28 | } | |
29 | SString(const SString &source) { | |
30 | s = StringDup(source.s); | |
31 | } | |
32 | SString(const char *s_) { | |
33 | s = StringDup(s_); | |
34 | } | |
35 | SString(int i) { | |
36 | char number[100]; | |
37 | sprintf(number, "%0d", i); | |
38 | //itoa(i, number, 10); | |
39 | s = StringDup(number); | |
40 | } | |
41 | ~SString() { | |
42 | delete []s; | |
43 | s = 0; | |
44 | } | |
45 | SString &operator=(const SString &source) { | |
46 | if (this != &source) { | |
47 | delete []s; | |
48 | s = StringDup(source.s); | |
49 | } | |
50 | return *this; | |
51 | } | |
52 | bool operator==(const SString &other) const { | |
53 | if ((s == 0) && (other.s == 0)) | |
54 | return true; | |
55 | if ((s == 0) || (other.s == 0)) | |
56 | return false; | |
57 | return strcmp(s, other.s) == 0; | |
58 | } | |
59 | bool operator==(const char *sother) const { | |
60 | if ((s == 0) && (sother == 0)) | |
61 | return true; | |
62 | if ((s == 0) || (sother == 0)) | |
63 | return false; | |
64 | return strcmp(s, sother) == 0; | |
65 | } | |
66 | const char *c_str() const { | |
67 | if (s) | |
68 | return s; | |
69 | else | |
70 | return ""; | |
71 | } | |
72 | int length() const { | |
73 | if (s) | |
74 | return strlen(s); | |
75 | else | |
76 | return 0; | |
77 | } | |
78 | char operator[](int i) { | |
79 | if (s) | |
80 | return s[i]; | |
81 | else | |
82 | return '\0'; | |
83 | } | |
84 | SString &operator +=(const char *sother) { | |
85 | int len = length(); | |
86 | int lenOther = strlen(sother); | |
87 | char *sNew = new char[len + lenOther + 1]; | |
88 | if (sNew) { | |
89 | if (s) | |
90 | memcpy(sNew, s, len); | |
91 | memcpy(sNew + len, sother, lenOther); | |
92 | sNew[len + lenOther] = '\0'; | |
93 | delete []s; | |
94 | s = sNew; | |
95 | } | |
96 | return *this; | |
97 | } | |
98 | int value() { | |
99 | if (s) | |
100 | return atoi(s); | |
101 | else | |
102 | return 0; | |
103 | } | |
104 | }; | |
105 | ||
106 | class PropSet { | |
107 | private: | |
108 | char **vals; | |
109 | int size; | |
110 | int used; | |
111 | public: | |
112 | PropSet *superPS; | |
113 | PropSet(); | |
114 | ~PropSet(); | |
115 | void EnsureCanAddEntry(); | |
116 | void Set(const char *key, const char *val); | |
117 | void Set(char *keyval); | |
118 | SString Get(const char *key); | |
119 | int GetInt(const char *key, int defaultValue=0); | |
120 | SString GetWild(const char *keybase, const char *filename); | |
121 | SString GetNewExpand(const char *keybase, const char *filename); | |
122 | void Clear(); | |
123 | void ReadFromMemory(const char *data, int len); | |
124 | void Read(const char *filename); | |
125 | }; | |
126 | ||
127 | // This is a fixed length list of strings suitable for display in combo boxes | |
128 | // as a memory of user entries | |
129 | template<int sz> | |
130 | class EntryMemory { | |
131 | SString entries[sz]; | |
132 | public: | |
133 | void Insert(SString s) { | |
134 | for (int i=0;i<sz;i++) { | |
135 | if (entries[i] == s) { | |
136 | for (int j=i;j>0;j--) { | |
137 | entries[j] = entries[j-1]; | |
138 | } | |
139 | entries[0] = s; | |
140 | return; | |
141 | } | |
142 | } | |
143 | for (int k=sz-1;k>0;k--) { | |
144 | entries[k] = entries[k-1]; | |
145 | } | |
146 | entries[0] = s; | |
147 | } | |
148 | int Length() const { | |
149 | int len = 0; | |
150 | for (int i=0;i<sz;i++) | |
151 | if (entries[i].length()) | |
152 | len++; | |
153 | return len; | |
154 | } | |
155 | SString At(int n) const { | |
156 | return entries[n]; | |
157 | } | |
158 | }; | |
159 | ||
160 | class WordList { | |
161 | public: | |
162 | // Each word contains at least one character - a empty word acts as sentinal at the end. | |
163 | char **words; | |
164 | char *list; | |
165 | int len; | |
166 | bool onlyLineEnds; // Delimited by any white space or only line ends | |
167 | int starts[256]; | |
168 | WordList(bool onlyLineEnds_ = false) : | |
169 | words(0), list(0), len(0), onlyLineEnds(onlyLineEnds_) {} | |
170 | ~WordList() { Clear(); } | |
171 | operator bool() { return list; } | |
172 | const char *operator[](int ind) { return words[ind]; } | |
173 | void Clear(); | |
174 | void Set(const char *s); | |
175 | char *Allocate(int size); | |
176 | void SetFromAllocated(); | |
177 | bool InList(const char *s); | |
178 | }; | |
179 | ||
180 | #endif |