/
inihandl.cpp
1407 lines (1096 loc) · 39.7 KB
/
inihandl.cpp
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
/*
* Profile functions
*
* Copyright 1993 Miguel de Icaza
* Copyright 1996 Alexandre Julliard
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "my_global.h"
#include <ctype.h>
//#include <errno.h>
#include <fcntl.h>
//#include <io.h> commented this line out to compile for solaris
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/stat.h>
//#include <sys/types.h>
//#include <memory.h>
#include "osutil.h"
#include "global.h"
#include "inihandl.h"
// The types and variables used locally
//typedef int bool;
typedef unsigned int uint;
//#define SVP(S) ((S) ? S : "<null>")
#define _strlwr(P) strlwr(P) //OB: changed this line
#define MAX_PATHNAME_LEN 256
#define N_CACHED_PROFILES 10
#ifndef WIN32
#define stricmp strcasecmp
#define _strnicmp strncasecmp
#endif // !WIN32
#define EnterCriticalSection(x)
#define LeaveCriticalSection(x)
#if defined(TEST_MODULE)
// Stand alone test program
#include <stdarg.h>
int trace = 0;
void htrc(char const *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
vfprintf(stderr, fmt, ap);
va_end (ap);
} /* end of htrc */
#else // !TEST_MODULE
// Normal included functions
//extern int trace;
//void htrc(char const *fmt, ...);
#endif // !TEST MODULE
typedef struct tagPROFILEKEY {
char *value;
struct tagPROFILEKEY *next;
char name[1];
} PROFILEKEY;
typedef struct tagPROFILESECTION {
struct tagPROFILEKEY *key;
struct tagPROFILESECTION *next;
char name[1];
} PROFILESECTION;
typedef struct {
BOOL changed;
PROFILESECTION *section;
//char *dos_name;
//char *unix_name;
char *filename;
time_t mtime;
} PROFILE;
#define memfree(P) if (P) free(P)
/* Cached profile files */
static PROFILE *MRUProfile[N_CACHED_PROFILES] = {NULL};
#define CurProfile (MRUProfile[0])
/* wine.ini config file registry root */
//static HKEY wine_profile_key;
#define PROFILE_MAX_LINE_LEN 1024
/* Wine profile name in $HOME directory; must begin with slash */
//static const char PROFILE_WineIniName[] = "/.winerc";
/* Wine profile: the profile file being used */
//static char PROFILE_WineIniUsed[MAX_PATHNAME_LEN] = "";
/* Check for comments in profile */
#define IS_ENTRY_COMMENT(str) ((str)[0] == ';')
//static const WCHAR wininiW[] = { 'w','i','n','.','i','n','i',0 };
//static CRITICAL_SECTION PROFILE_CritSect = CRITICAL_SECTION_INIT("PROFILE_CritSect");
static const char hex[17] = "0123456789ABCDEF";
BOOL WritePrivateProfileString(LPCSTR section, LPCSTR entry,
LPCSTR string, LPCSTR filename);
/***********************************************************************
* PROFILE_CopyEntry
*
* Copy the content of an entry into a buffer, removing quotes,
* and possibly translating environment variables.
***********************************************************************/
static void PROFILE_CopyEntry( char *buffer, const char *value, uint len,
int handle_env )
{
const char *p;
char quote = '\0';
if (!buffer)
return;
if ((*value == '\'') || (*value == '\"'))
if (value[1] && (value[strlen(value)-1] == *value))
quote = *value++;
if (!handle_env) {
strncpy(buffer, value, len);
if (quote && (len >= strlen(value)))
buffer[strlen(buffer)-1] = '\0';
return;
} // endif handle
for (p = value; (*p && (len > 1)); *buffer++ = *p++, len--) {
if ((*p == '$') && (p[1] == '{')) {
char env_val[1024];
const char *env_p;
const char *p2 = strchr(p, '}');
if (!p2)
continue; /* ignore it */
strncpy(env_val, p + 2, MY_MIN((int) sizeof(env_val), (int)(p2-p)-1));
if ((env_p = getenv(env_val)) != NULL) {
int buffer_len;
strncpy( buffer, env_p, len );
buffer_len = strlen( buffer );
buffer += buffer_len;
len -= buffer_len;
} // endif env_p
p = p2 + 1;
} // endif p
} // endfor p
if (quote && (len > 1))
buffer--;
*buffer = '\0';
} // end of PROFILE_CopyEntry
/***********************************************************************
* PROFILE_Save
*
* Save a profile tree to a file.
***********************************************************************/
static void PROFILE_Save( FILE *file, PROFILESECTION *section )
{
PROFILEKEY *key;
int secno;
for (secno= 0; section; section= section->next) {
if (section->name[0]) {
fprintf(file, "%s[%s]\n", secno ? "\n" : "", SVP(section->name));
secno++;
}
for (key = section->key; key; key = key->next)
if (key->name && key->name[0]) {
fprintf(file, "%s", SVP(key->name));
if (key->value)
fprintf(file, "=%s", SVP(key->value));
fprintf(file, "\n");
} // endif key->name
} // endfor section
} // end of PROFILE_Save
/***********************************************************************
* PROFILE_Free
*
* Free a profile tree.
***********************************************************************/
static void PROFILE_Free( PROFILESECTION *section )
{
PROFILESECTION *next_section;
PROFILEKEY *key, *next_key;
for (; section; section = next_section) {
for (key = section->key; key; key = next_key) {
next_key = key->next;
memfree(key->value);
free(key);
} // endfor key
next_section = section->next;
free(section);
} // endfor section
} // end of PROFILE_Free
static int PROFILE_isspace(char c)
{
/* CR and ^Z (DOS EOF) are spaces too (found on CD-ROMs) */
if (isspace(c) || c=='\r' || c==0x1a)
return 1;
return 0;
} // end of PROFILE_isspace
/***********************************************************************
* PROFILE_Load
*
* Load a profile tree from a file.
***********************************************************************/
static PROFILESECTION *PROFILE_Load( FILE *file )
{
char buffer[PROFILE_MAX_LINE_LEN];
char *p, *p2;
int line = 0;
PROFILESECTION *section, *first_section;
PROFILESECTION* *next_section;
PROFILEKEY *key, *prev_key, **next_key;
first_section = (PROFILESECTION*)malloc(sizeof(*section));
if (first_section == NULL)
return NULL;
first_section->name[0] = 0;
first_section->key = NULL;
first_section->next = NULL;
next_section = &first_section->next;
next_key = &first_section->key;
prev_key = NULL;
while (fgets(buffer, PROFILE_MAX_LINE_LEN, file)) {
line++;
p = buffer;
while (*p && PROFILE_isspace(*p))
p++;
if (*p == '[') { /* section start */
if (!(p2 = strrchr( p, ']'))) {
fprintf(stderr, "Invalid section header at line %d: '%s'\n",
line, p);
} else {
*p2 = '\0';
p++;
if (!(section = (PROFILESECTION*)malloc(sizeof(*section) + strlen(p))))
break;
strcpy(section->name, p);
section->key = NULL;
section->next = NULL;
*next_section = section;
next_section = §ion->next;
next_key = §ion->key;
prev_key = NULL;
if (trace > 1)
htrc("New section: '%s'\n",section->name);
continue;
} // endif p2
} // endif p
p2 = p + strlen(p) - 1;
while ((p2 > p) && ((*p2 == '\n') || PROFILE_isspace(*p2)))
*p2-- = '\0';
if ((p2 = strchr(p, '=')) != NULL) {
char *p3 = p2 - 1;
while ((p3 > p) && PROFILE_isspace(*p3))
*p3-- = '\0';
*p2++ = '\0';
while (*p2 && PROFILE_isspace(*p2))
p2++;
} // endif p2
if (*p || !prev_key || *prev_key->name) {
if (!(key = (PROFILEKEY*)malloc(sizeof(*key) + strlen(p))))
break;
strcpy(key->name, p);
if (p2) {
key->value = (char*)malloc(strlen(p2)+1);
strcpy(key->value, p2);
} else
key->value = NULL;
key->next = NULL;
*next_key = key;
next_key = &key->next;
prev_key = key;
if (trace > 1)
htrc("New key: name='%s', value='%s'\n",
key->name,key->value?key->value:"(none)");
} // endif p || prev_key
} // endif *p
return first_section;
} // end of PROFILE_Load
/***********************************************************************
* PROFILE_FlushFile
*
* Flush the current profile to disk if changed.
***********************************************************************/
static BOOL PROFILE_FlushFile(void)
{
//char *p, buffer[MAX_PATHNAME_LEN];
//const char *unix_name;
FILE *file = NULL;
struct stat buf;
if (trace > 1)
htrc("PROFILE_FlushFile: CurProfile=%p\n", CurProfile);
if (!CurProfile) {
fprintf(stderr, "No current profile!\n");
return FALSE;
} // endif !CurProfile
if (!CurProfile->changed || !CurProfile->filename)
return TRUE;
#if 0
if (!(file = fopen(unix_name, "w"))) {
/* Try to create it in $HOME/.wine */
/* FIXME: this will need a more general solution */
//strcpy( buffer, get_config_dir() );
//p = buffer + strlen(buffer);
//*p++ = '/';
char *p1 = strrchr(CurProfile->filename, '\\');
p = buffer; // OB: To be elaborate
if (p1)
p1++;
else
p1 = CurProfile->dos_name;
strcpy(p, p1);
_strlwr(p);
file = fopen(buffer, "w");
unix_name = buffer;
} // endif !unix_name
#endif // 0
if (!(file = fopen(CurProfile->filename, "w"))) {
fprintf(stderr, "could not save profile file %s\n", CurProfile->filename);
return FALSE;
} // endif !file
if (trace > 1)
htrc("Saving '%s'\n", CurProfile->filename);
PROFILE_Save(file, CurProfile->section);
fclose(file);
CurProfile->changed = FALSE;
if (!stat(CurProfile->filename, &buf))
CurProfile->mtime = buf.st_mtime;
return TRUE;
} // end of PROFILE_FlushFile
/***********************************************************************
* PROFILE_ReleaseFile
*
* Flush the current profile to disk and remove it from the cache.
***********************************************************************/
static void PROFILE_ReleaseFile(void)
{
PROFILE_FlushFile();
PROFILE_Free(CurProfile->section);
//memfree(CurProfile->dos_name);
//memfree(CurProfile->unix_name);
memfree(CurProfile->filename);
CurProfile->changed = FALSE;
CurProfile->section = NULL;
//CurProfile->dos_name = NULL;
//CurProfile->unix_name = NULL;
CurProfile->filename = NULL;
CurProfile->mtime = 0;
} // end of PROFILE_ReleaseFile
/***********************************************************************
* PROFILE_Open
*
* Open a profile file, checking the cached file first.
***********************************************************************/
static BOOL PROFILE_Open(LPCSTR filename)
{
//char buffer[MAX_PATHNAME_LEN];
//char *p;
FILE *file = NULL;
int i, j;
struct stat buf;
PROFILE *tempProfile;
if (trace > 1)
htrc("PROFILE_Open: CurProfile=%p N=%d\n", CurProfile, N_CACHED_PROFILES);
/* First time around */
if (!CurProfile)
for (i = 0; i < N_CACHED_PROFILES; i++) {
MRUProfile[i] = (PROFILE*)malloc(sizeof(PROFILE));
if (MRUProfile[i] == NULL)
break;
MRUProfile[i]->changed=FALSE;
MRUProfile[i]->section=NULL;
// MRUProfile[i]->dos_name=NULL;
// MRUProfile[i]->unix_name=NULL;
MRUProfile[i]->filename=NULL;
MRUProfile[i]->mtime=0;
} // endfor i
/* Check for a match */
for (i = 0; i < N_CACHED_PROFILES; i++) {
if (trace > 1)
htrc("MRU=%s i=%d\n", SVP(MRUProfile[i]->filename), i);
if (MRUProfile[i]->filename && !strcmp(filename, MRUProfile[i]->filename)) {
if (i) {
PROFILE_FlushFile();
tempProfile = MRUProfile[i];
for (j = i; j > 0; j--)
MRUProfile[j] = MRUProfile[j-1];
CurProfile=tempProfile;
} // endif i
if (!stat(CurProfile->filename, &buf) && CurProfile->mtime == buf.st_mtime) {
if (trace > 1)
htrc("(%s): already opened (mru=%d)\n", filename, i);
} else {
if (trace > 1)
htrc("(%s): already opened, needs refreshing (mru=%d)\n", filename, i);
} // endif stat
return TRUE;
} // endif filename
} // endfor i
/* Flush the old current profile */
PROFILE_FlushFile();
/* Make the oldest profile the current one only in order to get rid of it */
if (i == N_CACHED_PROFILES) {
tempProfile = MRUProfile[N_CACHED_PROFILES-1];
for(i = N_CACHED_PROFILES-1; i > 0; i--)
MRUProfile[i] = MRUProfile[i-1];
CurProfile = tempProfile;
} // endif i
if (CurProfile->filename)
PROFILE_ReleaseFile();
/* OK, now that CurProfile is definitely free we assign it our new file */
// newdos_name = HeapAlloc( GetProcessHeap(), 0, strlen(full_name.short_name)+1 );
// strcpy( newdos_name, full_name.short_name );
// newdos_name = malloc(strlen(filename)+1);
// strcpy(newdos_name, filename);
// CurProfile->dos_name = newdos_name;
CurProfile->filename = (char*)malloc(strlen(filename) + 1);
strcpy(CurProfile->filename, filename);
/* Try to open the profile file, first in $HOME/.wine */
/* FIXME: this will need a more general solution */
// strcpy( buffer, get_config_dir() );
// p = buffer + strlen(buffer);
// *p++ = '/';
// strcpy( p, strrchr( newdos_name, '\\' ) + 1 );
// p = buffer;
// strcpy(p, filename);
// _strlwr(p);
if (trace > 1)
htrc("Opening %s\n", filename);
if ((file = fopen(filename, "r"))) {
if (trace > 1)
htrc("(%s): found it\n", filename);
// CurProfile->unix_name = malloc(strlen(buffer)+1);
// strcpy(CurProfile->unix_name, buffer);
} /* endif file */
if (file) {
CurProfile->section = PROFILE_Load(file);
fclose(file);
if (!stat(CurProfile->filename, &buf))
CurProfile->mtime = buf.st_mtime;
} else {
/* Does not exist yet, we will create it in PROFILE_FlushFile */
fprintf(stderr, "profile file %s not found\n", filename);
} /* endif file */
return TRUE;
}
/***********************************************************************
* PROFILE_Close
*
* Flush the named profile to disk and remove it from the cache.
***********************************************************************/
void PROFILE_Close(LPCSTR filename)
{
int i;
BOOL close = FALSE;
struct stat buf;
PROFILE *tempProfile;
if (trace > 1)
htrc("PROFILE_Close: CurProfile=%p N=%d\n", CurProfile, N_CACHED_PROFILES);
/* Check for a match */
for (i = 0; i < N_CACHED_PROFILES; i++) {
if (trace > 1)
htrc("MRU=%s i=%d\n", SVP(MRUProfile[i]->filename), i);
if (MRUProfile[i]->filename && !strcmp(filename, MRUProfile[i]->filename)) {
if (i) {
/* Make the profile to close current */
tempProfile = MRUProfile[i];
MRUProfile[i] = MRUProfile[0];
MRUProfile[0] = tempProfile;
CurProfile=tempProfile;
} // endif i
if (trace > 1) {
if (!stat(CurProfile->filename, &buf) && CurProfile->mtime == buf.st_mtime)
htrc("(%s): already opened (mru=%d)\n", filename, i);
else
htrc("(%s): already opened, needs refreshing (mru=%d)\n", filename, i);
} // endif trace
close = TRUE;
break;
} // endif filename
} // endfor i
if (close)
PROFILE_ReleaseFile();
} // end of PROFILE_Close
/***********************************************************************
* PROFILE_End
*
* Terminate and release the cache.
***********************************************************************/
void PROFILE_End(void)
{
int i;
if (trace)
htrc("PROFILE_End: CurProfile=%p N=%d\n", CurProfile, N_CACHED_PROFILES);
if (!CurProfile) // Sergey Vojtovich
return;
/* Close all opened files and free the cache structure */
for (i = 0; i < N_CACHED_PROFILES; i++) {
if (trace)
htrc("MRU=%s i=%d\n", SVP(MRUProfile[i]->filename), i);
// CurProfile = MRUProfile[i]; Sergey Vojtovich
// PROFILE_ReleaseFile(); see MDEV-9997
free(MRUProfile[i]);
} // endfor i
} // end of PROFILE_End
/***********************************************************************
* PROFILE_DeleteSection
*
* Delete a section from a profile tree.
***********************************************************************/
static BOOL PROFILE_DeleteSection(PROFILESECTION* *section, LPCSTR name)
{
while (*section) {
if ((*section)->name[0] && !stricmp((*section)->name, name)) {
PROFILESECTION *to_del = *section;
*section = to_del->next;
to_del->next = NULL;
PROFILE_Free(to_del);
return TRUE;
} // endif section
section = &(*section)->next;
} // endwhile section
return FALSE;
} // end of PROFILE_DeleteSection
/***********************************************************************
* PROFILE_DeleteKey
*
* Delete a key from a profile tree.
***********************************************************************/
static BOOL PROFILE_DeleteKey(PROFILESECTION* *section,
LPCSTR section_name, LPCSTR key_name)
{
while (*section) {
if ((*section)->name[0] && !stricmp((*section)->name, section_name)) {
PROFILEKEY* *key = &(*section)->key;
while (*key) {
if (!stricmp((*key)->name, key_name)) {
PROFILEKEY *to_del = *key;
*key = to_del->next;
memfree(to_del->value);
free(to_del);
return TRUE;
} // endif name
key = &(*key)->next;
} // endwhile *key
} // endif section->name
section = &(*section)->next;
} // endwhile *section
return FALSE;
} // end of PROFILE_DeleteKey
/***********************************************************************
* PROFILE_DeleteAllKeys
*
* Delete all keys from a profile tree.
***********************************************************************/
static void PROFILE_DeleteAllKeys(LPCSTR section_name)
{
PROFILESECTION* *section= &CurProfile->section;
while (*section) {
if ((*section)->name[0] && !stricmp((*section)->name, section_name)) {
PROFILEKEY* *key = &(*section)->key;
while (*key) {
PROFILEKEY *to_del = *key;
*key = to_del->next;
memfree(to_del->value);
free(to_del);
CurProfile->changed = TRUE;
} // endwhile *key
} // endif section->name
section = &(*section)->next;
} // endwhile *section
} // end of PROFILE_DeleteAllKeys
/***********************************************************************
* PROFILE_Find
*
* Find a key in a profile tree, optionally creating it.
***********************************************************************/
static PROFILEKEY *PROFILE_Find(PROFILESECTION* *section,
const char *section_name,
const char *key_name,
BOOL create, BOOL create_always)
{
const char *p;
int seclen, keylen;
while (PROFILE_isspace(*section_name))
section_name++;
p = section_name + strlen(section_name) - 1;
while ((p > section_name) && PROFILE_isspace(*p))
p--;
seclen = p - section_name + 1;
while (PROFILE_isspace(*key_name))
key_name++;
p = key_name + strlen(key_name) - 1;
while ((p > key_name) && PROFILE_isspace(*p))
p--;
keylen = p - key_name + 1;
while (*section) {
if (((*section)->name[0])
&& (!(_strnicmp((*section)->name, section_name, seclen )))
&& (((*section)->name)[seclen] == '\0')) {
PROFILEKEY* *key = &(*section)->key;
while (*key) {
/* If create_always is FALSE then we check if the keyname already exists.
* Otherwise we add it regardless of its existence, to allow
* keys to be added more then once in some cases.
*/
if (!create_always) {
if ((!(_strnicmp( (*key)->name, key_name, keylen )))
&& (((*key)->name)[keylen] == '\0'))
return *key;
} // endif !create_always
key = &(*key)->next;
} // endwhile *key
if (!create)
return NULL;
if (!(*key = (PROFILEKEY*)malloc(sizeof(PROFILEKEY) + strlen(key_name))))
return NULL;
strcpy((*key)->name, key_name);
(*key)->value = NULL;
(*key)->next = NULL;
return *key;
} // endifsection->name
section = &(*section)->next;
} // endwhile *section
if (!create)
return NULL;
*section = (PROFILESECTION*)malloc(sizeof(PROFILESECTION) + strlen(section_name));
if (*section == NULL)
return NULL;
strcpy((*section)->name, section_name);
(*section)->next = NULL;
if (!((*section)->key = (tagPROFILEKEY*)malloc(sizeof(PROFILEKEY) + strlen(key_name)))) {
free(*section);
return NULL;
} // endif malloc
strcpy((*section)->key->name, key_name);
(*section)->key->value = NULL;
(*section)->key->next = NULL;
return (*section)->key;
} // end of PROFILE_Find
/***********************************************************************
* PROFILE_GetSection
*
* Returns all keys of a section.
* If return_values is TRUE, also include the corresponding values.
***********************************************************************/
static int PROFILE_GetSection(PROFILESECTION *section, LPCSTR section_name,
LPSTR buffer, uint len,
BOOL handle_env, BOOL return_values)
{
PROFILEKEY *key;
if(!buffer)
return 0;
while (section) {
if (section->name[0] && !stricmp(section->name, section_name)) {
uint oldlen = len;
for (key = section->key; key; key = key->next) {
if (len <= 2)
break;
if (!*key->name)
continue; /* Skip empty lines */
if (IS_ENTRY_COMMENT(key->name))
continue; /* Skip comments */
PROFILE_CopyEntry(buffer, key->name, len - 1, handle_env);
len -= strlen(buffer) + 1;
buffer += strlen(buffer) + 1;
if (len < 2)
break;
if (return_values && key->value) {
buffer[-1] = '=';
PROFILE_CopyEntry(buffer, key->value, len - 1, handle_env);
len -= strlen(buffer) + 1;
buffer += strlen(buffer) + 1;
} // endif return_values
} // endfor key
*buffer = '\0';
if (len <= 1) {
/*If either lpszSection or lpszKey is NULL and the supplied
destination buffer is too small to hold all the strings,
the last string is truncated and followed by two null characters.
In this case, the return value is equal to cchReturnBuffer
minus two. */
buffer[-1] = '\0';
return oldlen - 2;
} // endif len
return oldlen - len;
} // endif section->name
section = section->next;
} // endwhile section
buffer[0] = buffer[1] = '\0';
return 0;
} // end of PROFILE_GetSection
/* See GetPrivateProfileSectionNamesA for documentation */
static int PROFILE_GetSectionNames(LPSTR buffer, uint len)
{
LPSTR buf;
uint f,l;
PROFILESECTION *section;
if (trace > 1)
htrc("GetSectionNames: buffer=%p len=%u\n", buffer, len);
if (!buffer || !len)
return 0;
if (len == 1) {
*buffer='\0';
return 0;
} // endif len
f = len - 1;
buf = buffer;
section = CurProfile->section;
if (trace > 1)
htrc("GetSectionNames: section=%p\n", section);
while (section != NULL) {
if (trace > 1)
htrc("section=%s\n", section->name);
if (section->name[0]) {
l = strlen(section->name) + 1;
if (trace > 1)
htrc("l=%u f=%u\n", l, f);
if (l > f) {
if (f > 0) {
strncpy(buf, section->name, f-1);
buf += f-1;
*buf++='\0';
} // endif f
*buf = '\0';
return len - 2;
} // endif l
strcpy(buf, section->name);
buf += l;
f -= l;
} // endif section->name
section = section->next;
} // endwhile section
*buf='\0';
return buf-buffer;
} // end of PROFILE_GetSectionNames
/***********************************************************************
* PROFILE_GetString
*
* Get a profile string.
*
* Tests with GetPrivateProfileString16, W95a,
* with filled buffer ("****...") and section "set1" and key_name "1" valid:
* section key_name def_val res buffer
* "set1" "1" "x" 43 [data]
* "set1" "1 " "x" 43 [data] (!)
* "set1" " 1 "' "x" 43 [data] (!)
* "set1" "" "x" 1 "x"
* "set1" "" "x " 1 "x" (!)
* "set1" "" " x " 3 " x" (!)
* "set1" NULL "x" 6 "1\02\03\0\0"
* "set1" "" "x" 1 "x"
* NULL "1" "x" 0 "" (!)
* "" "1" "x" 1 "x"
* NULL NULL "" 0 ""
*
*************************************************************************/
static int PROFILE_GetString(LPCSTR section, LPCSTR key_name,
LPCSTR def_val, LPSTR buffer, uint len)
{
PROFILEKEY *key = NULL;
if(!buffer)
return 0;
if (!def_val)
def_val = "";
if (key_name && key_name[0]) {
key = PROFILE_Find(&CurProfile->section, section, key_name, FALSE, FALSE);
PROFILE_CopyEntry(buffer, (key && key->value) ? key->value : def_val, len, FALSE);
if (trace > 1)
htrc("('%s','%s','%s'): returning '%s'\n",
section, key_name, def_val, buffer );
return strlen(buffer);
} // endif key_name
if (key_name && !(key_name[0]))
/* Win95 returns 0 on keyname "". Tested with Likse32 bon 000227 */
return 0;
if (section && section[0])
return PROFILE_GetSection(CurProfile->section, section, buffer, len,
FALSE, FALSE);
buffer[0] = '\0';
return 0;