File: SQLGetPrivateProfileString.c

package info (click to toggle)
unixodbc 2.2.11-16
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 17,332 kB
  • ctags: 12,399
  • sloc: ansic: 116,624; cpp: 29,333; sh: 25,024; makefile: 3,002; lex: 241; yacc: 182; perl: 142; sed: 16; sql: 1
file content (434 lines) | stat: -rw-r--r-- 13,005 bytes parent folder | download | duplicates (2)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/****************************************************
 * SQLGetPrivateProfileString
 *
 * Mostly used with odbc.ini files but can be used for odbcinst.ini
 *
 * IF pszFileName[0] == '/' THEN
 *		use pszFileName
 * ELSE
 *		use _odbcinst_ConfigModeINI() to get the complete file name for the current mode.
 *
 **************************************************
 * This code was created by Peter Harvey @ CodeByDesign.
 * Released under LGPL 28.JAN.99
 *
 * Contributions from...
 * -----------------------------------------------
 * Peter Harvey		- pharvey@codebydesign.com
 **************************************************/
#include <time.h>
#include <odbcinstext.h>

#ifdef ENABLE_INI_CACHING

static struct ini_cache *ini_cache_head = NULL;

int check_ini_cache( int *ret,
                     LPCSTR  pszSection,
                     LPCSTR  pszEntry,
                     LPCSTR  pszDefault,
                     LPSTR   pRetBuffer,
                     int     nRetBuffer,
                     LPCSTR  pszFileName )
{
    struct ini_cache *ini_cache = ini_cache_head, *prev = NULL;
    UWORD config_mode;
    long tstamp = time( NULL );

    if ( pszSection == NULL || pszEntry == NULL )
    {
        return 0;
    }

    SQLGetConfigMode( &config_mode );

    /*
     * look for expired entries, remove one each call
     */

    for ( prev = NULL, ini_cache = ini_cache_head; ini_cache; ini_cache = ini_cache -> next )
    {
        if ( ini_cache -> timestamp < tstamp )
        {
            if ( prev )
            {
                prev -> next = ini_cache -> next;
            }
            else
            {
                ini_cache_head = ini_cache -> next;
            }

            if ( ini_cache -> fname )
                free( ini_cache -> fname );

            if ( ini_cache -> section )
                free( ini_cache -> section );

            if ( ini_cache -> entry )
                free( ini_cache -> entry );

            if ( ini_cache -> value )
                free( ini_cache -> value );

            if ( ini_cache -> default_value )
                free( ini_cache -> default_value );

            free( ini_cache );

            break;
        }

        prev = ini_cache;
    }

    for ( ini_cache = ini_cache_head; ini_cache; ini_cache = ini_cache -> next )
    {
        if ( !pszFileName && ini_cache -> fname )
            continue;
        if ( pszFileName && !ini_cache -> fname )
            continue;
        if ( pszFileName && ini_cache -> fname && strcmp( pszFileName, ini_cache -> fname ))
            continue;

        if ( ini_cache -> config_mode != config_mode )
            continue;

        if ( !pszSection && ini_cache -> section )
            continue;
        if ( pszSection && !ini_cache -> section )
            continue;
        if ( pszSection && ini_cache -> section && strcmp( pszSection, ini_cache -> section ))
            continue;

        if ( !pszEntry && ini_cache -> entry )
            continue;
        if ( pszEntry && !ini_cache -> entry )
            continue;
        if ( pszEntry && ini_cache -> entry && strcmp( pszEntry, ini_cache -> entry ))
            continue;

        if ( !pszDefault && ini_cache -> default_value )
            continue;
        if ( pszDefault && !ini_cache -> default_value )
            continue;
        if ( pszDefault && ini_cache -> default_value && strcmp( pszDefault, ini_cache -> default_value ))
            continue;

        if ( !pRetBuffer && ini_cache -> value )
            continue;
        if ( pRetBuffer && !ini_cache -> value )
            continue;

        if ( nRetBuffer != ini_cache -> buffer_size )
            continue;

        if ( pRetBuffer )
        {
            if ( ini_cache -> value )
                strcpy( pRetBuffer, ini_cache -> value );

            *ret = ini_cache -> ret_value;
            return 1;
        }
    }

    return 0;
}

