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