1/*
2 *  SQLGetAvailableDrivers.c
3 *
4 *  $Id: SQLGetAvailableDrivers.c,v 1.10 2006/01/20 15:58:35 source Exp $
5 *
6 *  Get a list of all available drivers
7 *
8 *  The iODBC driver manager.
9 *
10 *  Copyright (C) 1996-2006 by OpenLink Software <iodbc@openlinksw.com>
11 *  All Rights Reserved.
12 *
13 *  This software is released under the terms of either of the following
14 *  licenses:
15 *
16 *      - GNU Library General Public License (see LICENSE.LGPL)
17 *      - The BSD License (see LICENSE.BSD).
18 *
19 *  Note that the only valid version of the LGPL license as far as this
20 *  project is concerned is the original GNU Library General Public License
21 *  Version 2, dated June 1991.
22 *
23 *  While not mandated by the BSD license, any patches you make to the
24 *  iODBC source code may be contributed back into the iODBC project
25 *  at your discretion. Contributions will benefit the Open Source and
26 *  Data Access community as a whole. Submissions may be made at:
27 *
28 *      http://www.iodbc.org
29 *
30 *
31 *  GNU Library Generic Public License Version 2
32 *  ============================================
33 *  This library is free software; you can redistribute it and/or
34 *  modify it under the terms of the GNU Library General Public
35 *  License as published by the Free Software Foundation; only
36 *  Version 2 of the License dated June 1991.
37 *
38 *  This library is distributed in the hope that it will be useful,
39 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
40 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
41 *  Library General Public License for more details.
42 *
43 *  You should have received a copy of the GNU Library General Public
44 *  License along with this library; if not, write to the Free
45 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
46 *
47 *
48 *  The BSD License
49 *  ===============
50 *  Redistribution and use in source and binary forms, with or without
51 *  modification, are permitted provided that the following conditions
52 *  are met:
53 *
54 *  1. Redistributions of source code must retain the above copyright
55 *     notice, this list of conditions and the following disclaimer.
56 *  2. Redistributions in binary form must reproduce the above copyright
57 *     notice, this list of conditions and the following disclaimer in
58 *     the documentation and/or other materials provided with the
59 *     distribution.
60 *  3. Neither the name of OpenLink Software Inc. nor the names of its
61 *     contributors may be used to endorse or promote products derived
62 *     from this software without specific prior written permission.
63 *
64 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
65 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
66 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
67 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL OPENLINK OR
68 *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
69 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
70 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
71 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
72 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
73 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
74 *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
75 */
76
77
78#include <iodbc.h>
79#include <odbcinst.h>
80#include <unicode.h>
81
82#include "misc.h"
83#include "inifile.h"
84#include "iodbc_error.h"
85
86BOOL
87GetAvailableDrivers (LPCSTR lpszInfFile, LPSTR lpszBuf, WORD cbBufMax,
88    WORD * pcbBufOut, BOOL infFile)
89{
90  int sect_len = 0;
91  WORD curr = 0;
92  BOOL retcode = FALSE;
93  PCONFIG pCfg;
94  char *szId;
95
96  if (!lpszBuf || !cbBufMax)
97    {
98      PUSH_ERROR (ODBC_ERROR_INVALID_BUFF_LEN);
99      goto quit;
100    }
101
102  /* Open the file to read from */
103  if (_iodbcdm_cfg_init (&pCfg, lpszInfFile, FALSE))
104    {
105      PUSH_ERROR (ODBC_ERROR_COMPONENT_NOT_FOUND);
106      goto quit;
107    }
108
109  /* Get the ODBC Drivers section */
110#ifdef WIN32
111  if (_iodbcdm_cfg_find (pCfg, "ODBC 32 bit Drivers", NULL))
112#else
113  if (_iodbcdm_cfg_find (pCfg, "ODBC Drivers", NULL))
114#endif
115    {
116      PUSH_ERROR (ODBC_ERROR_COMPONENT_NOT_FOUND);
117      goto done;
118    }
119
120  while (curr < cbBufMax && 0 == _iodbcdm_cfg_nextentry (pCfg))
121    {
122      if (_iodbcdm_cfg_section (pCfg))
123	break;
124
125      if (_iodbcdm_cfg_define (pCfg) && pCfg->id)
126	{
127	  szId = pCfg->id;
128	  while (infFile && *szId == '"')
129	    szId++;
130
131	  sect_len = STRLEN (szId);
132	  if (!sect_len)
133	    {
134	      PUSH_ERROR (ODBC_ERROR_INVALID_INF);
135	      goto done;
136	    }
137
138	  while (infFile && *(szId + sect_len - 1) == '"')
139	    sect_len -= 1;
140
141	  sect_len = sect_len > cbBufMax - curr ? cbBufMax - curr : sect_len;
142
143	  if (sect_len)
144	    memmove (lpszBuf + curr, szId, sect_len);
145	  else
146	    {
147	      PUSH_ERROR (ODBC_ERROR_INVALID_INF);
148	      goto done;
149	    }
150
151	  curr += sect_len;
152	  lpszBuf[curr++] = 0;
153	}
154    }
155
156  if (curr < cbBufMax)
157    lpszBuf[curr + 1] = 0;
158  if (pcbBufOut)
159    *pcbBufOut = curr;
160  retcode = TRUE;
161
162done:
163  _iodbcdm_cfg_done (pCfg);
164
165quit:
166  return retcode;
167}
168
169
170BOOL INSTAPI
171SQLGetAvailableDrivers (LPCSTR lpszInfFile, LPSTR lpszBuf, WORD cbBufMax,
172    WORD * pcbBufOut)
173{
174  BOOL retcode = FALSE;
175  WORD lenBufOut;
176
177  /* Get from the user files */
178  CLEAR_ERROR ();
179
180  switch (configMode)
181    {
182    case ODBC_BOTH_DSN:
183    case ODBC_USER_DSN:
184      wSystemDSN = USERDSN_ONLY;
185      break;
186
187    case ODBC_SYSTEM_DSN:
188      wSystemDSN = SYSTEMDSN_ONLY;
189      break;
190    }
191
192  retcode =
193      GetAvailableDrivers (lpszInfFile, lpszBuf, cbBufMax, &lenBufOut, FALSE);
194
195  if (pcbBufOut)
196    *pcbBufOut = lenBufOut;
197
198  wSystemDSN = USERDSN_ONLY;
199  configMode = ODBC_BOTH_DSN;
200  return retcode;
201}
202
203BOOL INSTAPI
204SQLGetAvailableDriversW (LPCWSTR lpszInfFile, LPWSTR lpszBuf, WORD cbBufMax,
205    WORD FAR * pcbBufOut)
206{
207  BOOL retcode = FALSE;
208  char *_inf_u8 = NULL;
209  char *_buffer_u8 = NULL;
210  SQLCHAR *ptr;
211  SQLWCHAR *ptrW;
212  WORD len = 0, length;
213
214  _inf_u8 = (char *) dm_SQL_WtoU8 ((SQLWCHAR *) lpszInfFile, SQL_NTS);
215  if (_inf_u8 == NULL && lpszInfFile)
216    {
217      PUSH_ERROR (ODBC_ERROR_OUT_OF_MEM);
218      goto done;
219    }
220
221  if (cbBufMax > 0)
222    {
223      if ((_buffer_u8 = malloc (cbBufMax * UTF8_MAX_CHAR_LEN + 1)) == NULL)
224	{
225	  PUSH_ERROR (ODBC_ERROR_OUT_OF_MEM);
226	  goto done;
227	}
228    }
229
230  retcode =
231      SQLGetAvailableDrivers (_inf_u8, _buffer_u8,
232      cbBufMax * UTF8_MAX_CHAR_LEN, pcbBufOut);
233
234  if (retcode == TRUE)
235    {
236      length = 0;
237
238      for (ptr = _buffer_u8, ptrW = lpszBuf; *ptr;
239	  ptr += STRLEN (ptr) + 1, ptrW += WCSLEN (ptrW) + 1)
240	{
241	  dm_StrCopyOut2_U8toW (ptr, ptrW, cbBufMax - 1, &len);
242	  length += len;
243	}
244
245      *ptrW = L'\0';
246      if (pcbBufOut)
247	*pcbBufOut = length + 1;
248    }
249
250done:
251  MEM_FREE (_inf_u8);
252  MEM_FREE (_buffer_u8);
253
254  return retcode;
255}
256