-
Notifications
You must be signed in to change notification settings - Fork 2
/
ini.c
170 lines (161 loc) · 3.63 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "def.h"
#ifdef WIN32
#include "win_dig.h"
#endif
#define NEWL "\r\n"
/* Get a line from a buffer. This should be compatible with all 3 text file
formats: DOS, Unix and Mac. */
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(char *section,char *key,char *value,char *filename)
{
FILE *fp;
char *buffer,*p,*p0,s1[80],s2[80],s3[80];
int tl;
fp=fopen(filename,"rb");
if (fp==NULL) {
fp=fopen(filename,"wb");
if (fp==NULL)
return;
fprintf(fp,"[%s]" NEWL,section);
fprintf(fp,"%s=%s" NEWL NEWL,key,value);
fclose(fp);
return;
}
fseek(fp,0,2);
tl=(int)ftell(fp);
fseek(fp,0,0);
buffer=(char *)malloc(tl+1);
if (buffer==NULL) {
fclose(fp);
return;
}
fread(buffer,tl,1,fp);
buffer[tl]=0;
fclose(fp);
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) {
free(buffer);
return;
}
fwrite(buffer,p0-buffer,1,fp);
fprintf(fp,"%s=%s" NEWL,key,value);
fwrite(p,tl-(p-buffer),1,fp);
fclose(fp);
free(buffer);
return;
}
} while (s1[0]!=0);
fp=fopen(filename,"wb");
if (fp==NULL) {
free(buffer);
return;
}
fwrite(buffer,p0-buffer,1,fp);
fprintf(fp,"%s=%s" NEWL,key,value);
fwrite(p0,tl-(p0-buffer),1,fp);
fclose(fp);
free(buffer);
return;
}
} while (p<buffer+tl);
fp=fopen(filename,"wb");
if (fp==NULL) {
free(buffer);
return;
}
fprintf(fp,"[%s]" NEWL,section);
fprintf(fp,"%s=%s" NEWL NEWL,key,value);
fwrite(buffer,tl,1,fp);
fclose(fp);
free(buffer);
return;
}
void GetINIString(char *section,char *key,char *def,char *dest,
int destsize,char *filename)
{
FILE *fp;
char s1[80],s2[80],s3[80];
strcpy(dest,def);
fp=fopen(filename,"rb");
if (fp==NULL)
return;
strcpy(s2,"[");
strcat(s2,section);
strcat(s2,"]");
strcpy(s3,key);
strcat(s3,"=");
do {
fgets(s1,80,fp);
sgets(s1,s1);
if (stricmp(s1,s2)==0) {
do {
fgets(s1,80,fp);
sgets(s1,s1);
if (strnicmp(s1,s3,strlen(s3))==0) {
strcpy(dest,s1+strlen(s3));
fclose(fp);
return;
}
} while (s1[0]!=0 && !feof(fp) && !ferror(fp));
}
} while (!feof(fp) && !ferror(fp));
fclose(fp);
}
Sint5 GetINIInt(char *section,char *key,Sint5 def,char *filename)
{
char buf[80];
sprintf(buf,"%li",def);
GetINIString(section,key,buf,buf,80,filename);
return atol(buf);
}
void WriteINIInt(char *section,char *key,Sint5 value,
char *filename)
{
char buf[80];
sprintf(buf,"%li",value);
WriteINIString(section,key,buf,filename);
}
bool GetINIBool(char *section,char *key,bool def,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(char *section,char *key,bool value,
char *filename)
{
WriteINIString(section,key,value ? "True" : "False",filename);
}