]>
Commit | Line | Data |
---|---|---|
03f68f12 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: symbtabl.cpp | |
3 | // Purpose: wxResourceSymbolTable | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "symbtabl.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx/wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/wx.h" | |
25 | #endif | |
26 | ||
27 | #include <wx/file.h> | |
28 | ||
29 | #include <string.h> | |
30 | #include <stdlib.h> | |
31 | ||
32 | #include "symbtabl.h" | |
33 | ||
34 | wxResourceSymbolTable::wxResourceSymbolTable(): | |
35 | m_hashTable(wxKEY_STRING) | |
36 | { | |
37 | } | |
38 | ||
39 | wxResourceSymbolTable::~wxResourceSymbolTable() | |
40 | { | |
41 | Clear(); | |
42 | } | |
43 | ||
44 | // Operations | |
45 | ||
46 | bool wxResourceSymbolTable::ReadIncludeFile(const wxString& filename) | |
47 | { | |
48 | wxFile file; | |
5de76427 JS |
49 | if (!wxFileExists(filename)) |
50 | return FALSE; | |
51 | ||
03f68f12 JS |
52 | if (!file.Open(filename, wxFile::read)) |
53 | return FALSE; | |
54 | ||
55 | off_t len = file.Length(); | |
56 | if (len == -1) | |
57 | return FALSE; | |
58 | ||
5de76427 JS |
59 | Clear(); |
60 | AddStandardSymbols(); | |
61 | ||
03f68f12 JS |
62 | wxString str; |
63 | char* p = str.GetWriteBuf(len + 1); | |
64 | ||
5de76427 | 65 | if (file.Read(p, len) == wxFile::fd_invalid) |
03f68f12 JS |
66 | { |
67 | str.UngetWriteBuf(); | |
68 | return FALSE; | |
69 | } | |
70 | str.UngetWriteBuf(); | |
71 | ||
72 | // Look for #define occurrences | |
73 | size_t pos = str.Find("#define"); | |
74 | while (pos != -1) | |
75 | { | |
76 | size_t len = str.Length(); | |
77 | ||
78 | size_t i = pos + 8; | |
79 | ||
80 | // Eat whitespace until symbol | |
81 | while ((str[i] == ' ' || str[i] == '\t') && (i < len)) | |
82 | i ++; | |
83 | ||
84 | size_t start = i; | |
85 | ||
86 | // Eat symbol | |
87 | while (str[i] != ' ' && str[i] != '\t' && (i < len)) | |
88 | i ++; | |
89 | size_t end = i-1; | |
90 | ||
91 | wxString symbol(str.Mid(start, (end - start + 1))); | |
92 | ||
93 | // Eat whitespace until number | |
94 | while ((str[i] == ' ' || str[i] == '\t') && (i < len)) | |
95 | i ++; | |
96 | ||
97 | size_t startNum = i; | |
98 | ||
99 | // Eat number | |
100 | while (str[i] != ' ' && str[i] != '\t' && str[i] != '\n' && (i < len)) | |
101 | i ++; | |
102 | ||
103 | size_t endNum = i-1; | |
104 | ||
105 | wxString numStr(str.Mid(startNum, (endNum - startNum + 1))); | |
106 | ||
5de76427 | 107 | int id = atol(numStr); |
03f68f12 JS |
108 | |
109 | AddSymbol(symbol, id); | |
110 | ||
111 | str = str.Right(len - i); | |
112 | pos = str.Find("#define"); | |
113 | } | |
114 | ||
115 | return TRUE; | |
116 | } | |
117 | ||
118 | bool wxResourceSymbolTable::WriteIncludeFile(const wxString& filename) | |
119 | { | |
120 | wxFile file; | |
121 | if (!file.Open(filename, wxFile::write)) | |
122 | return FALSE; | |
123 | ||
bbcdf8bc JS |
124 | wxString fileOnly(wxFileNameFromPath(filename)); |
125 | wxString line; | |
126 | line.Printf("/*\n * %s\n * Window identifiers file written by Dialog Editor\n */\n\n", | |
127 | (const char*) fileOnly); | |
128 | ||
129 | file.Write(line, line.Length()); | |
130 | ||
03f68f12 JS |
131 | m_hashTable.BeginFind(); |
132 | ||
133 | wxNode* node = m_hashTable.Next(); | |
134 | while (node) | |
135 | { | |
136 | char* str = node->key.string; | |
5de76427 | 137 | int id = (int) node->Data() ; |
03f68f12 | 138 | |
5de76427 JS |
139 | if (!IsStandardSymbol(str)) |
140 | { | |
141 | wxString line; | |
142 | line.Printf("#define %s %ld\n", str, id); | |
03f68f12 | 143 | |
5de76427 JS |
144 | file.Write(line, line.Length()); |
145 | } | |
03f68f12 JS |
146 | |
147 | node = m_hashTable.Next(); | |
148 | } | |
149 | return TRUE; | |
150 | } | |
151 | ||
152 | void wxResourceSymbolTable::Clear() | |
153 | { | |
154 | m_hashTable.Clear(); | |
155 | } | |
156 | ||
5de76427 | 157 | bool wxResourceSymbolTable::AddSymbol(const wxString& symbol, int id) |
03f68f12 JS |
158 | { |
159 | m_hashTable.Put(symbol, (wxObject*) id); | |
160 | return TRUE; | |
161 | } | |
162 | ||
5de76427 JS |
163 | bool wxResourceSymbolTable::RemoveSymbol(const wxString& symbol) |
164 | { | |
165 | m_hashTable.Delete(symbol); | |
166 | return TRUE; | |
167 | } | |
168 | ||
169 | bool wxResourceSymbolTable::RemoveSymbol(int id) | |
170 | { | |
171 | wxString symbol(GetSymbolForId(id)); | |
172 | m_hashTable.Delete(symbol); | |
173 | return TRUE; | |
174 | } | |
175 | ||
03f68f12 | 176 | // Accessors |
5de76427 | 177 | wxString wxResourceSymbolTable::GetSymbolForId(int id) |
03f68f12 JS |
178 | { |
179 | m_hashTable.BeginFind(); | |
180 | ||
181 | wxNode* node = m_hashTable.Next(); | |
182 | while (node) | |
183 | { | |
184 | char* str = node->key.string; | |
5de76427 | 185 | if (str && ( ((int) node->Data()) == id) ) |
03f68f12 JS |
186 | return wxString(str); |
187 | ||
188 | node = m_hashTable.Next(); | |
189 | } | |
190 | return wxString(""); | |
191 | } | |
192 | ||
5de76427 | 193 | int wxResourceSymbolTable::GetIdForSymbol(const wxString& symbol) |
03f68f12 | 194 | { |
5de76427 | 195 | return (int) m_hashTable.Get(symbol); |
03f68f12 JS |
196 | } |
197 | ||
198 | bool wxResourceSymbolTable::SymbolExists(const wxString& symbol) const | |
199 | { | |
200 | return (m_hashTable.Get(symbol) != NULL); | |
201 | } | |
202 | ||
5de76427 JS |
203 | bool wxResourceSymbolTable::IdExists(int id) |
204 | { | |
205 | m_hashTable.BeginFind(); | |
206 | ||
207 | wxNode* node = m_hashTable.Next(); | |
208 | while (node) | |
209 | { | |
210 | if ( (((int) node->Data()) == id) ) | |
211 | return TRUE; | |
212 | ||
213 | node = m_hashTable.Next(); | |
214 | } | |
215 | return FALSE; | |
216 | } | |
217 | ||
218 | int wxResourceSymbolTable::FindHighestId() | |
219 | { | |
220 | int highest = 0; | |
221 | ||
222 | m_hashTable.BeginFind(); | |
223 | ||
224 | wxNode* node = m_hashTable.Next(); | |
225 | while (node) | |
226 | { | |
227 | int id = ((int) node->Data()); | |
228 | if (id > highest) | |
229 | highest = id; | |
230 | ||
231 | node = m_hashTable.Next(); | |
232 | } | |
233 | return highest; | |
234 | } | |
235 | ||
236 | /* | |
237 | * A table of the standard identifiers | |
238 | */ | |
239 | ||
240 | struct wxStandardSymbolStruct | |
241 | { | |
242 | char* m_name; | |
243 | int m_id; | |
244 | }; | |
245 | ||
246 | static wxStandardSymbolStruct sg_StandardSymbols[] = | |
247 | { | |
248 | { "wxID_OK", wxID_OK }, | |
249 | { "wxID_CANCEL", wxID_CANCEL }, | |
250 | { "wxID_APPLY", wxID_APPLY }, | |
251 | // { "wxID_STATIC", wxID_STATIC }, | |
252 | { "wxID_YES", wxID_YES }, | |
253 | { "wxID_NO", wxID_NO } | |
254 | }; | |
255 | ||
256 | static int sg_StandardSymbolSize = (sizeof(sg_StandardSymbols)/sizeof(wxStandardSymbolStruct)); | |
257 | ||
258 | void wxResourceSymbolTable::AddStandardSymbols() | |
259 | { | |
260 | int i; | |
261 | for (i = 0; i < sg_StandardSymbolSize; i++) | |
262 | { | |
263 | AddSymbol(sg_StandardSymbols[i].m_name, sg_StandardSymbols[i].m_id); | |
264 | } | |
265 | } | |
266 | ||
267 | bool wxResourceSymbolTable::IsStandardSymbol(const wxString& symbol) const | |
268 | { | |
269 | int i; | |
270 | for (i = 0; i < sg_StandardSymbolSize; i++) | |
271 | { | |
272 | if (symbol == sg_StandardSymbols[i].m_name) | |
273 | return TRUE; | |
274 | } | |
275 | return FALSE; | |
276 | } | |
277 | ||
278 | bool wxResourceSymbolTable::FillComboBox(wxComboBox* comboBox) | |
279 | { | |
280 | m_hashTable.BeginFind(); | |
281 | ||
282 | wxNode* node = m_hashTable.Next(); | |
283 | while (node) | |
284 | { | |
285 | char* str = node->key.string; | |
286 | ||
287 | comboBox->Append(str); | |
288 | node = m_hashTable.Next(); | |
289 | } | |
290 | return TRUE; | |
291 | } | |
292 |