int save_ini_cache( int ret,
                    LPCSTR  pszSection,
                    LPCSTR  pszEntry,
                    LPCSTR  pszDefault,
                    LPSTR   pRetBuffer,
                    int     nRetBuffer,
                    LPCSTR  pszFileName )
{
    struct ini_cache *ini_cache;
    UWORD config_mode;
    long tstamp = time( NULL ) + 20;    /* expiry every 20 seconds */

    ini_cache = calloc( sizeof( struct ini_cache ), 1 );
    if ( !ini_cache )
    {
        return 0;
    }

    if ( pszFileName )
        ini_cache -> fname = strdup( pszFileName );

    if ( pszSection )
        ini_cache -> section = strdup( pszSection );

    if ( pszEntry )
        ini_cache -> entry = strdup( pszEntry );

    if ( pRetBuffer && ret >= 0 )
        ini_cache -> value = strdup( pRetBuffer );

    if ( pszDefault )
        ini_cache -> default_value = strdup( pszDefault );

    ini_cache -> buffer_size = nRetBuffer;
    ini_cache -> ret_value = ret;

    SQLGetConfigMode( &config_mode );
    ini_cache -> config_mode = config_mode;

    ini_cache -> timestamp = tstamp;

    ini_cache -> next = ini_cache_head;
    ini_cache_head = ini_cache;

    return 0;
}

#else

int check_ini_cache( int *ret,
                     LPCSTR  pszSection,
                     LPCSTR  pszEntry,
                     LPCSTR  pszDefault,
                     LPSTR   pRetBuffer,
                     int     nRetBuffer,
                     LPCSTR  pszFileName )
{
    return 0;
}

int save_ini_cache( int ret,
                    LPCSTR  pszSection,
                    LPCSTR  pszEntry,
                    LPCSTR  pszDefault,
                    LPSTR   pRetBuffer,
                    int     nRetBuffer,
                    LPCSTR  pszFileName )
{
    return 0;
}

#endif

int SQLGetPrivateProfileString( LPCSTR  pszSection,
                                LPCSTR  pszEntry,
                                LPCSTR  pszDefault,
                                LPSTR   pRetBuffer,
                                int     nRetBuffer,
                                LPCSTR  pszFileName
                              )
{
    HINI    hIni;
    int     nBufPos         = 0;
    char    szValue[INI_MAX_PROPERTY_VALUE+1];
    char    szFileName[ODBC_FILENAME_MAX+1];
    UWORD   nConfigMode;
    int     ini_done = 0;
    int     ret;

    if ( check_ini_cache( &ret, pszSection, pszEntry, pszDefault, pRetBuffer, nRetBuffer, pszFileName ))
    {
        return ret;
    }

    /* SANITY CHECKS */
    if ( pRetBuffer == NULL || nRetBuffer < 2 )
    {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
        return -1;
    }
    if ( pszSection != NULL && pszEntry != NULL && pszDefault == NULL )
    {
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
        return -1;
    }

    *pRetBuffer = '\0';

    /*****************************************************
     * SOME MS CODE (ie some drivers) MAY USE THIS FUNCTION TO GET ODBCINST INFO SO...
     *****************************************************/
    if ( pszFileName != NULL )
    {
        if ( strstr( pszFileName, "odbcinst" ) || strstr( pszFileName, "ODBCINST" ) )
        {
            ret = _SQLGetInstalledDrivers(  pszSection, pszEntry, pszDefault, pRetBuffer, nRetBuffer );

            if ( ret == -1 )
            {
                /*
                 * Copy default value
                 */
                if ( pRetBuffer && nRetBuffer > 0 )
                {
                    strncpy( pRetBuffer, pszDefault, nRetBuffer );
                    pRetBuffer[ nRetBuffer - 1 ] = '\0';
                }
            }
            else
            {
                save_ini_cache( ret, pszSection, pszEntry, pszDefault, pRetBuffer, nRetBuffer, pszFileName );
            }

            return ret;
        }
    }

    /*****************************************************
     * GATHER ALL RELEVANT DSN INFORMATION INTO AN hIni
     *****************************************************/
    if ( pszFileName != 0 && pszFileName[0] == '/' )
    {
#ifdef __OS2__
        if ( iniOpen( &hIni, (char*)pszFileName, "#;", '[', ']', '=', TRUE, 1L )
             != INI_SUCCESS )
#else
        if ( iniOpen( &hIni, (char*)pszFileName, "#;", '[', ']', '=', TRUE )
             != INI_SUCCESS )
#endif
        {
            inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                             ODBC_ERROR_COMPONENT_NOT_FOUND, "" );
            return -1;
        }
    }
    else
    {
        nConfigMode     = ODBC_BOTH_DSN;
        SQLGetConfigMode( &nConfigMode );
        nBufPos         = 0;
        szFileName[0]   = '\0';

        switch ( nConfigMode )
        {
        case ODBC_BOTH_DSN:
            if ( _odbcinst_UserINI( szFileName, TRUE ))
            {
#ifdef __OS2__
                if ( iniOpen( &hIni, (char*) szFileName, "#;", '[', ']', '=', TRUE, 1L )
                     == INI_SUCCESS )
#else
                if ( iniOpen( &hIni, (char*) szFileName, "#;", '[', ']', '=', TRUE )
                     == INI_SUCCESS )
#endif
                {
                    ini_done = 1;
                }
            }
            _odbcinst_SystemINI( szFileName, TRUE );
            if ( !ini_done )
            {
#ifdef __OS2__
                if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE, 1L )
                     != INI_SUCCESS )
#else
                if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE )
                     != INI_SUCCESS )
