2
* This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3
* http://www.gnu.org/licenses/gpl-3.0.html
6
* $Id: cbiniparser.cpp 4909 2008-02-27 13:15:26Z mortenmacfly $
7
* $HeadURL: svn://svn.berlios.de/codeblocks/tags/8.02/src/plugins/contrib/devpak_plugin/cbiniparser.cpp $
10
#include "cbiniparser.h"
14
#include <wx/arrimpl.cpp> // this is a magic incantation which must be done!
15
WX_DEFINE_OBJARRAY(IniKeyValuePairArray);
16
WX_DEFINE_OBJARRAY(IniGroupArray);
18
IniParser::IniParser()
22
IniParser::~IniParser()
26
bool IniParser::ParseFile(const wxString& filename)
28
if (!wxFileExists(filename))
33
wxFile file(filename);
36
int len = file.Length();
42
char* buff = new char[len+1];
45
buffer = wxString(buff,wxConvUTF8);
50
return ParseBuffer(buffer);
53
bool IniParser::ParseBuffer(wxString& buffer)
57
while (!buffer.IsEmpty())
59
wxString line = ReadLineFromBuffer(buffer);
65
if (line.GetChar(0) == _T('['))
69
newgroup.name = line.Mid(1, line.Length() - 2);
70
newgroup.name.Trim(false);
71
newgroup.name.Trim(true);
72
if (newgroup.name.IsEmpty())
74
m_Array.Add(newgroup);
78
int pos = line.Find(_T('='));
81
IniKeyValuePair newpair;
82
newpair.key = line.Left(pos);
83
newpair.value = line.Right(line.Length() - pos - 1);
84
newpair.key.Trim(false);
85
newpair.key.Trim(true);
86
newpair.value.Trim(false);
87
newpair.value.Trim(true);
88
if (newpair.key.IsEmpty() || newpair.key.GetChar(0) < _T('A') || newpair.key.GetChar(0) > _T('z'))
90
if (m_Array.GetCount() == 0)
94
m_Array.Add(emptygroup);
96
m_Array[m_Array.GetCount() - 1].pairs.Add(newpair);
103
wxString IniParser::ReadLineFromBuffer(wxString& buffer)
105
int len = buffer.Length();
107
while (i < len && buffer.GetChar(i) != _T('\n'))
109
wxString str = buffer.Left(i);
110
while (i < len && (buffer.GetChar(i) == _T('\n') || buffer.GetChar(i) == _T('\r')))
117
int IniParser::GetGroupsCount() const
119
return m_Array.GetCount();
122
const wxString & IniParser::GetGroupName(int idx) const
124
return m_Array[idx].name;
127
int IniParser::FindGroupByName(const wxString& name, bool caseSensitive) const
129
for (int i = 0; i < GetGroupsCount(); ++i)
131
bool found = caseSensitive
132
? name.Cmp(m_Array[i].name) == 0
133
: name.CmpNoCase(m_Array[i].name) == 0;
140
int IniParser::GetKeysCount(int group) const
142
return m_Array[group].pairs.GetCount();
145
const wxString & IniParser::GetKeyName(int group, int idx) const
147
return m_Array[group].pairs[idx].key;
150
const wxString & IniParser::GetKeyValue(int group, int idx) const
152
return m_Array[group].pairs[idx].value;
155
const wxString& IniParser::GetKeyValue(int group, const wxString& key) const
157
static wxString empty;
158
int keyIdx = FindKeyByName(group, key);
161
return GetKeyValue(group, keyIdx);
164
int IniParser::FindKeyByName(int groupIdx, const wxString& name, bool caseSensitive) const
168
for (int i = 0; i < GetKeysCount(groupIdx); ++i)
170
bool found = caseSensitive
171
? name.Cmp(m_Array[groupIdx].pairs[i].key) == 0
172
: name.CmpNoCase(m_Array[groupIdx].pairs[i].key) == 0;
179
const wxString & IniParser::GetValue(const wxString& group, const wxString& key, bool caseSensitive) const
184
int g = FindGroupByName(group, caseSensitive);
185
int k = FindKeyByName(g, key, caseSensitive);
186
if (g != -1 && k != -1)
187
return m_Array[g].pairs[k].value;
191
int my_sorter(IniGroup** first, IniGroup** second)
193
return (*first)->name.Cmp((*second)->name);
196
int my_sorter_nocase(IniGroup** first, IniGroup** second)
198
return (*first)->name.CmpNoCase((*second)->name);
201
void IniParser::Sort(bool caseSensitive)
203
m_Array.Sort(caseSensitive ? my_sorter : my_sorter_nocase);