xref: /AOO41X/main/testgraphical/ui/java/ConvwatchGUIProject/src/IniFile.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir import java.io.File;
29*cdf0e10cSrcweir import java.io.RandomAccessFile;
30*cdf0e10cSrcweir import java.util.ArrayList;
31*cdf0e10cSrcweir import java.util.Enumeration;
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir /**
35*cdf0e10cSrcweir  * Simple implementation of a inifile manager
36*cdf0e10cSrcweir  */
37*cdf0e10cSrcweir class GlobalLogWriter
38*cdf0e10cSrcweir {
39*cdf0e10cSrcweir     public static void println(String _s)
40*cdf0e10cSrcweir     {
41*cdf0e10cSrcweir         System.out.println(_s);
42*cdf0e10cSrcweir     }
43*cdf0e10cSrcweir }
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir /**
46*cdf0e10cSrcweir    Helper class to give a simple API to read/write windows like ini files
47*cdf0e10cSrcweir */
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir /* public */ // is only need, if we need this class outside package convwatch
50*cdf0e10cSrcweir public class IniFile implements Enumeration
51*cdf0e10cSrcweir {
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir     /**
54*cdf0e10cSrcweir      * internal representation of the ini file content.
55*cdf0e10cSrcweir      * Problem, if ini file changed why other write something difference, we don't realise this.
56*cdf0e10cSrcweir      */
57*cdf0e10cSrcweir     private String m_sFilename;
58*cdf0e10cSrcweir     // private File m_aFile;
59*cdf0e10cSrcweir     private ArrayList<String> m_aList;
60*cdf0e10cSrcweir     boolean m_bListContainUnsavedChanges = false;
61*cdf0e10cSrcweir     private int m_aEnumerationPos = 0;
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir     /**
64*cdf0e10cSrcweir        open a ini file by it's name
65*cdf0e10cSrcweir        @param _sFilename string a filename, if the file doesn't exist, a new empty ini file will create.
66*cdf0e10cSrcweir        write back to disk only if there are really changes.
67*cdf0e10cSrcweir     */
68*cdf0e10cSrcweir     public IniFile(String _sFilename)
69*cdf0e10cSrcweir         {
70*cdf0e10cSrcweir             m_sFilename = _sFilename;
71*cdf0e10cSrcweir             // m_aFile = new File(_sFilename);
72*cdf0e10cSrcweir             m_aList = loadLines();
73*cdf0e10cSrcweir             m_aEnumerationPos = findNextSection(0);
74*cdf0e10cSrcweir         }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     /**
77*cdf0e10cSrcweir        open a ini file by it's name
78*cdf0e10cSrcweir        @param _aFile a java.io.File object, if the file doesn't exist, a new empty ini file will create.
79*cdf0e10cSrcweir        write back to disk only if there are really changes.
80*cdf0e10cSrcweir     */
81*cdf0e10cSrcweir     public IniFile(File _aFile)
82*cdf0e10cSrcweir     {
83*cdf0e10cSrcweir         m_sFilename = _aFile.getAbsolutePath();
84*cdf0e10cSrcweir         m_aList = loadLines();
85*cdf0e10cSrcweir         m_aEnumerationPos = findNextSection(0);
86*cdf0e10cSrcweir     }
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir     public void insertFirstComment(String[] _aList)
89*cdf0e10cSrcweir         {
90*cdf0e10cSrcweir             if (m_aList.size() == 0)
91*cdf0e10cSrcweir             {
92*cdf0e10cSrcweir                 // can only insert if there is nothing else already in the ini file
93*cdf0e10cSrcweir                 for (int i = 0; i < _aList.length; i++)
94*cdf0e10cSrcweir                 {
95*cdf0e10cSrcweir                     m_aList.add(_aList[i]);
96*cdf0e10cSrcweir                 }
97*cdf0e10cSrcweir             }
98*cdf0e10cSrcweir         }
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir     private ArrayList<String> loadLines()
101*cdf0e10cSrcweir         {
102*cdf0e10cSrcweir             ArrayList<String> aLines = new ArrayList<String>();
103*cdf0e10cSrcweir             File aFile = new File(m_sFilename);
104*cdf0e10cSrcweir             if (!aFile.exists())
105*cdf0e10cSrcweir             {
106*cdf0e10cSrcweir                 // GlobalLogWriter.println("couldn't find file '" + m_sFilename + "', will be created.");
107*cdf0e10cSrcweir                 // DebugHelper.exception(BasicErrorCode.SbERR_FILE_NOT_FOUND, "");
108*cdf0e10cSrcweir                 // m_bListContainUnsavedChanges = false;
109*cdf0e10cSrcweir                 return aLines;
110*cdf0e10cSrcweir             }
111*cdf0e10cSrcweir             RandomAccessFile aReader = null;
112*cdf0e10cSrcweir             // BufferedReader aReader;
113*cdf0e10cSrcweir             try
114*cdf0e10cSrcweir             {
115*cdf0e10cSrcweir                 aReader = new RandomAccessFile(aFile, "r");
116*cdf0e10cSrcweir                 String aLine = "";
117*cdf0e10cSrcweir                 while (aLine != null)
118*cdf0e10cSrcweir                 {
119*cdf0e10cSrcweir                     aLine = aReader.readLine();
120*cdf0e10cSrcweir                     if (aLine != null && aLine.length() > 0)
121*cdf0e10cSrcweir                     {
122*cdf0e10cSrcweir                         aLines.add(aLine);
123*cdf0e10cSrcweir                     }
124*cdf0e10cSrcweir                 }
125*cdf0e10cSrcweir             }
126*cdf0e10cSrcweir             catch (java.io.FileNotFoundException fne)
127*cdf0e10cSrcweir             {
128*cdf0e10cSrcweir                 GlobalLogWriter.println("couldn't open file " + m_sFilename);
129*cdf0e10cSrcweir                 GlobalLogWriter.println("Message: " + fne.getMessage());
130*cdf0e10cSrcweir                 // DebugHelper.exception(BasicErrorCode.SbERR_FILE_NOT_FOUND, "");
131*cdf0e10cSrcweir             }
132*cdf0e10cSrcweir             catch (java.io.IOException ie)
133*cdf0e10cSrcweir             {
134*cdf0e10cSrcweir                 GlobalLogWriter.println("Exception occurs while reading from file " + m_sFilename);
135*cdf0e10cSrcweir                 GlobalLogWriter.println("Message: " + ie.getMessage());
136*cdf0e10cSrcweir                 // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, ie.getMessage());
137*cdf0e10cSrcweir             }
138*cdf0e10cSrcweir             try
139*cdf0e10cSrcweir             {
140*cdf0e10cSrcweir                 aReader.close();
141*cdf0e10cSrcweir             }
142*cdf0e10cSrcweir             catch (java.io.IOException ie)
143*cdf0e10cSrcweir             {
144*cdf0e10cSrcweir                 GlobalLogWriter.println("Couldn't close file " + m_sFilename);
145*cdf0e10cSrcweir                 GlobalLogWriter.println("Message: " + ie.getMessage());
146*cdf0e10cSrcweir                 // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, ie.getMessage());
147*cdf0e10cSrcweir             }
148*cdf0e10cSrcweir             return aLines;
149*cdf0e10cSrcweir         }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir     /**
152*cdf0e10cSrcweir      * @return true, if the ini file contain some readable data
153*cdf0e10cSrcweir      */
154*cdf0e10cSrcweir     public boolean is()
155*cdf0e10cSrcweir         {
156*cdf0e10cSrcweir             return m_aList.size() > 1 ? true : false;
157*cdf0e10cSrcweir         }
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir     /**
160*cdf0e10cSrcweir      * Check if a given Section and Key exists in the ini file
161*cdf0e10cSrcweir      * @param _sSectionName
162*cdf0e10cSrcweir      * @param _sKey
163*cdf0e10cSrcweir      * @return true if the given Section, Key exists, now you can get the value
164*cdf0e10cSrcweir      */
165*cdf0e10cSrcweir     public boolean hasValue(String _sSectionName, String _sKey)
166*cdf0e10cSrcweir         {
167*cdf0e10cSrcweir             int n = findKey(_sSectionName, _sKey);
168*cdf0e10cSrcweir             if (n > 0)
169*cdf0e10cSrcweir             {
170*cdf0e10cSrcweir                 return true;
171*cdf0e10cSrcweir             }
172*cdf0e10cSrcweir             return false;
173*cdf0e10cSrcweir         }
174*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     private boolean isRemark(String _sLine)
177*cdf0e10cSrcweir         {
178*cdf0e10cSrcweir             if (((_sLine.length() < 2)) ||
179*cdf0e10cSrcweir                 (_sLine.startsWith("#")) ||
180*cdf0e10cSrcweir                 (_sLine.startsWith(";")))
181*cdf0e10cSrcweir             {
182*cdf0e10cSrcweir                 return true;
183*cdf0e10cSrcweir             }
184*cdf0e10cSrcweir             return false;
185*cdf0e10cSrcweir         }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir     private String getItem(int i)
188*cdf0e10cSrcweir         {
189*cdf0e10cSrcweir             return m_aList.get(i);
190*cdf0e10cSrcweir         }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir     private String buildSectionName(String _sSectionName)
193*cdf0e10cSrcweir         {
194*cdf0e10cSrcweir             String sFindSection = "[" + _sSectionName + "]";
195*cdf0e10cSrcweir             return sFindSection;
196*cdf0e10cSrcweir         }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir     private String sectionToString(String _sSectionName)
199*cdf0e10cSrcweir         {
200*cdf0e10cSrcweir             String sKeyName = _sSectionName;
201*cdf0e10cSrcweir             if (sKeyName.startsWith("[") &&
202*cdf0e10cSrcweir                 sKeyName.endsWith("]"))
203*cdf0e10cSrcweir             {
204*cdf0e10cSrcweir                 sKeyName = sKeyName.substring(1, sKeyName.length() - 1);
205*cdf0e10cSrcweir             }
206*cdf0e10cSrcweir             return sKeyName;
207*cdf0e10cSrcweir         }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir     private String toLowerIfNeed(String _sName)
210*cdf0e10cSrcweir         {
211*cdf0e10cSrcweir             return _sName.toLowerCase();
212*cdf0e10cSrcweir         }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir     // return the number where this section starts
215*cdf0e10cSrcweir     private int findSection(String _sSection)
216*cdf0e10cSrcweir         {
217*cdf0e10cSrcweir             String sFindSection = toLowerIfNeed(buildSectionName(_sSection));
218*cdf0e10cSrcweir             // ----------- find _sSection ---------------
219*cdf0e10cSrcweir             int i;
220*cdf0e10cSrcweir             for (i = 0; i < m_aList.size(); i++)
221*cdf0e10cSrcweir             {
222*cdf0e10cSrcweir                 String sLine = toLowerIfNeed(getItem(i).trim());
223*cdf0e10cSrcweir                 if (isRemark(sLine))
224*cdf0e10cSrcweir                 {
225*cdf0e10cSrcweir                     continue;
226*cdf0e10cSrcweir                 }
227*cdf0e10cSrcweir                 if (sFindSection.equals("[]"))
228*cdf0e10cSrcweir                 {
229*cdf0e10cSrcweir                     // special case, empty Section.
230*cdf0e10cSrcweir                     return i - 1;
231*cdf0e10cSrcweir                 }
232*cdf0e10cSrcweir                 if (sLine.startsWith(sFindSection))
233*cdf0e10cSrcweir                 {
234*cdf0e10cSrcweir                     return i;
235*cdf0e10cSrcweir                 }
236*cdf0e10cSrcweir             }
237*cdf0e10cSrcweir             return -1;
238*cdf0e10cSrcweir         }
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir     /**
241*cdf0e10cSrcweir      * Checks if a given section exists in the ini file
242*cdf0e10cSrcweir      * @param _sSection
243*cdf0e10cSrcweir      * @return true if the given _sSection was found
244*cdf0e10cSrcweir      */
245*cdf0e10cSrcweir     public boolean hasSection(String _sSection)
246*cdf0e10cSrcweir         {
247*cdf0e10cSrcweir             int i = findSection(_sSection);
248*cdf0e10cSrcweir             if (i == -1)
249*cdf0e10cSrcweir             {
250*cdf0e10cSrcweir                 return false;
251*cdf0e10cSrcweir             }
252*cdf0e10cSrcweir             return true;
253*cdf0e10cSrcweir         }
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir     // return the line number, where the key is found.
256*cdf0e10cSrcweir     private int findKey(String _sSection, String _sKey)
257*cdf0e10cSrcweir         {
258*cdf0e10cSrcweir             int i = findSection(_sSection);
259*cdf0e10cSrcweir             if (i == -1)
260*cdf0e10cSrcweir             {
261*cdf0e10cSrcweir                 // Section not found, therefore the value can't exist
262*cdf0e10cSrcweir                 return -1;
263*cdf0e10cSrcweir             }
264*cdf0e10cSrcweir             return findKeyFromKnownSection(i, _sKey);
265*cdf0e10cSrcweir         }
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir     // i must be the index in the list, where the well known section starts
268*cdf0e10cSrcweir     private int findKeyFromKnownSection(int _nSectionIndex, String _sKey)
269*cdf0e10cSrcweir         {
270*cdf0e10cSrcweir             _sKey = toLowerIfNeed(_sKey);
271*cdf0e10cSrcweir             for (int j = _nSectionIndex + 1; j < m_aList.size(); j++)
272*cdf0e10cSrcweir             {
273*cdf0e10cSrcweir                 String sLine = getItem(j).trim();
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir                 if (isRemark(sLine))
276*cdf0e10cSrcweir                 {
277*cdf0e10cSrcweir                     continue;
278*cdf0e10cSrcweir                 }
279*cdf0e10cSrcweir                 if (sLine.startsWith("[") /* && sLine.endsWith("]") */)
280*cdf0e10cSrcweir                 {
281*cdf0e10cSrcweir                     // TODO: due to the fact we would like to insert an empty line before new sections
282*cdf0e10cSrcweir                     // TODO: we should check if we are in an empty line and if, go back one line.
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir                     // found end.
285*cdf0e10cSrcweir                     break;
286*cdf0e10cSrcweir                 }
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir                 int nEqual = sLine.indexOf("=");
289*cdf0e10cSrcweir                 if (nEqual >= 0)
290*cdf0e10cSrcweir                 {
291*cdf0e10cSrcweir                     String sKey = toLowerIfNeed(sLine.substring(0, nEqual).trim());
292*cdf0e10cSrcweir                     if (sKey.equals(_sKey))
293*cdf0e10cSrcweir                     {
294*cdf0e10cSrcweir                         return j;
295*cdf0e10cSrcweir                     }
296*cdf0e10cSrcweir                 }
297*cdf0e10cSrcweir             }
298*cdf0e10cSrcweir             return -1;
299*cdf0e10cSrcweir         }
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir     // i must be the index in the list, where the well known section starts
302*cdf0e10cSrcweir     private int findLastKnownKeyIndex(int _nSectionIndex, String _sKey)
303*cdf0e10cSrcweir         {
304*cdf0e10cSrcweir             _sKey = toLowerIfNeed(_sKey);
305*cdf0e10cSrcweir             int i = _nSectionIndex + 1;
306*cdf0e10cSrcweir             for (int j = i; j < m_aList.size(); j++)
307*cdf0e10cSrcweir             {
308*cdf0e10cSrcweir                 String sLine = getItem(j).trim();
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir                 if (isRemark(sLine))
311*cdf0e10cSrcweir                 {
312*cdf0e10cSrcweir                     continue;
313*cdf0e10cSrcweir                 }
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir                 if (sLine.startsWith("[") /* && sLine.endsWith("]") */)
316*cdf0e10cSrcweir                 {
317*cdf0e10cSrcweir                     // found end.
318*cdf0e10cSrcweir                     return j;
319*cdf0e10cSrcweir                 }
320*cdf0e10cSrcweir 
321*cdf0e10cSrcweir                 int nEqual = sLine.indexOf("=");
322*cdf0e10cSrcweir                 if (nEqual >= 0)
323*cdf0e10cSrcweir                 {
324*cdf0e10cSrcweir                     String sKey = toLowerIfNeed(sLine.substring(0, nEqual).trim());
325*cdf0e10cSrcweir                     if (sKey.equals(_sKey))
326*cdf0e10cSrcweir                     {
327*cdf0e10cSrcweir                         return j;
328*cdf0e10cSrcweir                     }
329*cdf0e10cSrcweir                 }
330*cdf0e10cSrcweir             }
331*cdf0e10cSrcweir             return i;
332*cdf0e10cSrcweir         }
333*cdf0e10cSrcweir 
334*cdf0e10cSrcweir     private String getValue(int _nIndex)
335*cdf0e10cSrcweir         {
336*cdf0e10cSrcweir             String sLine = getItem(_nIndex).trim();
337*cdf0e10cSrcweir             if (isRemark(sLine))
338*cdf0e10cSrcweir             {
339*cdf0e10cSrcweir                 return "";
340*cdf0e10cSrcweir             }
341*cdf0e10cSrcweir             int nEqual = sLine.indexOf("=");
342*cdf0e10cSrcweir             if (nEqual >= 0)
343*cdf0e10cSrcweir             {
344*cdf0e10cSrcweir                 String sKey = sLine.substring(0, nEqual).trim();
345*cdf0e10cSrcweir                 String sValue = sLine.substring(nEqual + 1).trim();
346*cdf0e10cSrcweir                 return sValue;
347*cdf0e10cSrcweir             }
348*cdf0e10cSrcweir             return "";
349*cdf0e10cSrcweir         }
350*cdf0e10cSrcweir 
351*cdf0e10cSrcweir     /**
352*cdf0e10cSrcweir        @param _sSection string
353*cdf0e10cSrcweir        @param _sKey string
354*cdf0e10cSrcweir        @return the value found in the inifile which is given by the section and key parameter
355*cdf0e10cSrcweir     */
356*cdf0e10cSrcweir     // private int m_nCurrentPosition;
357*cdf0e10cSrcweir     // private String m_sOldKey;
358*cdf0e10cSrcweir     public String getValue(String _sSection, String _sKey)
359*cdf0e10cSrcweir         {
360*cdf0e10cSrcweir             String sValue = "";
361*cdf0e10cSrcweir             int m_nCurrentPosition = findKey(_sSection, _sKey);
362*cdf0e10cSrcweir             if (m_nCurrentPosition == -1)
363*cdf0e10cSrcweir             {
364*cdf0e10cSrcweir                 // Section not found, therefore the value can't exist
365*cdf0e10cSrcweir                 return "";
366*cdf0e10cSrcweir             }
367*cdf0e10cSrcweir 
368*cdf0e10cSrcweir             // m_sOldKey = _sKey;
369*cdf0e10cSrcweir             sValue = getValue(m_nCurrentPosition);
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir             return sValue;
372*cdf0e10cSrcweir         }
373*cdf0e10cSrcweir 
374*cdf0e10cSrcweir //    private String getNextValue()
375*cdf0e10cSrcweir //    {
376*cdf0e10cSrcweir //        if (m_nCurrentPosition >= 0)
377*cdf0e10cSrcweir //        {
378*cdf0e10cSrcweir //            ++m_nCurrentPosition;
379*cdf0e10cSrcweir //            String sValue = getValue(m_nCurrentPosition);
380*cdf0e10cSrcweir //            return sValue;
381*cdf0e10cSrcweir //        }
382*cdf0e10cSrcweir //        return "";
383*cdf0e10cSrcweir //    }
384*cdf0e10cSrcweir     /**
385*cdf0e10cSrcweir      * Returns the value at Section, Key converted to an integer
386*cdf0e10cSrcweir      * Check with hasValue(Section, Key) to check before you get into trouble.
387*cdf0e10cSrcweir      * @param _sSection
388*cdf0e10cSrcweir      * @param _sKey
389*cdf0e10cSrcweir      * @param _nDefault if there is a problem, key not found... this value will return
390*cdf0e10cSrcweir      * @return the value as integer if possible to convert, if not return default value.
391*cdf0e10cSrcweir      */
392*cdf0e10cSrcweir     public int getIntValue(String _sSection, String _sKey, int _nDefault)
393*cdf0e10cSrcweir         {
394*cdf0e10cSrcweir             String sValue = getValue(_sSection, _sKey);
395*cdf0e10cSrcweir             int nValue = _nDefault;
396*cdf0e10cSrcweir             if (sValue.length() > 0)
397*cdf0e10cSrcweir             {
398*cdf0e10cSrcweir                 try
399*cdf0e10cSrcweir                 {
400*cdf0e10cSrcweir                     nValue = Integer.valueOf(sValue).intValue();
401*cdf0e10cSrcweir                 }
402*cdf0e10cSrcweir                 catch (java.lang.NumberFormatException e)
403*cdf0e10cSrcweir                 {
404*cdf0e10cSrcweir                     GlobalLogWriter.println("IniFile.getIntValue(): Caught a number format exception, return the default value.");
405*cdf0e10cSrcweir                 }
406*cdf0e10cSrcweir             }
407*cdf0e10cSrcweir             return nValue;
408*cdf0e10cSrcweir         }
409*cdf0e10cSrcweir 
410*cdf0e10cSrcweir /**
411*cdf0e10cSrcweir  * close a open inifile.
412*cdf0e10cSrcweir  * If there are changes, all changes will store back to disk.
413*cdf0e10cSrcweir  */
414*cdf0e10cSrcweir     public void close()
415*cdf0e10cSrcweir         {
416*cdf0e10cSrcweir             store();
417*cdf0e10cSrcweir         }
418*cdf0e10cSrcweir 
419*cdf0e10cSrcweir     /**
420*cdf0e10cSrcweir        write back the ini file to the disk, only if there exist changes
421*cdf0e10cSrcweir        * @deprecated use close() instead!
422*cdf0e10cSrcweir        */
423*cdf0e10cSrcweir 
424*cdf0e10cSrcweir     // TODO: make private
425*cdf0e10cSrcweir     private void store()
426*cdf0e10cSrcweir         {
427*cdf0e10cSrcweir             if (m_bListContainUnsavedChanges == false)
428*cdf0e10cSrcweir             {
429*cdf0e10cSrcweir                 // nothing has changed, so no need to store
430*cdf0e10cSrcweir                 return;
431*cdf0e10cSrcweir             }
432*cdf0e10cSrcweir 
433*cdf0e10cSrcweir             File aFile = new File(m_sFilename);
434*cdf0e10cSrcweir             if (aFile.exists())
435*cdf0e10cSrcweir             {
436*cdf0e10cSrcweir                 // System.out.println("couldn't find file " + m_sFilename);
437*cdf0e10cSrcweir                 // TODO: little bit unsafe here, first rename, after write is complete, delete the old.
438*cdf0e10cSrcweir                 aFile.delete();
439*cdf0e10cSrcweir                 if (aFile.exists())
440*cdf0e10cSrcweir                 {
441*cdf0e10cSrcweir                     GlobalLogWriter.println("Couldn't delete the file " + m_sFilename);
442*cdf0e10cSrcweir                     return;
443*cdf0e10cSrcweir                     // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, "Couldn't delete the file " + m_sFilename);
444*cdf0e10cSrcweir                 }
445*cdf0e10cSrcweir             }
446*cdf0e10cSrcweir             // if (! aFile.canWrite())
447*cdf0e10cSrcweir             // {
448*cdf0e10cSrcweir             //    System.out.println("Couldn't write to file " + m_sFilename);
449*cdf0e10cSrcweir             //    DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, "");
450*cdf0e10cSrcweir             // }
451*cdf0e10cSrcweir             try
452*cdf0e10cSrcweir             {
453*cdf0e10cSrcweir                 RandomAccessFile aWriter = new RandomAccessFile(aFile, "rw");
454*cdf0e10cSrcweir                 for (int i = 0; i < m_aList.size(); i++)
455*cdf0e10cSrcweir                 {
456*cdf0e10cSrcweir                     String sLine = getItem(i);
457*cdf0e10cSrcweir                     if (sLine.startsWith("["))
458*cdf0e10cSrcweir                     {
459*cdf0e10cSrcweir                         // write an extra empty line before next section.
460*cdf0e10cSrcweir                         aWriter.writeByte((int) '\n');
461*cdf0e10cSrcweir                     }
462*cdf0e10cSrcweir                     aWriter.writeBytes(sLine);
463*cdf0e10cSrcweir                     aWriter.writeByte((int) '\n');
464*cdf0e10cSrcweir                 }
465*cdf0e10cSrcweir                 aWriter.close();
466*cdf0e10cSrcweir             }
467*cdf0e10cSrcweir             catch (java.io.FileNotFoundException fne)
468*cdf0e10cSrcweir             {
469*cdf0e10cSrcweir                 GlobalLogWriter.println("couldn't open file for writing " + m_sFilename);
470*cdf0e10cSrcweir                 GlobalLogWriter.println("Message: " + fne.getMessage());
471*cdf0e10cSrcweir                 // DebugHelper.exception(BasicErrorCode.SbERR_FILE_NOT_FOUND, "");
472*cdf0e10cSrcweir             }
473*cdf0e10cSrcweir             catch (java.io.IOException ie)
474*cdf0e10cSrcweir             {
475*cdf0e10cSrcweir                 GlobalLogWriter.println("Exception occurs while writing to file " + m_sFilename);
476*cdf0e10cSrcweir                 GlobalLogWriter.println("Message: " + ie.getMessage());
477*cdf0e10cSrcweir                 // DebugHelper.exception(BasicErrorCode.SbERR_INTERNAL_ERROR, ie.getMessage());
478*cdf0e10cSrcweir             }
479*cdf0e10cSrcweir         }
480*cdf0e10cSrcweir 
481*cdf0e10cSrcweir     public void insertValue(String _sSection, String _sKey, int _nValue)
482*cdf0e10cSrcweir         {
483*cdf0e10cSrcweir             insertValue(_sSection, _sKey, String.valueOf(_nValue));
484*cdf0e10cSrcweir         }
485*cdf0e10cSrcweir 
486*cdf0e10cSrcweir     public void insertValue(String _sSection, String _sKey, long _nValue)
487*cdf0e10cSrcweir         {
488*cdf0e10cSrcweir             insertValue(_sSection, _sKey, String.valueOf(_nValue));
489*cdf0e10cSrcweir         }
490*cdf0e10cSrcweir 
491*cdf0e10cSrcweir     /**
492*cdf0e10cSrcweir        insert a value
493*cdf0e10cSrcweir        there are 3 cases
494*cdf0e10cSrcweir        1. section doesn't exist, goto end and insert a new section, insert a new key value pair
495*cdf0e10cSrcweir        2. section exist but key not, search section, search key, if key is -1 get last known key position and insert new key value pair there
496*cdf0e10cSrcweir        3. section exist and key exist, remove the old key and insert the key value pair at the same position
497*cdf0e10cSrcweir      * @param _sSection
498*cdf0e10cSrcweir      * @param _sKey
499*cdf0e10cSrcweir      * @param _sValue
500*cdf0e10cSrcweir      */
501*cdf0e10cSrcweir     public void insertValue(String _sSection, String _sKey, String _sValue)
502*cdf0e10cSrcweir         {
503*cdf0e10cSrcweir             int i = findSection(_sSection);
504*cdf0e10cSrcweir             if (i == -1)
505*cdf0e10cSrcweir             {
506*cdf0e10cSrcweir                 // case 1: section doesn't exist
507*cdf0e10cSrcweir                 String sFindSection = buildSectionName(_sSection);
508*cdf0e10cSrcweir 
509*cdf0e10cSrcweir                 // TODO: before create a new Section, insert a empty line
510*cdf0e10cSrcweir                 m_aList.add(sFindSection);
511*cdf0e10cSrcweir                 if (_sKey.length() > 0)
512*cdf0e10cSrcweir                 {
513*cdf0e10cSrcweir                     String sKeyValuePair = _sKey + "=" + _sValue;
514*cdf0e10cSrcweir                     m_aList.add(sKeyValuePair);
515*cdf0e10cSrcweir                 }
516*cdf0e10cSrcweir                 m_bListContainUnsavedChanges = true;
517*cdf0e10cSrcweir                 return;
518*cdf0e10cSrcweir             }
519*cdf0e10cSrcweir             int j = findKeyFromKnownSection(i, _sKey);
520*cdf0e10cSrcweir             if (j == -1)
521*cdf0e10cSrcweir             {
522*cdf0e10cSrcweir                 // case 2: section exist, but not the key
523*cdf0e10cSrcweir                 j = findLastKnownKeyIndex(i, _sKey);
524*cdf0e10cSrcweir                 if (_sKey.length() > 0)
525*cdf0e10cSrcweir                 {
526*cdf0e10cSrcweir                     String sKeyValuePair = _sKey + "=" + _sValue;
527*cdf0e10cSrcweir                     m_aList.add(j, sKeyValuePair);
528*cdf0e10cSrcweir                     m_bListContainUnsavedChanges = true;
529*cdf0e10cSrcweir                 }
530*cdf0e10cSrcweir                 return;
531*cdf0e10cSrcweir             }
532*cdf0e10cSrcweir             else
533*cdf0e10cSrcweir             {
534*cdf0e10cSrcweir                 // case 3: section exist, and also the key
535*cdf0e10cSrcweir                 String sKeyValuePair = _sKey + "=" + _sValue;
536*cdf0e10cSrcweir                 m_aList.set(j, sKeyValuePair);
537*cdf0e10cSrcweir                 m_bListContainUnsavedChanges = true;
538*cdf0e10cSrcweir             }
539*cdf0e10cSrcweir         }
540*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
541*cdf0e10cSrcweir     // String replaceEvaluatedValue(String _sSection, String _sValue)
542*cdf0e10cSrcweir     //     {
543*cdf0e10cSrcweir     //         String sValue = _sValue;
544*cdf0e10cSrcweir     //         int nIndex = 0;
545*cdf0e10cSrcweir     //         while (( nIndex = sValue.indexOf("$(", nIndex)) >= 0)
546*cdf0e10cSrcweir     //         {
547*cdf0e10cSrcweir     //             int nNextIndex = sValue.indexOf(")", nIndex);
548*cdf0e10cSrcweir     //             if (nNextIndex >= 0)
549*cdf0e10cSrcweir     //             {
550*cdf0e10cSrcweir     //                 String sKey = sValue.substring(nIndex + 2, nNextIndex);
551*cdf0e10cSrcweir     //                 String sNewValue = getValue(_sSection, sKey);
552*cdf0e10cSrcweir     //                 if (sNewValue != null && sNewValue.length() > 0)
553*cdf0e10cSrcweir     //                 {
554*cdf0e10cSrcweir     //                     String sRegexpKey = "\\$\\(" + sKey + "\\)";
555*cdf0e10cSrcweir     //                     sValue = sValue.replaceAll(sRegexpKey, sNewValue);
556*cdf0e10cSrcweir     //                 }
557*cdf0e10cSrcweir     //                 nIndex = nNextIndex;
558*cdf0e10cSrcweir     //             }
559*cdf0e10cSrcweir     //             else
560*cdf0e10cSrcweir     //             {
561*cdf0e10cSrcweir     //                 nIndex += 2;
562*cdf0e10cSrcweir     //             }
563*cdf0e10cSrcweir     //         }
564*cdf0e10cSrcweir     //         return sValue;
565*cdf0e10cSrcweir     //     }
566*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
567*cdf0e10cSrcweir 
568*cdf0e10cSrcweir     // public String getLocalEvaluatedValue(String _sSection, String _sKey)
569*cdf0e10cSrcweir     //     {
570*cdf0e10cSrcweir     //         String sValue = getValue(_sSection, _sKey);
571*cdf0e10cSrcweir     //         sValue = replaceEvaluatedValue(_sSection, sValue);
572*cdf0e10cSrcweir     //         return sValue;
573*cdf0e10cSrcweir     //     }
574*cdf0e10cSrcweir 
575*cdf0e10cSrcweir     // -----------------------------------------------------------------------------
576*cdf0e10cSrcweir 
577*cdf0e10cSrcweir     // this is a special behaviour.
578*cdf0e10cSrcweir     // public String getGlobalLocalEvaluatedValue(String _sSection, String _sKey)
579*cdf0e10cSrcweir     //     {
580*cdf0e10cSrcweir     //         String sGlobalValue = getKey("global", _sKey);
581*cdf0e10cSrcweir     //         String sLocalValue = getKey(_sSection, _sKey);
582*cdf0e10cSrcweir     //         if (sLocalValue.length() == 0)
583*cdf0e10cSrcweir     //         {
584*cdf0e10cSrcweir     //             sGlobalValue = replaceEvaluatedKey(_sSection, sGlobalValue);
585*cdf0e10cSrcweir     //             sGlobalValue = replaceEvaluatedKey("global", sGlobalValue);
586*cdf0e10cSrcweir     //             return sGlobalValue;
587*cdf0e10cSrcweir     //         }
588*cdf0e10cSrcweir     //         sLocalValue = replaceEvaluatedKey(_sSection, sLocalValue);
589*cdf0e10cSrcweir     //         sLocalValue = replaceEvaluatedKey("global", sLocalValue);
590*cdf0e10cSrcweir     //
591*cdf0e10cSrcweir     //         return sLocalValue;
592*cdf0e10cSrcweir     //     }
593*cdf0e10cSrcweir     public void removeSection(String _sSectionToRemove)
594*cdf0e10cSrcweir         {
595*cdf0e10cSrcweir             // first, search for the name
596*cdf0e10cSrcweir             int i = findSection(_sSectionToRemove);
597*cdf0e10cSrcweir             if (i == -1)
598*cdf0e10cSrcweir             {
599*cdf0e10cSrcweir                 // Section to remove not found, do nothing.
600*cdf0e10cSrcweir                 return;
601*cdf0e10cSrcweir             }
602*cdf0e10cSrcweir             // second, find the next section
603*cdf0e10cSrcweir             int j = findNextSection(i + 1);
604*cdf0e10cSrcweir             if (j == -1)
605*cdf0e10cSrcweir             {
606*cdf0e10cSrcweir                 // if we are at the end, use size() as second section
607*cdf0e10cSrcweir                 j = m_aList.size();
608*cdf0e10cSrcweir             }
609*cdf0e10cSrcweir             // remove all between first and second section
610*cdf0e10cSrcweir             for (int k = i; k < j; k++)
611*cdf0e10cSrcweir             {
612*cdf0e10cSrcweir                 m_aList.remove(i);
613*cdf0e10cSrcweir             }
614*cdf0e10cSrcweir             // mark the list as changed
615*cdf0e10cSrcweir             m_bListContainUnsavedChanges = true;
616*cdf0e10cSrcweir         }
617*cdf0e10cSrcweir 
618*cdf0e10cSrcweir     /**
619*cdf0e10cSrcweir      * some tests for this class
620*cdf0e10cSrcweir      */
621*cdf0e10cSrcweir //    public static void main(String[] args)
622*cdf0e10cSrcweir //        {
623*cdf0e10cSrcweir //            String sTempFile = System.getProperty("java.io.tmpdir");
624*cdf0e10cSrcweir //            sTempFile += "inifile";
625*cdf0e10cSrcweir //
626*cdf0e10cSrcweir //
627*cdf0e10cSrcweir //            IniFile aIniFile = new IniFile(sTempFile);
628*cdf0e10cSrcweir //            String sValue = aIniFile.getValue("Section", "Key");
629*cdf0e10cSrcweir //            // insert a new value to a already exist section
630*cdf0e10cSrcweir //            aIniFile.insertValue("Section", "Key2", "a new value in a existing section");
631*cdf0e10cSrcweir //            // replace a value
632*cdf0e10cSrcweir //            aIniFile.insertValue("Section", "Key", "replaced value");
633*cdf0e10cSrcweir //            // create a new value
634*cdf0e10cSrcweir //            aIniFile.insertValue("New Section", "Key", "a new key value pair");
635*cdf0e10cSrcweir //            aIniFile.insertValue("New Section", "Key2", "a new second key value pair");
636*cdf0e10cSrcweir //
637*cdf0e10cSrcweir //            String sValue2 = aIniFile.getValue("Section2", "Key");
638*cdf0e10cSrcweir //
639*cdf0e10cSrcweir //            aIniFile.removeSection("Section");
640*cdf0e10cSrcweir //            aIniFile.removeSection("New Section");
641*cdf0e10cSrcweir //
642*cdf0e10cSrcweir //            aIniFile.close();
643*cdf0e10cSrcweir //        }
644*cdf0e10cSrcweir 
645*cdf0e10cSrcweir     /**
646*cdf0e10cSrcweir      * Enumeration Interface
647*cdf0e10cSrcweir      * @return true, if there are more Key values
648*cdf0e10cSrcweir      */
649*cdf0e10cSrcweir     public boolean hasMoreElements()
650*cdf0e10cSrcweir         {
651*cdf0e10cSrcweir             if (m_aEnumerationPos >= 0 &&
652*cdf0e10cSrcweir                 m_aEnumerationPos < m_aList.size())
653*cdf0e10cSrcweir             {
654*cdf0e10cSrcweir                 return true;
655*cdf0e10cSrcweir             }
656*cdf0e10cSrcweir             return false;
657*cdf0e10cSrcweir         }
658*cdf0e10cSrcweir 
659*cdf0e10cSrcweir     /**
660*cdf0e10cSrcweir      * Find the next line, which starts with '['
661*cdf0e10cSrcweir      * @param i start position
662*cdf0e10cSrcweir      * @return the line where '[' found or -1
663*cdf0e10cSrcweir      */
664*cdf0e10cSrcweir     private int findNextSection(int i)
665*cdf0e10cSrcweir         {
666*cdf0e10cSrcweir             if (i >= 0)
667*cdf0e10cSrcweir             {
668*cdf0e10cSrcweir                 while (i < m_aList.size())
669*cdf0e10cSrcweir                 {
670*cdf0e10cSrcweir                     String sLine =  m_aList.get(i);
671*cdf0e10cSrcweir                     if (sLine.startsWith("["))
672*cdf0e10cSrcweir                     {
673*cdf0e10cSrcweir                         return i;
674*cdf0e10cSrcweir                     }
675*cdf0e10cSrcweir                     i++;
676*cdf0e10cSrcweir                 }
677*cdf0e10cSrcweir             }
678*cdf0e10cSrcweir             return -1;
679*cdf0e10cSrcweir         }
680*cdf0e10cSrcweir 
681*cdf0e10cSrcweir     /**
682*cdf0e10cSrcweir      * Enumeration Interface
683*cdf0e10cSrcweir      * @return a key without the enveloped '[' ']'
684*cdf0e10cSrcweir      */
685*cdf0e10cSrcweir     public Object nextElement()
686*cdf0e10cSrcweir         {
687*cdf0e10cSrcweir             int nLineWithSection = findNextSection(m_aEnumerationPos);
688*cdf0e10cSrcweir             if (nLineWithSection != -1)
689*cdf0e10cSrcweir             {
690*cdf0e10cSrcweir                 String sSection =  m_aList.get(nLineWithSection);
691*cdf0e10cSrcweir                 m_aEnumerationPos = findNextSection(nLineWithSection + 1);
692*cdf0e10cSrcweir                 sSection = sectionToString(sSection);
693*cdf0e10cSrcweir                 return sSection;
694*cdf0e10cSrcweir             }
695*cdf0e10cSrcweir             else
696*cdf0e10cSrcweir             {
697*cdf0e10cSrcweir                 m_aEnumerationPos = m_aList.size();
698*cdf0e10cSrcweir             }
699*cdf0e10cSrcweir             return null;
700*cdf0e10cSrcweir         }
701*cdf0e10cSrcweir 
702*cdf0e10cSrcweir     /**
703*cdf0e10cSrcweir      * Helper to count the occurence of Sections
704*cdf0e10cSrcweir      * @return returns the count of '^['.*']$' Elements
705*cdf0e10cSrcweir      */
706*cdf0e10cSrcweir     public int getElementCount()
707*cdf0e10cSrcweir         {
708*cdf0e10cSrcweir             int nCount = 0;
709*cdf0e10cSrcweir             int nPosition = 0;
710*cdf0e10cSrcweir             while ((nPosition = findNextSection(nPosition)) != -1)
711*cdf0e10cSrcweir             {
712*cdf0e10cSrcweir                 nCount++;
713*cdf0e10cSrcweir                 nPosition++;
714*cdf0e10cSrcweir             }
715*cdf0e10cSrcweir             return nCount;
716*cdf0e10cSrcweir         }
717*cdf0e10cSrcweir }
718*cdf0e10cSrcweir 
719