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