File: SQLGetPrivateProfileString.c

package info (click to toggle)
unixodbc 2.1.1-8
  • links: PTS
  • area: main
  • in suites: woody
  • size: 12,668 kB
  • ctags: 12,486
  • sloc: ansic: 107,685; cpp: 33,663; sh: 13,300; makefile: 2,926; yacc: 499; lex: 241; sed: 93; sql: 1
file content (185 lines) | stat: -rw-r--r-- 5,496 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/****************************************************
 * SQLGetPrivateProfileString
 *
 * Mostly used with odbc.ini files but can be used for odbcinst.ini
 *
 * IF pszFileName[0] == '/' THEN
 *		use pszFileName
 * ELSE
 *		use _odbcinst_ConfigModeINI() to get the complete file name for the current mode.
 *
 **************************************************
 * This code was created by Peter Harvey @ CodeByDesign.
 * Released under LGPL 28.JAN.99
 *
 * Contributions from...
 * -----------------------------------------------
 * Peter Harvey		- pharvey@codebydesign.com
 **************************************************/
#include <odbcinstext.h>

int SQLGetPrivateProfileString(	LPCSTR	pszSection,
								LPCSTR	pszEntry,
								LPCSTR	pszDefault,
								LPSTR	pRetBuffer,
								int		nRetBuffer,
								LPCSTR	pszFileName
								)
{
	HINI	hIni;
	int		nBufPos			= 0;
	char	szValue[INI_MAX_PROPERTY_VALUE+1];
	char	szFileName[ODBC_FILENAME_MAX+1];
	UWORD	nConfigMode;
    int     ini_done = 0;


	/* SANITY CHECKS */
	if ( pRetBuffer == NULL || nRetBuffer < 2 )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
		return -1;
	}
	if ( pszSection != NULL && pszEntry != NULL && pszDefault == NULL )
	{
        inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
		return -1;
	}

	*pRetBuffer = '\0';

	/*****************************************************
	 * SOME MS CODE (ie some drivers) MAY USE THIS FUNCTION TO GET ODBCINST INFO SO...
	 *****************************************************/
	if ( pszFileName != NULL )
	{
		if ( strstr( pszFileName, "odbcinst" ) || strstr( pszFileName, "ODBCINST" ) )
			return _SQLGetInstalledDrivers(	pszSection,	pszEntry, pszDefault, pRetBuffer, nRetBuffer );
	}

	/*****************************************************
	 * GATHER ALL RELEVANT DSN INFORMATION INTO AN hIni
	 *****************************************************/
	if ( pszFileName != 0 && pszFileName[0] == '/' )
	{
			if ( iniOpen( &hIni, (char*)pszFileName, '#', '[', ']', '=', TRUE )
                != INI_SUCCESS )
			{
				inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                    ODBC_ERROR_COMPONENT_NOT_FOUND, "" );
				return -1;
			}
	}
	else
	{
		nConfigMode		= ODBC_BOTH_DSN;
		SQLGetConfigMode( &nConfigMode );
		nBufPos 		= 0;
		szFileName[0]	= '\0';
		switch ( nConfigMode )
		{
		case ODBC_BOTH_DSN:
    	    if ( _odbcinst_UserINI( szFileName, TRUE ))
        	{
            	if ( iniOpen( &hIni, (char*) szFileName, '#', '[', ']', '=', TRUE )
                    == INI_SUCCESS )
            	{
                	ini_done = 1;
            	}
        	}
        	_odbcinst_SystemINI( szFileName, TRUE );
        	if ( !ini_done )
        	{
            	if ( iniOpen( &hIni, szFileName, '#', '[', ']', '=', TRUE )
                    != INI_SUCCESS )
            	{
                	inst_logPushMsg( __FILE__, __FILE__, __LINE__,
                        LOG_CRITICAL, ODBC_ERROR_COMPONENT_NOT_FOUND, "" );
                	return -1;
            	}
        	}
            else
            {
                iniAppend( hIni, szFileName );
            }
		break;

		case ODBC_USER_DSN:
			_odbcinst_UserINI( szFileName, TRUE );
			if ( iniOpen( &hIni, szFileName, '#', '[', ']', '=', TRUE )
                != INI_SUCCESS )
			{
				inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                    ODBC_ERROR_COMPONENT_NOT_FOUND, "" );
				return -1;
			}
		break;

		case ODBC_SYSTEM_DSN:
			_odbcinst_SystemINI( szFileName, TRUE );
			if ( iniOpen( &hIni, szFileName, '#', '[', ']', '=', TRUE )
                != INI_SUCCESS )
			{
				inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                    ODBC_ERROR_COMPONENT_NOT_FOUND, "" );
				return -1;
			}
		break;

		default:
        	inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL,
                ODBC_ERROR_GENERAL_ERR, "Invalid Config Mode" );
			return -1;
		}
	}
	
	/*****************************************************
	 * EXTRACT SECTIONS
	 *****************************************************/
	if ( pszSection == NULL )
	{
		_odbcinst_GetSections( hIni, pRetBuffer, nRetBuffer, &nBufPos );
	}
	/*****************************************************
	 * EXTRACT ENTRIES
	 *****************************************************/
	else if ( pszEntry == NULL )
	{
		_odbcinst_GetEntries( hIni, pszSection, pRetBuffer, nRetBuffer, &nBufPos );
	}
	/*****************************************************
	 * EXTRACT AN ENTRY
	 *****************************************************/
	else
	{
		if ( pszSection == NULL || pszEntry == NULL || pszDefault == NULL )
		{
			inst_logPushMsg( __FILE__, __FILE__, __LINE__, LOG_CRITICAL, ODBC_ERROR_GENERAL_ERR, "" );
			return -1;
		}

		/* TRY TO GET THE ONE ITEM MATCHING Section & Entry */
		if ( iniPropertySeek( hIni, (char *)pszSection, (char *)pszEntry, "" ) != INI_SUCCESS )
		{
            /*
             * (NG) this seems to be ignoring the length of pRetBuffer !!!
             */
			/* strncpy( pRetBuffer, pszDefault, INI_MAX_PROPERTY_VALUE ); */
			strncpy( pRetBuffer, pszDefault, nRetBuffer );
			pRetBuffer[ nRetBuffer - 1 ] = '\0';
		}
		else
		{
			iniValue( hIni, szValue );
			strncpy( pRetBuffer, szValue, nRetBuffer );
            pRetBuffer[ nRetBuffer - 1 ] = '\0';
			nBufPos = strlen( szValue );
		}
	}

	iniClose( hIni );

	return strlen( pRetBuffer );
}