File: Configuration.cc

package info (click to toggle)
postman 2.1-6
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,248 kB
  • ctags: 3,249
  • sloc: cpp: 33,376; ansic: 2,014; sh: 768; makefile: 300
file content (493 lines) | stat: -rw-r--r-- 14,719 bytes parent folder | download | duplicates (3)
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*                                            
Configuration.cc
*/

#include "Configuration.h"

Configuration::Configuration (const char *afnconf, bool aAbortInError)
  {
  fnconf = afnconf;
  AbortInError = aAbortInError;
  }
  
Configuration::~Configuration ()
  {
  }

void Configuration::Error (void)
  {
  if (AbortInError == true) 
    {
    printf ("Error reading configuration file \"%s\"! Aborting ...\n", fnconf.cstr()); 
    exit (1);
    }
  }  

void Configuration::setFileName (const char *afilename)
  {
  fnconf = afilename;
  }
      
const char *Configuration::getFileName (void)
  {
  return fnconf.cstr();
  }

bool Configuration::Reload (void)
  {
  return ConfRead ();
  }    

bool Configuration::Sync (void)
  {
  bool b;
  b = ConfSave ();
  Reload ();
  return b;
  }
  
//METO TODO EL FICHERO DE CONFIGURACION EN MEMORIA
bool Configuration::ConfRead (void)
  {
  char Buffer[XXBUFFERSIZE + 1];
  FILE *CfgFile;
  SLFile.Clear();
  CfgFile = fopen (fnconf.cstr(), "r");
  if (NULL == CfgFile) {Error(); return false;}
  while (NULL != fgets (Buffer, XXBUFFERSIZE, CfgFile))
    {
    if (IsEmpty(Buffer) == false) SLFile.Add (Buffer);              
    }
  fclose (CfgFile);
  return true;
  }
  
//GUARDO EN DISCO TODO EL FICHERO DE CONFIGURACION DE MEMORIA
bool Configuration::ConfSave (void)
  {
  TBuffer abuf;
  FILE *CfgFile;
  CfgFile = fopen (fnconf.cstr(), "w");
  if (NULL == CfgFile) {Error(); return false;}
  for (int yy = 0; yy < SLFile.Count(); ++yy)
    {
    xstrncpy (abuf, CMAXBUFFER, SLFile.getString(yy).cstr());
    QuitaRetornoCarroDeLinea (abuf);
    if (IsEmpty(abuf) == false) fprintf (CfgFile, "%s\n", abuf);
    }
  fclose (CfgFile);
  return true;
  }  

bool Configuration::newFile (const char *filecont)
  {
  FILE *CfgFile;
  CfgFile = fopen (fnconf.cstr(), "w");
  if (NULL == CfgFile) {Error(); return false;}
  fprintf (CfgFile, "%s", filecont);
  fclose (CfgFile);
  return true;  
  }    
 
void Configuration::dumpDebug (void)
  {
  for (int yy = 0; yy < SLFile.Count(); ++yy)
    {
    DEBUG ("%s", SLFile.getString(yy).cstr());  //of DEBUG, MUST BE UNCOMMENTED
    }
  }
 
//RETURNS A CONFIGURATION STRING. LIKE AS getprivateprofilestring OF WINDOWS API
char *Configuration::getString (const char *asection, const char *akey, const char *adefault, int amaxsize)
  {
  bool EncontradaSeccion = false;
  int i=0, maxsize;
  char cbegin, cend, *pos;
  TBuffer linea, seccion, key, result, upseccion, upkey;
  
  maxsize = amaxsize; if (maxsize > CMAXBUFFER) {maxsize = CMAXBUFFER;}
  xstrncpy (upseccion, CMAXBUFFER, asection); xucase (upseccion);
  xstrncpy (upkey, CMAXBUFFER, akey); xucase (upkey);
  initStr(seccion); initStr (key); initStr (result);
  while (i < SLFile.Count())
    {
    xstrncpy (linea, CMAXBUFFER, SLFile.getString(i).trim().cstr());
    if ((strcmp (linea, "") != 0) && (linea != NULL) && (strcmp (linea, " ") != 0))
      {
      cbegin = linea[0]; cend = linea[strlen(linea) - 1];
      if (cbegin != ';')
        {
        if ((cbegin == '[') && (cend == ']'))
          {
          XString axs (SLFile.getString(i).trim());
          xstrncpy (seccion, CMAXBUFFER, axs.substring(1, axs.length() - 1).toUpperCase().cstr());
          if (strcmp (seccion, upseccion) == 0) {EncontradaSeccion = true;}
          else {EncontradaSeccion = false;}
          }
        else
          {
          if (EncontradaSeccion == true)
            {
            pos = strchr (linea, '=');
            if (pos != NULL)
              {
              xstrncpy (key, CMAXBUFFER, linea); 
              key [pos - linea] = '\0'; trim (key); xucase (key);
              if (strcmp (key, upkey) == 0)
                {
                xstrncpy (result, maxsize, pos + 1); trim (result);
                }
              }
            }
          }  
        }
      }
    ++i;
    }
  if (strcmp (result, "") == 0) {xstrncpy (result, CMAXBUFFER, adefault);}  
  /*
  DEBUG ("*******************************");
  DEBUG ("SECCION=%s", asection);
  DEBUG ("KEY=%s", akey);
  DEBUG ("DEFAULT=%s", adefault);
  DEBUG ("RESULT=%s", result);
  */
  return xstrdup (result);  
  }  
  
