added: program reads its configuration from a config file
added: confparser directory and confparser.h and confparser.cpp class ConfParser used to parse a config file this is a generic parser, can be used by another project added: config.h, config.cpp class Config used for assigning values from a config file into the data object added: function for signals: SIGINT, SIGHUP after receiving SIGHUP the program will read its config file again git-svn-id: svn://ttmath.org/publicrep/cmslu/trunk@463 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
208
confparser/confparser.cpp
Executable file
208
confparser/confparser.cpp
Executable file
@@ -0,0 +1,208 @@
|
||||
/*
|
||||
* This file is a part of CMSLU -- Content Management System like Unix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2008, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "confparser.h"
|
||||
|
||||
|
||||
ConfParser::ConfParser()
|
||||
{
|
||||
separator = '=';
|
||||
commentary = '#';
|
||||
}
|
||||
|
||||
|
||||
ConfParser::Status ConfParser::Parse(const char * file_name)
|
||||
{
|
||||
file.open( file_name );
|
||||
|
||||
if( !file )
|
||||
return cant_open_file;
|
||||
|
||||
line = 1;
|
||||
table.clear();
|
||||
ReadChar();
|
||||
status = ParseFile();
|
||||
|
||||
file.close();
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ConfParser::Status ConfParser::ParseFile()
|
||||
{
|
||||
while( lastc != -1 )
|
||||
{
|
||||
if( ReadVariable() )
|
||||
{
|
||||
if( lastc != separator )
|
||||
return syntax_error;
|
||||
|
||||
ReadChar(); // skipping separator
|
||||
|
||||
if( !ReadValue() )
|
||||
return syntax_error;
|
||||
|
||||
table.insert( std::make_pair(variable, value) );
|
||||
}
|
||||
|
||||
if( lastc == commentary )
|
||||
SkipLine();
|
||||
|
||||
if( lastc != -1 && lastc != '\n' )
|
||||
return syntax_error;
|
||||
|
||||
ReadChar(); // skipping '\n' if was
|
||||
}
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
bool ConfParser::IsVariableChar(int c)
|
||||
{
|
||||
if( (c>='a' && c<='z') ||
|
||||
(c>='A' && c<='Z') ||
|
||||
(c>='0' && c<='9') ||
|
||||
c=='.' || c==',' || c=='_' )
|
||||
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
bool ConfParser::IsValueSimpleChar(int c)
|
||||
{
|
||||
if( c==-1 || c=='\n' || IsWhite(c) || c==commentary )
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool ConfParser::ReadVariable()
|
||||
{
|
||||
variable.clear();
|
||||
|
||||
SkipWhite();
|
||||
|
||||
while( IsVariableChar(lastc) )
|
||||
{
|
||||
variable += lastc;
|
||||
ReadChar();
|
||||
}
|
||||
|
||||
SkipWhite();
|
||||
|
||||
return !variable.empty();
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool ConfParser::ReadValue()
|
||||
{
|
||||
value.clear();
|
||||
|
||||
SkipWhite();
|
||||
|
||||
|
||||
if( lastc == '"' )
|
||||
// quoted value
|
||||
return ReadValueQuoted();
|
||||
else
|
||||
return ReadValueSimple();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
bool ConfParser::ReadValueQuoted()
|
||||
{
|
||||
ReadChar(); // skipping the first quote
|
||||
|
||||
while( lastc != '\n' && lastc != '"' && lastc != -1 )
|
||||
{
|
||||
if( lastc == '\\' )
|
||||
ReadChar();
|
||||
|
||||
value += lastc;
|
||||
ReadChar();
|
||||
}
|
||||
|
||||
if( lastc != '"' )
|
||||
return false;
|
||||
|
||||
ReadChar(); // skipping the last quote
|
||||
|
||||
SkipWhite();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
bool ConfParser::ReadValueSimple()
|
||||
{
|
||||
while( IsValueSimpleChar(lastc) )
|
||||
{
|
||||
value += lastc;
|
||||
ReadChar();
|
||||
}
|
||||
|
||||
SkipWhite();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int ConfParser::ReadChar()
|
||||
{
|
||||
lastc = file.get();
|
||||
|
||||
if( lastc == '\n' )
|
||||
++line;
|
||||
|
||||
return lastc;
|
||||
}
|
||||
|
||||
|
||||
bool ConfParser::IsWhite(int c)
|
||||
{
|
||||
if( c==' ' || c=='\t' || c==13 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ConfParser::SkipWhite()
|
||||
{
|
||||
while( IsWhite(lastc) )
|
||||
ReadChar();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ConfParser::SkipLine()
|
||||
{
|
||||
while( lastc != -1 && lastc != '\n' )
|
||||
ReadChar();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
74
confparser/confparser.h
Executable file
74
confparser/confparser.h
Executable file
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* This file is a part of CMSLU -- Content Management System like Unix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2008, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef headerfileconfparser
|
||||
#define headerfileconfparser
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <map>
|
||||
|
||||
|
||||
class ConfParser
|
||||
{
|
||||
public:
|
||||
|
||||
enum Status { ok, cant_open_file, syntax_error };
|
||||
|
||||
ConfParser();
|
||||
Status Parse(const char * file_name);
|
||||
|
||||
|
||||
// last status
|
||||
Status status;
|
||||
|
||||
// line in which there is a syntax_error
|
||||
int line;
|
||||
|
||||
typedef std::map<std::string, std::string> Table;
|
||||
Table table;
|
||||
|
||||
private:
|
||||
|
||||
// last read variable, value
|
||||
std::string variable, value;
|
||||
|
||||
// separator between a variable and a value, usually '='
|
||||
int separator;
|
||||
|
||||
// commentary char
|
||||
int commentary;
|
||||
|
||||
// last read char
|
||||
int lastc;
|
||||
|
||||
// current file
|
||||
std::ifstream file;
|
||||
|
||||
|
||||
Status ParseFile();
|
||||
|
||||
bool IsVariableChar(int c);
|
||||
bool IsValueSimpleChar(int c);
|
||||
|
||||
bool ReadVariable();
|
||||
bool ReadValue();
|
||||
bool ReadValueQuoted();
|
||||
bool ReadValueSimple();
|
||||
|
||||
int ReadChar();
|
||||
bool IsWhite(int c);
|
||||
void SkipWhite();
|
||||
void SkipLine();
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user