#ifndef _CONFIGTABLE_H_ #define _CONFIGTABLE_H_ // // $Id$ //----------------------------------------------------------------------- // // (c) Copyright 2002 by GATS, Inc., // 11864 Canon Blvd, Suite 101, Newport News VA 23606 // // All Rights Reserved. No part of this software or publication may be // reproduced, stored in a retrieval system, or transmitted, in any form // or by any means, electronic, mechanical, photocopying, recording, or // otherwise without the prior written permission of GATS, Inc. // //----------------------------------------------------------------------- // // Module: ConfigTable.h // // Author: John Burton // // Date: Fri Oct 18 16:37:53 2002 // //----------------------------------------------------------------------- // // Modification History: // // $Log$ // //----------------------------------------------------------------------- // //----------------------------------------------------------------------- // Include Files: //----------------------------------------------------------------------- // #include #include //#include "FlexLexer.h" #include "Value.h" #include "GATS_Exception.h" // //----------------------------------------------------------------------- // Class Definitions: //----------------------------------------------------------------------- // class yyFlexLexer; using namespace std; class ConfigFileException : public GATS_Exception { public: ConfigFileException(std::string msg, const char *filename, const unsigned int linenumber) : GATS_Exception(msg.c_str(),filename,linenumber) {} }; typedef map ValMap; typedef map > MapMap; // //----------------------------------------------------------------------- // Class Definition: //----------------------------------------------------------------------- // /// /// ConfigFile is used to read a configuration file with an enhanced INI /// format.A ConfigFile is broken up into sections which start with a /// [\] statement where \c \ is replaced by the name /// of the section. Values are assigned using \ = \ syntax, /// where \c \ is the name of the symbol and \c \ can be an /// integer, a float, or a string. If the string has any whitespace characters /// it needs to be enclosed in quotes (""). \c \ can also be a 1-D array of /// integer, float, or strings by using '{' ... '}' to delimit the array. /// Comments can be included within the file using the standard C++ /// comment syntax "//". /// class ConfigFile { public: /// /// Default ConfigFile constructor ConfigFile(); /// /// The ConfigFile constructor opens \c filename and parses the /// contents into a symbol table containing \ tuples. /// @param filename - string containing the name of the configuration file to open /// @exception ConfigFileException - unable to open or unable to parse the configuration file ConfigFile(string filename); /// /// Opens \c filename and parses the /// contents into a symbol table containing \ tuples. /// @param filename - string containing the name of the configuration file to open /// @exception ConfigFileException - unable to open or unable to parse the configuration file void open(string filename); /// /// GetStr takes two character strings \c Section and \c Symbol and returns the /// character string value associated with them. /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired string quantity /// @return The desired string value from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// char* GetStr(char *Section, char *Symbol); /// /// GetStr takes two const character strings \c Section and \c Symbol and returns the /// character string value associated with them. /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired string quantity /// @return The desired string value from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// char* GetStr(const char *Section, const char *Symbol); /// /// GetStr takes two STL strings \c Section and \c Symbol and returns the /// STL string value associated with them. /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired string quantity /// @return The desired string value from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// string GetStr(string Section, string Symbol); /// /// GetInt takes two character strings \c Section and \c Symbol and returns the /// integer value associated with them. /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired string quantity /// @return The desired integer value from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// int GetInt(char *Section, char *Symbol); /// /// GetInt takes two character strings \c Section and \c Symbol and returns the /// integer value associated with them. /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired string quantity /// @return The desired integer value from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// int GetInt(const char *Section, const char *Symbol); /// /// GetInt takes two character strings \c Section and \c Symbol and returns the /// integer value associated with them. /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired string quantity /// @return The desired integer value from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// int GetInt(string Section, string Symbol); /// /// GetReal takes two character strings \c Section and \c Symbol and returns the /// floating point value associated with them. /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired string quantity /// @return The desired real value from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// float GetReal(char *Section, char *Symbol); /// /// GetReal takes two character strings \c Section and \c Symbol and returns the /// floating point value associated with them. /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired string quantity /// @return The desired real value from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// float GetReal(const char *Section, const char *Symbol); /// /// GetReal takes two character strings \c Section and \c Symbol and returns the /// floating point value associated with them. /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired string quantity /// @return The desired real value from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// float GetReal(string Section, string Symbol); /// /// GetStrArray takes two character strings \c Section and \c Symbol and returns the /// array of character strings associated with them. The number of elements in /// the array is returned in \c nvals (Note: This routine is deprecated use GetStrValues instead) /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired string quantity /// @param nvals - Number of elements in the returned array /// @return An array of the desired string values from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// char** GetStrArray(char *Section, char *Symbol, int *nvals); /// /// GetStrArray takes two character strings \c Section and \c Symbol and returns the /// array of character strings associated with them. The number of elements in /// the array is returned in \c nvals (Note: This routine is deprecated use GetIntValues instead) /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired string quantity /// @param nvals - Number of elements in the returned array /// @return An array of the desired integer values from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// char** GetStrArray(const char *Section, const char *Symbol, int *nvals); /// /// GetIntArray takes two character strings \c Section and \c Symbol and returns the /// array of integer values associated with them. The number of elements in /// the array is returned in \c nvals (Note: This routine is deprecated use GetIntValues instead) /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired string quantity /// @param nvals - Number of elements in the returned array /// @return An array of the desired integer values from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// int* GetIntArray(char *Section, char *Symbol, int *nvals); /// /// GetIntArray takes two character strings \c Section and \c Symbol and returns the /// array of integer values associated with them. The number of elements in /// the array is returned in \c nvals (Note: This routine is deprecated use GetRealValues instead) /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired string quantity /// @param nvals - Number of elements in the returned array /// @return An array of the desired real values from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// int* GetIntArray(const char *Section, const char *Symbol, int *nvals); /// /// GetRealArray takes two character strings \c Section and \c Symbol and returns the /// array of real values associated with them. The number of elements in /// the array is returned in \c nvals (Note: This routine is deprecated use GetRealValues instead) /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired string quantity /// @param nvals - Number of elements in the returned array /// @return An array of the desired real values from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// float* GetRealArray(char *Section, char *Symbol, int *nvals); /// /// GetRealArray takes two character strings \c Section and \c Symbol and returns the /// array of real values associated with them. The number of elements in /// the array is returned in \c nvals (Note: This routine is deprecated use GetRealValues instead) /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired string quantity /// @param nvals - Number of elements in the returned array /// @return An array of the desired real values from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// float* GetRealArray(const char *Section, const char *Symbol, int *nvals); /// /// GetStrValues takes two std::strings \c Section and \c Symbol and returns a /// std::vector associated with them. The number of elements in /// the vector is returned via the std::vector::size() method /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired string quantity /// @return A vector containing the desired string values from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// vector GetStrValues(string Section, string Symbol); /// /// GetIntValues takes two std::strings \c Section and \c Symbol and returns a /// std::vector containing the values associated with them. The number of elements in /// the vector is returned via the std::vector::size() method /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired integer quantity /// @return A vector containing the desired integer values from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// vector GetIntValues(string Section, string Symbol); /// /// GetRealValues takes two std::strings \c Section and \c Symbol and returns a /// std::vector containing the values associated with them. The number of elements in /// the vector is returned via the std::vector::size() method /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired real quantity /// @return A vector containing the desired real values from the configuration file /// @exception ConfigFileException - unable to find \c Symbol or \c Section /// vector GetRealValues(string Section, string Symbol); /// /// PrintTable prints the contents of the symbol table to \c stderr, in alphabetical order, /// grouped by section. /// int PrintTable(); /// /// ValidEntry returns true if \c Symbol exists in \c Section, false otherwise /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired quantity /// @return An integer returns false if \c Symbol does not exist int \c Section /// bool ValidEntry(string Section, string Symbol); /// GetFlag returns true if \c Symbol exists in \c Section and has a value != 0, false /// otherwise /// @param Section - Name of the Section of the configuration file containing \C Symbol /// @param Symbol - Name of the Symbol containing the desired quantity /// @return A boolean value of false if \c Symbol does not exist int \c Section, or exists /// and has a value of 0 /// bool GetFlag(string Section, string Symbol); private: MapMap ConfigMap; int ParseFile(yyFlexLexer *clex); ValueClass *ParseValues(yyFlexLexer *clex); ValueClass *ParseSimpleValue(int token, yyFlexLexer *clex); ValueClass *CatValueList(ValueClass *vl1, ValueClass *vl2); ValueClass *ParseValueList(yyFlexLexer *clex); ValueClass *GetValue(string Section, string Symbol); void PutValue(string SectionName, string SymbolName, ValueClass *value); int PrintValue(ValMap::iterator vitr); }; #endif