1/**************************************************
2 *
3 **************************************************
4 * This code was created by Peter Harvey @ CodeByDesign.
5 * Released under LGPL 28.JAN.99
6 *
7 * Contributions from...
8 * -----------------------------------------------
9 * Peter Harvey - pharvey@codebydesign.com
10 **************************************************/
11#include <config.h>
12#include <odbcinstext.h>
13
14BOOL SQLWriteFileDSN( LPCSTR pszFileName,
15 LPCSTR pszAppName,
16 LPCSTR pszKeyName,
17 LPCSTR pszString )
18{
19 HINI hIni;
20 char szFileName[ODBC_FILENAME_MAX+1];
21
22 if ( pszFileName[0] == '/' )
23 {
24 strncpy( szFileName, pszFileName, sizeof(szFileName) - 5 );
25 }
26 else
27 {
28 char szPath[ODBC_FILENAME_MAX+1];
29 *szPath = '\0';
30 _odbcinst_FileINI( szPath );
31 snprintf( szFileName, sizeof(szFileName) - 5, "%s/%s", szPath, pszFileName );
32 }
33
34 if ( strlen( szFileName ) < 4 || strcmp( szFileName + strlen( szFileName ) - 4, ".dsn" ))
35 {
36 strcat( szFileName, ".dsn" );
37 }
38
39#ifdef __OS2__
40 if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE, 0L ) != INI_SUCCESS )
41#else
42 if ( iniOpen( &hIni, szFileName, "#;", '[', ']', '=', TRUE ) != INI_SUCCESS )
43#endif
44 {
45 inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_PATH, "" );
46 return FALSE;
47 }
48
49 /* delete section */
50 if ( pszString == NULL && pszKeyName == NULL )
51 {
52 if ( iniObjectSeek( hIni, (char *)pszAppName ) == INI_SUCCESS )
53 {
54 iniObjectDelete( hIni );
55 }
56 }
57 /* delete entry */
58 else if ( pszString == NULL )
59 {
60 if ( iniPropertySeek( hIni, (char *)pszAppName, (char *)pszKeyName, "" ) == INI_SUCCESS )
61 {
62 iniPropertyDelete( hIni );
63 }
64 }
65 else
66 {
67 /* add section */
68 if ( iniObjectSeek( hIni, (char *)pszAppName ) != INI_SUCCESS )
69 {
70 iniObjectInsert( hIni, (char *)pszAppName );
71 }
72 /* update entry */
73 if ( iniPropertySeek( hIni, (char *)pszAppName, (char *)pszKeyName, "" ) == INI_SUCCESS )
74 {
75 iniObjectSeek( hIni, (char *)pszAppName );
76 iniPropertyUpdate( hIni, (char *)pszKeyName, (char *)pszString );
77 }
78 /* add entry */
79 else
80 {
81 iniObjectSeek( hIni, (char *)pszAppName );
82 iniPropertyInsert( hIni, (char *)pszKeyName, (char *)pszString );
83 }
84 }
85
86 if ( iniCommit( hIni ) != INI_SUCCESS )
87 {
88 iniClose( hIni );
89 inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_REQUEST_FAILED, "" );
90 return FALSE;
91 }
92
93 iniClose( hIni );
94
95 return TRUE;
96}
97
98BOOL INSTAPI SQLWriteFileDSNW(LPCWSTR lpszFileName,
99 LPCWSTR lpszAppName,
100 LPCWSTR lpszKeyName,
101 LPCWSTR lpszString)
102{
103 BOOL ret;
104 char *file;
105 char *app;
106 char *key;
107 char *str;
108
109 file = lpszFileName ? _single_string_alloc_and_copy( lpszFileName ) : (char*)NULL;
110 app = lpszAppName ? _single_string_alloc_and_copy( lpszAppName ) : (char*)NULL;
111 key = lpszKeyName ? _single_string_alloc_and_copy( lpszKeyName ) : (char*)NULL;
112 str = lpszString ? _single_string_alloc_and_copy( lpszString ) : (char*)NULL;
113
114 ret = SQLWriteFileDSN( file, app, key, str );
115
116 if ( file )
117 free( file );
118 if ( app )
119 free( app );
120 if ( key )
121 free( key );
122 if ( str )
123 free( str );
124
125 return ret;
126}
127