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 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
|
/****************************************************
* 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 <config.h>
#include <time.h>
#include <odbcinstext.h>
#ifdef ENABLE_INI_CACHING
#ifdef HAVE_LIBPTH
#include <pth.h>
static pth_mutex_t mutex_ini = PTH_MUTEX_INIT;
static int pth_init_called = 0;
static int mutex_entry( pth_mutex_t *mutex )
{
if ( !pth_init_called )
{
pth_init();
pth_init_called = 1;
}
return pth_mutex_acquire( mutex, 0, NULL );
}
static int mutex_exit( pth_mutex_t *mutex )
{
return pth_mutex_release( mutex );
}
#elif HAVE_LIBPTHREAD
#include <pthread.h>
static pthread_mutex_t mutex_ini = PTHREAD_MUTEX_INITIALIZER;
static int mutex_entry( pthread_mutex_t *mutex )
{
return pthread_mutex_lock( mutex );
}
static int mutex_exit( pthread_mutex_t *mutex )
{
return pthread_mutex_unlock( mutex );
}
#elif HAVE_LIBTHREAD
#include <thread.h>
static mutex_t mutex_ini;
static int mutex_entry( mutex_t *mutex )
{
return mutex_lock( mutex );
}
static int mutex_exit( mutex_t *mutex )
{
return mutex_unlock( mutex );
}
#else
#define mutex_entry(x)
#define mutex_exit(x)
#endif
static struct ini_cache *ini_cache_head = NULL;
static 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;
}
config_mode = __get_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;
}
static 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;
config_mode = __get_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;
}
/*
* wrappers to provide therad safety
*/
static int check_ini_cache( int *ret,
LPCSTR pszSection,
LPCSTR pszEntry,
LPCSTR pszDefault,
LPSTR pRetBuffer,
int nRetBuffer,
LPCSTR pszFileName )
{
int rval;
mutex_entry( &mutex_ini );
rval = _check_ini_cache( ret, pszSection, pszEntry, pszDefault,
pRetBuffer, nRetBuffer, pszFileName );
mutex_exit( &mutex_ini );
return rval;
}
static int save_ini_cache( int ret,
LPCSTR pszSection,
LPCSTR pszEntry,
LPCSTR pszDefault,
LPSTR pRetBuffer,
int nRetBuffer,
LPCSTR pszFileName )
{
int rval, cval;
mutex_entry( &mutex_ini );
/*
* check its not been inserted since the last check
*/
if ( !_check_ini_cache( &cval, pszSection, pszEntry, pszDefault,
pRetBuffer, nRetBuffer, pszFileName )) {
rval = _save_ini_cache( ret, pszSection, pszEntry, pszDefault,
pRetBuffer, nRetBuffer, pszFileName );
}
mutex_exit( &mutex_ini );
return rval;
}
#else
static int check_ini_cache( int *ret,
LPCSTR pszSection,
LPCSTR pszEntry,
LPCSTR pszDefault,
LPSTR pRetBuffer,
int nRetBuffer,
LPCSTR pszFileName )
{
return 0;
}
static 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;
inst_logClear();
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, "need default value - try empty string" );
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 )
{
/* try to use any default provided */
if ( pRetBuffer && nRetBuffer > 0 )
{
if ( pszDefault )
{
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 = __get_config_mode();
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 && pszDefault )
{
strncpy( pRetBuffer, pszDefault, nRetBuffer );
pRetBuffer[ nRetBuffer - 1 ] = '\0';
}
}
else
{
iniValue( hIni, szValue );
if ( pRetBuffer )
{
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;
}
int INSTAPI SQLGetPrivateProfileStringW( LPCWSTR lpszSection,
LPCWSTR lpszEntry,
LPCWSTR lpszDefault,
LPWSTR lpszRetBuffer,
int cbRetBuffer,
LPCWSTR lpszFilename)
{
int ret;
char *sect;
char *entry;
char *def;
char *buf;
char *name;
inst_logClear();
sect = lpszSection ? _single_string_alloc_and_copy( lpszSection ) : (char*)NULL;
entry = lpszEntry ? _single_string_alloc_and_copy( lpszEntry ) : (char*)NULL;
def = lpszDefault ? _single_string_alloc_and_copy( lpszDefault ) : (char*)NULL;
name = lpszFilename ? _single_string_alloc_and_copy( lpszFilename ) : (char*)NULL;
if ( lpszRetBuffer )
{
if ( cbRetBuffer > 0 )
{
buf = calloc( cbRetBuffer + 1, 1 );
}
else
{
buf = NULL;
}
}
else
{
buf = NULL;
}
ret = SQLGetPrivateProfileString( sect, entry, def, buf, cbRetBuffer, name );
if ( sect )
free( sect );
if ( entry )
free( entry );
if ( def )
free( def );
if ( name )
free( name );
if ( ret > 0 )
{
if ( buf && lpszRetBuffer )
{
_single_copy_to_wide( lpszRetBuffer, buf, ret + 1 );
}
}
if ( buf )
{
free( buf );
}
return ret;
}
|