1/**************************************************
2 * SQLWritePrivateProfileString
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
16extern void __clear_ini_cache( void );
17
18BOOL SQLWritePrivateProfileString(
19 LPCSTR pszSection,
20 LPCSTR pszEntry,
21 LPCSTR pszString,
22 LPCSTR pszFileName )
23{
24 HINI hIni;
25 char szFileName[ODBC_FILENAME_MAX+1];
26
27 inst_logClear();
28
29 /* SANITY CHECKS */
30 if ( pszSection == NULL )
31 {
32 inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
33 return FALSE;
34 }
35 if ( pszSection[0] == '\0' )
36 {
37 inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
38 return FALSE;
39 }
40 if ( pszFileName == NULL )
41 {
42 inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
43 return FALSE;
44 }
45
46 /*****************************************************
47 * SOME MS CODE (ie some drivers) MAY USE THIS FUNCTION TO WRITE ODBCINST INFO SO...
48 *****************************************************/
49 if ( strstr( pszFileName, "odbcinst" ) || strstr( pszFileName, "ODBCINST" ) )
50 return _SQLWriteInstalledDrivers( pszSection, pszEntry, pszString );
51
52 if ( pszFileName[0] == '/' )
53 {
54 strcpy( szFileName, pszFileName );
55 }
56 else
57 {
58 if ( _odbcinst_ConfigModeINI( szFileName ) == FALSE )
59 {
60 inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" );
61 return FALSE;
62 }
63 }
64#ifdef __OS2__
65 if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE, 1L ) != INI_SUCCESS )
66#else
67 if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE ) != INI_SUCCESS )
68#endif
69 {
70 inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" );
71 return FALSE;
72 }
73
74 /* delete section */
75 if ( pszEntry == NULL )
76 {
77 if ( iniObjectSeek( hIni, (char *)pszSection ) == INI_SUCCESS )
78 iniObjectDelete( hIni );
79 }
80 /* delete entry */
81 else if ( pszString == NULL )
82 {
83 if ( iniPropertySeek( hIni, (char *)pszSection, (char *)pszEntry, "" ) == INI_SUCCESS )
84 {
85 iniPropertyDelete( hIni );
86 }
87 }
88 else
89 {
90 /* add section */
91 if ( iniObjectSeek( hIni, (char *)pszSection ) != INI_SUCCESS )
92 iniObjectInsert( hIni, (char *)pszSection );
93 /* update entry */
94 if ( iniPropertySeek( hIni, (char *)pszSection, (char *)pszEntry, "" ) == INI_SUCCESS )
95 {
96 iniObjectSeek( hIni, (char *)pszSection );
97 /*
98 * Get the correct property to update
99 */
100 iniPropertySeek( hIni, (char *)pszSection, (char *)pszEntry, "" );
101 iniPropertyUpdate( hIni, (char *)pszEntry, (char *)pszString );
102 }
103 /* add entry */
104 else
105 {
106 iniObjectSeek( hIni, (char *)pszSection );
107 iniPropertyInsert( hIni, (char *)pszEntry, (char *)pszString );
108 }
109
110 }
111
112 if ( iniCommit( hIni ) != INI_SUCCESS )
113 {
114 iniClose( hIni );
115 inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" );
116 return FALSE;
117 }
118
119 iniClose( hIni );
120
121 __clear_ini_cache();
122
123 return TRUE;
124}
125
126BOOL INSTAPI SQLWritePrivateProfileStringW(LPCWSTR lpszSection,
127 LPCWSTR lpszEntry,
128 LPCWSTR lpszString,
129 LPCWSTR lpszFilename)
130{
131 char *sect;
132 char *entry;
133 char *string;
134 char *file;
135 BOOL ret;
136
137 sect = lpszSection ? _single_string_alloc_and_copy( lpszSection ) : (char*)NULL;
138 entry = lpszEntry ? _single_string_alloc_and_copy( lpszEntry ) : (char*)NULL;
139 string = lpszString ? _single_string_alloc_and_copy( lpszString ) : (char*)NULL;
140 file = lpszFilename ? _single_string_alloc_and_copy( lpszFilename ) : (char*)NULL;
141
142 ret = SQLWritePrivateProfileString( sect, entry, string, file );
143
144 if ( sect )
145 free( sect );
146 if ( entry )
147 free( entry );
148 if ( string )
149 free( string );
150 if ( file )
151 free( file );
152
153 return ret;
154}
155