bool Configuration::setString (const char *asection, const char *akey, const char *astring, int amaxsize)
  {
  bool EncontradaSeccion = false;
  int i=0, maxsize;
  char cbegin, cend, *pos;
  TBuffer linea, seccion, key, result, anewline, upseccion, upkey;
  
  maxsize = amaxsize; if (maxsize > CMAXBUFFER) {maxsize = CMAXBUFFER;}
  xstrncpy (upseccion, CMAXBUFFER, asection); xucase (upseccion);
  xstrncpy (upkey, CMAXBUFFER, akey); xucase (upkey);
  initStr(seccion); initStr (key); initStr (result);
  while (i < SLFile.Count())
    {
    xstrncpy (linea, CMAXBUFFER, SLFile.getString(i).trim().cstr());
    if ((strcmp (linea, "") != 0) && (linea != NULL) && (strcmp (linea, " ") != 0))
      {
      cbegin = linea[0]; cend = linea[strlen(linea) - 1];
      if (cbegin != ';')
        {
        if ((cbegin == '[') && (cend == ']'))
          {
          if (EncontradaSeccion == true)
            {
            //add a new line for new variable not in the ini file
            snprintf (anewline, CMAXBUFFER, "%s=%s", akey, astring);
            SLFile.insertElementAt (anewline, i);
            return true;
            }          
          XString axs (SLFile.getString(i).trim());
          xstrncpy (seccion, CMAXBUFFER, axs.substring(1, axs.length() - 1).toUpperCase().cstr());
          if (strcmp (seccion, upseccion) == 0) {EncontradaSeccion = true;}
          else {EncontradaSeccion = false;}
          }
        else
          {
          if (EncontradaSeccion == true)
            {
            pos = strchr (linea, '=');
            if (pos != NULL)
              {
              xstrncpy (key, CMAXBUFFER, linea); 
              key [pos - linea] = '\0'; trim (key); xucase (key);
              if (strcmp (key, upkey) == 0)
                {
                xstrncpy (result, maxsize, pos + 1); trim (result);
                snprintf (linea, CMAXBUFFER, "%s=%s", akey, astring);
                SLFile.setElementAt (linea, i); 
                return true;
                }
              }
            }
          }  
        }
      }
    ++i;
    }
 if (EncontradaSeccion == true)
   {
   //add a new line for new variable not in the ini file
   snprintf (anewline, CMAXBUFFER, "%s=%s", akey, astring);
   SLFile.insertElementAt (anewline, i - 1);
   return true;
   }     
   
  if (IsThereSection (asection) == false)
    {
    snprintf (linea, CMAXBUFFER, "[%s]", asection);       SLFile.Add (linea);
    snprintf (linea, CMAXBUFFER, "%s=%s", akey, astring); SLFile.Add (linea);
    }
     
  return false;  
  }  
  
