aboutsummaryrefslogtreecommitdiff
path: root/Src/Wasabi/api/config/config.cpp
blob: 3b45d93447a968dc2f4a78a8342420d7a27bfb75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <precomp.h>

#include "config.h"
#include <bfc/pair.h>
#include <bfc/ptrlist.h>
#include <bfc/parse/pathparse.h>
#include <api/xml/xmlwrite.h>

#include "../xml/ifc_xmlreadercallbackI.h"
#include "../xml/obj_xml.h"
#include <bfc/string/url.h>
#include <api/service/waservicefactory.h>

static StringW iniFile;
#define XNFNAME L"studio.xnf"

class StringPairCompare
{
public:
  static int compareItem(StringPair *p1, StringPair *p2)
  {
    int r = wcscmp(p1->a.getValue(), p2->a.getValue());
    if (r == 0)
			return CMP3(p1, p2);
    else 
			return r;
  }
  static int compareAttrib(const wchar_t *attrib, StringPair *item)
  {
    return wcscmp(attrib, item->a.getValue());
  }
};

static PtrListQuickSorted<StringPair, StringPairCompare> strings;

class Reader : public ifc_xmlreadercallbackI
{
public:
  ~Reader();
  void xmlReaderOnStartElementCallback(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params);
  void xmlReaderOnEndElementCallback(const wchar_t *xmlpath, const wchar_t *xmltag);

  void readem();

private:
  PtrList<StringW> sections;
};

Reader::~Reader()
{
  sections.deleteAll();
}

void Reader::xmlReaderOnStartElementCallback(const wchar_t *xmlpath, const wchar_t *xmltag, ifc_xmlreaderparams *params)
{
  if (!WCSICMP(xmltag, L"section"))
  {
    const wchar_t *name = params->getItemValue(L"name");
    if (name == NULL) 
      return ;

    StringW *strName = new StringW(name);
    Url::decode(*strName);
    sections.addItem(strName);
  }
  else if (!WCSICMP(xmltag, L"entry"))
  {
    const wchar_t *name = params->getItemValue(L"name");
    const wchar_t *value = params->getItemValue(L"value");

    // just in case
    if (!WCSICMP(name, L"(null)")) name = NULL;
    if (!WCSICMP(value, L"(null)")) value = NULL;

    if (name == NULL /*| !*name */ || value == NULL /*|| !*value*/) return ;
    StringW strName = name;
    Url::decode(strName);

		StringW strValue = value;
    Url::decode(strValue);

    StringW n;
    for (int i = 0; i < sections.getNumItems(); i++)
    {
      n.catPostSeparator(*sections[i], '/');
    }
    n += strName;
    
    StringPair *p = new StringPair(n, strValue);
    strings.addItem(p);
  }
}

void Reader::xmlReaderOnEndElementCallback(const wchar_t *xmlpath, const wchar_t *xmltag)
{
  if (!WCSICMP(xmltag, L"section"))
  {
    StringW *last = sections.getLast();
    sections.removeLastItem();
    delete last;
  }
}

void LoadXmlFile(obj_xml *parser, const wchar_t *filename);

void Reader::readem()
{
  strings.deleteAll();
 
  if (iniFile.isempty())
  {   
    iniFile = StringPathCombine(WASABI_API_APP->path_getUserSettingsPath(), XNFNAME);
  }

  waServiceFactory *parserFactory = WASABI_API_SVC->service_getServiceByGuid(obj_xmlGUID);
  if (parserFactory)
  {
    obj_xml *parser = (obj_xml *)parserFactory->getInterface();

    if (parser)
    {

      parser->xmlreader_registerCallback(L"WinampXML\fconfiguration\f*", this);
      parser->xmlreader_registerCallback(L"WasabiXML\fconfiguration\f*", this);
      parser->xmlreader_open();
      LoadXmlFile(parser, iniFile);

      parser->xmlreader_unregisterCallback(this);
      parser->xmlreader_close();
      parserFactory->releaseInterface(parser);
      parser = 0;
    }
  }
}

StringPair *ConfigFile::getPair(const wchar_t *name)
{
  ASSERT(!sectionname.isempty());
	StringW nname;
		nname.catPostSeparator(sectionname.getValue(), '/');
		nname.cat(name);
  return strings.findItem(nname.getValue()); // cast to make PtrListSorted happy
}

StringPair *ConfigFile::makePair(const wchar_t *name, const wchar_t *value)
{
  StringPair *ret = getPair(name);
  if (ret == NULL)
  {
		StringW nname;
		nname.catPostSeparator(sectionname.getValue(), '/');
		nname.cat(name);

    ret = new StringPair(nname, value);
    strings.addItem(ret);
  }
  else
  {
    ret->b.setValue(value);
  }
  return ret;
}

static int ninstances, inited;

