Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/prolifics/ejb/IniFile.java


1   /* @(#)IniFile.java  77.1 00/04/19 13:17:11 */
2   
3   /*************************************************/
4   /*  Copyright (c) 2000         */
5   /*      by         */
6   /*  JYACC, Inc., New York NY USA     */
7   /*  and contributors.         */
8   /*  Use of this program is governed by the   */
9   /*  JYACC Public License Version 1.0, a copy of   */
10  /*  which can be obtained at       */
11  /*  http://www.possl.org/jyacc-license.html   */
12  /*************************************************/
13  
14  package com.prolifics.ejb;
15  
16  import java.lang.*;
17  import java.io.*;
18  
19  public class IniFile
20  {
21    static boolean debug = false;
22  
23    public static void main(String args[])
24    {
25      int offset = 0;
26  
27      if (args[offset+0].equalsIgnoreCase("-debug"))
28      {
29        debug = true;
30        offset++;
31      }
32      String value =
33        GetPrivateProfileString(args[offset+0],
34          args[offset+1],
35          args[offset+2],
36          args[offset+3]);
37      System.out.println("[" + args[offset+0] + "]");
38      System.out.println(args[offset+1] + "=" + value);
39      System.out.flush();
40    }
41  
42    public static void debugMsg(String str)
43    {
44      if (debug)
45      {
46        System.out.println("debug: " + str);
47        System.out.flush();
48      }
49    }
50  
51    public static String GetPrivateProfileString(String sectionName,
52      String keyName, String defaultValue, String fileName)
53    {
54      BufferedReader in = null;
55      String value = defaultValue;
56  
57      if (sectionName == null ||
58          keyName == null ||
59          defaultValue == null ||
60          fileName == null)
61      {
62        return null;
63      }
64  
65      try
66      {
67        in = new BufferedReader(new FileReader(fileName));
68  
69        value = GetPrivateProfileString1(sectionName,
70          keyName, defaultValue, fileName, in);
71      }
72      catch (java.io.FileNotFoundException excp)
73      {
74      }
75      finally
76      {
77        if (in != null)
78        {
79          try
80          {
81            in.close();
82          }
83          catch (java.io.IOException excp)
84          {
85          }
86        }
87      }
88  
89      return value;
90    }
91  
92    private static String GetPrivateProfileString1(String sectionName,
93      String keyName, String defaultValue, String fileName,
94      BufferedReader in)
95    {
96      String line = null;
97      String currentSection = null;
98      String currentKey = null;
99      int equalPos = 0;
100     String value = defaultValue;
101 
102     for (;;)
103     {
104       try
105       {
106         line = in.readLine();
107       }
108       catch (java.io.IOException excp)
109       {
110         break;
111       }
112       if (line == null)
113       {
114         break;
115       }
116       line = line.trim();
117 
118       if (line.length() == 0)
119       {
120         debugMsg("blank   " + line);
121         continue;
122       }
123 
124       if (line.startsWith(";"))
125       {
126         debugMsg("comment " + line);
127         continue;
128       }
129 
130       if (line.startsWith("[") && line.endsWith("]"))
131       {
132         currentSection =
133           line.substring(1, line.length()-1);
134         debugMsg("section [" + currentSection + "]");
135         continue;
136       }
137 
138       if (! currentSection.equalsIgnoreCase( sectionName))
139       {
140         debugMsg("section!=" + line);
141         continue;
142       }
143 
144       equalPos = line.indexOf('=');
145 
146       if (equalPos < 0)
147       {
148         debugMsg("no equal " + line);
149         continue;
150       }
151 
152       currentKey = line.substring(0,equalPos);
153       if (! currentKey.equalsIgnoreCase(keyName))
154       {
155         debugMsg("key !=   " + line);
156         continue;
157       }
158 
159       debugMsg("key =    " + line);
160       value = line.substring(equalPos+1);
161       break;
162     }
163 
164     return value;
165   }
166 }