#endif
                {
                    inst_logPushMsg( __FILE__, __FILE__, __LINE__,
                                     LOG_CRITICAL, ODBC_ERROR_COMPONENT_NOT_FOUND, "" );
                    return -1;
                }
            }
            else
            {
                iniAppend( hIni, szFileName );
            }
            break;

        case ODBC_USER_DSN:
            _odbcinst_UserINI( szFileName, TRUE );
#ifdef __OS2__
            if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE, 1L )
                 != INI_SUCCESS )
#else
            if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE )
                 != INI_SUCCESS )
#endif
            {
                inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                                 ODBC_ERROR_COMPONENT_NOT_FOUND, "" );
                return -1;
            }
            break;

        case ODBC_SYSTEM_DSN:
            _odbcinst_SystemINI( szFileName, TRUE );
#ifdef __OS2__
            if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE, 1L )
                 != INI_SUCCESS )
#else
            if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE )
                 != INI_SUCCESS )
#endif
            {
                inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                                 ODBC_ERROR_COMPONENT_NOT_FOUND, "" );
                return -1;
            }
            break;

        default:
            inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                             ODBC_ERROR_GENERAL_ERR, "Invalid Config Mode" );
            return -1;
        }
    }

    /*****************************************************
     * EXTRACT SECTIONS
     *****************************************************/
    if ( pszSection == NULL )
    {
        _odbcinst_GetSections( hIni, pRetBuffer, nRetBuffer, &nBufPos );
    }
    /*****************************************************
     * EXTRACT ENTRIES
     *****************************************************/
    else if ( pszEntry == NULL )
    {
        _odbcinst_GetEntries( hIni, pszSection, pRetBuffer, nRetBuffer, &nBufPos );
    }
    /*****************************************************
     * EXTRACT AN ENTRY
     *****************************************************/
    else
    {
        if ( pszSection == NULL || pszEntry == NULL || pszDefault == NULL )
        {
            inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
            return -1;
        }

        /* TRY TO GET THE ONE ITEM MATCHING Section & Entry */
        if ( iniPropertySeek( hIni, (char *)pszSection, (char *)pszEntry, "" ) != INI_SUCCESS )
        {
            /*
             * (NG) this seems to be ignoring the length of pRetBuffer !!!
             */
            /* strncpy( pRetBuffer, pszDefault, INI_MAX_PROPERTY_VALUE ); */
            if ( pRetBuffer && nRetBuffer > 0 )
            {
                strncpy( pRetBuffer, pszDefault, nRetBuffer );
                pRetBuffer[ nRetBuffer - 1 ] = '\0';
            }
        }
        else
        {
            iniValue( hIni, szValue );
            strncpy( pRetBuffer, szValue, nRetBuffer );
            pRetBuffer[ nRetBuffer - 1 ] = '\0';
            nBufPos = strlen( szValue );
        }
    }

    iniClose( hIni );

    ret = strlen( pRetBuffer );

    save_ini_cache( ret, pszSection, pszEntry, pszDefault, pRetBuffer, nRetBuffer, pszFileName );

    return ret;
}