1/*
2 *  SQLReadFileDSN.c
3 *
4 *  $Id: SQLReadFileDSN.c,v 1.8 2006/07/10 13:43:55 source Exp $
5 *
6 *  These functions intentionally left blank
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#include <iodbc.h>
78#include <odbcinst.h>
79#include <unicode.h>
80
81#include "inifile.h"
82#include "iodbc_error.h"
83#include "misc.h"
84
85extern int GetPrivateProfileString (LPCSTR lpszSection, LPCSTR lpszEntry,
86    LPCSTR lpszDefault, LPSTR lpszRetBuffer, int cbRetBuffer,
87    LPCSTR lpszFilename);
88
89BOOL INSTAPI
90SQLReadFileDSN (LPCSTR lpszFileName, LPCSTR lpszAppName, LPCSTR lpszKeyName,
91    LPSTR lpszString, WORD cbString, WORD * pcbString)
92{
93  BOOL retcode = FALSE;
94  WORD len = 0, i;
95  char filename[1024];
96
97  /* Check input parameters */
98  CLEAR_ERROR ();
99
100  if (!lpszString || !cbString)
101    {
102      PUSH_ERROR (ODBC_ERROR_INVALID_BUFF_LEN);
103      goto quit;
104    }
105
106  if (!lpszAppName && lpszKeyName)
107    {
108      PUSH_ERROR (ODBC_ERROR_INVALID_REQUEST_TYPE);
109      goto quit;
110    }
111
112  /* Is a file is specified */
113  if (lpszFileName)
114    {
115      _iodbcdm_getdsnfile (lpszFileName, filename, sizeof (filename));
116      len =
117	  GetPrivateProfileString (lpszAppName, lpszKeyName, "", lpszString,
118	  cbString, filename);
119      if (numerrors == -1)
120	retcode = TRUE;
121      goto quit;
122    }
123
124  PUSH_ERROR (ODBC_ERROR_INVALID_PATH);
125  goto quit;
126
127quit:
128  for (i = 0; i < len; i++)
129    if (!lpszString[i])
130      lpszString[i] = ';';
131
132  if (pcbString)
133    *pcbString = len;
134
135  if (len == cbString - 1)
136    {
137      PUSH_ERROR (ODBC_ERROR_OUTPUT_STRING_TRUNCATED);
138      retcode = FALSE;
139    }
140
141  return retcode;
142}
143
144BOOL INSTAPI
145SQLReadFileDSNW (LPCWSTR lpszFileName, LPCWSTR lpszAppName,
146    LPCWSTR lpszKeyName, LPWSTR lpszString, WORD cbString, WORD * pcbString)
147{
148  char *_filename_u8 = NULL;
149  char *_appname_u8 = NULL;
150  char *_keyname_u8 = NULL;
151  char *_string_u8 = NULL;
152  BOOL retcode = FALSE;
153
154  _filename_u8 = (char *) dm_SQL_WtoU8 ((SQLWCHAR *) lpszFileName, SQL_NTS);
155  if (_filename_u8 == NULL && lpszFileName)
156    {
157      PUSH_ERROR (ODBC_ERROR_OUT_OF_MEM);
158      goto done;
159    }
160
161  _appname_u8 = (char *) dm_SQL_WtoU8 ((SQLWCHAR *) lpszAppName, SQL_NTS);
162  if (_appname_u8 == NULL && lpszAppName)
163    {
164      PUSH_ERROR (ODBC_ERROR_OUT_OF_MEM);
165      goto done;
166    }
167
168  _keyname_u8 = (char *) dm_SQL_WtoU8 ((SQLWCHAR *) lpszKeyName, SQL_NTS);
169  if (_keyname_u8 == NULL && lpszKeyName)
170    {
171      PUSH_ERROR (ODBC_ERROR_OUT_OF_MEM);
172      goto done;
173    }
174
175  if (cbString > 0)
176    {
177      if ((_string_u8 = malloc (cbString * UTF8_MAX_CHAR_LEN + 1)) == NULL)
178	{
179	  PUSH_ERROR (ODBC_ERROR_OUT_OF_MEM);
180	  goto done;
181	}
182    }
183
184  retcode =
185      SQLReadFileDSN (_filename_u8, _appname_u8, _keyname_u8, _string_u8,
186      cbString * UTF8_MAX_CHAR_LEN, pcbString);
187
188  if (retcode == TRUE)
189    {
190      dm_StrCopyOut2_U8toW (_string_u8, lpszString, cbString, pcbString);
191    }
192
193done:
194  MEM_FREE (_filename_u8);
195  MEM_FREE (_appname_u8);
196  MEM_FREE (_keyname_u8);
197  MEM_FREE (_string_u8);
198
199  return retcode;
200}
201