//DEVUELVE UN INTEGER DE CONFIGURATION. 
int Configuration::getInt (const char *asection, const char *akey, int adefault)
  {
  bool EncontradaSeccion = false;
  int i=0, maxsize, resint;
  char cbegin, cend, *pos;
  TBuffer linea, seccion, key, result, upseccion, upkey;
  
  maxsize = CMAXBUFFER;
  xstrncpy (upseccion, CMAXBUFFER, asection); xucase (upseccion);
  xstrncpy (upkey, CMAXBUFFER, akey); xucase (upkey);
  initStr(seccion); initStr (key); initStr (result);
  while (i < SLFile.Count())
    {
    xstrncpy (linea, CMAXBUFFER, SLFile.getString(i).trim().cstr());
    if ((strcmp (linea, "") != 0) && (linea != NULL) && (strcmp (linea, " ") != 0))
      {
      cbegin = linea[0]; cend = linea[strlen(linea) - 1];
      if (cbegin != ';')
        {
        if ((cbegin == '[') && (cend == ']'))
          {
          XString axs (SLFile.getString(i).trim());
          xstrncpy (seccion, CMAXBUFFER, axs.substring(1, axs.length() - 1).toUpperCase().cstr());
          if (strcmp (seccion, upseccion) == 0) {EncontradaSeccion = true;}
          else {EncontradaSeccion = false;}
          }
        else
          {
          if (EncontradaSeccion == true)
            {
            pos = strchr (linea, '=');
            if (pos != NULL)
              {
              xstrncpy (key, CMAXBUFFER, linea); 
              key [pos - linea] = '\0'; trim (key); xucase (key);
              if (strcmp (key, upkey) == 0)
                {
                xstrncpy (result, maxsize, pos + 1); trim (result);
                }
              }
            }
          }  
        }
      }
    ++i;
    }
  resint = xatoidef (result, adefault);
  /*
  DEBUG ("*******************************");
  DEBUG ("SECCION=%s", asection);
  DEBUG ("KEY=%s", akey);
  DEBUG ("DEFAULT=%d", adefault);
  DEBUG ("RESULT=%d", resint);
  */
  return resint;  
  }   
  
bool Configuration::setInt (const char *asection, const char *akey, int aint)
  {
  bool EncontradaSeccion = false;
  int i=0, maxsize;
  char cbegin, cend, *pos;
  TBuffer linea, seccion, key, result, anewline, upseccion, upkey;
  
  maxsize = CMAXBUFFER;
  xstrncpy (upseccion, CMAXBUFFER, asection); xucase (upseccion);
  xstrncpy (upkey, CMAXBUFFER, akey); xucase (upkey);
  initStr(seccion); initStr (key); initStr (result);
  while (i < SLFile.Count())
    {
    xstrncpy (linea, CMAXBUFFER, SLFile.getString(i).trim().cstr());
    if ((strcmp (linea, "") != 0) && (linea != NULL) && (strcmp (linea, " ") != 0))
      {
      cbegin = linea[0]; cend = linea[strlen(linea) - 1];
      if (cbegin != ';')
        {
        if ((cbegin == '[') && (cend == ']'))
          {          
          if (EncontradaSeccion == true)
            {
            //add a new line for new variable not in the ini file
            snprintf (anewline, CMAXBUFFER, "%s=%d", akey, aint);
            SLFile.insertElementAt (anewline, i);
            return true;
            }          
          XString axs (SLFile.getString(i).trim());
          xstrncpy (seccion, CMAXBUFFER, axs.substring(1, axs.length() - 1).toUpperCase().cstr());
          if (strcmp (seccion, upseccion) == 0) {EncontradaSeccion = true;}
          else {EncontradaSeccion = false;}
          }
        else
          {
          if (EncontradaSeccion == true)
            {
            pos = strchr (linea, '=');
            if (pos != NULL)
              {
              xstrncpy (key, CMAXBUFFER, linea); 
              key [pos - linea] = '\0'; trim (key); xucase (key);
              if (strcmp (key, upkey) == 0)
                {
                xstrncpy (result, maxsize, pos + 1); trim (result);
                snprintf (linea, CMAXBUFFER, "%s=%d", akey, aint);
                SLFile.setElementAt (linea, i); 
                return true;
                }
              }
            }
          }  
        }
      }
    ++i;
    }
  if (EncontradaSeccion == true)
    {
    //add a new line for new variable not in the ini file
    snprintf (anewline, CMAXBUFFER, "%s=%d", akey, aint);
    SLFile.insertElementAt (anewline, i - 1);
    return true;
    }            
    
  if (IsThereSection (asection) == false)
    {
    snprintf (linea, CMAXBUFFER, "[%s]", asection);       SLFile.Add (linea);
    snprintf (linea, CMAXBUFFER, "%s=%d", akey, aint); SLFile.Add (linea);
    }
     
  return false;  
  }     
  
