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 | |
14 | BOOL SQLRemoveDriver( LPCSTR pszDriver, |
15 | BOOL nRemoveDSN, |
16 | LPDWORD pnUsageCount ) |
17 | { |
18 | HINI hODBCInstIni; |
19 | char szValue[INI_MAX_PROPERTY_VALUE+1]; |
20 | char szIniName[ ODBC_FILENAME_MAX * 2 + 1 ]; |
21 | char b1[ ODBC_FILENAME_MAX + 1 ], b2[ ODBC_FILENAME_MAX + 1 ]; |
22 | |
23 | inst_logClear(); |
24 | |
25 | /* SANITY CHECKS */ |
26 | if ( pszDriver == NULL ) |
27 | { |
28 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_NAME, "" ); |
29 | return FALSE; |
30 | } |
31 | if ( pszDriver[0] == '\0' ) |
32 | { |
33 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_INVALID_NAME, "" ); |
34 | return FALSE; |
35 | } |
36 | if ( nRemoveDSN != TRUE && nRemoveDSN != FALSE ) |
37 | { |
38 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" ); |
39 | return FALSE; |
40 | } |
41 | |
42 | (*pnUsageCount) = 0; |
43 | |
44 | #ifdef VMS |
45 | sprintf( szIniName, "%s:%s" , odbcinst_system_file_path( b1 ), odbcinst_system_file_name( b2 ) ); |
46 | #else |
47 | sprintf( szIniName, "%s/%s" , odbcinst_system_file_path( b1 ), odbcinst_system_file_name( b2 ) ); |
48 | #endif |
49 | |
50 | /* PROCESS ODBC INST INI FILE */ |
51 | #ifdef __OS2__ |
52 | if ( iniOpen( &hODBCInstIni, szIniName, "#;" , '[', ']', '=', TRUE, 1L ) != INI_SUCCESS ) |
53 | #else |
54 | if ( iniOpen( &hODBCInstIni, szIniName, "#;" , '[', ']', '=', TRUE ) != INI_SUCCESS ) |
55 | #endif |
56 | { |
57 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_COMPONENT_NOT_FOUND, "" ); |
58 | return FALSE; |
59 | } |
60 | |
61 | /* LETS GET ITS FILE USAGE VALUE (if any) */ |
62 | if ( iniPropertySeek( hODBCInstIni, (char *)pszDriver, "UsageCount" , "" ) == INI_SUCCESS ) |
63 | { |
64 | iniValue( hODBCInstIni, szValue ); |
65 | (*pnUsageCount) = atoi( szValue ); |
66 | } |
67 | |
68 | /* DOES THE OBJECT ALREADY EXIST? (also ensures that we have correct current object) */ |
69 | if ( iniObjectSeek( hODBCInstIni, (char *)pszDriver ) == INI_SUCCESS ) |
70 | { |
71 | if ( (*pnUsageCount) == 0 ) |
72 | (*pnUsageCount) = 1; |
73 | |
74 | (*pnUsageCount)--; |
75 | if ( (*pnUsageCount) == 0 ) |
76 | { |
77 | iniObjectDelete( hODBCInstIni ); |
78 | if ( nRemoveDSN ) |
79 | { |
80 | /*********************************** |
81 | * TO DO |
82 | ***********************************/ |
83 | } |
84 | } |
85 | else |
86 | { |
87 | if ( iniPropertySeek( hODBCInstIni, (char *)pszDriver, "UsageCount" , "" ) == INI_SUCCESS ) |
88 | { |
89 | sprintf( szValue, "%ld" , (long int)(*pnUsageCount) ); |
90 | iniPropertyUpdate( hODBCInstIni, "UsageCount" , szValue ); |
91 | } |
92 | else |
93 | { |
94 | iniPropertyInsert( hODBCInstIni, "UsageCount" , szValue ); |
95 | } |
96 | } |
97 | if ( iniCommit( hODBCInstIni ) != INI_SUCCESS ) |
98 | { |
99 | inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" ); |
100 | iniClose( hODBCInstIni ); |
101 | return FALSE; |
102 | } |
103 | } |
104 | |
105 | |
106 | iniClose( hODBCInstIni ); |
107 | |
108 | return TRUE; |
109 | } |
110 | |
111 | BOOL INSTAPI SQLRemoveDriverW(LPCWSTR lpszDriver, |
112 | BOOL fRemoveDSN, |
113 | LPDWORD lpdwUsageCount) |
114 | { |
115 | BOOL ret; |
116 | char *drv = _single_string_alloc_and_copy( lpszDriver ); |
117 | |
118 | inst_logClear(); |
119 | |
120 | ret = SQLRemoveDriver( drv, fRemoveDSN, lpdwUsageCount ); |
121 | |
122 | free( drv ); |
123 | |
124 | return ret; |
125 | } |
126 | |