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