]> git.saurik.com Git - wxWidgets.git/blob - utils/dialoged/src/symbtabl.cpp
a2f42f5a93834745a2be45845571d5d8af8c7cc0
[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 (!file.Open(filename, wxFile::read))
50 return FALSE;
51
52 off_t len = file.Length();
53 if (len == -1)
54 return FALSE;
55
56 wxString str;
57 char* p = str.GetWriteBuf(len + 1);
58
59 if (file.Read(p, len) == ofsInvalid)
60 {
61 str.UngetWriteBuf();
62 return FALSE;
63 }
64 str.UngetWriteBuf();
65
66 // Look for #define occurrences
67 size_t pos = str.Find("#define");
68 while (pos != -1)
69 {
70 size_t len = str.Length();
71
72 size_t i = pos + 8;
73
74 // Eat whitespace until symbol
75 while ((str[i] == ' ' || str[i] == '\t') && (i < len))
76 i ++;
77
78 size_t start = i;
79
80 // Eat symbol
81 while (str[i] != ' ' && str[i] != '\t' && (i < len))
82 i ++;
83 size_t end = i-1;
84
85 wxString symbol(str.Mid(start, (end - start + 1)));
86
87 // Eat whitespace until number
88 while ((str[i] == ' ' || str[i] == '\t') && (i < len))
89 i ++;
90
91 size_t startNum = i;
92
93 // Eat number
94 while (str[i] != ' ' && str[i] != '\t' && str[i] != '\n' && (i < len))
95 i ++;
96
97 size_t endNum = i-1;
98
99 wxString numStr(str.Mid(startNum, (endNum - startNum + 1)));
100
101 long id = atol(numStr);
102
103 AddSymbol(symbol, id);
104
105 str = str.Right(len - i);
106 pos = str.Find("#define");
107 }
108
109 return TRUE;
110 }
111
112 bool wxResourceSymbolTable::WriteIncludeFile(const wxString& filename)
113 {
114 wxFile file;
115 if (!file.Open(filename, wxFile::write))
116 return FALSE;
117
118 m_hashTable.BeginFind();
119
120 wxNode* node = m_hashTable.Next();
121 while (node)
122 {
123 char* str = node->key.string;
124 long id = (long) node->Data() ;
125
126 wxString line;
127 line.Printf("#define %s %ld\n", str, id);
128
129 file.Write(line, line.Length());
130
131 node = m_hashTable.Next();
132 }
133 return TRUE;
134 }
135
136 void wxResourceSymbolTable::Clear()
137 {
138 m_hashTable.Clear();
139 }
140
141 bool wxResourceSymbolTable::AddSymbol(const wxString& symbol, long id)
142 {
143 m_hashTable.Put(symbol, (wxObject*) id);
144 return TRUE;
145 }
146
147 // Accessors
148 wxString wxResourceSymbolTable::GetSymbolForId(long id)
149 {
150 m_hashTable.BeginFind();
151
152 wxNode* node = m_hashTable.Next();
153 while (node)
154 {
155 char* str = node->key.string;
156 if (str && ( ((long) node->Data()) == id) )
157 return wxString(str);
158
159 node = m_hashTable.Next();
160 }
161 return wxString("");
162 }
163
164 long wxResourceSymbolTable::GetIdForSymbol(const wxString& symbol)
165 {
166 return (long) m_hashTable.Get(symbol);
167 }
168
169 bool wxResourceSymbolTable::SymbolExists(const wxString& symbol) const
170 {
171 return (m_hashTable.Get(symbol) != NULL);
172 }
173