-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgpps.cpp
More file actions
225 lines (184 loc) · 5.32 KB
/
gpps.cpp
File metadata and controls
225 lines (184 loc) · 5.32 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
// GetPrivateProfileString() -- approximate implementation of
// Windows NT System Services version of GetPrivateProfileString()
// probably doesn't handle the NULL key for section name or value key
// correctly also, doesn't provide Microsoft backwards compatability
// wrt TAB characters in the value string -- Microsoft terminates value
// at the first TAB, but I couldn't discover what the behavior should
// be regarding TABS in quoted strings so, I treat tabs like any other
// characters -- NO comments following value string separated by a TAB
// are allowed (that is an anachronism anyway)
// 20040111 mvh Fixes: defaults now work, allow DOS type files, case insensitive
// 20070330 mvh Change by Mark Pearson: also allow tabs instead of spaces around =
#include <stdio.h>
#ifdef UNIX
#include <unistd.h>
#endif
#include <sys/types.h>
#include <string.h>
#include "gpps.hpp"
#include "dicom.hpp"
DWORD
GetPrivateProfileString(char *theSection, // section name
char *theKey, // search key name
char *theDefault, // default value if not found
char *theReturnBuffer, // return valuse stored here
size_t theReturnBufferLength, // byte length of return buffer
char *theIniFileName) // pathname of ini file to search
{
FILE *aFile = theIniFileName ? fopen(theIniFileName, "r") : NULL;
size_t aLength = (theDefault == NULL) ? 0 : strlen(theDefault);
if(theReturnBufferLength == 0 || theReturnBuffer == NULL)
{
if(aFile)
{
fclose(aFile);
}
return 0;
}
if(aFile == NULL)
{
// no ini file specified, return the default
++aLength; // room for NULL char
aLength = theReturnBufferLength < aLength ?
theReturnBufferLength : aLength;
strncpy(theReturnBuffer, theDefault, aLength);
theReturnBuffer[aLength - 1] = '\0';
return aLength - 1;
}
char aLine[2048];
char *aValue;
char *aString;
size_t aLineLength;
size_t aReturnLength = 0;
BOOL aSectionFound = FALSE;
while(fgets(aLine, sizeof(aLine), aFile) != NULL)
{
aLineLength = strlen(aLine);
// strip final '\n'
if(aLineLength > 0 && aLine[aLineLength - 1] == '\n')
{
aLine[aLineLength - 1] = '\0';
aLineLength--;
}
// strip final '\r'
if(aLineLength > 0 && aLine[aLineLength - 1] == '\r')
{
aLine[aLineLength - 1] = '\0';
aLineLength--;
}
switch(*aLine)
{
case ' ': // blank line
case ';': // comment line
continue;
break;
case '[': // section marker
if(aString = strchr(aLine, ']'))
{
*aString = '\0';
// accept as matched if NULL key or exact match
if(!theSection || !stricmp(aLine + 1, theSection))
{
aSectionFound = TRUE;
}
}
break;
default:
// try to match value keys if in proper section
if(aSectionFound)
{
// try to match requested key
if(aString = aValue = strchr(aLine, '='))
{
*aValue = '\0';
++aValue;
// strip leading blanks in value field
while((*aValue == ' ' || *aValue == '\t') && aValue < aLine + sizeof(aLine))
{
*aValue++ = '\0';
}
if(aValue >= aLine + sizeof(aLine))
{
aValue = "";
}
}
else
{
aValue = "";
}
// strip trailing blanks from key
if(aString)
{
while(--aString >= aLine && (*aString == ' ' || *aString == '\t'))
{
*aString = '\0';
}
}
// see if key is matched
if(theKey == NULL || !stricmp(theKey, aLine))
{
// matched -- first, terminate value part
aLineLength = strlen(aValue);
// remove trailing blanks from aValue if any
aString = aValue + aLineLength - 1;
while(--aString > aValue && (*aString == ' ' || *aString == '\t'))
{
*aString = '\0';
--aLineLength;
}
// unquote value if quoted
if(aLineLength >= 2 && aValue[0] == '"' &&
aValue[aLineLength - 1] == '"')
{
// string quoted with double quotes
aValue[aLineLength - 1] = '\0';
++aValue;
aLineLength -= 2;
}
else
{
// single quotes allowed also...
if(aLineLength >= 2 && aValue[0] == '\'' &&
aValue[aLineLength - 1] == '\'')
{
aValue[aLineLength - 1] = '\0';
++aValue;
aLineLength -= 2;
}
}
// compute maximum length copyable
aLineLength = (aLineLength <
theReturnBufferLength - aReturnLength) ? aLineLength :
theReturnBufferLength - aReturnLength;
// do the copy to return buffer
if(aLineLength)
{
strncpy(&theReturnBuffer[aReturnLength],
aValue, aLineLength);
aReturnLength += aLineLength;
if(aReturnLength < theReturnBufferLength)
{
theReturnBuffer[aReturnLength] = '\0';
++aReturnLength;
}
}
}
}
break;
}
}
if(aFile)
{
fclose(aFile);
}
if (aReturnLength==0)
{
++aLength; // room for NULL char
aLength = theReturnBufferLength < aLength ?
theReturnBufferLength : aLength;
strncpy(theReturnBuffer, theDefault, aLength);
theReturnBuffer[aLength - 1] = '\0';
return aLength - 1;
}
return aReturnLength > 0 ? aReturnLength - 1 : 0;
}