]> git.saurik.com Git - wxWidgets.git/blob - src/iodbc/misc.c
New toolbar event processing
[wxWidgets.git] / src / iodbc / misc.c
1 /** miscellaneous functions
2
3 Copyright (C) 1995 by Ke Jin <kejin@empress.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 **/
15
16 #include <../iodbc/iodbc.h>
17
18 #include <../iodbc/isql.h>
19 #include <../iodbc/isqlext.h>
20
21 #include <stdio.h>
22
23 static int
24 upper_strneq(
25 char* s1,
26 char* s2,
27 int n )
28 {
29 int i;
30 char c1, c2;
31
32 for(i=1;i<n;i++)
33 {
34 c1 = s1[i];
35 c2 = s2[i];
36
37 if( c1 >= 'a' && c1 <= 'z' )
38 {
39 c1 += ('A' - 'a');
40 }
41 else if( c1 == '\n' )
42 {
43 c1 = '\0';
44 }
45
46 if( c2 >= 'a' && c2 <= 'z' )
47 {
48 c2 += ('A' - 'a');
49 }
50 else if( c2 == '\n' )
51 {
52 c2 = '\0';
53 }
54
55 if( (c1 - c2) || !c1 || !c2 )
56 {
57 break;
58 }
59 }
60
61 return (int)!(c1 - c2);
62 }
63
64 static char* /* return new position in input str */
65 readtoken(
66 char* istr, /* old position in input buf */
67 char* obuf ) /* token string ( if "\0", then finished ) */
68 {
69 for(; *istr && *istr != '\n' ; istr ++ )
70 {
71 char c, nx;
72
73 c = *(istr);
74
75 if( c == ' ' || c == '\t' )
76 {
77 continue;
78 }
79
80 nx = *(istr + 1);
81
82 *obuf = c;
83 obuf ++;
84
85 if( c == ';' || c == '=' )
86 {
87 istr ++;
88 break;
89 }
90
91 if( nx == ' ' || nx == '\t' || nx == ';' || nx == '=' )
92 {
93 istr ++;
94 break;
95 }
96 }
97
98 *obuf = '\0';
99
100 return istr;
101 }
102
103 #if !defined(WINDOWS) && !defined(WIN32) && !defined(OS2)
104 # include <pwd.h>
105 # define UNIX_PWD
106 #endif
107
108 static char*
109 getinitfile(char* buf, int size)
110 {
111 int i, j;
112 char* ptr;
113
114 j = STRLEN("/odbc.ini") + 1;
115
116 if( size < j )
117 {
118 return NULL;
119 }
120
121 #if !defined(UNIX_PWD)
122
123 i = GetWindowsDirectory((LPSTR)buf, size );
124
125 if( i == 0 || i > size - j )
126 {
127 return NULL;
128 }
129
130 sprintf( buf + i, "/odbc.ini");
131
132 return buf;
133 #else
134 ptr = (char*)getpwuid(getuid());
135
136 if( ptr == NULL )
137 {
138 return NULL;
139 }
140
141 ptr = ((struct passwd*)ptr)->pw_dir;
142
143 if( ptr == NULL || *ptr == '\0' )
144 {
145 ptr = "/home";
146 }
147
148 if( size < STRLEN(ptr) + j )
149 {
150 return NULL;
151 }
152
153 sprintf( buf, "%s%s", ptr, "/.odbc.ini");
154 /* i.e. searching ~/.odbc.ini */
155 #endif
156
157 return buf;
158 }
159
160 char* _iodbcdm_getkeyvalbydsn(
161 char* dsn,
162 int dsnlen,
163 char* keywd,
164 char* value,
165 int size )
166 /* read odbc init file to resolve the value of specified
167 * key from named or defaulted dsn section
168 */
169 {
170 char buf[1024];
171 char dsntk[SQL_MAX_DSN_LENGTH + 3] = { '[', '\0' };
172 char token[1024]; /* large enough */
173 FILE* file;
174 char pathbuf[1024];
175 char* path;
176
177 #define DSN_NOMATCH 0
178 #define DSN_NAMED 1
179 #define DSN_DEFAULT 2
180
181 int dsnid = DSN_NOMATCH;
182 int defaultdsn = DSN_NOMATCH;
183
184 if( dsn == NULL || *dsn == 0 )
185 {
186 dsn = "default";
187 dsnlen = STRLEN(dsn);
188 }
189
190 if( dsnlen == SQL_NTS )
191 {
192 dsnlen = STRLEN(dsn);
193 }
194
195 if( dsnlen <= 0 || keywd == NULL || buf == 0 || size <= 0 )
196 {
197 return NULL;
198 }
199
200 if( dsnlen > sizeof(dsntk) - 2 )
201 {
202 return NULL;
203 }
204
205 value[0] = '\0';
206
207 STRNCAT( dsntk, dsn, dsnlen );
208 STRCAT( dsntk, "]" );
209
210 dsnlen = dsnlen + 2;
211
212 path = getinitfile(pathbuf, sizeof(pathbuf));
213
214 if( path == NULL )
215 {
216 return NULL;
217 }
218
219 file = (FILE*)fopen(path, "r");
220
221 if( file == NULL )
222 {
223 return NULL;
224 }
225
226 for(;;)
227 {
228 char* str;
229
230 str = fgets(buf, sizeof(buf), file);
231
232 if( str == NULL )
233 {
234 break;
235 }
236
237 if( *str == '[' )
238 {
239 if( upper_strneq(str, "[default]", STRLEN("[default]")) )
240 {
241 /* we only read first dsn default dsn
242 * section (as well as named dsn).
243 */
244 if( defaultdsn == DSN_NOMATCH )
245 {
246 dsnid = DSN_DEFAULT;
247 defaultdsn = DSN_DEFAULT;
248 }
249 else
250 {
251 dsnid = DSN_NOMATCH;
252 }
253
254 continue;
255 }
256 else if( upper_strneq( str, dsntk, dsnlen ) )
257 {
258 dsnid = DSN_NAMED;
259 }
260 else
261 {
262 dsnid = DSN_NOMATCH;
263 }
264
265 continue;
266 }
267 else if( dsnid == DSN_NOMATCH )
268 {
269 continue;
270 }
271
272 str = readtoken(str, token);
273
274 if( upper_strneq( keywd, token, STRLEN(keywd)) )
275 {
276 str = readtoken(str, token);
277
278 if( ! STREQ( token, "=") )
279 /* something other than = */
280 {
281 continue;
282 }
283
284 str = readtoken(str, token);
285
286 if( STRLEN(token) > size - 1)
287 {
288 break;
289 }
290
291 STRNCPY(value, token, size);
292 /* copy the value(i.e. next token) to buf */
293
294 if( dsnid != DSN_DEFAULT )
295 {
296 break;
297 }
298 }
299 }
300
301 fclose(file);
302
303 return (*value)? value:NULL;
304 }
305
306 char* _iodbcdm_getkeyvalinstr(
307 char* cnstr,
308 int cnlen,
309 char* keywd,
310 char* value,
311 int size )
312 {
313 char token[1024] = { '\0' };
314 int flag = 0;
315
316 if( cnstr == NULL || value == NULL
317 || keywd == NULL || size < 1 )
318 {
319 return NULL;
320 }
321
322 if( cnlen == SQL_NTS )
323 {
324 cnlen = STRLEN (cnstr);
325 }
326
327 if( cnlen <= 0 )
328 {
329 return NULL;
330 }
331
332 for(;;)
333 {
334 cnstr = readtoken(cnstr, token);
335
336 if( *token == '\0' )
337 {
338 break;
339 }
340
341 if( STREQ( token, ";" ) )
342 {
343 flag = 0;
344 continue;
345 }
346
347 switch(flag)
348 {
349 case 0:
350 if( upper_strneq(token, keywd, strlen(keywd)) )
351 {
352 flag = 1;
353 }
354 break;
355
356 case 1:
357 if( STREQ( token, "=" ) )
358 {
359 flag = 2;
360 }
361 break;
362
363 case 2:
364 if( size < strlen(token) + 1 )
365 {
366 return NULL;
367 }
368
369 STRNCPY( value, token, size );
370
371 return value;
372
373 default:
374 break;
375 }
376 }
377
378 return NULL;
379 }