1 'encoding UTF-8 Do not remove or change this line!
2 '**************************************************************************
3 '* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 '* Copyright 2008 by Sun Microsystems, Inc.
7 '* OpenOffice.org - a multi-platform office productivity suite
9 '* $RCSfile: inivalue.inc,v $
13 '* last change: $Author: jsi $ $Date: 2008-06-13 10:27:05 $
15 '* This file is part of OpenOffice.org.
17 '* OpenOffice.org is free software: you can redistribute it and/or modify
18 '* it under the terms of the GNU Lesser General Public License version 3
19 '* only, as published by the Free Software Foundation.
21 '* OpenOffice.org is distributed in the hope that it will be useful,
22 '* but WITHOUT ANY WARRANTY; without even the implied warranty of
23 '* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 '* GNU Lesser General Public License version 3 for more details
25 '* (a copy is included in the LICENSE file that accompanied this code).
27 '* You should have received a copy of the GNU Lesser General Public License
28 '* version 3 along with OpenOffice.org. If not, see
29 '* <http://www.openoffice.org/license.html>
30 '* for a copy of the LGPLv3 License.
32 '/************************************************************************
34 '* owner : thorsten.bosbach@sun.com
36 '* short description : routines to handle ini-files ( read/write items )
38 '*****************************************************************
40 ' #1 GetIniValue ' get a value of an entry
41 ' #1 GetIniValue2 ' subroutine for GetIniValue
42 ' #1 SetIniValue ' set a value of an entry
43 ' #1 SetIniValue2 ' subroutine for SetIniValue
44 ' #1 ChangeExt ' change the extension of an file
45 ' #1 AnhaengenAnDatei ' add a string into a file
46 ' #1 DateiSperren ' set the hidden flag for file
47 ' #1 DateiFreigeben ' reset the hidden flag for file
49 '\****************************************************************
51 function GetIniValue ( Datei$, Gruppe$, Variable$ ) as String
52 '/// wrapper for GetIniValue2 ///'
53 '///+ reads a value from an ini-file ///'
54 '///+ INPUT : name of ini-file; name of group (the one in braces []); the item (left of '=') ///'
55 '///+ OUTPUT: value (the right of the '=') ///'
56 if Dir(Datei$) = "" then
57 Warnlog "Error in GetIniValue(...):" + Datei$ + " not found"
61 GetIniValue = GetIniValue2( Datei$, Gruppe$, Variable$ ) ' Arbeiten
64 function SetIniValue( Datei$, Gruppe$, Variable$, Value$ ) as String
65 '/// wrapper for SetIniValue2 ///'
66 '///+ writes a value to an ini-file ///'
67 '///+ INPUT : name of ini-file; name of group (the one in braces []); the item (left of '='); value (the right of the '=') ///'
69 Dim FileNum as Integer
71 if Dir(Datei$) = "" then
72 WarnLog "Error in SetIniValue(...):" + Datei$ + " not found. File will be created now!"
74 Open Datei$ For Output As #FileNum ' make empty file
79 SetIniValue = SetIniValue2( Datei$, Gruppe$, Variable$, Value$ )
83 function GetIniValue2( Datei$, Gruppe$, Variable$ ) as String
84 '/// see the wrapper for it : GetIniValue ///'
85 Dim FileNum% : Dim GruppeOK% : Dim Pos% : Dim IniZeile$ : Dim IniZeile2$
93 Open Datei$ For Input As #FileNum%
94 do until EOF(#FileNum%) = True
95 Line input #FileNum%, IniZeile$
97 IniZeile$ = TRIM(IniZeile$)
98 iniZeile2$ = UCASE( IniZeile$ ) ' compare case insensitive
99 if GruppeOK% = FALSE then ' still no group
100 if IniZeile2$= "[" + UCASE( Gruppe$ ) + "]" then 'Is it the wanted group?
104 If Left(IniZeile2$, 1) = "[" then 'sadly new group - goodby
107 Pos% = Instr( IniZeile2$, "=" ) 'is the item valid?
108 if Pos%>0 then ' '=' not found
109 if Left( IniZeile2$ , Pos%-1 ) = UCASE( Variable$ ) then 'compare leftvalue
110 GetIniValue2 = Trim(Mid$( IniZeile$ , Pos%+ 1 )) 'return part right of '=' : with initial case
124 sub SetIniValue2( Datei$, Gruppe$, Variable$, Value$ ) as String
125 '/// see the wrapper for it : SetIniValue ///'
126 Dim DateiBak$ : Dim D$ : Dim IniZeile$ : Dim IniZeile2$
127 Dim FileBak% : Dim GruppeOK% : Dim Gefunden% : Dim FileNum% : Dim Pos%
130 DateiBak$ = ChangeExt( Datei$, "BAK" )
135 if Dir(DateiBak$)<>"" then
139 if Dir( Datei$ )<>"" then
141 name Datei$ as DateiBak$
144 Open Datei$ For Output As #FileNum%
145 Print #FileNum%, "[" + Trim(Gruppe$) + "]"
146 Print #FileNum%, Variable$ + "=" + Trim(Value$)
147 Close #FileNum% ' finished here
152 Open Datei$ For Output As #FileNum%
155 Open DateiBak$ For Input As #FileBak%
157 do until EOF(#FileBak%) = True
159 Line input #FileBak%, IniZeile$
161 IniZeile$ = TRIM(IniZeile$)
163 if IniZeile$ <> "" then
165 IniZeile2$ = UCASE( IniZeile$ )
167 if Left(IniZeile$, 1) = "[" then
168 if GruppeOK% = TRUE then 'groupchange
169 if Gefunden%=FALSE then
170 Print #FileNum%, Variable$ + "=" + Trim(Value$)
175 Print #FileNum%, "" 'empty line
176 Print #FileNum%, IniZeile$
177 if IniZeile2$= "[" + UCASE( Gruppe$ ) + "]" then
181 if GruppeOK% = TRUE then ' found group
183 Pos% = Instr( IniZeile$, "=" )
184 if Left( IniZeile2$ , Pos%-1 ) = UCASE( Variable$ ) then
185 IniZeile$ = Left( IniZeile$ , Pos% ) +Trim( Value$ )' after the '='
189 Print #FileNum%, IniZeile$
196 if Gefunden% = FALSE then
197 ' set new group and value
198 if GruppeOK%=FALSE then
200 Print #FileNum%, "[" + Trim(Gruppe$) + "]"
202 Print #FileNum%, Variable$ + "=" + Value$
212 sub AnhaengenAnDatei ( Datei as String, Texte as String )
213 '/// append a string at the end of the file ///'
214 '///+ INPUT : filename; string///'
219 Open Datei for Append as #FileNum%
221 Print #FileNum%, Texte
226 function ChangeExt( Datei$, Ext$ )as String
227 '/// change the extension of a file ///'
228 '///+ INPUT : filename; extension ///'
232 i% = InStr( Right(Datei$, 4 ) , "." )
235 ChangeExt = Datei$ +"."+Ext$
237 ChangeExt = Left( Datei$, Len(Datei$)-4+i% ) + Ext$
241 ChangeExt = Left( Datei$, Len(Datei$)-5+i% )
245 sub DateiSperren( Datei$ )
246 '/// set the hidden flag of a file; lock the file ///'
247 '///+ INPUT : filename ///'
251 if hFileExists ( Datei$ ) <> TRUE then
252 Warnlog "File '" + Datei$ + "' doesn't exist; exiting now!"
256 i% = GetAttr( Datei$ )
257 do while (i% AND 2) = 2 ' is file already locked?
258 Wait( int( 400 * Rnd + 5 )
259 i% = GetAttr( Datei$ )
261 SetAttr( Datei$ , i% OR 2 ) ' Lock
267 sub DateiFreigeben( Datei$ )
268 '/// reset the hidden flag of a file; release the file ///'
269 '///+ INPUT : filename ///'
273 if hFileExists ( Datei$ ) <> TRUE then
274 Warnlog "File '" + Datei$ + "' doesn't exist; exiting now!"
278 i% = GetAttr( Datei$ )
279 SetAttr( Datei$ , i% AND NOT 2 ) ' release