~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/plugins/contrib/devpak_plugin/cbiniparser.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
4
 *
 
5
 * $Revision: 4909 $
 
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 $
 
8
 */
 
9
 
 
10
#include "cbiniparser.h"
 
11
#include <wx/utils.h>
 
12
#include <wx/file.h>
 
13
 
 
14
#include <wx/arrimpl.cpp> // this is a magic incantation which must be done!
 
15
WX_DEFINE_OBJARRAY(IniKeyValuePairArray);
 
16
WX_DEFINE_OBJARRAY(IniGroupArray);
 
17
 
 
18
IniParser::IniParser()
 
19
{
 
20
}
 
21
 
 
22
IniParser::~IniParser()
 
23
{
 
24
}
 
25
 
 
26
bool IniParser::ParseFile(const wxString& filename)
 
27
{
 
28
    if (!wxFileExists(filename))
 
29
        return false;
 
30
 
 
31
    // open file
 
32
    wxString buffer;
 
33
    wxFile file(filename);
 
34
    if (!file.IsOpened())
 
35
        return false;
 
36
    int len = file.Length();
 
37
 
 
38
    if(len==0)
 
39
        buffer.Clear();
 
40
    else
 
41
    {
 
42
        char* buff = new char[len+1];
 
43
        file.Read(buff, len);
 
44
        buff[len]='\0';
 
45
        buffer = wxString(buff,wxConvUTF8);
 
46
        delete[] buff;
 
47
    }
 
48
    file.Close();
 
49
 
 
50
    return ParseBuffer(buffer);
 
51
}
 
52
 
 
53
bool IniParser::ParseBuffer(wxString& buffer)
 
54
{
 
55
    m_Array.Clear();
 
56
 
 
57
    while (!buffer.IsEmpty())
 
58
    {
 
59
        wxString line = ReadLineFromBuffer(buffer);
 
60
        line.Trim(false);
 
61
        line.Trim(true);
 
62
        if (line.IsEmpty())
 
63
            continue;
 
64
 
 
65
        if (line.GetChar(0) == _T('['))
 
66
        {
 
67
            // new group
 
68
            IniGroup newgroup;
 
69
            newgroup.name = line.Mid(1, line.Length() - 2);
 
70
            newgroup.name.Trim(false);
 
71
            newgroup.name.Trim(true);
 
72
            if (newgroup.name.IsEmpty())
 
73
                continue;
 
74
            m_Array.Add(newgroup);
 
75
        }
 
76
        else
 
77
        {
 
78
            int pos = line.Find(_T('='));
 
79
            if (pos == -1)
 
80
                pos = line.Length();
 
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'))
 
89
                continue;
 
90
            if (m_Array.GetCount() == 0)
 
91
            {
 
92
                // add empty group
 
93
                IniGroup emptygroup;
 
94
                m_Array.Add(emptygroup);
 
95
            }
 
96
            m_Array[m_Array.GetCount() - 1].pairs.Add(newpair);
 
97
        }
 
98
    }
 
99
 
 
100
    return true;
 
101
}
 
102
 
 
103
wxString IniParser::ReadLineFromBuffer(wxString& buffer)
 
104
{
 
105
    int len = buffer.Length();
 
106
    int i = 0;
 
107
    while (i < len && buffer.GetChar(i) != _T('\n'))
 
108
        ++i;
 
109
    wxString str = buffer.Left(i);
 
110
    while (i < len && (buffer.GetChar(i) == _T('\n') || buffer.GetChar(i) == _T('\r')))
 
111
        ++i;
 
112
    buffer.Remove(0, i);
 
113
    buffer.Trim();
 
114
    return str;
 
115
}
 
116
 
 
117
int IniParser::GetGroupsCount() const
 
118
{
 
119
    return m_Array.GetCount();
 
120
}
 
121
 
 
122
const wxString & IniParser::GetGroupName(int idx) const
 
123
{
 
124
    return m_Array[idx].name;
 
125
}
 
126
 
 
127
int IniParser::FindGroupByName(const wxString& name, bool caseSensitive) const
 
128
{
 
129
    for (int i = 0; i < GetGroupsCount(); ++i)
 
130
    {
 
131
        bool found = caseSensitive
 
132
                    ? name.Cmp(m_Array[i].name) == 0
 
133
                    : name.CmpNoCase(m_Array[i].name) == 0;
 
134
        if (found)
 
135
            return i;
 
136
    }
 
137
    return -1;
 
138
}
 
139
 
 
140
int IniParser::GetKeysCount(int group) const
 
141
{
 
142
    return m_Array[group].pairs.GetCount();
 
143
}
 
144
 
 
145
const wxString & IniParser::GetKeyName(int group, int idx) const
 
146
{
 
147
    return m_Array[group].pairs[idx].key;
 
148
}
 
149
 
 
150
const wxString & IniParser::GetKeyValue(int group, int idx) const
 
151
{
 
152
    return m_Array[group].pairs[idx].value;
 
153
}
 
154
 
 
155
const wxString& IniParser::GetKeyValue(int group, const wxString& key) const
 
156
{
 
157
    static wxString empty;
 
158
    int keyIdx = FindKeyByName(group, key);
 
159
    if (keyIdx == -1)
 
160
        return empty;
 
161
    return GetKeyValue(group, keyIdx);
 
162
}
 
163
 
 
164
int IniParser::FindKeyByName(int groupIdx, const wxString& name, bool caseSensitive) const
 
165
{
 
166
    if (groupIdx == -1)
 
167
        return -1;
 
168
    for (int i = 0; i < GetKeysCount(groupIdx); ++i)
 
169
    {
 
170
        bool found = caseSensitive
 
171
                    ? name.Cmp(m_Array[groupIdx].pairs[i].key) == 0
 
172
                    : name.CmpNoCase(m_Array[groupIdx].pairs[i].key) == 0;
 
173
        if (found)
 
174
            return i;
 
175
    }
 
176
    return -1;
 
177
}
 
178
 
 
179
const wxString & IniParser::GetValue(const wxString& group, const wxString& key, bool caseSensitive) const
 
180
{
 
181
    static wxString ret;
 
182
    ret.Clear();
 
183
 
 
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;
 
188
    return ret;
 
189
}
 
190
 
 
191
int my_sorter(IniGroup** first, IniGroup** second)
 
192
{
 
193
    return (*first)->name.Cmp((*second)->name);
 
194
}
 
195
 
 
196
int my_sorter_nocase(IniGroup** first, IniGroup** second)
 
197
{
 
198
    return (*first)->name.CmpNoCase((*second)->name);
 
199
}
 
200
 
 
201
void IniParser::Sort(bool caseSensitive)
 
202
{
 
203
    m_Array.Sort(caseSensitive ? my_sorter : my_sorter_nocase);
 
204
}