#include "configparser.h" #include "stringtools.h" #include "Server.h" #include "Proxy.h" #include "Web.h" #include "Ftp.h" #include "exceptions.h" #include <string> ConfigParser::ConfigParser(const std::string& filename, Configuration * configuration) : _configFilename(filename), _configuration(configuration), _lineno(0) { _constructorMap["p4d"] = &ConfigParser::createP4D; _constructorMap["p4p"] = &ConfigParser::createP4P; _constructorMap["p4web"] = &ConfigParser::createP4WEB; _constructorMap["p4ftp"] = &ConfigParser::createP4FTP; } bool ConfigParser::getLine(std::ifstream& fin, std::string& line) { bool ok = !std::getline(fin, line).eof(); if (ok) { _lineno++; trim(line); removeComments(line); } return ok; } void ConfigParser::parse() throw(ConfigException) { std::ifstream fin(_configFilename.c_str()); if (!fin) { std::string err("Cannot find configuration file '"); err += _configFilename; err += "'"; throw VerificationException(err); } // loop // grap a line // look for comment // look for empty line // look for "=" // look for single name // until eof std::string line; while (getLine(fin, line)) { if (line != "") { if (!findVariable(line, _configuration)) { findExecutable(line, fin); } } if (fin.eof()) break; } } bool ConfigParser::findVariable(std::string& line, VariableHolder * holder) { std::string::size_type pos = line.find('='); if (pos != std::string::npos) { std::string variable = std::string(line, 0, pos); std::string value = std::string(line, pos + 1, line.size() - pos - 1); holder->addVariable(trim(variable), trim(value)); return true; } return false; } void ConfigParser::findExecutable(std::string& name, std::ifstream& fin) { std::string serverType; std::string serverName; std::string::size_type pos = name.find_first_of(" \t"); if (pos != std::string::npos) { serverType = name.substr(0, pos); serverName = name.substr(pos + 1); trim(serverName); pos = serverName.find_first_of(" \t{"); if (pos != std::string::npos) { serverName.erase(pos); } } else { std::string err = "illegal server specification, must be 'type name'."; err += "Found : "; err += name; throw ParseException(err, _lineno); } Executable * exec = NULL; ctor_map::const_iterator iter = _constructorMap.find(serverType); if (iter != _constructorMap.end()) { exec = (this->*iter->second)(serverName, serverType, _configuration); } else { std::string err("Unknown type "); err += serverType; throw ParseException(err, _lineno); } // if the name line does not contain the '{', go look for it if (name.find('{') == std::string::npos) { eatBracket(fin, "{"); } std::string line; while (getLine(fin, line)) { if (line != "") { if (!findVariable(line, exec)) { break; } } } if (!(line == "}")) throw ParseException("bracket missing", _lineno); exec->verifyVariables(); _configuration->addExecutable(exec); } bool ConfigParser::eatBracket(std::ifstream& fin, const char * bracket) { std::string line; while (getLine(fin, line)) { if (line != "") { if (line == bracket) { return true; } else { throw ParseException("bracket missing", _lineno); } } } throw ParseException("bracket missing", _lineno); } Executable * ConfigParser::createP4D(std::string& name, std::string& type, Configuration * config) { return new Server(name, type, config); } Executable * ConfigParser::createP4P(std::string& name, std::string& type, Configuration * config) { return new Proxy(name, type, config); } Executable * ConfigParser::createP4WEB(std::string& name, std::string& type, Configuration * config) { return new Web(name, type, config); } Executable * ConfigParser::createP4FTP(std::string& name, std::string& type, Configuration * config) { return new Ftp(name, type, config); }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 5987 | Sven Erik Knop |
Few minor changes: All commands now accept variable called "Options". This can be used to collect all options for which Perforce has not defined an environment variable - for example the undocumented "-C1" for p4d to start a case insensitive server. p4d also accepts the variables P4USER and P4PASSWD. These are only used for stopping the service, which is done first via "p4 admin stop". Note that if the stop via the admin command fails, p4dcfg will stop the service via a kill |
||
#1 | 5882 | Sven Erik Knop | initial publish |