1 Attribute VB_Name
= "IniSupport"
2 '/*************************************************************************
4 ' * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 ' * Copyright 2008 by Sun Microsystems, Inc.
8 ' * OpenOffice.org - a multi-platform office productivity suite
10 ' * $RCSfile: IniSupport.bas,v $
11 ' * $Revision: 1.6.148.1 $
13 ' * This file is part of OpenOffice.org.
15 ' * OpenOffice.org is free software: you can redistribute it and/or modify
16 ' * it under the terms of the GNU Lesser General Public License version 3
17 ' * only, as published by the Free Software Foundation.
19 ' * OpenOffice.org is distributed in the hope that it will be useful,
20 ' * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ' * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ' * GNU Lesser General Public License version 3 for more details
23 ' * (a copy is included in the LICENSE file that accompanied this code).
25 ' * You should have received a copy of the GNU Lesser General Public License
26 ' * version 3 along with OpenOffice.org. If not, see
27 ' * <http://www.openoffice.org/license.html>
28 ' * for a copy of the LGPLv3 License.
30 ' ************************************************************************/
33 Private Declare Function GetPrivateProfileString
Lib "kernel32" _
34 Alias "GetPrivateProfileStringA" _
35 (ByVal lpSectionName
As String, _
36 ByVal lpKeyName
As Any
, _
37 ByVal lpDefault
As String, _
38 ByVal lpReturnedString
As String, _
39 ByVal nSize
As Long, _
40 ByVal lpFileName
As String) As Long
42 Private Declare Function WritePrivateProfileString
Lib "kernel32" _
43 Alias "WritePrivateProfileStringA" _
44 (ByVal lpSectionName
As String, _
45 ByVal lpKeyName
As Any
, _
46 ByVal lpString
As Any
, _
47 ByVal lpFileName
As String) As Long
50 Public Function ProfileGetItem(lpSectionName
As String, _
51 lpKeyName
As String, _
52 defaultValue
As String, _
53 inifile
As String) As String
55 'Retrieves a value from an ini file corresponding
56 'to the section and key name passed.
62 'call the API with the parameters passed.
63 'The return value is the length of the string
64 'in ret, including the terminating null. If a
65 'default value was passed, and the section or
66 'key name are not in the file, that value is
67 'returned. If no default value was passed (""),
68 'then success will = 0 if not found.
70 'Pad a string large enough to hold the data.
73 success
= GetPrivateProfileString(lpSectionName
, _
81 ProfileGetItem
= Left
$(ret, success
)
87 Public Sub ProfileDeleteItem(lpSectionName
As String, _
88 lpKeyName
As String, _
91 'this call will remove the keyname and its
92 'corresponding value from the section specified
93 'in lpSectionName. This is accomplished by passing
94 'vbNullString as the lpValue parameter. For example,
95 'assuming that an ini file had:
101 'and this sub was called passing "Colour2"
102 'as lpKeyName, the resulting ini file
108 Call WritePrivateProfileString(lpSectionName
, _
116 Public Sub ProfileDeleteSection(lpSectionName
As String, _
119 'this call will remove the entire section
120 'corresponding to lpSectionName. This is
121 'accomplished by passing vbNullString
122 'as both the lpKeyName and lpValue parameters.
123 'For example, assuming that an ini file had:
129 'and this sub was called passing "Colours"
130 'as lpSectionName, the resulting Colours
131 'section in the ini file would be deleted.
133 Call WritePrivateProfileString(lpSectionName
, _
140 Private Function StripNulls(startStrg
As String) As String
142 'take a string separated by nulls, split off 1 item, and shorten the string
143 'so the next item is ready for removal.
144 'The passed string must have a terminating null for this function to work correctly.
145 'If you remain in a loop, check this first!
150 pos
= InStr(1, startStrg
, Chr
$(0))
154 item
= Mid$(startStrg, 1, pos
- 1)
155 startStrg
= Mid$(startStrg, pos
+ 1, Len(startStrg
))
162 Public Function ProfileLoadList(lst
As ComboBox
, _
163 lpSectionName
As String, _
164 inifile
As String) As Long
168 Dim KeyData
As String
169 Dim lpKeyName
As String
172 ' call the API passing lpKeyName = null. This causes
173 ' the API to return a list of all keys under that section.
174 ' Pad the passed string large enough to hold the data.
177 success
= GetPrivateProfileString( _
178 lpSectionName
, vbNullString
, "", ret
, nSize
, inifile
)
180 ' The returned string is a null-separated list of key names,
181 ' terminated by a pair of null characters.
182 ' If the Get call was successful, success holds the length of the
183 ' string in ret up to but not including that second terminating null.
184 ' The ProfileGetItem function below extracts each key item using the
185 ' nulls as markers, so trim off the terminating null.
188 'trim terminating null and trailing spaces
189 ret
= Left
$(ret, success
)
191 'with the resulting string extract each element
193 'strip off an item (i.e. "Item1", "Item2")
194 lpKeyName
= StripNulls(ret
)
196 'pass the lpKeyName received to a routine that
197 'again calls GetPrivateProfileString, this
198 'time passing the real key name. Returned
199 'is the value associated with that key,
200 'ie the "Apple" corresponding to the ini
202 KeyData
= ProfileGetItem( _
203 lpSectionName
, lpKeyName
, "", inifile
)
205 'add the item retruned to the listbox
211 'return the number of items as an
212 'indicator of success
213 ProfileLoadList
= lst
.ListCount
216 Public Function ProfileLoadDict(dict
As Scripting
.Dictionary
, _
217 lpSectionName
As String, _
218 inifile
As String) As Long
222 Dim KeyData
As String
223 Dim lpKeyName
As String
226 ' call the API passing lpKeyName = null. This causes
227 ' the API to return a list of all keys under that section.
228 ' Pad the passed string large enough to hold the data.
231 success
= GetPrivateProfileString( _
232 lpSectionName
, vbNullString
, "", ret
, nSize
, inifile
)
234 ' The returned string is a null-separated list of key names,
235 ' terminated by a pair of null characters.
236 ' If the Get call was successful, success holds the length of the
237 ' string in ret up to but not including that second terminating null.
238 ' The ProfileGetItem function below extracts each key item using the
239 ' nulls as markers, so trim off the terminating null.
242 'trim terminating null and trailing spaces
243 ret
= Left
$(ret, success
)
245 'with the resulting string extract each element
247 'strip off an item (i.e. "Item1", "Item2")
248 lpKeyName
= StripNulls(ret
)
250 'pass the lpKeyName received to a routine that
251 'again calls GetPrivateProfileString, this
252 'time passing the real key name. Returned
253 'is the value associated with that key,
254 'ie the "Apple" corresponding to the ini
256 KeyData
= ProfileGetItem( _
257 lpSectionName
, lpKeyName
, "", inifile
)
259 dict
.add lpKeyName
, KeyData
264 ProfileLoadDict
= dict
.count