| 412 |
ira |
1 |
/*******************************************************************************
|
| 415 |
ira |
2 |
* ConfigParser.java
|
| 412 |
ira |
3 |
*
|
|
|
4 |
* Holds the ConfigParser class, which automatically parses the given
|
|
|
5 |
* configuration file.
|
|
|
6 |
*
|
|
|
7 |
* Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
|
|
|
8 |
*
|
|
|
9 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
10 |
* of this software and associated documentation files (the "Software"), to deal
|
|
|
11 |
* in the Software without restriction, including without limitation the rights
|
|
|
12 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
13 |
* copies of the Software, and to permit persons to whom the Software is
|
|
|
14 |
* furnished to do so, subject to the following conditions:
|
|
|
15 |
*
|
|
|
16 |
* The above copyright notice and this permission notice shall be included in
|
|
|
17 |
* all copies or substantial portions of the Software.
|
|
|
18 |
*
|
|
|
19 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
20 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
21 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
22 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
23 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
24 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
25 |
* IN THE SOFTWARE.
|
|
|
26 |
******************************************************************************/
|
|
|
27 |
|
|
|
28 |
import java.io.*;
|
|
|
29 |
import java.lang.String;
|
|
|
30 |
import java.util.Vector;
|
|
|
31 |
|
| 415 |
ira |
32 |
/**
|
|
|
33 |
* Automatic parser for the CS431 Project #1 configuration format. This will
|
|
|
34 |
* read from the keyboard unless a filename is given.
|
|
|
35 |
*
|
|
|
36 |
* @class CS431 Spring 2006
|
|
|
37 |
* @author Ira W. Snyder (devel@irasnyder.com)
|
|
|
38 |
*/
|
|
|
39 |
public class ConfigParser
|
| 412 |
ira |
40 |
{
|
| 415 |
ira |
41 |
/** Current input device: keyboard or file */
|
| 412 |
ira |
42 |
private BufferedReader input;
|
| 415 |
ira |
43 |
|
|
|
44 |
/** Flag specifying the input type */
|
| 412 |
ira |
45 |
private boolean kbd_input = false;
|
| 415 |
ira |
46 |
|
|
|
47 |
/** The Round Robin Interval listed in the configuration */
|
| 412 |
ira |
48 |
private int RRInterval;
|
| 415 |
ira |
49 |
|
|
|
50 |
/** The Processes that were listed in the configuration */
|
| 412 |
ira |
51 |
private Vector<Process> Processes = new Vector<Process> ();
|
|
|
52 |
|
| 415 |
ira |
53 |
/**
|
|
|
54 |
* Constructor for the ConfigParser class. This is the default constructor,
|
|
|
55 |
* which will read from the keyboard.
|
|
|
56 |
*/
|
| 412 |
ira |
57 |
public ConfigParser ()
|
|
|
58 |
{
|
|
|
59 |
input = new BufferedReader (
|
|
|
60 |
new InputStreamReader (System.in));
|
|
|
61 |
kbd_input = true;
|
|
|
62 |
|
|
|
63 |
parse ();
|
|
|
64 |
}
|
|
|
65 |
|
| 415 |
ira |
66 |
/**
|
|
|
67 |
* Constructor for the the ConfigParser class. This constructor reads its
|
|
|
68 |
* input from a file.
|
|
|
69 |
*
|
|
|
70 |
* @param filename the file to read input from
|
|
|
71 |
*/
|
| 412 |
ira |
72 |
public ConfigParser (String filename)
|
|
|
73 |
{
|
|
|
74 |
try
|
|
|
75 |
{
|
|
|
76 |
input = new BufferedReader (
|
|
|
77 |
new InputStreamReader (
|
|
|
78 |
new FileInputStream (filename)));
|
|
|
79 |
}
|
|
|
80 |
catch (java.io.FileNotFoundException e)
|
|
|
81 |
{
|
|
|
82 |
System.err.println ("Input file: " + filename + " not found!");
|
|
|
83 |
System.exit (1);
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
parse ();
|
|
|
87 |
}
|
|
|
88 |
|
| 415 |
ira |
89 |
/**
|
|
|
90 |
* Read all values via the current input method, and store them so that
|
|
|
91 |
* they can be read later.
|
|
|
92 |
* <p>
|
|
|
93 |
* This method is automatically called by the constructors of this class.
|
|
|
94 |
*/
|
| 412 |
ira |
95 |
private void parse ()
|
|
|
96 |
{
|
|
|
97 |
String s = null;
|
|
|
98 |
int ptime;
|
|
|
99 |
String pname;
|
|
|
100 |
|
|
|
101 |
try
|
|
|
102 |
{
|
|
|
103 |
/* Read the RR Interval */
|
|
|
104 |
s = input.readLine ();
|
|
|
105 |
this.RRInterval = Integer.parseInt (s);
|
|
|
106 |
|
|
|
107 |
/* Read each process' values */
|
|
|
108 |
pname = input.readLine ();
|
|
|
109 |
while (pname != null)
|
|
|
110 |
{
|
|
|
111 |
ptime = Integer.parseInt (input.readLine ());
|
|
|
112 |
Processes.add (new Process (pname, ptime));
|
|
|
113 |
|
|
|
114 |
/* Read the next Process name */
|
|
|
115 |
pname = input.readLine ();
|
|
|
116 |
}
|
|
|
117 |
}
|
|
|
118 |
catch (java.io.IOException e)
|
|
|
119 |
{
|
|
|
120 |
System.err.println ("Error reading file!");
|
|
|
121 |
System.exit (2);
|
|
|
122 |
}
|
| 421 |
ira |
123 |
catch (Exception e)
|
|
|
124 |
{
|
|
|
125 |
System.err.println ("Error parsing config... check the format! :)");
|
|
|
126 |
System.exit (255);
|
|
|
127 |
}
|
| 412 |
ira |
128 |
}
|
|
|
129 |
|
| 415 |
ira |
130 |
/**
|
|
|
131 |
* Accessor method for the private variable Processes.
|
|
|
132 |
* <p>
|
|
|
133 |
* NOTE: this is NOT a copy, so don't modify this! Make a copy first.
|
|
|
134 |
*
|
|
|
135 |
* @return the private variable Processes.
|
|
|
136 |
*/
|
| 412 |
ira |
137 |
public Vector<Process> getProcesses ()
|
|
|
138 |
{
|
|
|
139 |
return this.Processes;
|
|
|
140 |
}
|
|
|
141 |
|
| 415 |
ira |
142 |
/**
|
|
|
143 |
* Accessor method for the private variable RRInterval.
|
|
|
144 |
*
|
|
|
145 |
* @return the Round Robin Interval from the configuration file
|
|
|
146 |
*/
|
| 412 |
ira |
147 |
public int getRRInterval ()
|
|
|
148 |
{
|
|
|
149 |
return this.RRInterval;
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/* vim: set ts=4 sts=4 sw=4 expandtab: */
|
|
|
154 |
|