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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
|
//=============================================================================
//
// File : libkviconfig.cpp
// Creation date : Thu Jan 31 2002 22:50:12 GMT by Szymon Stefanek
//
// This config is part of the KVirc irc client distribution
// Copyright (C) 2002-2005 Szymon Stefanek (pragma@kvirc.net)
//
// This program is FREE software. You can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your opinion) any later version.
//
// This program is distributed in the HOPE that it will be USEFUL,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, write to the Free Software Foundation,
// Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================
#include "kvi_module.h"
#include "kvi_string.h"
#include "kvi_config.h"
#include "kvi_fileutils.h"
#include "kvi_app.h"
#include "kvi_locale.h"
#include "kvi_pointerhashtable.h"
static KviPointerHashTable<TQString,KviConfig> * g_pConfigDict = 0;
static int g_iNextConfigId = 0;
/*
@doc: config.open
@type:
function
@title:
$config.open
@short:
Opens a config file
@syntax:
$config.open(<filename:string>[,<flags:string>])
@description:
Opens a config file.
If the file already exists , its contents
are parsed and available for extracting by the [fnc]$config.read[/fnc]() function.[br]
<filename> may be an absolute path or a relative path: if a relative path is used,
KVIrc will complete it with a local kvirc directory suitable for writing script config files.[br]
If the config file doesn't exist , it is opened as empty config file.[br]
Flags can contain a combination of letters 'r' and 'w'.[br]
If only 'r' is specified , the config file is opened in "read-only" mode: no changes will be written to disk.[br]
If only 'w' is specified , the config file is opened in "write-only" mode: the contents of the file on disk
are not readed.[br]
If <flags> are not specified then 'rw' is assumed.[br]
The function returns an identifier for the open config file. This identifier
is a mandatory parameter in all the other config.* functions and commands.[br]
The config section is set to the default section name: you can change it by using
[cmd]config.setsection[/cmd].[br]
After you have finished using the file , don't forget to call [cmd]config.close[/cmd]!
KVIrc tries to not open a config file twice: if the file was already opened then
the identifier of the open file is returned.[br]
You can check if a config file is already open by using the
[fnc]$config.id[/fnc]() function.[br]
@examples:
[example]
# It is a good idea to store the returned id to a variable :)
%cfg = $config.open(myscript.kvc)
%num = [fnc]$config.read[/fnc](%cfg,Number,0)
[cmd]echo[/cmd] Number of entries is %num
%num++;
[cmd]config.write[/cmd] %cfg Number %num
[cmd]config.close[/cmd] %cfg
[/example]
@seealso:
[module:config]Config module documentation[/module]
*/
static bool config_kvs_fnc_open(KviKvsModuleFunctionCall * c)
{
TQString szFile;
TQString szMode;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("filename",KVS_PT_STRING,0,szFile)
KVSM_PARAMETER("mode",KVS_PT_STRING,KVS_PF_OPTIONAL,szMode)
KVSM_PARAMETERS_END(c)
KviConfig::FileMode fileMode;
if(szMode.contains('r'))
{
if(szMode.contains('w'))fileMode = KviConfig::ReadWrite;
else fileMode = KviConfig::Read;
} else {
if(szMode.contains('w'))fileMode = KviConfig::Write;
else fileMode = KviConfig::ReadWrite;
}
KviFileUtils::adjustFilePath(szFile);
TQString szAbsFile;
if(KviFileUtils::isAbsolutePath(szFile))szAbsFile = szFile;
else g_pApp->getLocalKvircDirectory(szAbsFile,KviApp::ConfigScripts,szFile,true);
KviPointerHashTableIterator<TQString,KviConfig> it(*g_pConfigDict);
while(it.current())
{
if(KviTQString::equalCI(it.current()->fileName(),szAbsFile))
{
c->returnValue()->setString(it.currentKey());
if(it.current()->readOnly() && (fileMode & KviConfig::Write))
{
it.current()->setReadOnly(false);
}
return true;
}
++it;
}
KviConfig * cfg = new KviConfig(szAbsFile,fileMode);
g_iNextConfigId++;
TQString tmp = TQString("%1").tqarg(g_iNextConfigId);
g_pConfigDict->insert(tmp,cfg);
c->returnValue()->setString(tmp);
return true;
}
/*
@doc: config.id
@type:
function
@title:
$config.id
@short:
Returns the id of an open config file
@syntax:
$config.id(<filename>)
@description:
Returns the id of an open config file specified by <filename>.[br]
(This is the same id that is returned by [fnc]$config.open[/fnc].[br]
If no such file is open, 0 is returned (that is an invalid config identifier).[br]
@seealso:
[module:config]Config module documentation[/module]
*/
static bool config_kvs_fnc_id(KviKvsModuleFunctionCall * c)
{
TQString szFile;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("file",KVS_PT_STRING,0,szFile)
KVSM_PARAMETERS_END(c)
KviFileUtils::adjustFilePath(szFile);
TQString szAbsFile;
if(KviFileUtils::isAbsolutePath(szFile))szAbsFile = szFile;
else g_pApp->getLocalKvircDirectory(szAbsFile,KviApp::ConfigScripts,szFile,true);
KviPointerHashTableIterator<TQString,KviConfig> it(*g_pConfigDict);
while(it.current())
{
if(KviTQString::equalCI(it.current()->fileName(),szAbsFile))
{
c->returnValue()->setString(it.currentKey());
return true;
}
++it;
}
c->returnValue()->setString("0");
return true;
}
/*
@doc: config.read
@type:
function
@title:
$config.read
@short:
Reads an entry from a config file
@syntax:
$config.read(<id:string>,<key:string>[,<default:string>])
@description:
Reads and returns the value associated to <key> in the current section of the config
file identified by <id>.[br]
<id> must be a valid config identifier returned by [fnc]$config.open[/fnc]()
If no value is associated with <key> in the current section the return value
is an empty string or the string <default> if it has been passed.[br]
@seealso:
[module:config]Config module documentation[/module]
*/
static bool config_kvs_fnc_read(KviKvsModuleFunctionCall * c)
{
TQString szId;
TQString szKey;
TQString szDefault;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("id",KVS_PT_STRING,0,szId)
KVSM_PARAMETER("key",KVS_PT_STRING,0,szKey)
KVSM_PARAMETER("default",KVS_PT_STRING,KVS_PF_OPTIONAL,szDefault)
KVSM_PARAMETERS_END(c)
KviConfig * cfg = g_pConfigDict->find(szId);
if(cfg)
{
c->returnValue()->setString(cfg->readEntry(szKey,szDefault));
} else {
c->warning(__tr2qs("The config file with id '%Q' is not open"),&szId);
}
return true;
}
/*
@doc: config.section
@type:
function
@title:
$config.section
@short:
Returns the name of the current section of a config file
@syntax:
$config.section(<id>)
@description:
Returns the name of the current section in the config file identifier by <id>.[br]
<id> must be a valid config identifier returned by [fnc]$config.open[/fnc]()
@seealso:
[module:config]Config module documentation[/module]
*/
static bool config_kvs_fnc_section(KviKvsModuleFunctionCall * c)
{
TQString szId;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("id",KVS_PT_STRING,0,szId)
KVSM_PARAMETERS_END(c)
KviConfig * cfg = g_pConfigDict->find(szId);
if(cfg)
{
c->returnValue()->setString(cfg->group());
} else {
c->warning(__tr2qs("The config file with id '%Q' is not open"),&szId);
}
return true;
}
/*
@doc: config.readonly
@type:
function
@title:
$config.readonly
@short:
Checks wheter a config file is opened in readonly mode
@syntax:
$config.readonly(<id>)
@description:
Returns 1 if the config file identified by <id> is opened in readonly mode,
0 otherwise.[br]
<id> must be a valid config identifier returned by [fnc]$config.open[/fnc]()
If <id> does not identify an open config file, a warning is printed and 0 is returned.[br]
@seealso:
[module:config]Config module documentation[/module]
*/
static bool config_kvs_fnc_readonly(KviKvsModuleFunctionCall * c)
{
TQString szId;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("id",KVS_PT_STRING,0,szId)
KVSM_PARAMETERS_END(c)
KviConfig * cfg = g_pConfigDict->find(szId);
if(cfg)
{
c->returnValue()->setBoolean(cfg->readOnly());
} else {
c->warning(__tr2qs("The config file with id '%Q' is not open"),&szId);
c->returnValue()->setInteger(false);
}
return true;
}
/*
@doc: config.filename
@type:
function
@title:
$config.filename
@short:
Returns the filename of an open config file
@syntax:
$config.filename(<id>)
@description:
Returns the filename of the [b]open[/b] config file identifier by <id>.[br]
<id> must be a valid config identifier returned by [fnc]$config.open[/fnc]()
If the <id> doesn't match any open file, and empty string is returned.[br]
@seealso:
[module:config]Config module documentation[/module]
*/
static bool config_kvs_fnc_filename(KviKvsModuleFunctionCall * c)
{
TQString szId;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("id",KVS_PT_STRING,0,szId)
KVSM_PARAMETERS_END(c)
KviConfig * cfg = g_pConfigDict->find(szId);
if(cfg)
{
c->returnValue()->setString(cfg->fileName());
}
return true;
}
/*
@doc: config.hassection
@type:
function
@title:
$config.hassection
@short:
Checks wheter a section is present in a config file
@syntax:
$config.hassection(<id>,<section_name>)
@description:
Returns 1 if the section <section_name> exists in the config file identifier by <id>,
0 otherwise.[br]
<id> must be a valid config identifier returned by [fnc]$config.open[/fnc]()
@seealso:
[module:config]Config module documentation[/module]
*/
static bool config_kvs_fnc_hassection(KviKvsModuleFunctionCall * c)
{
TQString szId;
TQString szSect;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("id",KVS_PT_STRING,0,szId)
KVSM_PARAMETER("id",KVS_PT_STRING,0,szSect)
KVSM_PARAMETERS_END(c)
KviConfig * cfg = g_pConfigDict->find(szId);
if(cfg)
{
c->returnValue()->setBoolean(cfg->hasGroup(szSect));
} else {
c->warning(__tr2qs("The config file with id '%Q' is not open"),&szId);
}
return true;
}
/*
@doc: config.sectionlist
@type:
function
@title:
$config.sectionlist
@short:
Returns a list of the sections in a config file
@syntax:
$config.sectionlist(<id>)
@description:
Returns an array of sections present in the config file identified by <id>.[br]
<id> must be a valid config identifier returned by [fnc]$config.open[/fnc]()
@seealso:
[module:config]Config module documentation[/module]
*/
static bool config_kvs_fnc_sectionlist(KviKvsModuleFunctionCall * c)
{
TQString szId;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("id",KVS_PT_STRING,0,szId)
KVSM_PARAMETERS_END(c)
KviConfig * cfg = g_pConfigDict->find(szId);
if(cfg)
{
KviConfigIterator it(*(cfg->dict()));
KviKvsArray* pArray = new KviKvsArray();
int id=0;
while(it.current())
{
pArray->set(id++, new KviKvsVariant(it.currentKey()));
++it;
}
c->returnValue()->setArray(pArray);
} else {
c->warning(__tr2qs("The config file with id '%Q' is not open"),&szId);
}
return true;
}
/*
@doc: config.keylist
@type:
function
@title:
$config.keylist
@short:
Returns a list of keys in a config file
@syntax:
$config.keylist(<id>)
@description:
Returns an array of keys present in the current section of the config file identified by <id>.[br]
<id> must be a valid config identifier returned by [fnc]$config.open[/fnc]()
@seealso:
[module:config]Config module documentation[/module]
*/
static bool config_kvs_fnc_keylist(KviKvsModuleFunctionCall * c)
{
TQString szId;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("id",KVS_PT_STRING,0,szId)
KVSM_PARAMETERS_END(c)
KviConfig * cfg = g_pConfigDict->find(szId);
if(cfg)
{
KviConfigGroup * d = cfg->dict()->find(cfg->group());
if(!d)return true;
KviConfigGroupIterator it(*d);
KviKvsArray* pArray = new KviKvsArray();
int id=0;
while(it.current())
{
pArray->set(id++, new KviKvsVariant(it.currentKey()));
++it;
}
c->returnValue()->setArray(pArray);
} else {
c->warning(__tr2qs("The config file with id '%Q' is not open"),&szId);
}
return true;
}
/*
@doc: config.filelist
@type:
function
@title:
$config.filelist
@short:
Returns a list of open config files
@syntax:
$config.filelist
@description:
Returns an array of open config files.[br]
@seealso:
[module:config]Config module documentation[/module]
*/
static bool config_kvs_fnc_filelist(KviKvsModuleFunctionCall * c)
{
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETERS_END(c)
KviKvsArray* pArray = new KviKvsArray();
int id=0;
KviPointerHashTableIterator<TQString,KviConfig> it(*g_pConfigDict);
while(it.current())
{
pArray->set(id++, new KviKvsVariant(it.currentKey()));
++it;
}
c->returnValue()->setArray(pArray);
return true;
}
/*
@doc: config.close
@type:
command
@title:
config.close
@keyterms:
storing & reading configuration entries
@short:
Closes a config file
@syntax:
config.close [-q] <id>
@description:
Closes the config file identified by <id>.[br]
<id> must be a a valid config file identifier returned by [fnc]$config.open[/fnc].[br]
If the config file was opened as read-write (default), the changes will be stored
to disk. If the config was opened as read-only , changes will not be written.[br]
If the <id> does not match any open config file, a warning is printed unless
the -q switch is used.[br]
@seealso:
[module:config]Config module documentation[/module]
*/
static bool config_kvs_cmd_close(KviKvsModuleCommandCall * c)
{
TQString szId;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("id",KVS_PT_STRING,0,szId)
KVSM_PARAMETERS_END(c)
KviConfig * cfg = g_pConfigDict->find(szId);
if(cfg)
{
if(cfg->dirty() && cfg->readOnly())
{
if(!c->hasSwitch('q',"quiet"))
c->warning(__tr2qs("The config file '%Q' has been changed but is opened as read-only: changes will be lost"),&cfg->fileName());
}
g_pConfigDict->remove(szId);
} else {
if(!c->hasSwitch('q',"quiet"))
c->warning(__tr2qs("The config file with id '%Q' is not open"),&szId);
}
return true;
}
/*
@doc: config.flush
@type:
command
@title:
config.flush
@keyterms:
storing & reading configuration entries
@short:
Flushes a config file
@syntax:
config.flush <id>
@description:
Flushes the config file identified by <id>.[br]
<id> must be a a valid config file identifier returned by [fnc]$config.open[/fnc].[br]
If the config file was opened as read-write (default), the changes will be stored
to disk. If the config was opened as read-only ,an error is printed.[br]
If the <id> does not match any open config file, a warning is printed.
@seealso:
[module:config]Config module documentation[/module]
*/
static bool config_kvs_cmd_flush(KviKvsModuleCommandCall * c)
{
TQString szId;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("id",KVS_PT_STRING,0,szId)
KVSM_PARAMETERS_END(c)
KviConfig * cfg = g_pConfigDict->find(szId);
if(cfg)
{
if(cfg->readOnly())
c->warning(__tr2qs("The config file with id '%Q' is read only"),&szId);
else
cfg->sync();
} else {
c->warning(__tr2qs("The config file with id '%Q' is not open"),&szId);
}
return true;
}
/*
@doc: config.clear
@type:
command
@title:
config.clear
@keyterms:
storing & reading configuration entries
@short:
Clears a config file
@syntax:
config.clear <id>
@description:
Clears the contents of the config file identified by <id>.[br]
All the sections are removed (except for the default section) and all the key=value
pairs are destroyed.[br]
<id> must be a a valid config file identifier returned by [fnc]$config.open[/fnc].[br]
If the <id> does not match any open config file, a warning is printed.
@seealso:
[module:config]Config module documentation[/module]
*/
static bool config_kvs_cmd_clear(KviKvsModuleCommandCall * c)
{
TQString szId;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("id",KVS_PT_STRING,0,szId)
KVSM_PARAMETERS_END(c)
KviConfig * cfg = g_pConfigDict->find(szId);
if(cfg)
{
cfg->clear();
} else {
c->warning(__tr2qs("The config file with id '%Q' is not open"),&szId);
}
return true;
}
/*
@doc: config.clearsection
@type:
command
@title:
config.clearsection
@keyterms:
storing & reading configuration entries
@short:
Clears a section in a config file
@syntax:
config.clearsection <id> <section_name>
@description:
Clears the contents of the section <section_name> in the config file identified by <id>.[br]
All the key=value pairs in the section are destroyed.[br]
<id> must be a a valid config file identifier returned by [fnc]$config.open[/fnc].[br]
If the <id> does not match any open config file, a warning is printed.
If <section_name> was also the current section, the current section is set to the default one.[br]
No warning is printed if the section wasn't existing.[br]
@seealso:
[module:config]Config module documentation[/module]
*/
static bool config_kvs_cmd_clearsection(KviKvsModuleCommandCall * c)
{
TQString szId;
TQString szSect;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("id",KVS_PT_STRING,0,szId)
KVSM_PARAMETER("section",KVS_PT_STRING,0,szSect)
KVSM_PARAMETERS_END(c)
KviConfig * cfg = g_pConfigDict->find(szId);
if(cfg)
{
cfg->clearGroup(szSect);
} else {
c->warning(__tr2qs("The config file with id '%Q' is not open"),&szId);
}
return true;
}
/*
@doc: config.write
@type:
command
@title:
config.write
@keyterms:
storing & reading configuration entries
@short:
Writes a data field to a config file
@syntax:
config.write <id> <key> <value>
@description:
Writes the <key>=<value> pair to the config file identifier by <id>.[br]
<id> must be a valid config file id returned by [fnc]$config.open[/fnc]().[br]
<key> and <value> can be any strings.[br]
The <key>=<value> pair is written in the current section of the config file.[br]
If <key> already exists in the current section of the config , the corresponding value is replaced with <value>.[br]
If <value> is an empty string, the <key> is simply removed from the current section.[br]
If a section remains empty (with no keys) at file write time, that section will be removed.[br]
The changes are NOT written to disk: you must call [cmd]config.flush[/cmd] or (better) [cmd]config.close[/cmd]
for the changes to be effectively written.[br]
@seealso:
[module:config]Config module documentation[/module]
*/
static bool config_kvs_cmd_write(KviKvsModuleCommandCall * c)
{
TQString szId;
TQString szKey;
TQString szVal;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("id",KVS_PT_STRING,0,szId)
KVSM_PARAMETER("key",KVS_PT_STRING,0,szKey)
KVSM_PARAMETER("value",KVS_PT_STRING,0,szVal)
KVSM_PARAMETERS_END(c)
KviConfig * cfg = g_pConfigDict->find(szId);
if(cfg)
{
cfg->writeEntry(szKey,szVal);
} else {
c->warning(__tr2qs("The config file with id '%Q' is not open"),&szId);
}
return true;
}
/*
@doc: config.setsection
@type:
command
@title:
config.setsection
@keyterms:
storing & reading configuration entries
@short:
Changes the current section of a config file
@syntax:
config.setsection <id> <section_name>
@description:
Changes the current section of the config file <id> to <section_name>.[br]
Sections are effectively namespaces for the keys: each section can contain
a set of keys and equal keys in different sections do not collide.[br]
<id> must be a valid config file id returned by [fnc]$config.open[/fnc]().[br]
@seealso:
[module:config]Config module documentation[/module]
*/
static bool config_kvs_cmd_setsection(KviKvsModuleCommandCall * c)
{
TQString szId;
TQString szSect;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("id",KVS_PT_STRING,0,szId)
KVSM_PARAMETER("section",KVS_PT_STRING,0,szSect)
KVSM_PARAMETERS_END(c)
KviConfig * cfg = g_pConfigDict->find(szId);
if(cfg)
{
cfg->setGroup(szSect);
} else {
c->warning(__tr2qs("The config file with id '%Q' is not open"),&szId);
}
return true;
}
/*
@doc: config
@type:
module
@short:
Managing of configuration files
@title:
The config module
@body:
The config module is a powerful interface that manages text configuration files.[br]
The files are arranged in named sections and the data is stored in key=value pairs
belonging to each section.[br]
[br]
[section1_name][br]
<key1>=<value1>[br]
<key2>=<value2>[br]
<key3>=<value3>[br]
....[br]
[section2_name][br]
<key1>=<value1>[br]
....[br]
[br]
Keys and values are plain text strings. Eventual newlines in values are encoded using
a simple hexadecimal notation.[br]
The config files can be also edited by using any text editor.[br]
The config module works on config files loaded in memory. For this reason,
you must first open a config file by the means of [fnc]$config.open[/fnc]().[br]
Once you open the file , if it exists on disk , its contents are loaded into memory
and you can access the data stored in it. If the file wasn't existing, and empty
memory config file is created.[br]
The config file is opened in read-write mode by default. You can also choose
the "read-only" method: a read-only config file is never flushed to disk (but you can
set the values in memory!).[br]
It is not a good idea to keep a config file open forever: a good approach
is to keep settings in memory variables and write them all at once when it comes to
write settings: you open the config file , write all your values and close the file.[br]
You can obviously keep the config file open for some time but remember that
no change is written to the disk until [cmd]config.flush[/cmd] or [cmd]config.close[/cmd] is called.[br]
You write entries by using [cmd]config.write[/cmd] and read them by using [fnc]$config.read[/fnc]().[br]
You can change the "current" config section by using [cmd]config.setsection[/cmd].[br]
Here comes a complete list of commands and functions exported by this module:[br]
[fnc]$config.open[/fnc][br]
[fnc]$config.read[/fnc][br]
[fnc]$config.section[/fnc][br]
[fnc]$config.hassection[/fnc][br]
[fnc]$config.sectionlist[/fnc][br]
[fnc]$config.keylist[/fnc][br]
[fnc]$config.filelist[/fnc][br]
[fnc]$config.filename[/fnc][br]
[fnc]$config.id[/fnc][br]
[fnc]$config.readonly[/fnc][br]
[cmd]config.close[/cmd][br]
[cmd]config.flush[/cmd][br]
[cmd]config.clear[/cmd][br]
[cmd]config.clearsection[/cmd][br]
[cmd]config.setsection[/cmd][br]
[cmd]config.write[/cmd][br]
*/
static bool config_module_init(KviModule * m)
{
g_pConfigDict = new KviPointerHashTable<TQString,KviConfig>;
g_pConfigDict->setAutoDelete(true);
KVSM_REGISTER_FUNCTION(m,"open",config_kvs_fnc_open);
KVSM_REGISTER_FUNCTION(m,"id",config_kvs_fnc_id);
KVSM_REGISTER_FUNCTION(m,"read",config_kvs_fnc_read);
KVSM_REGISTER_FUNCTION(m,"section",config_kvs_fnc_section);
KVSM_REGISTER_FUNCTION(m,"hassection",config_kvs_fnc_hassection);
KVSM_REGISTER_FUNCTION(m,"sectionlist",config_kvs_fnc_sectionlist);
KVSM_REGISTER_FUNCTION(m,"keylist",config_kvs_fnc_keylist);
KVSM_REGISTER_FUNCTION(m,"filelist",config_kvs_fnc_filelist);
KVSM_REGISTER_FUNCTION(m,"filename",config_kvs_fnc_filename);
KVSM_REGISTER_FUNCTION(m,"readonly",config_kvs_fnc_readonly);
KVSM_REGISTER_SIMPLE_COMMAND(m,"close",config_kvs_cmd_close);
KVSM_REGISTER_SIMPLE_COMMAND(m,"flush",config_kvs_cmd_flush);
KVSM_REGISTER_SIMPLE_COMMAND(m,"clear",config_kvs_cmd_clear);
KVSM_REGISTER_SIMPLE_COMMAND(m,"clearsection",config_kvs_cmd_clearsection);
KVSM_REGISTER_SIMPLE_COMMAND(m,"setsection",config_kvs_cmd_setsection);
KVSM_REGISTER_SIMPLE_COMMAND(m,"write",config_kvs_cmd_write);
return true;
}
static bool config_module_cleanup(KviModule *m)
{
delete g_pConfigDict;
g_pConfigDict = 0;
return true;
}
static bool config_module_can_unload(KviModule *m)
{
return g_pConfigDict->isEmpty();
}
KVIRC_MODULE(
"Config", // module name
"1.0.0", // module version
"Copyright (C) 2001 Szymon Stefanek (pragma at kvirc dot net)", // author & (C)
"Config file system for KVIrc",
config_module_init,
config_module_can_unload,
0,
config_module_cleanup
)
|