//EXISTE SECCION
bool Configuration::IsThereSection (const char *asection)
  {
  int i=0;
  char cbegin, cend;
  TBuffer seccion, linea, upseccion;
  
  xstrncpy (upseccion, CMAXBUFFER, asection); xucase (upseccion);
  initStr(seccion); 
  while (i < SLFile.Count())
    {
    xstrncpy (linea, CMAXBUFFER, SLFile.getString(i).trim().cstr());
    if ((strcmp (linea, "") != 0) && (linea != NULL) && (strcmp (linea, " ") != 0))
      {
      cbegin = linea[0]; cend = linea[strlen(linea) - 1];
      if (cbegin != ';')
        {
        if ((cbegin == '[') && (cend == ']'))
          {
          XString axs (SLFile.getString(i).trim());
          xstrncpy (seccion, CMAXBUFFER, axs.substring(1, axs.length() - 1).toUpperCase().cstr());
          if (strcmp (seccion, upseccion) == 0) {return true;}
          }
        }
      }
    ++i;
    }
  return false;  
  }    
  
//COGE EL CONTENIDO DE UNA SECCION ENTERA
bool Configuration::getSection (const char *asection, StringList *ASection)
  {
  bool HabiaSeccion = false, EncontradaSeccion = false;
  int i=0;
  char cbegin, cend;
  TBuffer seccion, linea, upseccion;
  
  ASection->Clear();
  xstrncpy (upseccion, CMAXBUFFER, asection); xucase (upseccion);
  initStr(seccion); 
  while (i < SLFile.Count())
    {
    xstrncpy (linea, CMAXBUFFER, SLFile.getString(i).trim().cstr());
    if ((EncontradaSeccion == true) && (linea != NULL))
      {
      if (linea[0] != '[') {ASection->Add (linea);}
      }
    if ((strcmp (linea, "") != 0) && (linea != NULL) && (strcmp (linea, " ") != 0))
      {
      cbegin = linea[0]; cend = linea[strlen(linea) - 1];
      if (cbegin != ';')
        {
        if ((cbegin == '[') && (cend == ']'))
          {
          XString axs (SLFile.getString(i).trim());
          xstrncpy (seccion, CMAXBUFFER, axs.substring(1, axs.length() - 1).toUpperCase().cstr());
          if (strcmp (seccion, upseccion) == 0) {HabiaSeccion = true; EncontradaSeccion = true;}
          else {EncontradaSeccion = false;}
          }
        }
      }
    ++i;
    }
  return HabiaSeccion;  
  }      

//slot could be "name_%d"
int Configuration::getFreeSlot (const char *slot, int CMAX)
  {
  TBuffer sectname;
  int i = 0;   
  bool b; 
  do
    {
    ++i;
    xsnprintf (sectname, CMAXBUFFER, slot, i);
    b = IsThereSection (sectname);    
    }
  while ((b == true) && (i < CMAX)); 
  if (b == false) return i; else return -1;
  }



bool Configuration::deleteSection (const char *asection)
  {
  bool HabiaSeccion = false, EncontradaSeccion = false;
  int i=0;
  char cbegin, cend;
  TBuffer seccion, linea, upseccion;  
  
  xstrncpy (upseccion, CMAXBUFFER, asection); xucase (upseccion);
  initStr(seccion); 
  while (i < SLFile.Count())
    {
    xstrncpy (linea, CMAXBUFFER, SLFile.getString(i).trim().cstr());
    if ((EncontradaSeccion == true) && (linea != NULL))
      {
      if (linea[0] != '[') 
        {
        SLFile[i] = "";
        }
      }
    if ((strcmp (linea, "") != 0) && (linea != NULL) && (strcmp (linea, " ") != 0))
      {
      cbegin = linea[0]; cend = linea[strlen(linea) - 1];
      if (cbegin != ';')
        {
        if ((cbegin == '[') && (cend == ']'))
          {
          XString axs (SLFile.getString(i).trim());
          xstrncpy (seccion, CMAXBUFFER, axs.substring(1, axs.length() - 1).toUpperCase().cstr());
          if (strcmp (seccion, upseccion) == 0) 
            {
            HabiaSeccion = true; 
            EncontradaSeccion = true;
            SLFile[i] = "";
            }
          else 
            {
            EncontradaSeccion = false;
            }
          }
        }
      }
    ++i;
    }
  return HabiaSeccion;  
  }