ConfigFile::ConfigFile(const wchar_t *section, const wchar_t *name)
{
  sectionname = section;
  prettyname = name;
  ninstances++;
}

void ConfigFile::initialize()
{
  Reader().readem();
}

ConfigFile::~ConfigFile()
{
  ninstances--;
  if (ninstances == 0)
  {
    FILE *fp = _wfopen(iniFile, WF_WRITE_TEXT);
    if (fp != NULL)
    {
      // write out the file
      XMLWrite xml(fp, L"WasabiXML");
      const wchar_t *app = WASABI_API_APP->main_getVersionString();
      if (!app) 
				app = L"thousands of irascible butt monkeys";
      xml.comment(StringPrintfW(L"Generated by: %s (%d)", app, WASABI_API_APP->main_getBuildNumber()));
      xml.pushCategory(L"configuration");

      PtrList<StringW> cats;

      for (int i = 0; i < strings.getNumItems(); i++)
      {
        StringPair *p = strings[i];
        PathParserW pp(p->a);

        int climit = MIN(pp.getNumStrings() - 1, cats.getNumItems());
        int j;
        for (j = 0; j < climit; j++)
        {
          if (WCSICMP(*cats[j], pp.enumString(j)))
          {
            climit = j;
            break;
          }
        }

        while (cats.getNumItems() > climit)
        {
          StringW *s = cats.getLast();
          xml.popCategory();
          cats.removeLastItem();
          delete s;
        }
        for (j = climit; j < pp.getNumStrings() - 1; j++)
        {
          StringW *s = cats.addItem(new StringW(pp.enumString(j)));
          xml.pushCategoryAttrib(L"section");
          StringW enc = *s;
          Url::encode(enc, FALSE, URLENCODE_EXCLUDE_ABOVEEQ32);
          xml.writeCategoryAttrib(L"name", enc);
          xml.closeCategoryAttrib();
        }

        xml.pushCategoryAttrib(L"entry", TRUE);
        StringW enc = pp.getLastString();
        Url::encode(enc, FALSE, URLENCODE_EXCLUDE_ABOVEEQ32);
        xml.writeCategoryAttrib(L"name", enc);
        enc = p->b;
        Url::encode(enc, FALSE, URLENCODE_EXCLUDE_ABOVEEQ32);
        if (enc.iscaseequal(L"(null)") || enc.getValue() == NULL)
          enc.setValue(L"");
        xml.writeCategoryAttrib(L"value", enc);
        xml.closeCategoryAttrib();
        xml.popCategory();
      }

      while (xml.popCategory()) ;

      strings.deleteAll();
      fclose(fp);
    } // fp != NULL
  }	// ninstances==0
}

int verifyName(const wchar_t *str)
{
  for (const wchar_t *p = str; *p; p++)
  {
    if (!ISALPHA(*p) &&
      !ISDIGIT(*p) &&
      !ISPUNCT(*p) &&
      !ISSPACE(*p) &&
      *p != '|' && *p != '_')
      return 0;
  }
  return 1;
}

void ConfigFile::setInt(const wchar_t *name, int val)
{
  INCRITICALSECTION(cs);
  if (name == NULL) return ;
  if (!verifyName(name))
  {
    DebugStringW(L"illegal name given\n");
    //__asm { int 3 };
    return ;
  }
  makePair(name, StringPrintfW(val));
}

int ConfigFile::getInt(const wchar_t *name, int def_val)
{
  INCRITICALSECTION(cs);
  if (name == NULL) return def_val;
  StringPair *p = getPair(name);
  if (p == NULL) return def_val;
  return WTOI(p->b.getValue());
}

void ConfigFile::setString(const wchar_t *name, const wchar_t *str)
{
  INCRITICALSECTION(cs);
  if (name == NULL) return ;
  if (!verifyName(name))
  {
    DebugStringW(L"illegal name given\n");
    return ;
  }
  if (str == NULL)
  {
    StringPair *p = getPair(name);
    if (p != NULL)
    {
      strings.delItem(p);
      delete p;
      return ;
    }
  }
  makePair(name, str);
}

int ConfigFile::getString(const wchar_t *name, wchar_t *buf, int buf_len, const wchar_t *def_str)
{
  INCRITICALSECTION(cs);
  if (name == NULL || buf == NULL) return -1;
  if (def_str == NULL)
		def_str = L"";
  StringPair *p = getPair(name);
  if (p == NULL)
    WCSCPYN(buf, def_str, buf_len);
  else
    WCSCPYN(buf, p->b.getValueSafe(), buf_len);
  return 1;
}

int ConfigFile::getStringLen(const wchar_t *name)
{
  INCRITICALSECTION(cs);
  if (name == NULL) return -1;
  StringPair *p = getPair(name);
  if (p == NULL) return -1;
  return wcslen(p->b.getValue());
}