-
Notifications
You must be signed in to change notification settings - Fork 12
/
ini.c
188 lines (178 loc) · 4.25 KB
/
ini.c
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
/* Digger Remastered
Copyright (c) Andrew Jenner 1998-2004 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include "def.h"
#define NEWL "\r\n"
/* Get a line from a buffer. This should be compatible with all 3 text file
formats: DOS, Unix and Mac. */
static char *sgets(char *buffer,char *s)
{
int i;
for (i=0;buffer[i]!=10 && buffer[i]!=13 && buffer[i]!=0;i++)
s[i]=buffer[i];
s[i]=0;
if (buffer[i]==13)
i++;
if (buffer[i]==10)
i++;
return buffer+i;
}
/* These are re-implementations of the Windows version of INI filing. */
void WriteINIString(const char *section,const char *key,const char *value,const char *filename)
{
FILE *fp;
char *buffer,*p,*p0,s1[80],s2[80],s3[80];
int tl;
fp=fopen(filename,"rb");
if (fp==NULL) {
goto do_write;
}
fseek(fp,0,2);
tl=(int)ftell(fp);
if (tl < 0) {
goto out_0;
}
fseek(fp,0,0);
buffer=(char *)malloc(tl+1);
if (buffer==NULL) {
goto out_0;
}
if (fread(buffer, tl, 1, fp) <= 0) {
fprintf(stderr, "short read, recreating file: %s\n", filename);
fclose(fp);
free(buffer);
goto do_write;
}
buffer[tl]=0;
fclose(fp);
fp = NULL;
strcpy(s2,"[");
strcat(s2,section);
strcat(s2,"]");
strcpy(s3,key);
strcat(s3,"=");
p=buffer;
do {
p=sgets(p,s1);
if (stricmp(s1,s2)==0) {
do {
p0=p;
p=sgets(p,s1);
if (strnicmp(s1,s3,strlen(s3))==0) {
fp=fopen(filename,"wb");
if (fp==NULL) {
goto out_1;
}
fwrite(buffer,p0-buffer,1,fp);
fprintf(fp,"%s=%s" NEWL,key,value);
fwrite(p,tl-(p-buffer),1,fp);
goto out_1;
}
} while (s1[0]!=0);
fp=fopen(filename,"wb");
if (fp==NULL) {
goto out_1;
}
fwrite(buffer,p0-buffer,1,fp);
fprintf(fp,"%s=%s" NEWL,key,value);
fwrite(p0,tl-(p0-buffer),1,fp);
goto out_1;
}
} while (p<buffer+tl);
fp=fopen(filename,"wb");
if (fp==NULL) {
goto out_1;
}
fprintf(fp,"[%s]" NEWL,section);
fprintf(fp,"%s=%s" NEWL NEWL,key,value);
fwrite(buffer,tl,1,fp);
out_1:
free(buffer);
out_0:
if (fp != NULL) {
fclose(fp);
}
return;
do_write:
fp = fopen(filename, "wb");
if (fp == NULL)
return;
fprintf(fp, "[%s]" NEWL, section);
fprintf(fp, "%s=%s" NEWL NEWL, key, value);
fclose(fp);
return;
}
void GetINIString(const char *section,const char*key,const char*def,char*dest,
int destsize,const char *filename)
{
FILE *fp;
char s1[80],s2[80],s3[80];
/* FIXME: no sense in copying from to the same destination as source,
* figure out what is really attempted here
*/
if (dest != def)
strcpy(dest,def);
fp=fopen(filename,"rb");
if (fp==NULL)
return;
strcpy(s2,"[");
strcat(s2,section);
strcat(s2,"]");
strcpy(s3,key);
strcat(s3,"=");
do {
if (fgets(s1, 80, fp) == NULL) {
fprintf(stderr, "GetINIString: read failed: %s\n", filename);
goto out_0;
}
sgets(s1,s1);
if (stricmp(s1,s2)==0) {
do {
if (fgets(s1, 80, fp) == NULL) {
fprintf(stderr, "GetINIString: read failed: %s\n", filename);
goto out_0;
}
sgets(s1,s1);
if (strnicmp(s1,s3,strlen(s3))==0) {
strcpy(dest,s1+strlen(s3));
goto out_0;
}
} while (s1[0]!=0 && !feof(fp) && !ferror(fp));
}
} while (!feof(fp) && !ferror(fp));
out_0:
fclose(fp);
}
int32_t GetINIInt(const char *section,const char*key,int32_t def,const char*filename)
{
char buf[80];
sprintf(buf,"%i",def);
GetINIString(section,key,buf,buf,80,filename);
return atol(buf);
}
void WriteINIIntconst (char *section,const char*key,int32_t value,
char *filename)
{
char buf[80];
sprintf(buf,"%i",value);
WriteINIString(section,key,buf,filename);
}
bool GetINIBool(const char *section,const char*key,bool def,const char*filename)
{
char buf[80];
sprintf(buf,"%i",def);
GetINIString(section,key,buf,buf,80,filename);
strupr(buf);
if (buf[0]=='T')
return true;
else
return atoi(buf);
}
void WriteINIBool(const char *section,const char*key,bool value,
const char *filename)
{
WriteINIString(section,key,value ? "True" : "False",filename);
}