int iniObjectSeekSure( HINI hIni, char *pszObject ) { int nReturn; /* SANITY CHECKS */ if ( hIni == NULL ) return INI_ERROR; if ( !pszObject ) return INI_ERROR; if ( (nReturn = iniObjectSeek( hIni, pszObject )) == INI_NO_DATA ) return iniObjectInsert( hIni, pszObject ); return nReturn; }
BOOL SQLWriteFileDSN( LPCSTR pszFileName, LPCSTR pszAppName, LPCSTR pszKeyName, LPCSTR pszString ) { HINI hIni; char szFileName[ODBC_FILENAME_MAX+1]; if ( pszFileName[0] == '/' ) { strcpy( szFileName, pszFileName ); } else { char szPath[ODBC_FILENAME_MAX+1]; *szPath = '\0'; _odbcinst_FileINI( szPath ); sprintf( szFileName, "%s/%s", szPath, pszFileName ); } if ( strlen( szFileName ) < 4 || strcmp( szFileName + strlen( szFileName ) - 4, ".dsn" )) { strcat( szFileName, ".dsn" ); } #ifdef __OS2__ if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE, 0L ) != INI_SUCCESS ) #else if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE ) != INI_SUCCESS ) #endif { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_PATH, "" ); return FALSE; } /* delete section */ if ( pszString == NULL && pszKeyName == NULL ) { if ( iniObjectSeek( hIni, (char *)pszAppName ) == INI_SUCCESS ) { iniObjectDelete( hIni ); } } /* delete entry */ else if ( pszString == NULL ) { if ( iniPropertySeek( hIni, (char *)pszAppName, (char *)pszKeyName, "" ) == INI_SUCCESS ) { iniPropertyDelete( hIni ); } } else { /* add section */ if ( iniObjectSeek( hIni, (char *)pszAppName ) != INI_SUCCESS ) { iniObjectInsert( hIni, (char *)pszAppName ); } /* update entry */ if ( iniPropertySeek( hIni, (char *)pszAppName, (char *)pszKeyName, "" ) == INI_SUCCESS ) { iniObjectSeek( hIni, (char *)pszAppName ); iniPropertyUpdate( hIni, (char *)pszKeyName, (char *)pszString ); } /* add entry */ else { iniObjectSeek( hIni, (char *)pszAppName ); iniPropertyInsert( hIni, (char *)pszKeyName, (char *)pszString ); } } if ( iniCommit( hIni ) != INI_SUCCESS ) { iniClose( hIni ); inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" ); return FALSE; } iniClose( hIni ); return TRUE; }
BOOL SQLWriteDSNToIni( LPCSTR pszDSN, LPCSTR pszDriver ) { HINI hIni; char szFileName[ODBC_FILENAME_MAX+1]; SQLRemoveDSNFromIni( pszDSN ); /* SANITY CHECKS */ if ( pszDSN == NULL ) { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" ); return FALSE; } if ( pszDSN[0] == '\0' ) { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" ); return FALSE; } if ( (strcasecmp( pszDSN, "DEFAULT" ) != 0 ) && (pszDriver == NULL ) ) { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_NAME, "" ); return FALSE; } if ( (strcasecmp( pszDSN, "DEFAULT" ) != 0 ) && (pszDriver[0] == '\0') ) { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_NAME, "" ); return FALSE; } if ( SQLValidDSN( pszDSN ) == FALSE ) { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_DSN, "" ); return FALSE; } /* OK */ if ( _odbcinst_ConfigModeINI( szFileName ) == FALSE ) { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" ); return FALSE; } #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_REQUEST_FAILED, "" ); return FALSE; } iniObjectInsert( hIni, (char *)pszDSN ); if ( pszDriver != NULL ) { iniPropertyInsert( hIni, "Driver", (char *)pszDriver ); } if ( iniCommit( hIni ) != INI_SUCCESS ) { iniClose( hIni ); inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" ); return FALSE; } iniClose( hIni ); return TRUE; }
BOOL SQLWritePrivateProfileString( LPCSTR pszSection, LPCSTR pszEntry, LPCSTR pszString, LPCSTR pszFileName ) { HINI hIni; char szFileName[ODBC_FILENAME_MAX+1]; inst_logClear(); /* SANITY CHECKS */ if ( pszSection == NULL ) { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" ); return FALSE; } if ( pszSection[0] == '\0' ) { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" ); return FALSE; } if ( pszFileName == NULL ) { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" ); return FALSE; } /***************************************************** * SOME MS CODE (ie some drivers) MAY USE THIS FUNCTION TO WRITE ODBCINST INFO SO... *****************************************************/ if ( strstr( pszFileName, "odbcinst" ) || strstr( pszFileName, "ODBCINST" ) ) return _SQLWriteInstalledDrivers( pszSection, pszEntry, pszString ); if ( pszFileName[0] == '/' ) { strcpy( szFileName, pszFileName ); } else { if ( _odbcinst_ConfigModeINI( szFileName ) == FALSE ) { inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" ); return FALSE; } } #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_REQUEST_FAILED, "" ); return FALSE; } /* delete section */ if ( pszEntry == NULL ) { if ( iniObjectSeek( hIni, (char *)pszSection ) == INI_SUCCESS ) iniObjectDelete( hIni ); } /* delete entry */ else if ( pszString == NULL ) { if ( iniPropertySeek( hIni, (char *)pszSection, (char *)pszEntry, "" ) == INI_SUCCESS ) { iniPropertyDelete( hIni ); } } else { /* add section */ if ( iniObjectSeek( hIni, (char *)pszSection ) != INI_SUCCESS ) iniObjectInsert( hIni, (char *)pszSection ); /* update entry */ if ( iniPropertySeek( hIni, (char *)pszSection, (char *)pszEntry, "" ) == INI_SUCCESS ) { iniObjectSeek( hIni, (char *)pszSection ); /* * Get the correct property to update */ iniPropertySeek( hIni, (char *)pszSection, (char *)pszEntry, "" ); iniPropertyUpdate( hIni, (char *)pszEntry, (char *)pszString ); } /* add entry */ else { iniObjectSeek( hIni, (char *)pszSection ); iniPropertyInsert( hIni, (char *)pszEntry, (char *)pszString ); } } if ( iniCommit( hIni ) != INI_SUCCESS ) { iniClose( hIni ); inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" ); return FALSE; } iniClose( hIni ); return TRUE; }
int iniAppend( HINI hIni, char *pszFileName ) { FILE *hFile; char szLine[INI_MAX_LINE+1]; char szObjectName[INI_MAX_OBJECT_NAME+1]; char szPropertyName[INI_MAX_PROPERTY_NAME+1]; char szPropertyValue[INI_MAX_PROPERTY_VALUE+1]; /* SANITY CHECK */ if ( strlen( pszFileName ) > ODBC_FILENAME_MAX ) return INI_ERROR; /* OPEN FILE */ hFile = uo_fopen( pszFileName, "r" ); if ( !hFile ) return INI_ERROR; iniObjectLast( hIni ); iniPropertyLast( hIni ); /* SCAN UNTIL WE GET TO AN OBJECT NAME OR EOF */ szLine[0] = '\0'; if ( _iniScanUntilObject( hIni, hFile, szLine ) == INI_SUCCESS ) { do { if ( szLine[0] == hIni->cLeftBracket ) { _iniObjectRead( hIni, szLine, szObjectName ); if ( iniObjectSeek( hIni, szObjectName ) == INI_SUCCESS ) { iniObjectLast( hIni ); iniPropertyLast( hIni ); if ( _iniScanUntilNextObject( hIni, hFile, szLine ) != INI_SUCCESS) break; } else { iniObjectInsert( hIni, szObjectName ); if ( uo_fgets( szLine, INI_MAX_LINE, hFile ) == NULL ) break; } } else if ( (strchr( hIni->cComment, szLine[0] ) == NULL ) && isalnum(szLine[0]) ) { _iniPropertyRead( hIni, szLine, szPropertyName, szPropertyValue ); iniPropertyInsert( hIni, szPropertyName, szPropertyValue ); if ( uo_fgets( szLine, INI_MAX_LINE, hFile ) == NULL ) break; } else { if ( uo_fgets( szLine, INI_MAX_LINE, hFile ) == NULL ) break; } } while( 1 ); } /* WE ARE NOT GOING TO TRY TO BE SMART ENOUGH TO SAVE THIS STUFF */ hIni->bReadOnly = 1; /* CLEANUP */ if ( hFile != NULL ) uo_fclose( hFile ); return INI_SUCCESS; }