]> git.saurik.com Git - wxWidgets.git/blame - src/iodbc/misc.c
added an error message if a bitmap can't be addedto the image list
[wxWidgets.git] / src / iodbc / misc.c
CommitLineData
1a6944fd 1/** miscellaneous functions
7e616b10
RR
2
3 Copyright (C) 1995 by Ke Jin <kejin@empress.com>
1a6944fd
RR
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
7e616b10
RR
16#ifdef DLDAPI_OS2
17# define INCL_DOSMODULEMGR /* Module Manager values */
18# define INCL_DOSERRORS /* Error values */
19# include <os2.h>
20# include <stdio.h>
21#endif
22
1a6944fd
RR
23#include <../iodbc/iodbc.h>
24
25#include <../iodbc/isql.h>
26#include <../iodbc/isqlext.h>
27
28#include <stdio.h>
bd7d06f2 29#include <strings.h>
1a6944fd 30
7e616b10
RR
31#include <unistd.h>
32
33
34static int
35upper_strneq(
36 char* s1,
37 char* s2,
38 int n )
1a6944fd 39{
7e616b10
RR
40 int i;
41 char c1, c2;
42
43 for(i=1;i<n;i++)
44 {
45 c1 = s1[i];
46 c2 = s2[i];
47
48 if( c1 >= 'a' && c1 <= 'z' )
49 {
50 c1 += ('A' - 'a');
51 }
52 else if( c1 == '\n' )
53 {
54 c1 = '\0';
55 }
56
57 if( c2 >= 'a' && c2 <= 'z' )
58 {
59 c2 += ('A' - 'a');
60 }
61 else if( c2 == '\n' )
62 {
63 c2 = '\0';
64 }
65
66 if( (c1 - c2) || !c1 || !c2 )
67 {
68 break;
69 }
70 }
71
72 return (int)!(c1 - c2);
1a6944fd
RR
73}
74
7e616b10
RR
75static char* /* return new position in input str */
76readtoken(
77 char* istr, /* old position in input buf */
78 char* obuf ) /* token string ( if "\0", then finished ) */
1a6944fd 79{
7e616b10
RR
80 for(; *istr && *istr != '\n' ; istr ++ )
81 {
82 char c, nx;
1a6944fd 83
7e616b10 84 c = *(istr);
1a6944fd 85
7e616b10
RR
86 if( c == ' ' || c == '\t' )
87 {
88 continue;
89 }
1a6944fd 90
7e616b10 91 nx = *(istr + 1);
1a6944fd 92
7e616b10
RR
93 *obuf = c;
94 obuf ++;
1a6944fd 95
7e616b10
RR
96 if( c == ';' || c == '=' )
97 {
98 istr ++;
99 break;
100 }
1a6944fd 101
7e616b10
RR
102 if( nx == ' ' || nx == '\t' || nx == ';' || nx == '=' )
103 {
104 istr ++;
105 break;
106 }
107 }
1a6944fd 108
7e616b10 109 *obuf = '\0';
1a6944fd 110
7e616b10 111 return istr;
1a6944fd
RR
112}
113
7e616b10
RR
114#if !defined(WINDOWS) && !defined(WIN32) && !defined(OS2)
115# include <pwd.h>
116# define UNIX_PWD
1a6944fd
RR
117#endif
118
119static char*
7e616b10 120getinitfile(char* buf, int size)
1a6944fd 121{
7e616b10
RR
122 int i, j;
123 char* ptr;
1a6944fd 124
f6fcbb63 125 j = STRLEN("/iodbc.ini") + 1;
1a6944fd 126
7e616b10
RR
127 if( size < j )
128 {
129 return NULL;
130 }
1a6944fd 131
7e616b10 132#ifdef FIX_INI_FILE
f6fcbb63 133 sprintf( buf, "%s/iodbc.ini", DIR_INI_FILE );
1a6944fd 134#else
7e616b10
RR
135# ifdef OS2
136 *buf = '\0';
f6fcbb63 137 if( NULL != getenv("IODBC_INI") )
7e616b10 138 {
f6fcbb63 139 strcpy( buf, getenv("IODBC_INI") );
7e616b10
RR
140 }
141 else
142 {
143 HMODULE hModule;
144
145 if( NO_ERROR == DosQueryModuleHandle(DLL_NAME, &hModule) &&
146 NO_ERROR == DosQueryModuleName(hModule, 256L, buf) )
147 {
148 if( NULL != strrchr(buf, '.') )
149 *(strchr(buf, '.')) = '\0';
150 strcat( buf, ".ini" );
151 }
152 else
153 {
f6fcbb63 154 strcpy( buf, "iodbc.ini" );
7e616b10
RR
155 }
156
157 }
158 return buf;
159# else
160# if !defined(UNIX_PWD)
161
162 i = GetWindowsDirectory((LPSTR)buf, size );
163
164 if( i == 0 || i > size - j )
165 {
166 return NULL;
167 }
168
f6fcbb63 169 sprintf( buf + i, "/iodbc.ini");
7e616b10
RR
170
171 return buf;
172# else
173 ptr = (char*)getpwuid(getuid());
174
175 if( ptr == NULL )
176 {
177 return NULL;
178 }
179
180 ptr = ((struct passwd*)ptr)->pw_dir;
181
182 if( ptr == NULL || *ptr == '\0' )
183 {
184 ptr = "/home";
185 }
186
187 if( size < STRLEN(ptr) + j )
188 {
189 return NULL;
190 }
191
f6fcbb63
RR
192 sprintf( buf, "%s%s", ptr, "/.iodbc.ini");
193 /* i.e. searching ~/.iodbc.ini */
7e616b10
RR
194# endif
195# endif
1a6944fd
RR
196#endif
197
7e616b10 198 return buf;
1a6944fd
RR
199}
200
7e616b10
RR
201char* _iodbcdm_getkeyvalbydsn(
202 char* dsn,
203 int dsnlen,
204 char* keywd,
205 char* value,
206 int size )
1a6944fd 207/* read odbc init file to resolve the value of specified
7e616b10 208 * key from named or defaulted dsn section
1a6944fd
RR
209 */
210{
7e616b10
RR
211 char buf[1024];
212 char dsntk[SQL_MAX_DSN_LENGTH + 3] = { '[', '\0' };
213 char token[1024]; /* large enough */
214 FILE* file;
215 char pathbuf[1024];
216 char* path;
217
218#define DSN_NOMATCH 0
219#define DSN_NAMED 1
220#define DSN_DEFAULT 2
221
222 int dsnid = DSN_NOMATCH;
223 int defaultdsn = DSN_NOMATCH;
224
225 if( dsn == NULL || *dsn == 0 )
226 {
227 dsn = "default";
228 dsnlen = STRLEN(dsn);
229 }
230
231 if( dsnlen == SQL_NTS )
232 {
233 dsnlen = STRLEN(dsn);
234 }
235
236 if( dsnlen <= 0 || keywd == NULL || buf == 0 || size <= 0 )
237 {
238 return NULL;
239 }
240
241 if( dsnlen > sizeof(dsntk) - 2 )
242 {
243 return NULL;
244 }
245
246 value[0] = '\0';
247
248 STRNCAT( dsntk, dsn, dsnlen );
249 STRCAT( dsntk, "]" );
250
251 dsnlen = dsnlen + 2;
252
253 path = getinitfile(pathbuf, sizeof(pathbuf));
254
255 if( path == NULL )
256 {
257 return NULL;
258 }
259
260 file = (FILE*)fopen(path, "r");
261
262 if( file == NULL )
263 {
264 return NULL;
265 }
266
267 for(;;)
268 {
269 char* str;
270
271 str = fgets(buf, sizeof(buf), file);
272
273 if( str == NULL )
274 {
275 break;
276 }
277
278 strtok( str, "\n\r" );
279
280 if( *str == '[' )
281 {
282 if( upper_strneq(str, "[default]", STRLEN("[default]")) )
283 {
284 /* we only read first dsn default dsn
285 * section (as well as named dsn).
286 */
287 if( defaultdsn == DSN_NOMATCH )
288 {
289 dsnid = DSN_DEFAULT;
290 defaultdsn = DSN_DEFAULT;
291 }
292 else
293 {
294 dsnid = DSN_NOMATCH;
295 }
296
297 continue;
298 }
299 else if( upper_strneq( str, dsntk, dsnlen ) )
300 {
301 dsnid = DSN_NAMED;
302 }
303 else
304 {
305 dsnid = DSN_NOMATCH;
306 }
307
308 continue;
309 }
310 else if( dsnid == DSN_NOMATCH )
311 {
312 continue;
313 }
314
315 str = readtoken(str, token);
316
317 if( upper_strneq( keywd, token, STRLEN(keywd)) )
318 {
319 str = readtoken(str, token);
320
321 if( ! STREQ( token, "=") )
322 /* something other than = */
323 {
324 continue;
325 }
326
327 str = readtoken(str, token);
328
329 if( STRLEN(token) > size - 1)
330 {
331 break;
332 }
333
334 STRNCPY(value, token, size);
335 /* copy the value(i.e. next token) to buf */
336
337 if( dsnid != DSN_DEFAULT )
338 {
339 break;
340 }
341 }
342 }
343
344 fclose(file);
345
346 return (*value)? value:NULL;
1a6944fd
RR
347}
348
7e616b10
RR
349char* _iodbcdm_getkeyvalinstr(
350 char* cnstr,
351 int cnlen,
352 char* keywd,
353 char* value,
354 int size )
1a6944fd 355{
7e616b10
RR
356 char token[1024] = { '\0' };
357 int flag = 0;
358
359 if( cnstr == NULL || value == NULL
360 || keywd == NULL || size < 1 )
361 {
362 return NULL;
363 }
364
365 if( cnlen == SQL_NTS )
366 {
367 cnlen = STRLEN (cnstr);
368 }
369
370 if( cnlen <= 0 )
371 {
372 return NULL;
373 }
374
375 for(;;)
376 {
377 cnstr = readtoken(cnstr, token);
378
379 if( *token == '\0' )
380 {
381 break;
382 }
383
384 if( STREQ( token, ";" ) )
385 {
386 flag = 0;
387 continue;
388 }
389
390 switch(flag)
391 {
392 case 0:
393 if( upper_strneq(token, keywd, strlen(keywd)) )
394 {
395 flag = 1;
396 }
397 break;
398
399 case 1:
400 if( STREQ( token, "=" ) )
401 {
402 flag = 2;
403 }
404 break;
405
406 case 2:
407 if( size < strlen(token) + 1 )
408 {
409 return NULL;
410 }
411
412 STRNCPY( value, token, size );
413
414 return value;
415
416 default:
417 break;
418 }
419 }
420
421 return NULL;
1a6944fd 422}