]> git.saurik.com Git - wxWidgets.git/blob - utils/dialoged/src/symbtabl.cpp
65e038b3d22f4883f34881d5dc3ec5270f4691a8
[wxWidgets.git] / utils / dialoged / src / symbtabl.cpp
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;
49 if (!wxFileExists(filename))
50 return FALSE;
51
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
59 Clear();
60 AddStandardSymbols();
61
62 wxString str;
63 char* p = str.GetWriteBuf(len + 1);
64
65 if (file.Read(p, len) == wxFile::fd_invalid)
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
107 int id = atol(numStr);
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
124 m_hashTable.BeginFind();
125
126 wxNode* node = m_hashTable.Next();
127 while (node)
128 {
129 char* str = node->key.string;
130 int id = (int) node->Data() ;
131
132 if (!IsStandardSymbol(str))
133 {
134 wxString line;
135 line.Printf("#define %s %ld\n", str, id);
136
137 file.Write(line, line.Length());
138 }
139
140 node = m_hashTable.Next();
141 }
142 return TRUE;
143 }
144
145 void wxResourceSymbolTable::Clear()
146 {
147 m_hashTable.Clear();
148 }
149
150 bool wxResourceSymbolTable::AddSymbol(const wxString& symbol, int id)
151 {
152 m_hashTable.Put(symbol, (wxObject*) id);
153 return TRUE;
154 }
155
156 bool wxResourceSymbolTable::RemoveSymbol(const wxString& symbol)
157 {
158 m_hashTable.Delete(symbol);
159 return TRUE;
160 }
161
162 bool wxResourceSymbolTable::RemoveSymbol(int id)
163 {
164 wxString symbol(GetSymbolForId(id));
165 m_hashTable.Delete(symbol);
166 return TRUE;
167 }
168
169 // Accessors
170 wxString wxResourceSymbolTable::GetSymbolForId(int id)
171 {
172 m_hashTable.BeginFind();
173
174 wxNode* node = m_hashTable.Next();
175 while (node)
176 {
177 char* str = node->key.string;
178 if (str && ( ((int) node->Data()) == id) )
179 return wxString(str);
180
181 node = m_hashTable.Next();
182 }
183 return wxString("");
184 }
185
186 int wxResourceSymbolTable::GetIdForSymbol(const wxString& symbol)
187 {
188 return (int) m_hashTable.Get(symbol);
189 }
190
191 bool wxResourceSymbolTable::SymbolExists(const wxString& symbol) const
192 {
193 return (m_hashTable.Get(symbol) != NULL);
194 }
195
196 bool wxResourceSymbolTable::IdExists(int id)
197 {
198 m_hashTable.BeginFind();
199
200 wxNode* node = m_hashTable.Next();
201 while (node)
202 {
203 if ( (((int) node->Data()) == id) )
204 return TRUE;
205
206 node = m_hashTable.Next();
207 }
208 return FALSE;
209 }
210
211 int wxResourceSymbolTable::FindHighestId()
212 {
213 int highest = 0;
214
215 m_hashTable.BeginFind();
216
217 wxNode* node = m_hashTable.Next();
218 while (node)
219 {
220 int id = ((int) node->Data());
221 if (id > highest)
222 highest = id;
223
224 node = m_hashTable.Next();
225 }
226 return highest;
227 }
228
229 /*
230 * A table of the standard identifiers
231 */
232
233 struct wxStandardSymbolStruct
234 {
235 char* m_name;
236 int m_id;
237 };
238
239 static wxStandardSymbolStruct sg_StandardSymbols[] =
240 {
241 { "wxID_OK", wxID_OK },
242 { "wxID_CANCEL", wxID_CANCEL },
243 { "wxID_APPLY", wxID_APPLY },
244 // { "wxID_STATIC", wxID_STATIC },
245 { "wxID_YES", wxID_YES },
246 { "wxID_NO", wxID_NO }
247 };
248
249 static int sg_StandardSymbolSize = (sizeof(sg_StandardSymbols)/sizeof(wxStandardSymbolStruct));
250
251 void wxResourceSymbolTable::AddStandardSymbols()
252 {
253 int i;
254 for (i = 0; i < sg_StandardSymbolSize; i++)
255 {
256 AddSymbol(sg_StandardSymbols[i].m_name, sg_StandardSymbols[i].m_id);
257 }
258 }
259
260 bool wxResourceSymbolTable::IsStandardSymbol(const wxString& symbol) const
261 {
262 int i;
263 for (i = 0; i < sg_StandardSymbolSize; i++)
264 {
265 if (symbol == sg_StandardSymbols[i].m_name)
266 return TRUE;
267 }
268 return FALSE;
269 }
270
271 bool wxResourceSymbolTable::FillComboBox(wxComboBox* comboBox)
272 {
273 m_hashTable.BeginFind();
274
275 wxNode* node = m_hashTable.Next();
276 while (node)
277 {
278 char* str = node->key.string;
279
280 comboBox->Append(str);
281 node = m_hashTable.Next();
282 }
283 return TRUE;
284 }
285