-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvConfigInfo.cc
More file actions
1334 lines (1206 loc) · 31.8 KB
/
AvConfigInfo.cc
File metadata and controls
1334 lines (1206 loc) · 31.8 KB
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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//# Copyright (C) 1995-2000 Board of Trustees of the University of Illinois
//#
//# This software, both binary and source, is copyrighted by The
//# Board of Trustees of the University of Illinois. Ownership
//# remains with the University. You should have received a copy
//# of a licensing agreement with this software. See the file
//# "AIPSVIEW_COPYRIGHT", or contact the University at this address:
//#
//# The NCSA AipsView Visualization System
//# National Center for Supercomputing Applications
//# University of Illinois
//# 405 North Mathews Ave.
//# Urbana, IL 61801
//# aipsview@ncsa.uiuc.edu
//# --------------------------------------------------
//
//
// $Header: /home/cvs/aips++/code/trial/apps/aipsview/Attic/AvConfigInfo.cc,v 19.0 2003/07/16 05:47:22 aips2adm Exp $
//
// $Log: AvConfigInfo.cc,v $
// Revision 19.0 2003/07/16 05:47:22 aips2adm
// exhale: Base release 19.000.00
//
// Revision 18.0 2002/06/07 21:29:03 aips2adm
// exhale: Base release 18.000.00
//
// Revision 17.0 2001/11/12 19:42:32 aips2adm
// exhale: Base release 17.000.00
//
// Revision 16.0 2001/05/03 01:42:39 aips2adm
// exhale: Base release 16.000.00
//
// Revision 15.0 2000/10/26 17:10:50 aips2adm
// exhale: Base release 15.000.00
//
// Revision 14.1 2000/07/18 17:01:17 hravlin
// Update copyright.
//
// Revision 14.0 2000/03/23 16:08:26 aips2adm
// exhale: Base release 14.000.00
//
// Revision 13.2 1999/09/24 21:13:01 hravlin
// Removed leading and trailing spaces when outputting a list.
//
// Revision 13.1 1999/08/25 19:55:30 hravlin
// Edits (mostly casts) to remove compiler warnings.
//
// Revision 13.0 1999/08/10 18:40:14 aips2adm
// exhale: Base release 13.000.00
//
// Revision 12.0 1999/07/15 00:23:04 aips2adm
// exhale: Base release 12.000.00
//
// Revision 11.0 1998/10/03 07:00:21 aips2adm
// exhale: Base release 11.000.00
//
// Revision 10.0 1998/07/20 17:54:18 aips2adm
// exhale: Base release 10.000.00
//
// Revision 9.2 1998/05/04 20:33:11 hr
// Removed a couple of unneeded variables.
//
// Revision 9.1 1997/09/17 14:37:13 hr
// getRCName() now leaves on the file name extension. The suffix is preceded by
// a '.' and neither are written if the suffix is blank.
//
// Revision 9.0 1997/08/25 21:28:24 aips2adm
// exhale: Base release 09.000.00
//
// Revision 8.1 1997/02/25 16:41:18 hr
// When the get<xxx> functions write a default, they now use the data set's
// name rather than "aipsview".
// Added setIntList() for AvIPositions.
//
// Revision 8.0 1997/02/20 03:17:21 aips2adm
// exhale: Base release 08.000.00
//
// Revision 7.4 1997/01/24 21:44:50 hr
// Fixes to allow compiling w/o TCL.
//
// Revision 7.3 1997/01/24 20:01:27 hr
// The get<xxx>() routines now write the default to def_ if the requested
// variable does not exist. The set<xxx>() routines accept an argument to
// write the value to def_ rather than set_.
// Don't write old file entries as comments.
// Change which variables get dumped when writeOptions() is called.
//
// Revision 7.2 1996/12/16 17:22:22 droberts
// some of the get routines were not returning the default value if TCL was not turned on.
//
// Revision 7.1 1996/12/12 08:11:02 droberts
// *** empty log message ***
//
// Revision 1.5 1996/11/06 22:44:25 hr
// Had previously misspelled __SVR4 as __SVR.
//
// Revision 1.4 1996/11/06 17:04:50 hr
// Sun's 4.1.3 compiler complains about variables named "end".
// It also complains about arguments to free() that aren't "char *".
//
// Revision 1.3 1996/11/05 22:21:03 hr
// Non TCL version of loadDefaults() was typed as void.
//
// Revision 1.2 1996/11/05 22:09:59 hr
// IRIX compiler can't handle two functions of the same name one with an
// int arg and one with char. (getBoolean()).
//
// Revision 1.1 1996/11/05 21:10:58 hr
// Initial revision
//
//
//---------------------------------------------------------------------------
/* AvConfigInfo.cc
*/
#include <string.h>
#include <stdlib.h> // free().
#include <sys/types.h> // stat
#include <sys/stat.h> // ..
#include "AvConfigInfo.h"
#include "cregex.h"
#include <pwd.h> // getpwnam
extern int obscure_syntax;
#ifndef FALSE
#define FALSE 0
#define TRUE 1
typedef int Boolean;
#endif
// Environment variable that hold name of rc file to use.
static const char *AIPSVIEWRC= "AIPSVIEWRC";
// If variable doesn't exist, use this.
static const char *DEFAULTRC= "~/.aipsviewrc";
static const char *DEFAULTVAR = "aipsview";
#ifdef TCL
/* Create a ConfigInfo object.
filename Name of the data file for which we need the configuration
information. Any trailing extension will be removed and
"rc" will be appended. If the rc file exists in the current
directory, it will be used. If not, the path will be used.
*/
AvConfigInfo::AvConfigInfo(const char *filename, const char *moduleName,
const char *defaultScript)
{
initialize();
// Data file name w/o path.
if(filename == NULL)
fileVar_ = defaultVar();
else
fileVar_ = getRCName(filename, FALSE, "");
module_ = moduleName;
// Load module specific defaults.
if(defaultScript != NULL)
def_.eval(defaultScript);
// Load data set specific defaults.
AvString fileRC = getRCName(filename);
if(fileExists(fileRC)) // Does it exist locally?
rc_.evalFile(fileRC);
else // If not, try with full path.
{ fileRC = getRCName(filename, TRUE);
if(fileExists(fileRC))
rc_.evalFile(fileRC);
}
}
#else
AvConfigInfo::AvConfigInfo(const char *, const char *,
const char *)
{
fileVar_ = "";
module_ = "";
}
#endif
AvConfigInfo::~AvConfigInfo()
{
}
#ifdef TCL
static int initialized_=0;
// Interpreters to hold all the defaults.
AvTcl AvConfigInfo::def_; // Defaults set internally by aipsview.
AvTcl AvConfigInfo::rc_; // Defaults from rc files.
AvTcl AvConfigInfo::set_; // Defaults set by user
AvTclHashTable AvConfigInfo::forget_;// Table of variable names to forget.
AvTclHashTable AvConfigInfo::remember_;// Table of variable names to dump.
// One time only global initializations.
void AvConfigInfo::initialize()
{
if( initialized_)
return;
// Keys will be strings.
forget_.init(TCL_STRING_KEYS);
remember_.init(TCL_STRING_KEYS);
// Keep track of variable names that the interpreter creates.
forgetAll(def_);
// Try to run the user's .aipsviewrc script. The name may be
// overridden by an environment variable.
const char *avrc = defaultName();
// Use tcl's fileExist routine since it handles "~".
if(rc_.fileExists(avrc))
rc_.evalFile(avrc);
initialized_ = 1;
}
#else
void AvConfigInfo::initialize(){}
#endif
// Build a TCL array spec of the form:
// "<fileName>(<moduleName>,<variableName>)"
// fileName and moduleName are the internal variables.
// If def is TRUE, then defaultVar() is substituted for fileName_.
// No check is made to make sure var is valid.
AvString AvConfigInfo::makeIndex( const char *var, const int def)const
{AvString s = (def) ? AvString(defaultVar()) : fileVar_;
s += "(" + module_ + "," + var + ")";
return s;
}
/////////////////////////////////////////////////////////////////////////
//////////////// Set/get values. /////////
/////////////////////////////////////////////////////////////////////////
#ifdef TCL
// Routines to retrieve variables. Returns 1 if the variable was found,
// otherwise, 0.
// The routines try the databases in this order:
// set_ and rc_ with the filename.
// set_, rc_, def_ with defaultVar().
// If the variable isn't found, the default is returned AND it is written
// to def_.
int AvConfigInfo::getInt(const char *var, int &value, const int def)const
{ AvString index = makeIndex(var);
if(set_.getIntVal(index.chars(), value) == TCL_OK)
return 1;
if(rc_.getIntVal(index.chars(), value) == TCL_OK)
return 1;
AvString index1 = makeIndex(var, TRUE);
if(set_.getIntVal(index1.chars(), value) == TCL_OK)
return 1;
if(rc_.getIntVal(index1.chars(), value) == TCL_OK)
return 1;
if(def_.getIntVal(index1.chars(), value) == TCL_OK)
return 1;
value = def;
def_.setIntVal(index.chars(), value);
return 0;
}
#else
int AvConfigInfo::getInt( const char *, int &value, const int def)const
{
value = def;
return 0;
}
#endif
// Read in a variable and convert to an int.
int AvConfigInfo::getMappedInt(const char *variable, int &value,
const IntMap *map)const
{AvString str;
if(! getString(variable, str))
return 0;
return stringToInt(str.chars(), value, map);
}
// Read in a variable and convert to an int.
int AvConfigInfo::getMappedInt(const char *variable, int &value,
const IntMap *map, const int def)const
{
if(getMappedInt(variable, value, map))
return 1;
value = def;
#ifdef TCL
{const char *str;
if((str = intToString(value, map)) != NULL)
def_.setVar(makeIndex(variable), str);
}
#endif
return 0;
}
#ifdef TCL
int AvConfigInfo::getDouble(const char *var, double &value,
const double def)const
{ AvString index = makeIndex(var);
if(set_.getDoubleVal(index.chars(), value) == TCL_OK)
return 1;
if(rc_.getDoubleVal(index.chars(), value) == TCL_OK)
return 1;
AvString index1 = makeIndex(var, TRUE);
if(set_.getDoubleVal(index1.chars(), value) == TCL_OK)
return 1;
if(rc_.getDoubleVal(index1.chars(), value) == TCL_OK)
return 1;
if(def_.getDoubleVal(index1.chars(), value) == TCL_OK)
return 1;
value = def;
def_.setDoubleVal(index.chars(), value);
return 0;
}
#else
int AvConfigInfo::getDouble(const char *, double &value, const double def)const
{
value = def;
return 0;
}
#endif
#ifdef TCL
int AvConfigInfo::getBoolean(const char *var, int &value, const int def)const
{ AvString index = makeIndex(var);
if(set_.getBooleanVal(index.chars(), value) == TCL_OK)
return 1;
if(rc_.getBooleanVal(index.chars(), value) == TCL_OK)
return 1;
AvString index1 = makeIndex(var, TRUE);
if(set_.getBooleanVal(index1.chars(), value) == TCL_OK)
return 1;
if(rc_.getBooleanVal(index1.chars(), value) == TCL_OK)
return 1;
if(def_.getBooleanVal(index1.chars(), value) == TCL_OK)
return 1;
value = def;
def_.setBooleanVal(index.chars(), value);
return 0;
}
#else
int AvConfigInfo::getBoolean( const char *, int &value, const int def)const
{
value = def;
return 0;
}
#endif
#ifdef TCL
int AvConfigInfo::getString(const char *var, AvString &value)const
{ AvString index = makeIndex(var);
const char *rtn;
if((rtn = set_.getVar(index.chars())) != NULL)
{ value = rtn;
return 1;
}
if((rtn = rc_.getVar(index.chars())) != NULL)
{ value = rtn;
return 1;
}
index = makeIndex(var, TRUE);
if((rtn = set_.getVar(index.chars())) != NULL)
{ value = rtn;
return 1;
}
if((rtn = rc_.getVar(index.chars())) != NULL)
{ value = rtn;
return 1;
}
if((rtn = def_.getVar(index.chars())) != NULL)
{ value = rtn;
return 1;
}
return 0;
}
#else
int AvConfigInfo::getString( const char *, AvString &)const
{
return 0;
}
#endif
int AvConfigInfo::getString(const char *var, AvString &value,
const AvString &def)const
{
if(getString(var, value))
return 1;
value = def;
#ifdef TCL
def_.setVar(makeIndex(var), value);
#endif
return 0;
}
#ifdef TCL
int AvConfigInfo::getDoubleList(const char *var, AvWPosition &value)const
{ AvString index = makeIndex(var);
if(set_.getDoubleList(index.chars(), value) == TCL_OK)
return 1;
if(rc_.getDoubleList(index.chars(), value) == TCL_OK)
return 1;
index = makeIndex(var, TRUE);
if(set_.getDoubleList(index.chars(), value) == TCL_OK)
return 1;
if(rc_.getDoubleList(index.chars(), value) == TCL_OK)
return 1;
if(def_.getDoubleList(index.chars(), value) == TCL_OK)
return 1;
return 0;
}
#else
int AvConfigInfo::getDoubleList(const char *, AvWPosition &)const
{
return 0;
}
#endif
int AvConfigInfo::getDoubleList(const char *var, AvWPosition &value,
const AvWPosition &def)const
{
if(getDoubleList(var, value))
return 1;
value = def;
#ifdef TCL
def_.setDoubleList(makeIndex(var), value);
#endif
return 0;
}
#ifdef TCL
int AvConfigInfo::getIntList(const char *var, AvIPosition &value)const
{ AvString index = makeIndex(var);
if(set_.getIntList(index.chars(), value) == TCL_OK)
return 1;
if(rc_.getIntList(index.chars(), value) == TCL_OK)
return 1;
index = makeIndex(var, TRUE);
if(set_.getIntList(index.chars(), value) == TCL_OK)
return 1;
if(rc_.getIntList(index.chars(), value) == TCL_OK)
return 1;
if(def_.getIntList(index.chars(), value) == TCL_OK)
return 1;
return 0;
}
#else
int AvConfigInfo::getIntList(const char *, AvIPosition &)const
{
return 0;
}
#endif
int AvConfigInfo::getIntList(const char *var, AvIPosition &value,
const AvIPosition &def)const
{
if(getIntList(var, value))
return 1;
value = def;
#ifdef TCL
def_.setIntList(makeIndex(var), value);
#endif
return 0;
}
#ifdef TCL
int AvConfigInfo::getMappedIntList(const char *var, AvIPosition &value,
const IntMap *map)const
{AvString lst;
if(!getString(var, lst))
return 0;
int size, i;
char **list, **ptr;
if( def_.splitList(lst.chars(), size, &list) != TCL_OK)
return 0;
value.resize(size);
int rtn = 0;
for(ptr=list, i=0; i< size; i++, ptr++)
{int v;
if(stringToInt(*ptr, v, map))
{ value(i) = v;
rtn += 1;
}
}
#if defined(sparc) && ! defined(__SVR4)
free((char *)list);
#else
free(list);
#endif
return rtn;
}
#else
int AvConfigInfo::getMappedIntList(const char *, AvIPosition &,
const IntMap *)const
{
return 0;
}
#endif
#ifdef TCL
void AvConfigInfo::setInt(const char *var, const int value, const int def)
{
if(def)
def_.setIntVal(makeIndex(var), value);
else
set_.setIntVal(makeIndex(var), value);
}
#else
void AvConfigInfo::setInt(const char *, const int, const int )
{
}
#endif
#ifdef TCL
void AvConfigInfo::setHex(const char *var, const int value, const int def)
{
if(def)
def_.setIntVal(makeIndex(var), value, 0, "0X%x");
else
set_.setIntVal(makeIndex(var), value, 0, "0X%x");
}
#else
void AvConfigInfo::setHex(const char *, const int, const int )
{
}
#endif
#ifdef TCL
void AvConfigInfo::setDouble(const char *var, const double value,
const int def)
{
if(def)
def_.setDoubleVal(makeIndex(var), value);
else
set_.setDoubleVal(makeIndex(var), value);
}
#else
void AvConfigInfo::setDouble(const char *, const double, const int )
{
}
#endif
#ifdef TCL
void AvConfigInfo::setDoubleList(const char *var, const AvWPosition &value,
const int def)
{
if(def)
def_.setDoubleList(makeIndex(var), value);
else
set_.setDoubleList(makeIndex(var), value);
}
#else
void AvConfigInfo::setDoubleList(const char *, const AvWPosition &,
const int)
{
}
#endif
#ifdef TCL
void AvConfigInfo::setDoubleList(const char *var,
const int length, const double *values,
const int def)
{
if(def)
def_.setDoubleList(makeIndex(var), length, values);
else
set_.setDoubleList(makeIndex(var), length, values);
}
#else
void AvConfigInfo::setDoubleList(const char *, const int , const double *,
const int)
{
}
#endif
#ifdef TCL
void AvConfigInfo::setIntList(const char *var,
const int length, const int *values,
const int def)
{
if(def)
def_.setIntList(makeIndex(var), length, values);
else
set_.setIntList(makeIndex(var), length, values);
}
#else
void AvConfigInfo::setIntList(const char *, const int , const int *,
const int)
{
}
#endif
#ifdef TCL
void AvConfigInfo::setIntList(const char *var, const AvIPosition &values,
const int def)
{
if(def)
def_.setIntList(makeIndex(var), values);
else
set_.setIntList(makeIndex(var), values);
}
#else
void AvConfigInfo::setIntList(const char *, const AvIPosition &,
const int)
{
}
#endif
#ifdef TCL
void AvConfigInfo::setMappedIntList(const char *var, const AvIPosition &lst,
const IntMap *map, const int def)
{//AvString v = makeIndex(var);
AvString list = "{";
int n = lst.nelements();
for(int i = 0; i < n; i++)
{const char *str = intToString(lst(i), map);
if(str != NULL)
{ list += " ";
list += str;
}
}
list += "}";
if(def)
def_.setVar(makeIndex(var), list.chars());
else
set_.setVar(makeIndex(var), list.chars());
}
#else
void AvConfigInfo::setMappedIntList(const char *, const AvIPosition &,
const IntMap *, const int )
{
}
#endif
#ifdef TCL
void AvConfigInfo::setBoolean(const char *var, const int value,
const int def)
{
if(def)
def_.setBooleanVal(makeIndex(var), value);
else
set_.setBooleanVal(makeIndex(var), value);
}
#else
void AvConfigInfo::setBoolean(const char *, const int, const int )
{
}
#endif
#ifdef TCL
void AvConfigInfo::setString(const char *var, AvString &value,
const int def)
{
if(def)
def_.setVar(makeIndex(var), value);
else
set_.setVar(makeIndex(var), value);
}
void AvConfigInfo::setString(const char *var, const char *value,
const int def)
{
if(def)
def_.setVar(makeIndex(var), value);
else
set_.setVar(makeIndex(var), value);
}
#else
void AvConfigInfo::setString(const char *, AvString &, const int )
{
}
void AvConfigInfo::setString(const char *, const char *, const int )
{
}
#endif
// Returns a pointer to the string that matches val. Returns map[0].name
// if no value matches. Returns NULL if map is NULL.
// (Note that the search starts at map[0] in case the 'default' value is
// also a valid selection.
const char *AvConfigInfo::intToString(const int val, const IntMap *map)
{
if(map == NULL)
return NULL;
const IntMap *ptr = map;
while(ptr->name != NULL)
if(val == ptr->value)
return ptr->name;
else
ptr += 1;
return map[0].name;
}
// Returns the value corresponding to name or map[0].value if none matches
// or name is NULL. Returns 0 if map is NULL. If the name wasn't found
// sets value to map[0].value and returns 0.
// Otherwise, returns 1.
int AvConfigInfo::stringToInt(const char *name, int &value, const IntMap *map)
{
if(map == NULL)
return 0;
if(name == NULL)
{ value = map[0].value;
return 0;
}
const IntMap *ptr = map;
while(ptr->name != NULL)
if(strcmp(name, ptr->name) == 0)
{ value = ptr->value;
return 1;
}
else
ptr += 1;
value = map[0].value;
return 0;
}
void AvConfigInfo::setMappedInt(const char *var, const int value,
const IntMap *map, const int def)
{
const char *str = intToString(value, map);
if(str == NULL)
return;
setString(var, str, def);
}
// Convert a list of mapped ints into a TCL style list.
// ( { <elem1> ... <elemn> } ).
AvString AvConfigInfo::buildMappedIntList(const AvIPosition &list,
const IntMap *map)
{ AvString s = "";
if(map == NULL)
return s;
s = "{";
int len = list.nelements();
for(int i=0; i < len; i++)
{ s += " ";
s += intToString(list(i), map);
}
s += "}";
return s;
}
/////////////////////////////////////////////////////////////////////////
//////////////// Write current values to output file. ////////////
/////////////////////////////////////////////////////////////////////////
#if 0
#ifdef TCL
// Test routine called from the debugger.
static void pa(const char *array, AvTcl &tcl)
{int length;
AvString id, var;
length = tcl.arraySize(array);
cout << array << " has " << length << " elements\n";
const char *elem;
for(id = tcl.arrayStartSearch(array); tcl.arrayAnymore(array, id);)
{
var = array;
var += "(";
elem = tcl.nextElement(array, id);
var += elem;
var += ")";
cout << var << " " << tcl.getVar(var.chars()) << endl;
}
tcl.arrayDoneSearch(array, id);
}
#endif
#endif
// Logs varName as a variable that will need to be updated when
// saveTo() is called.
#ifdef TCL
void AvConfigInfo::rememberVar(const char *varName, AvTcl &interp)
{
if(varName == NULL)
return;
remember_.setValue(varName, &interp);
}
#endif
#ifdef TCL
// Remember the elements of an array.
void AvConfigInfo::rememberArray(const char *array, AvTcl &tcl)
{ AvString id, var;
const char *elem;
for(id = tcl.arrayStartSearch(array); tcl.arrayAnymore(array, id);)
{
var = array;
var += "(";
elem = tcl.nextElement(array, id);
var += elem;
var += ")";
rememberVar(var.chars(), tcl);
}
tcl.arrayDoneSearch(array, id);
}
// Remember all the variables for an interpreter. (Except the ones to 'forget')
// If all is false, all variables for file fn are remembered.
// If not, all variables for all files are remembered.
void AvConfigInfo::rememberAll(AvTcl &interp, const char *fn, const int all)
{
// Pick up a list of the interpreter's variables.
int nvars;
char **vars;
interp.eval("info vars");
interp.splitList(interp.result(), nvars, &vars);
// For each variable, remember simple ones and arrays.
for(int i=0; i< nvars; i++)
{const char *var = vars[i];
if(forget_.findHashEntry(var) != NULL)
continue;
if(!all && (strcmp(fn, var) != 0))
continue;
if(interp.arraySize(var) == 0)
rememberVar(var, interp);
else
rememberArray(var, interp);
}
#if defined(sparc) && ! defined(__SVR4)
free((char *)vars);
#else
free(vars);
#endif
}
#endif
// Names in here aren't updated when save is called.
// (Used internally).
#ifdef TCL
void AvConfigInfo::forget(const char *name)
{ if(name == NULL)
return;
// All we remember is the name.
forget_.setValue(name, NULL);
}
// Put currently existing variables in the forget table so they
// don't get dumped on output.
void AvConfigInfo::forgetAll(AvTcl & tcl)
{int argc;
char **argv;
tcl.eval("info vars");
tcl.splitList(tcl.result(), argc, &argv);
for(int i=0; i< argc; i++)
forget(argv[i]);
#if defined(sparc) && ! defined(__SVR4)
free((char *)argv);
#else
free(argv);
#endif
}
#else
void AvConfigInfo::forget(const char *)
{
}
#endif
// Copy remembered variables to fileName. If fileName already exists,
// a backup is made and copied to the new version.
// A simple scan is made to look for lines like "set <remembered variable>...".
// When one is found, it is replaced by an updated set command.
#ifdef TCL
void AvConfigInfo::saveTo(const char *fileName, const char *dataFile)
{
// Pick up a copy of script if it exists.
AvString script = "";
AvString rc;
if(dataFile == NULL)
rc = defaultName();
else
{ rc = getRCName(dataFile); // Local version
if(!fileExists(rc)) // version with data file.
rc = getRCName(dataFile, TRUE);
}
// The rc_ interpreter is used below, but it could have been
// either of the others.
if(fileExists(rc))
{ rc_.varEval("open ", rc.chars());
AvString fileid = rc_.result(); // Copy fileID.
// Read in the script.
rc_.varEval("read ", fileid.chars());
script = rc_.result();
rc_.varEval("close ", fileid);
}
else
script = "";
if(fileExists(fileName))
{ if(rc_.varEval( "file rename -force ", fileName, " ",
fileName, ".bak")!= TCL_OK)
cerr << "AvConfigInfo::saveTo: Could not rename "
<< fileName << endl;
}
// Remove all variables in the forget table from the remember
// table.
Tcl_HashEntry *ef, *er;
Tcl_HashSearch searchF;
for(ef = forget_.firstHashEntry(&searchF); ef != NULL;
ef = forget_.nextHashEntry(&searchF))
{ char *key = forget_.getHashKey(ef);
er = remember_.findHashEntry(key);
remember_.deleteHashEntry(er);
}
// Write out the file.
fstream out;
AvString outName = expandPath(fileName);
if(outName.length() > 0)
{ out.open(outName, ios::out);
if(out.rdstate() == ios::goodbit)
{ writeScript(out, script);
writeVars(out); // Dump any remaining.
out.close();
}
}
}
#else
void AvConfigInfo::saveTo(const char *, const char *)
{
}
#endif
// Copy the tcl script to the output. Wherever a "set var" is detected,
// var is looked up in the remember table. If it is found, the current
// value is written instead. var is removed from the table so it won't
// be dumped later.
void AvConfigInfo::writeScript(fstream &out, AvString &script)
{
if(script.length() == 0) // Ignore if empty.
return;
// Set cregex bits the way we want them.
int old_syntax = obscure_syntax;
obscure_syntax = RE_NO_BK_PARENS | RE_NO_BK_VBAR ;
// Regular expression to detect (continuation) lines.
// (The "\\\\$" winds up as "\$" (backslash $, not quoted $).
// Register 1 will contain a continued line or register 2 will
// conain a non-continued line.
AvRegex reg("(^.*\\\\$)|(^.*$)");
const char *ptr = script.chars(), *endp = ptr+script.length();
AvString line;
while(ptr < endp)
{
if(reg.match(ptr, (int)(endp-ptr), 0) < 0)
break;
int start, length;
// If reg 1 matches, line is continued
// If reg 2 matches, line is finished, print and reset.
if(reg.match_info(start, length, 1)) // Line is continued.
{int startpos = (int)(ptr-script.chars()) + start;
length += 1; // Pick up the trailing char.
line += script.at(startpos, length);
}
else // Line ends
if(reg.match_info(start, length, 2))
{int startpos = (int)(ptr-script.chars()) + start;
length += 1; // Pick up the trailing newline.
line += script.at(startpos, length);
writeLine(out, line);
line = "";
}
ptr += length;