1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxResourceSymbolTable
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "symbtabl.h"
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
34 wxResourceSymbolTable::wxResourceSymbolTable():
35 m_hashTable(wxKEY_STRING
)
39 wxResourceSymbolTable::~wxResourceSymbolTable()
46 bool wxResourceSymbolTable::ReadIncludeFile(const wxString
& filename
)
49 if (!wxFileExists(filename
))
52 if (!file
.Open(filename
, wxFile::read
))
55 off_t len
= file
.Length();
63 char* p
= str
.GetWriteBuf(len
+ 1);
65 if (file
.Read(p
, len
) == wxFile::fd_invalid
)
72 // Look for #define occurrences
73 size_t pos
= str
.Find("#define");
76 size_t len
= str
.Length();
80 // Eat whitespace until symbol
81 while ((str
[i
] == ' ' || str
[i
] == '\t') && (i
< len
))
87 while (str
[i
] != ' ' && str
[i
] != '\t' && (i
< len
))
91 wxString
symbol(str
.Mid(start
, (end
- start
+ 1)));
93 // Eat whitespace until number
94 while ((str
[i
] == ' ' || str
[i
] == '\t') && (i
< len
))
100 while (str
[i
] != ' ' && str
[i
] != '\t' && str
[i
] != '\n' && (i
< len
))
105 wxString
numStr(str
.Mid(startNum
, (endNum
- startNum
+ 1)));
107 int id
= atol(numStr
);
109 AddSymbol(symbol
, id
);
111 str
= str
.Right(len
- i
);
112 pos
= str
.Find("#define");
118 bool wxResourceSymbolTable::WriteIncludeFile(const wxString
& filename
)
121 if (!file
.Open(filename
, wxFile::write
))
124 m_hashTable
.BeginFind();
126 wxNode
* node
= m_hashTable
.Next();
129 char* str
= node
->key
.string
;
130 int id
= (int) node
->Data() ;
132 if (!IsStandardSymbol(str
))
135 line
.Printf("#define %s %ld\n", str
, id
);
137 file
.Write(line
, line
.Length());
140 node
= m_hashTable
.Next();
145 void wxResourceSymbolTable::Clear()
150 bool wxResourceSymbolTable::AddSymbol(const wxString
& symbol
, int id
)
152 m_hashTable
.Put(symbol
, (wxObject
*) id
);
156 bool wxResourceSymbolTable::RemoveSymbol(const wxString
& symbol
)
158 m_hashTable
.Delete(symbol
);
162 bool wxResourceSymbolTable::RemoveSymbol(int id
)
164 wxString
symbol(GetSymbolForId(id
));
165 m_hashTable
.Delete(symbol
);
170 wxString
wxResourceSymbolTable::GetSymbolForId(int id
)
172 m_hashTable
.BeginFind();
174 wxNode
* node
= m_hashTable
.Next();
177 char* str
= node
->key
.string
;
178 if (str
&& ( ((int) node
->Data()) == id
) )
179 return wxString(str
);
181 node
= m_hashTable
.Next();
186 int wxResourceSymbolTable::GetIdForSymbol(const wxString
& symbol
)
188 return (int) m_hashTable
.Get(symbol
);
191 bool wxResourceSymbolTable::SymbolExists(const wxString
& symbol
) const
193 return (m_hashTable
.Get(symbol
) != NULL
);
196 bool wxResourceSymbolTable::IdExists(int id
)
198 m_hashTable
.BeginFind();
200 wxNode
* node
= m_hashTable
.Next();
203 if ( (((int) node
->Data()) == id
) )
206 node
= m_hashTable
.Next();
211 int wxResourceSymbolTable::FindHighestId()
215 m_hashTable
.BeginFind();
217 wxNode
* node
= m_hashTable
.Next();
220 int id
= ((int) node
->Data());
224 node
= m_hashTable
.Next();
230 * A table of the standard identifiers
233 struct wxStandardSymbolStruct
239 static wxStandardSymbolStruct sg_StandardSymbols
[] =
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
}
249 static int sg_StandardSymbolSize
= (sizeof(sg_StandardSymbols
)/sizeof(wxStandardSymbolStruct
));
251 void wxResourceSymbolTable::AddStandardSymbols()
254 for (i
= 0; i
< sg_StandardSymbolSize
; i
++)
256 AddSymbol(sg_StandardSymbols
[i
].m_name
, sg_StandardSymbols
[i
].m_id
);
260 bool wxResourceSymbolTable::IsStandardSymbol(const wxString
& symbol
) const
263 for (i
= 0; i
< sg_StandardSymbolSize
; i
++)
265 if (symbol
== sg_StandardSymbols
[i
].m_name
)
271 bool wxResourceSymbolTable::FillComboBox(wxComboBox
* comboBox
)
273 m_hashTable
.BeginFind();
275 wxNode
* node
= m_hashTable
.Next();
278 char* str
= node
->key
.string
;
280 comboBox
->Append(str
);
281 node
= m_hashTable
.Next();