| 1 | /************************************************** |
| 2 | * SQLWriteDSNToIni |
| 3 | * |
| 4 | ************************************************** |
| 5 | * This code was created by Peter Harvey @ CodeByDesign. |
| 6 | * Released under LGPL 28.JAN.99 |
| 7 | * |
| 8 | * Contributions from... |
| 9 | * ----------------------------------------------- |
| 10 | * Peter Harvey - pharvey@codebydesign.com |
| 11 | **************************************************/ |
| 12 | |
| 13 | #include <config.h> |
| 14 | #include <odbcinstext.h> |
| 15 | |
| 16 | extern void __clear_ini_cache( void ); |
| 17 | |
| 18 | BOOL SQLWriteDSNToIni( LPCSTR pszDSN, |
| 19 | LPCSTR pszDriver ) |
| 20 | { |
| 21 | HINI hIni; |
| 22 | char szFileName[ODBC_FILENAME_MAX+1]; |
| 23 | |
| 24 | SQLRemoveDSNFromIni( pszDSN ); |
| 25 | |
| 26 | /* SANITY CHECKS */ |
| 27 | if ( pszDSN == NULL ) |
| 28 | { |
| 29 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" ); |
| 30 | return FALSE; |
| 31 | } |
| 32 | if ( pszDSN[0] == '\0' ) |
| 33 | { |
| 34 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" ); |
| 35 | return FALSE; |
| 36 | } |
| 37 | if ( (strcasecmp( pszDSN, "DEFAULT" ) != 0 ) && (pszDriver == NULL ) ) |
| 38 | { |
| 39 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_NAME, "" ); |
| 40 | return FALSE; |
| 41 | } |
| 42 | if ( (strcasecmp( pszDSN, "DEFAULT" ) != 0 ) && (pszDriver[0] == '\0') ) |
| 43 | { |
| 44 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_NAME, "" ); |
| 45 | return FALSE; |
| 46 | } |
| 47 | if ( SQLValidDSN( pszDSN ) == FALSE ) |
| 48 | { |
| 49 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_DSN, "" ); |
| 50 | return FALSE; |
| 51 | } |
| 52 | |
| 53 | /* OK */ |
| 54 | if ( _odbcinst_ConfigModeINI( szFileName ) == FALSE ) |
| 55 | { |
| 56 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" ); |
| 57 | return FALSE; |
| 58 | } |
| 59 | #ifdef __OS2__ |
| 60 | if ( iniOpen( &hIni, szFileName, "#;" , '[', ']', '=', TRUE, 1L ) != INI_SUCCESS ) |
| 61 | #else |
| 62 | if ( iniOpen( &hIni, szFileName, "#;" , '[', ']', '=', TRUE ) != INI_SUCCESS ) |
| 63 | #endif |
| 64 | { |
| 65 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" ); |
| 66 | return FALSE; |
| 67 | } |
| 68 | iniObjectInsert( hIni, (char *)pszDSN ); |
| 69 | if ( pszDriver != NULL ) |
| 70 | { |
| 71 | iniPropertyInsert( hIni, "Driver" , (char *)pszDriver ); |
| 72 | } |
| 73 | if ( iniCommit( hIni ) != INI_SUCCESS ) |
| 74 | { |
| 75 | iniClose( hIni ); |
| 76 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" ); |
| 77 | return FALSE; |
| 78 | } |
| 79 | |
| 80 | iniClose( hIni ); |
| 81 | |
| 82 | __clear_ini_cache(); |
| 83 | |
| 84 | return TRUE; |
| 85 | } |
| 86 | |
| 87 | BOOL INSTAPI SQLWriteDSNToIniW (LPCWSTR lpszDSN, |
| 88 | LPCWSTR lpszDriver) |
| 89 | { |
| 90 | char *drv, *dsn; |
| 91 | BOOL ret; |
| 92 | |
| 93 | dsn = _single_string_alloc_and_copy( lpszDSN ); |
| 94 | drv = _single_string_alloc_and_copy( lpszDriver ); |
| 95 | |
| 96 | ret = SQLWriteDSNToIni( dsn, drv ); |
| 97 | |
| 98 | free( dsn ); |
| 99 | free( drv ); |
| 100 | |
| 101 | return ret; |
| 102 | } |
| 103 | |