Subversion Repositories programming

Rev

Rev 415 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*******************************************************************************
 * ConfigParser.java
 *
 * Holds the ConfigParser class, which automatically parses the given
 * configuration file.
 *
 * Copyright (c) 2006, Ira W. Snyder (devel@irasnyder.com)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 ******************************************************************************/

import java.io.*;
import java.lang.String;
import java.util.Vector;

/**
 * Automatic parser for the CS431 Project #1 configuration format. This will
 * read from the keyboard unless a filename is given.
 *
 * @class CS431 Spring 2006
 * @author Ira W. Snyder (devel@irasnyder.com)
 */
public class ConfigParser
{
    /** Current input device: keyboard or file */
    private BufferedReader input;

    /** Flag specifying the input type */
    private boolean kbd_input = false;

    /** The Round Robin Interval listed in the configuration */
    private int RRInterval;

    /** The Processes that were listed in the configuration */
    private Vector<Process> Processes = new Vector<Process> ();

    /**
     * Constructor for the ConfigParser class. This is the default constructor,
     * which will read from the keyboard.
     */
    public ConfigParser ()
    {
        input = new BufferedReader (
                    new InputStreamReader (System.in));
        kbd_input = true;

        parse ();
    }

    /**
     * Constructor for the the ConfigParser class. This constructor reads its
     * input from a file.
     *
     * @param filename the file to read input from
     */
    public ConfigParser (String filename)
    {
        try
        {
            input = new BufferedReader (
                        new InputStreamReader (
                            new FileInputStream (filename)));
        }
        catch (java.io.FileNotFoundException e)
        {
            System.err.println ("Input file: " + filename + " not found!");
            System.exit (1);
        }

        parse ();
    }

    /**
     * Read all values via the current input method, and store them so that
     * they can be read later.
     * <p>
     * This method is automatically called by the constructors of this class.
     */
    private void parse ()
    {
        String s = null;
        int ptime;
        String pname;

        try
        {
            /* Read the RR Interval */
            s = input.readLine ();
            this.RRInterval = Integer.parseInt (s);

            /* Read each process' values */
            pname = input.readLine ();
            while (pname != null)
            {
                ptime = Integer.parseInt (input.readLine ());
                Processes.add (new Process (pname, ptime));

                /* Read the next Process name */
                pname = input.readLine ();
            }
        }
        catch (java.io.IOException e)
        {
            System.err.println ("Error reading file!");
            System.exit (2);
        }
        catch (Exception e)
        {
            System.err.println ("Error parsing config... check the format! :)");
            System.exit (255);
        }
    }

    /**
     * Accessor method for the private variable Processes.
     * <p>
     * NOTE: this is NOT a copy, so don't modify this! Make a copy first.
     *
     * @return the private variable Processes.
     */
    public Vector<Process> getProcesses ()
    {
        return this.Processes;
    }

    /**
     * Accessor method for the private variable RRInterval.
     *
     * @return the Round Robin Interval from the configuration file
     */
    public int getRRInterval ()
    {
        return this.RRInterval;
    }
}

/* vim: set ts=4 sts=4 sw=4 expandtab: */