[Cluster-devel] conga/ricci/common Logger.cpp Makefile Module. ...
rmccabe at sourceware.org
rmccabe at sourceware.org
Fri Aug 31 04:57:39 UTC 2007
CVSROOT: /cvs/cluster
Module name: conga
Changes by: rmccabe at sourceware.org 2007-08-31 04:57:37
Modified files:
ricci/common : Logger.cpp Makefile Module.cpp Network.cpp
Random.cpp ServerSocket.cpp Thread.cpp Time.cpp
Variable.cpp XML.cpp daemon_init.c signals.c
utils.cpp
Log message:
More cleanup
Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/Logger.cpp.diff?cvsroot=cluster&r1=1.2&r2=1.3
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/Makefile.diff?cvsroot=cluster&r1=1.9&r2=1.10
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/Module.cpp.diff?cvsroot=cluster&r1=1.6&r2=1.7
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/Network.cpp.diff?cvsroot=cluster&r1=1.1&r2=1.2
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/Random.cpp.diff?cvsroot=cluster&r1=1.2&r2=1.3
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/ServerSocket.cpp.diff?cvsroot=cluster&r1=1.4&r2=1.5
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/Thread.cpp.diff?cvsroot=cluster&r1=1.2&r2=1.3
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/Time.cpp.diff?cvsroot=cluster&r1=1.5&r2=1.6
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/Variable.cpp.diff?cvsroot=cluster&r1=1.8&r2=1.9
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/XML.cpp.diff?cvsroot=cluster&r1=1.10&r2=1.11
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/daemon_init.c.diff?cvsroot=cluster&r1=1.1&r2=1.2
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/signals.c.diff?cvsroot=cluster&r1=1.1&r2=1.2
http://sourceware.org/cgi-bin/cvsweb.cgi/conga/ricci/common/utils.cpp.diff?cvsroot=cluster&r1=1.9&r2=1.10
--- conga/ricci/common/Logger.cpp 2006/08/10 22:53:07 1.2
+++ conga/ricci/common/Logger.cpp 2007/08/31 04:57:37 1.3
@@ -1,5 +1,5 @@
/*
- Copyright Red Hat, Inc. 2005
+ Copyright Red Hat, Inc. 2005-2007
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
@@ -13,7 +13,7 @@
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
+ Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
MA 02139, USA.
*/
/*
@@ -38,139 +38,146 @@
static counting_auto_ptr<Logger> logger(new Logger());
-
Logger::Logger() :
- _fd(-1),
- _domain_c(NULL)
+ _fd(-1),
+ _domain_c(NULL)
{}
-Logger::Logger(const String& filepath,
- const String& domain,
- LogLevel level) :
- _level(level)
-{
- const char* c_str = domain.c_str();
- const char* path_c = filepath.c_str();
-
- _domain_c = (char*) malloc(domain.size()+1);
- if (_domain_c == NULL)
- throw String("Logger::Logger(): malloc() failed");
- strcpy(_domain_c, c_str);
-
- _fd = open(path_c,
- O_CREAT|O_WRONLY|O_APPEND,
- S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
- if (_fd == -1) {
- free(_domain_c);
- throw String("Logger::Logger(): open() failed");
- }
+Logger::Logger( const String& filepath,
+ const String& domain,
+ LogLevel level) :
+ _level(level)
+{
+ const char *c_str = domain.c_str();
+ const char *path_c = filepath.c_str();
+
+ _domain_c = (char *) malloc(domain.size() + 1);
+ if (_domain_c == NULL)
+ throw String("Logger::Logger(): malloc() failed");
+ strcpy(_domain_c, c_str);
+
+ _fd = open(path_c, O_CREAT | O_WRONLY | O_APPEND, 0640);
+ if (_fd == -1) {
+ free(_domain_c);
+ throw String("Logger::Logger(): open() failed");
+ }
}
Logger::Logger(int fd, const String& domain, LogLevel level) :
- _fd(fd),
- _level(level)
+ _fd(fd),
+ _level(level)
{
- const char* c_str = domain.c_str();
-
- _domain_c = (char*) malloc(domain.size()+1);
- if (_domain_c == NULL) {
- close_fd();
- throw String("Logger::Logger(): malloc() failed");
- }
- strcpy(_domain_c, c_str);
+ const char *c_str = domain.c_str();
+
+ _domain_c = (char *) malloc(domain.size() + 1);
+ if (_domain_c == NULL) {
+ close_fd();
+ throw String("Logger::Logger(): malloc() failed");
+ }
+ strcpy(_domain_c, c_str);
}
void
Logger::close_fd()
{
- if (_fd > -1)
- fsync(_fd);
- if (_fd > 2) {
- int e;
- do {
- e = close(_fd);
- } while (e == -1 && (errno == EINTR));
- _fd = -1;
- }
+ if (_fd > -1)
+ fsync(_fd);
+
+ if (_fd > 2) {
+ int e;
+ do {
+ e = close(_fd);
+ } while (e == -1 && (errno == EINTR));
+ _fd = -1;
+ }
}
Logger::~Logger()
{
- close_fd();
- free(_domain_c);
+ close_fd();
+ free(_domain_c);
}
-void
+void
Logger::log(const String& msg, LogLevel level)
{
- log_sigsafe(msg.c_str(), level);
+ log_sigsafe(msg.c_str(), level);
}
-void
-Logger::log_sigsafe(const char* msg, LogLevel level)
+void
+Logger::log_sigsafe(const char *msg, LogLevel level)
{
- if (_fd > 0 && _level & level) {
- char time[64];
- time_t t = time_sec();
- ctime_r(&t, time);
- time[sizeof(time)-1] = 0;
- for (int i=0; time[i]; i++)
- if (time[i] == '\n') {
- time[i] = 0;
- break;
- }
-
- char m[2048];
- if (_fd > 2 && (_domain_c != NULL))
- snprintf(m, sizeof(m), "%s %s: %s\n", time, _domain_c, msg);
- else
- snprintf(m, sizeof(m), "%s: %s\n", time, msg);
- m[sizeof(m)-1] = 0;
-
- int l, e;
- for (l=0; m[l]; l++) ;
- do {
- e = write(_fd, m, l);
- } while (e == -1 && errno == EINTR);
- }
+ if (_fd > 0 && (_level & level)) {
+ char time[64];
+ time_t t = time_sec();
+ char *p;
+ int ret;
+
+ ctime_r(&t, time);
+ time[sizeof(time) - 1] = '\0';
+
+ p = strchr(time, '\n');
+ if (p != NULL)
+ *p = '\0';
+
+ char m[4096] = { 0, };
+ if (_fd > 2 && _domain_c != NULL)
+ ret = snprintf(m, sizeof(m), "%s %s: %s\n", time, _domain_c, msg);
+ else
+ ret = snprintf(m, sizeof(m), "%s: %s\n", time, msg);
+
+ if (ret < 0 || (size_t) ret >= sizeof(m)) {
+ m[sizeof(m) - 1] = '\0';
+ ret = strlen(m);
+ }
+
+ while (ret > 0) {
+ ssize_t w = write(_fd, m, ret);
+ if (w < 0) {
+ if (errno == EINTR)
+ continue;
+ break;
+ }
+ ret -= w;
+ }
+ }
}
-
// ### helper functions ###
-String
+String
operator+ (const String& s, int i)
{
- char buff[128];
- snprintf(buff, sizeof(buff), "%d", i);
- return s + buff;
+ char buff[128];
+ snprintf(buff, sizeof(buff), "%d", i);
+ return s + buff;
}
-String
+String
operator+ (int i, const String& s)
{
- char buff[128];
- snprintf(buff, sizeof(buff), "%d", i);
- return String(buff) + s;
+ char buff[128];
+ snprintf(buff, sizeof(buff), "%d", i);
+ return String(buff) + s;
}
-void
+void
log(const String& msg, LogLevel level)
{
- logger->log(msg, level);
+ logger->log(msg, level);
}
-void
+void
log_sigsafe(const char* msg, LogLevel level)
{
- logger->log_sigsafe(msg, level);
+ logger->log_sigsafe(msg, level);
}
-void
+void
set_logger(counting_auto_ptr<Logger> l)
{
- if (l.get() == NULL)
- l = counting_auto_ptr<Logger>(new Logger());
- logger = l;
+ if (l.get() == NULL)
+ l = counting_auto_ptr<Logger>(new Logger());
+ logger = l;
}
--- conga/ricci/common/Makefile 2007/07/17 22:30:44 1.9
+++ conga/ricci/common/Makefile 2007/08/31 04:57:37 1.10
@@ -1,14 +1,15 @@
-################################################################################################################################################################
+################################################################################
##
-## Copyright (C) 2005 Red Hat, Inc. All rights reserved.
+## Copyright (C) 2005-2007 Red Hat, Inc. All rights reserved.
##
-## This copyrighted material is made available to anyone wishing to use,
-## modify, copy, or redistribute it subject to the terms and conditions
-## of the GNU General Public License v.2.
+## This copyrighted material is made available to anyone wishing to use,
+## modify, copy, or redistribute it subject to the terms and conditions
+## of the GNU General Public License v.2.
##
-##############################################################################################################################################################
+################################################################################
top_srcdir=..
+
UNINSTALL = ${top_srcdir}/scripts/uninstall.pl
include ${top_srcdir}/make/defines.mk
@@ -16,7 +17,8 @@
#TARGET = main
-OBJECTS = Except.o \
+OBJECTS = \
+ Except.o \
executils.o \
signals.o \
Thread.o \
@@ -35,12 +37,10 @@
base64.o \
Module.o
-
-INCLUDE +=
-CXXFLAGS +=
-CFLAGS +=
-LDFLAGS +=
-
+INCLUDE +=
+CXXFLAGS +=
+CFLAGS +=
+LDFLAGS +=
all: $(OBJECTS)
--- conga/ricci/common/Module.cpp 2007/06/25 16:03:42 1.6
+++ conga/ricci/common/Module.cpp 2007/08/31 04:57:37 1.7
@@ -13,7 +13,7 @@
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
+ Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
MA 02139, USA.
*/
/*
@@ -37,9 +37,9 @@
using namespace std;
-static const unsigned int timeout = 3000; // milliseconds
+static const unsigned int timeout = 3000; // milliseconds
-static ApiFcnMap _api_fcns; // api->name->function map
+static ApiFcnMap _api_fcns; // api->name->function map
static VarMap list_APIs(const VarMap& args);
static VarMap extract_vars(const XMLObject& xml);
@@ -48,220 +48,218 @@
+// ######## Module ########
-// ######## Module ########
-
-
-#define APIs_FUNC_NAME "APIs"
+#define APIs_FUNC_NAME "APIs"
Module::Module(const ApiFcnMap& api_fcns)
{
- for (ApiFcnMap::const_iterator api_iter = api_fcns.begin();
- api_iter != api_fcns.end();
- api_iter++) {
- const String& api_vers = api_iter->first;
- if (api_vers.empty())
- continue;
- FcnMap funcs = api_iter->second;
- funcs[APIs_FUNC_NAME] = list_APIs;
- _api_fcns[api_vers] = funcs;
- }
+ for (ApiFcnMap::const_iterator
+ api_iter = api_fcns.begin();
+ api_iter != api_fcns.end();
+ api_iter++)
+ {
+ const String& api_vers = api_iter->first;
+
+ if (api_vers.empty())
+ continue;
+
+ FcnMap funcs = api_iter->second;
+ funcs[APIs_FUNC_NAME] = list_APIs;
+ _api_fcns[api_vers] = funcs;
+ }
}
Module::~Module()
{}
-
-XMLObject
+XMLObject
Module::process(const XMLObject& request)
{
- try {
- if (request.tag() != REQUEST_TAG)
- throw APIerror("missing request tag");
-
- String version = request.get_attr(MOD_VERSION_TAG);
- if (_api_fcns.find(version) == _api_fcns.end())
- throw APIerror("unsupported API version");
-
- if (request.children().size() != 1)
- throw APIerror(String("missing ") + FUNC_CALL_TAG);
- const XMLObject& func_xml = request.children().front();
- if (func_xml.tag() != FUNC_CALL_TAG)
- throw APIerror(String("missing ") + FUNC_CALL_TAG);
-
- String fcn_name = func_xml.get_attr("name");
- if (fcn_name.empty())
- throw APIerror("missing function name");
- FcnMap& fcns = _api_fcns[version];
- if (fcns.find(fcn_name) == fcns.end())
- throw APIerror(String("function '") + fcn_name + "' not in API '" + version + "'");
-
- // construct response xml
- XMLObject response(RESPONSE_TAG);
- response.set_attr(MOD_VERSION_TAG, version);
- response.set_attr(SEQUENCE_TAG, request.get_attr(SEQUENCE_TAG));
- XMLObject func_resp_xml(FUNC_RESPONSE_TAG);
- func_resp_xml.set_attr("function_name", fcn_name);
- try {
- map<String, Variable> in_vars = extract_vars(func_xml);
- map<String, Variable> out_vars = (fcns[fcn_name])(in_vars);
- insert_vars(out_vars, func_resp_xml);
- func_resp_xml.add_child(Variable("success", true).xml());
- } catch (Except e) {
- func_resp_xml.add_child(Variable("success", false).xml());
- func_resp_xml.add_child(Variable("error_code", e.code()).xml());
- func_resp_xml.add_child(Variable("error_description", e.description()).xml());
- } catch ( String e ) {
- func_resp_xml.add_child(Variable("success", false).xml());
- func_resp_xml.add_child(Variable("error_code", Except::generic_error).xml());
- func_resp_xml.add_child(Variable("error_description", e).xml());
- } catch ( APIerror e ) {
- throw;
- } catch ( ... ) {
- func_resp_xml.add_child(Variable("success", false).xml());
- func_resp_xml.add_child(Variable("error_code", Except::generic_error).xml());
- func_resp_xml.add_child(Variable("error_description", String("No description")).xml());
- }
- response.add_child(func_resp_xml);
- return response;
- } catch ( APIerror e ) {
- XMLObject err_resp("API_error");
- err_resp.set_attr("description", e.msg);
- insert_vars(list_APIs(map<String, Variable>()), err_resp);
- return err_resp;
- } catch ( ... ) {
- XMLObject err_resp("internal_error");
- return err_resp;
- }
-}
+ try {
+ if (request.tag() != REQUEST_TAG)
+ throw APIerror("missing request tag");
+
+ String version = request.get_attr(MOD_VERSION_TAG);
+ if (_api_fcns.find(version) == _api_fcns.end())
+ throw APIerror("unsupported API version");
+
+ if (request.children().size() != 1)
+ throw APIerror(String("missing ") + FUNC_CALL_TAG);
+
+ const XMLObject& func_xml = request.children().front();
+ if (func_xml.tag() != FUNC_CALL_TAG)
+ throw APIerror(String("missing ") + FUNC_CALL_TAG);
+
+ String fcn_name = func_xml.get_attr("name");
+ if (fcn_name.empty())
+ throw APIerror("missing function name");
+
+ FcnMap& fcns = _api_fcns[version];
+ if (fcns.find(fcn_name) == fcns.end()) {
+ throw APIerror(String("function '") + fcn_name
+ + "' not in API '" + version + "'");
+ }
+ // construct response xml
+ XMLObject response(RESPONSE_TAG);
+ response.set_attr(MOD_VERSION_TAG, version);
+ response.set_attr(SEQUENCE_TAG, request.get_attr(SEQUENCE_TAG));
+
+ XMLObject func_resp_xml(FUNC_RESPONSE_TAG);
+ func_resp_xml.set_attr("function_name", fcn_name);
+
+ try {
+ map<String, Variable> in_vars = extract_vars(func_xml);
+ map<String, Variable> out_vars = (fcns[fcn_name])(in_vars);
+
+ insert_vars(out_vars, func_resp_xml);
+ func_resp_xml.add_child(Variable("success", true).xml());
+ } catch (Except e) {
+ func_resp_xml.add_child(Variable("success", false).xml());
+ func_resp_xml.add_child(Variable("error_code", e.code()).xml());
+ func_resp_xml.add_child(Variable("error_description",
+ e.description()).xml());
+ } catch ( String e ) {
+ func_resp_xml.add_child(Variable("success", false).xml());
+ func_resp_xml.add_child(Variable("error_code",
+ Except::generic_error).xml());
+ func_resp_xml.add_child(Variable("error_description", e).xml());
+ } catch ( APIerror e ) {
+ throw;
+ } catch ( ... ) {
+ func_resp_xml.add_child(Variable("success", false).xml());
+ func_resp_xml.add_child(Variable("error_code",
+ Except::generic_error).xml());
+ func_resp_xml.add_child(Variable("error_description",
+ String("No description")).xml());
+ }
+ response.add_child(func_resp_xml);
+ return response;
+ } catch ( APIerror e ) {
+ XMLObject err_resp("API_error");
+ err_resp.set_attr("description", e.msg);
+ insert_vars(list_APIs(map<String, Variable>()), err_resp);
+ return err_resp;
+ } catch ( ... ) {
+ XMLObject err_resp("internal_error");
+ return err_resp;
+ }
+}
VarMap
extract_vars(const XMLObject& xml)
{
- map<String, Variable> args;
- for (list<XMLObject>::const_iterator iter = xml.children().begin();
- iter != xml.children().end();
- iter++) {
- try {
- Variable var(*iter);
- args.insert(pair<String, Variable>(var.name(), var));
- } catch ( ... ) {}
- }
- return args;
+ map<String, Variable> args;
+
+ for (list<XMLObject>::const_iterator
+ iter = xml.children().begin() ;
+ iter != xml.children().end() ;
+ iter++)
+ {
+ try {
+ Variable var(*iter);
+ args.insert(pair<String, Variable>(var.name(), var));
+ } catch ( ... ) {}
+ }
+
+ return args;
}
-void
-insert_vars(const VarMap& vars,
- XMLObject& xml)
+void
+insert_vars(const VarMap& vars, XMLObject& xml)
{
- for (VarMap::const_iterator iter = vars.begin();
- iter != vars.end();
- iter++)
- xml.add_child(iter->second.xml());
+ for (VarMap::const_iterator
+ iter = vars.begin() ;
+ iter != vars.end() ;
+ iter++)
+ {
+ xml.add_child(iter->second.xml());
+ }
}
VarMap
list_APIs(const VarMap& args)
{
- list<String> apis;
- for (ApiFcnMap::const_iterator iter = _api_fcns.begin();
- iter != _api_fcns.end();
- iter++)
- apis.push_back(iter->first);
-
- Variable api_var("APIs", apis);
- VarMap ret;
- ret.insert(pair<String, Variable>(api_var.name(), api_var));
- return ret;
+ list<String> apis;
+ for (ApiFcnMap::const_iterator
+ iter = _api_fcns.begin() ;
+ iter != _api_fcns.end() ;
+ iter++)
+ {
+ apis.push_back(iter->first);
+ }
+
+ Variable api_var("APIs", apis);
+ VarMap ret;
+ ret.insert(pair<String, Variable>(api_var.name(), api_var));
+ return ret;
}
-
-
-
-
-
-
-
-
-
-
-
-
-// ################ ModuleDriver ######################
-
-
-
+// ################ ModuleDriver ######################
#include <fcntl.h>
-static void
-close_fd(int fd);
-
-static int
-__stdin_out_module_driver(Module& module);
+static void close_fd(int fd);
+static int __stdin_out_module_driver(Module& module);
int
-stdin_out_module_driver(Module& module,
- int argc,
- char** argv)
+stdin_out_module_driver(Module& module, int argc, char** argv)
{
- bool display_err = false;
- int rv;
- while ((rv = getopt(argc, argv, "e")) != EOF)
- switch (rv) {
- case 'e':
- display_err = true;
- break;
- default:
- break;
- }
-
- int old_err;
- if (!display_err) {
- // redirect stderr to /dev/null
- old_err = dup(2);
- int devnull = open("/dev/null", O_RDWR);
- if (devnull == -1) {
- perror("stdin_out_module_driver(): Can't open /dev/null");
- exit(1);
- }
- dup2(devnull, 2);
- close_fd(devnull);
- }
-
- try {
-
- return __stdin_out_module_driver(module);
-
- } catch ( ... ) {
- if (!display_err) {
- // restore stderr
- dup2(old_err, 2);
- close_fd(old_err);
- }
- throw;
- }
-}
+ bool display_err = false;
+ int rv;
+ while ((rv = getopt(argc, argv, "e")) != EOF) {
+ switch (rv) {
+ case 'e':
+ display_err = true;
+ break;
+ default:
+ break;
+ }
+ }
+ int old_err;
+ if (!display_err) {
+ // redirect stderr to /dev/null
+ old_err = dup(2);
+ int devnull = open("/dev/null", O_RDWR);
+ if (devnull == -1) {
+ perror("stdin_out_module_driver(): Can't open /dev/null");
+ exit(1);
+ }
+ dup2(devnull, 2);
+ close_fd(devnull);
+ }
+
+ try {
+ return __stdin_out_module_driver(module);
+ } catch ( ... ) {
+ if (!display_err) {
+ // restore stderr
+ dup2(old_err, 2);
+ close_fd(old_err);
+ }
+ throw;
+ }
+}
int
__stdin_out_module_driver(Module& module)
{
unsigned int time_beg = time_mil();
String data;
-
+
while (time_mil() < time_beg + timeout) {
poll_fd poll_data;
poll_data.fd = 0;
poll_data.events = POLLIN;
poll_data.revents = 0;
-
+
// wait for events
int ret = poll(&poll_data, 1, 500);
@@ -286,7 +284,7 @@
else
throw String("poll() error: ") + String(strerror(errno));
}
-
+
// process event
if (poll_data.revents & POLLIN) {
char buff[4096];
@@ -318,19 +316,16 @@
if (poll_data.revents & (POLLERR | POLLHUP | POLLNVAL))
throw String("stdin error: ") + String(strerror(errno));
} // while
-
- // cout << data << endl;
+
+ // cout << data << endl;
throw String("invalid input");
}
-
-
-
void
close_fd(int fd)
{
- int e;
- do {
- e = close(fd);
- } while (e && (errno == EINTR));
+ int e;
+ do {
+ e = close(fd);
+ } while (e && (errno == EINTR));
}
--- conga/ricci/common/Network.cpp 2007/03/23 17:25:12 1.1
+++ conga/ricci/common/Network.cpp 2007/08/31 04:57:37 1.2
@@ -13,7 +13,7 @@
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
+ Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
MA 02139, USA.
*/
/*
@@ -28,57 +28,58 @@
#include <arpa/inet.h>
-
-counting_auto_ptr<Network::Hostent>
+counting_auto_ptr<Network::Hostent>
Network::getHostByName(const String& hostname)
{
- counting_auto_ptr<Hostent> ent_d(new Hostent());
- struct hostent *ent = 0;
- int error;
- gethostbyname2_r(hostname.c_str(), AF_INET,
- &(ent_d->ent),
- ent_d->data, sizeof(ent_d->data),
- &ent,
- &error);
- if (ent == &(ent_d->ent))
- return ent_d;
- throw String("unable to resolve ") + hostname;
+ counting_auto_ptr<Hostent> ent_d(new Hostent());
+ struct hostent *ent = 0;
+ int error;
+
+ gethostbyname2_r(hostname.c_str(),
+ AF_INET,
+ &(ent_d->ent),
+ ent_d->data, sizeof(ent_d->data),
+ &ent,
+ &error);
+ if (ent == &(ent_d->ent))
+ return ent_d;
+ throw String("unable to resolve ") + hostname;
}
-
std::vector<String>
Network::name2IP(const String& hostname)
{
- std::vector<String> addrs;
- try {
- char buff[INET_ADDRSTRLEN+1];
- counting_auto_ptr<Hostent> hent = getHostByName(hostname);
- char** addrs_b = (*hent)->h_addr_list;
- for (int i=0; addrs_b[i]; i++) {
- struct in_addr addr;
- addr.s_addr = *((u_int32_t*) addrs_b[i]);
- if (inet_ntop(AF_INET, &addr, buff, sizeof(buff)))
- addrs.push_back(buff);
- }
- } catch ( ... ) {}
- return addrs;
-}
+ std::vector<String> addrs;
+ try {
+ char buff[INET_ADDRSTRLEN + 1];
+ counting_auto_ptr<Hostent> hent = getHostByName(hostname);
+ char **addrs_b = (*hent)->h_addr_list;
+
+ for (int i = 0 ; addrs_b[i] ; i++) {
+ struct in_addr addr;
+ addr.s_addr = *((u_int32_t*) addrs_b[i]);
+ if (inet_ntop(AF_INET, &addr, buff, sizeof(buff)))
+ addrs.push_back(buff);
+ }
+ } catch ( ... ) {}
+ return addrs;
+}
String
Network::localhost()
{
- // get hostname
- char name[1024];
- if (gethostname(name, sizeof(name)-1))
- return "";
- name[sizeof(name)-1] = '\0';
-
- try {
- // get fqdn
- counting_auto_ptr<Hostent> ent = getHostByName(name);
- return String((*ent)->h_name);
- } catch ( ... ) {
- return name;
- }
+ // get hostname
+ char name[1024];
+ if (gethostname(name, sizeof(name) - 1))
+ return "";
+ name[sizeof(name) - 1] = '\0';
+
+ try {
+ // get fqdn
+ counting_auto_ptr<Hostent> ent = getHostByName(name);
+ return String((*ent)->h_name);
+ } catch ( ... ) {
+ return name;
+ }
}
--- conga/ricci/common/Random.cpp 2006/08/10 22:53:07 1.2
+++ conga/ricci/common/Random.cpp 2007/08/31 04:57:37 1.3
@@ -1,5 +1,5 @@
/*
- Copyright Red Hat, Inc. 2005
+ Copyright Red Hat, Inc. 2005-2007
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
@@ -13,7 +13,7 @@
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
+ Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
MA 02139, USA.
*/
/*
@@ -25,24 +25,33 @@
#include "Mutex.h"
#include "Time.h"
+#include <unistd.h>
#include <stdlib.h>
+#include <fcntl.h>
#include "String.h"
-
static Mutex mutex;
-static unsigned int seed = 0;
-
+static uint32_t seed = 0;
-int
+int
random_generator(int min, int max)
{
- MutexLocker l(mutex);
- if (!seed)
- seed = time_mil();
-
- int range = max - min;
- if (range <= 5)
- throw String("random_generate(min, max): range too small");
-
- return (int) (min + range * ((double) rand_r(&seed))/RAND_MAX);
+ MutexLocker l(mutex);
+
+ int fd = open("/dev/urandom", O_RDONLY);
+ if (fd > 0) {
+ ssize_t ret = read(fd, &seed, sizeof(seed));
+ close(fd);
+ if (ret > 0 && (size_t) ret != sizeof(seed))
+ seed = 0;
+ }
+
+ if (!seed)
+ seed = time_mil();
+
+ int range = max - min;
+ if (range <= 5)
+ throw String("random_generate(min, max): range too small");
+
+ return (int) (min + range * ((double) rand_r(&seed)) / RAND_MAX);
}
--- conga/ricci/common/ServerSocket.cpp 2006/10/14 17:51:35 1.4
+++ conga/ricci/common/ServerSocket.cpp 2007/08/31 04:57:37 1.5
@@ -1,5 +1,5 @@
/*
- Copyright Red Hat, Inc. 2005
+ Copyright Red Hat, Inc. 2005-2007
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
@@ -13,7 +13,7 @@
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
+ Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
MA 02139, USA.
*/
/*
@@ -33,127 +33,129 @@
#include "String.h"
-
ServerSocket::ServerSocket(const String& sock_path) :
- Socket(-1),
- _unix_sock(true),
- _sock_path(sock_path)
-{
- _sock = socket(PF_UNIX, SOCK_STREAM, 0);
- if (_sock == -1) {
- String m = String("ServerSocket(sock_path=") + sock_path + "): socket() failed, errno=" + errno;
- throw m;
- }
-
- int dummy_true = 1;
- if (setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, &dummy_true, sizeof(dummy_true))) {
- String m = String("ServerSocket(sock_path=") + sock_path + "): set SO_REUSEADDR, failed, errno=" + errno;
- throw m;
- }
-
- struct sockaddr_un {
- sa_family_t sun_family;
- char sun_path[100];
- } addr;
- addr.sun_family = AF_UNIX;
- memcpy(addr.sun_path, sock_path.c_str(), sock_path.size()+1);
-
- unlink(_sock_path.c_str());
- if (bind(_sock, (struct sockaddr*) &addr, sizeof(addr))) {
- String m = String("ServerSocket(sock_path=") + sock_path + "): bind() failed, errno=" + errno;
- throw m;
- }
-
- if (listen(_sock, 5)) {
- String m = String("ServerSocket(sock_path=") + sock_path + "): listen() failed, errno=" + errno;
- throw m;
- }
-
- String msg = String("created unix server socket, ") + _sock + ", " + sock_path;
- // log(msg, LogSocket);
+ Socket(-1),
+ _unix_sock(true),
+ _sock_path(sock_path)
+{
+ _sock = socket(PF_UNIX, SOCK_STREAM, 0);
+ if (_sock == -1) {
+ throw String("ServerSocket(sock_path=") + sock_path
+ + "): socket() failed: " + String(strerror(errno));
+ }
+
+ int t = 1;
+ if (setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, &t, sizeof(t))) {
+ throw String("ServerSocket(sock_path=") + sock_path
+ + "): set SO_REUSEADDR, failed: " + String(strerror(errno));
+ }
+
+ struct sockaddr_un addr;
+ addr.sun_family = AF_UNIX;
+ memcpy(addr.sun_path, sock_path.c_str(), sock_path.size() + 1);
+
+ unlink(_sock_path.c_str());
+
+ if (bind(_sock, (struct sockaddr*) &addr, sizeof(addr))) {
+ throw String("ServerSocket(sock_path=") + sock_path
+ + "): bind() failed: " + String(strerror(errno));
+ }
+
+ if (listen(_sock, 5)) {
+ throw String("ServerSocket(sock_path=") + sock_path
+ + "): listen() failed: " + String(strerror(errno));
+ }
+ //String msg = String("created unix server socket, ")
+ // + _sock + ", " + sock_path;
+ //log(msg, LogSocket);
}
ServerSocket::ServerSocket(unsigned short port) :
- Socket(-1),
- _unix_sock(false),
- _sock_path("")
-{
- _sock = socket(PF_INET, SOCK_STREAM, 0);
- if (_sock == -1) {
- String m = String("ServerSocket(port=") + port + "): socket() failed, errno=" + errno;
- throw m;
- }
-
- int dummy_true = 1;
- if (setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, &dummy_true, sizeof(dummy_true))) {
- String m = String("ServerSocket(port=") + port + "): set SO_REUSEADDR, failed, errno=" + errno;
- throw m;
- }
-
- struct sockaddr_in addr;
- addr.sin_family = AF_INET;
- addr.sin_port = htons(port);
- addr.sin_addr.s_addr = INADDR_ANY;
- if (bind(_sock, (struct sockaddr*) &addr, sizeof(addr))) {
- String m = String("ServerSocket(port=") + port + "): bind() failed, errno=" + errno;
- throw m;
- }
-
- if (listen(_sock, 5)) {
- String m = String("ServerSocket(port=") + port + "): listen() failed, errno=" + errno;
- throw m;
- }
-
- String msg = String("created tcp server socket, ") + _sock + ", port " + port;
- // log(msg, LogSocket);
+ Socket(-1),
+ _unix_sock(false),
+ _sock_path("")
+{
+ _sock = socket(PF_INET, SOCK_STREAM, 0);
+ if (_sock == -1) {
+ throw String("ServerSocket(port=") + port
+ + "): socket() failed: " + String(strerror(errno));
+ }
+
+ int t = 1;
+ if (setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, &t, sizeof(t))) {
+ throw String("ServerSocket(port=") + port
+ + "): set SO_REUSEADDR, failed: " + String(strerror(errno));
+ }
+
+ struct sockaddr_in addr;
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(port);
+ addr.sin_addr.s_addr = INADDR_ANY;
+
+ if (bind(_sock, (struct sockaddr*) &addr, sizeof(addr))) {
+ throw String("ServerSocket(port=") + port
+ + "): bind() failed: " + String(strerror(errno));
+ }
+
+ if (listen(_sock, 5)) {
+ throw String("ServerSocket(port=") + port
+ + "): listen() failed: " + String(strerror(errno));
+ }
+
+ //String msg = String("created tcp server socket, ")
+ // + _sock + ", port " + port;
+ //log(msg, LogSocket);
}
ServerSocket::ServerSocket(const ServerSocket& s) :
- Socket(s),
- _unix_sock(s._unix_sock),
- _sock_path(s._sock_path)
+ Socket(s),
+ _unix_sock(s._unix_sock),
+ _sock_path(s._sock_path)
{}
-ServerSocket&
+ServerSocket&
ServerSocket::operator= (const ServerSocket& s)
{
- if (&s != this) {
- this->Socket::operator= (s);
- _unix_sock = s._unix_sock;
- _sock_path = s._sock_path;
- }
- return *this;
+ if (&s != this) {
+ this->Socket::operator= (s);
+ _unix_sock = s._unix_sock;
+ _sock_path = s._sock_path;
+ }
+
+ return *this;
}
ServerSocket::~ServerSocket()
{
- if (_unix_sock && (*_counter == 1))
- unlink(_sock_path.c_str());
+ if (_unix_sock && (*_counter == 1))
+ unlink(_sock_path.c_str());
}
-ClientSocket
+ClientSocket
ServerSocket::accept()
{
- while (true) {
- struct sockaddr_in addr_in;
- socklen_t size = sizeof(addr_in);
-
- int ret = ::accept(_sock, (struct sockaddr*) &addr_in, &size);
- if (ret == -1) {
- if (errno == EINTR)
- continue;
- throw String("ServerSocket(): accept() failed");
- }
- // log("ServerSocket: accepted connection", LogSocket);
- return ClientSocket(ret, addr_in.sin_addr.s_addr);
- }
+ while (true) {
+ struct sockaddr_in addr_in;
+ socklen_t size = sizeof(addr_in);
+
+ int ret = ::accept(_sock, (struct sockaddr *) &addr_in, &size);
+ if (ret == -1) {
+ if (errno == EINTR)
+ continue;
+ throw String("ServerSocket(): accept() failed: ")
+ + String(strerror(errno));
+ }
+ //log("ServerSocket: accepted connection", LogSocket);
+ return ClientSocket(ret, addr_in.sin_addr.s_addr);
+ }
}
-bool
+bool
ServerSocket::ready(int timeout)
{
- bool read = true;
- bool write = false;
- poll(read, write, timeout);
- return read;
+ bool read = true;
+ bool write = false;
+
+ poll(read, write, timeout);
+ return read;
}
--- conga/ricci/common/Thread.cpp 2006/08/10 22:53:07 1.2
+++ conga/ricci/common/Thread.cpp 2007/08/31 04:57:37 1.3
@@ -1,5 +1,5 @@
/*
- Copyright Red Hat, Inc. 2005
+ Copyright Red Hat, Inc. 2005-2007
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
@@ -13,7 +13,7 @@
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
+ Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
MA 02139, USA.
*/
/*
@@ -25,80 +25,81 @@
//#include "Logger.h"
#include "String.h"
-
-
void*
-start_thread(void* thread_obj)
+start_thread(void *thread_obj)
{
- try {
- ((Thread*) thread_obj)->run();
- }
- catch ( ... ) {}
- return NULL;
+ try {
+ ((Thread*) thread_obj)->run();
+ }
+ catch ( ... ) {}
+ return NULL;
}
-
Thread::Thread() :
- _stop(true),
- _running(false)
+ _stop(true),
+ _running(false)
{}
Thread::~Thread()
{
- // log(String("entered destructor of thread ") + (int) _thread, LogThread);
- Thread::stop();
+ //log(String("entered destructor of thread ") + (int) _thread, LogThread);
+ Thread::stop();
}
-
-void
+void
Thread::start()
{
- // log("entered Thread::start()", LogThread);
- MutexLocker l1(_main_mutex);
- if (!_running) {
- {
- MutexLocker l2(_stop_mutex);
- _stop = false;
- }
- pthread_create(&_thread, NULL, start_thread, this);
- // log(String("created thread ") + (int) _thread, LogThread);
- _running = true;
- }
+ //log("entered Thread::start()", LogThread);
+ MutexLocker l1(_main_mutex);
+ if (!_running) {
+ {
+ MutexLocker l2(_stop_mutex);
+ _stop = false;
+ }
+
+ int ret = pthread_create(&_thread, NULL, start_thread, this);
+ if (ret == 0)
+ _running = true;
+ else
+ throw String("Error starting thread: ") + String(strerror(ret));
+ //log(String("created thread ") + (int) _thread, LogThread);
+ }
}
-void
+void
Thread::stop()
{
- // log(String("entered Thread::stop() for thread ") + (int) _thread, LogThread);
- MutexLocker l1(_main_mutex);
- if (_running) {
- {
- // log(String("Thread::stop(): locking stop mutex for thread ") + (int) _thread, LogThread);
- MutexLocker l2(_stop_mutex);
- _stop = true;
- }
- // log(String("entering pthread_join() for thread ") + (int) _thread, LogThread);
- if (pthread_join(_thread, NULL))
- throw String("error stopping thread");
- // log(String("stopped thread ") + (int) _thread, LogThread);
- _running = false;
- }
+ //log(String("entered Thread::stop() for thread ") + (int) _thread, LogThread);
+ MutexLocker l1(_main_mutex);
+ if (_running) {
+ {
+ //log(String("Thread::stop(): locking stop mutex for thread ") + (int) _thread, LogThread);
+ MutexLocker l2(_stop_mutex);
+ _stop = true;
+ }
+ //log(String("entering pthread_join() for thread ") + (int) _thread, LogThread);
+
+ if (pthread_join(_thread, NULL))
+ throw String("error stopping thread");
+ //log(String("stopped thread ") + (int) _thread, LogThread);
+ _running = false;
+ }
}
-bool
+bool
Thread::running()
{
- // log(String("entered Thread::running() for thread ") + (int) _thread, LogThread);
- MutexLocker l1(_main_mutex);
- bool ret = _running;
- return ret;
+ //log(String("entered Thread::running() for thread ") + (int) _thread, LogThread);
+ MutexLocker l1(_main_mutex);
+ bool ret = _running;
+ return ret;
}
-bool
+bool
Thread::shouldStop()
{
- // log(String("entered Thread::shouldStop() for thread ") + (int) _thread, LogThread);
- MutexLocker l(_stop_mutex);
- bool ret = _stop;
- return ret;
+ //log(String("entered Thread::shouldStop() for thread ") + (int) _thread, LogThread);
+ MutexLocker l(_stop_mutex);
+ bool ret = _stop;
+ return ret;
}
--- conga/ricci/common/Time.cpp 2007/03/21 20:12:58 1.5
+++ conga/ricci/common/Time.cpp 2007/08/31 04:57:37 1.6
@@ -13,7 +13,7 @@
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
+ Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
MA 02139, USA.
*/
/*
@@ -27,48 +27,54 @@
#include <sys/time.h>
-unsigned int
+unsigned int
time_sec()
{
- struct timeval t;
- gettimeofday(&t, NULL);
- return t.tv_sec;
+ struct timeval t;
+ gettimeofday(&t, NULL);
+
+ return t.tv_sec;
}
-unsigned int
+unsigned int
time_mil()
{
- struct timeval t;
- gettimeofday(&t, NULL);
- return t.tv_sec*1000 + t.tv_usec/1000;
+ struct timeval t;
+ gettimeofday(&t, NULL);
+
+ return t.tv_sec * 1000 + t.tv_usec / 1000;
}
-String
+String
time_formated()
{
- char time[64];
- time_t t = time_sec();
- ctime_r(&t, time);
- String m(time);
- return m.substr(0, m.size()-1);
+ char time[64];
+ time_t t = time_sec();
+
+ ctime_r(&t, time);
+ String m(time);
+ return m.substr(0, m.size() - 1);
}
void
sleep_mil(unsigned char num)
{
- struct timespec req, rem;
- rem.tv_sec = 0;
- rem.tv_nsec = num * 1000 * 1000;
- do {
- req.tv_sec = rem.tv_sec;
- req.tv_nsec = rem.tv_nsec;
- } while (nanosleep(&req, &rem) == -1 && errno == EINTR);
+ struct timespec req, rem;
+
+ rem.tv_sec = 0;
+ rem.tv_nsec = num * 1000 * 1000;
+
+ do {
+ req.tv_sec = rem.tv_sec;
+ req.tv_nsec = rem.tv_nsec;
+ } while (nanosleep(&req, &rem) == -1 && errno == EINTR);
}
void
sleep_sec(unsigned char num)
{
- for (unsigned char i=0; i<num; i++)
- for (int j=0; j<4; j++)
- sleep_mil(250);
+ for (unsigned char i = 0 ; i < num ; i++) {
+ for (int j = 0 ; j < 4 ; j++)
+ sleep_mil(250);
+ }
}
--- conga/ricci/common/Variable.cpp 2006/09/26 01:04:20 1.8
+++ conga/ricci/common/Variable.cpp 2007/08/31 04:57:37 1.9
@@ -1,5 +1,5 @@
/*
- Copyright Red Hat, Inc. 2005
+ Copyright Red Hat, Inc. 2005-2007
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
@@ -13,7 +13,7 @@
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
+ Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
MA 02139, USA.
*/
/*
@@ -31,698 +31,750 @@
using namespace std;
-
-// ##### class Variable #####
+// ##### class Variable #####
Variable::Variable(const XMLObject& xml)
{
- if (xml.tag() != VARIABLE_TAG)
- throw String("not a variable");
-
- _name = xml.get_attr("name");
- if (_name == "")
- throw String("invalid variable name");
-
- _mutable = (xml.get_attr("mutable") == "true");
-
- // _validator = Validator(xml); // incoming constraints are not to be trusted anyhow
-
- // conditionals
- _cond_bool_if = xml.get_attr("if_bool");
- _cond_bool_ifnot = xml.get_attr("ifnot_bool");
-
- String type(xml.get_attr("type"));
- if (type == VARIABLE_INT) {
- _type = Integer;
- _val_int = utils::to_long(xml.get_attr("value").c_str());
- } else if (type == VARIABLE_INT_SEL) {
- _type = IntSel;
- _val_int = utils::to_long(xml.get_attr("value").c_str());
- } else if (type == VARIABLE_BOOL) {
- _type = Boolean;
- _val_bool = (xml.get_attr("value") == "true");
- } else if (type == VARIABLE_STR) {
- _type = StringVar;
- _val_str = xml.get_attr("value");
- } else if (type == VARIABLE_STR_SEL) {
- _type = StrSel;
- _val_str = xml.get_attr("value");
- } else if (type == VARIABLE_XML) {
- _type = XMLVar;
- if (xml.children().empty())
- throw String("variable missing XML value");
- else
- _val_xml = xml.children().front();
- } else if (type == VARIABLE_LIST_INT) {
- _type = ListInt;
- for (list<XMLObject>::const_iterator iter = xml.children().begin();
- iter != xml.children().end();
- iter++) {
- const XMLObject& node = *iter;
- if (node.tag() == VARIABLE_LISTENTRY)
- _val_list_int.push_back(utils::to_long(node.get_attr("value").c_str()));
- }
- } else if (type == VARIABLE_LIST_STR) {
- _type = ListStr;
- for (list<XMLObject>::const_iterator iter = xml.children().begin();
- iter != xml.children().end();
- iter++) {
- const XMLObject& node = *iter;
- if (node.tag() == VARIABLE_LISTENTRY)
- _val_list_str.push_back(node.get_attr("value"));
- }
- } else if (type == VARIABLE_LIST_XML) {
- _type = ListXML;
- for (list<XMLObject>::const_iterator iter = xml.children().begin();
- iter != xml.children().end();
- iter++)
- _val_list_XML.push_back(*iter);
- } else
- throw String("invalid variable type");
-
-
-}
-
+ if (xml.tag() != VARIABLE_TAG)
+ throw String("not a variable");
+ _name = xml.get_attr("name");
+ if (_name == "")
+ throw String("invalid variable name");
+
+ _mutable = (xml.get_attr("mutable") == "true");
+
+ //_validator = Validator(xml); // incoming constraints are not to be trusted anyhow
+
+ // conditionals
+ _cond_bool_if = xml.get_attr("if_bool");
+ _cond_bool_ifnot = xml.get_attr("ifnot_bool");
+
+ String type(xml.get_attr("type"));
+ if (type == VARIABLE_INT) {
+ _type = Integer;
+ _val_int = utils::to_long(xml.get_attr("value").c_str());
+ } else if (type == VARIABLE_INT_SEL) {
+ _type = IntSel;
+ _val_int = utils::to_long(xml.get_attr("value").c_str());
+ } else if (type == VARIABLE_BOOL) {
+ _type = Boolean;
+ _val_bool = (xml.get_attr("value") == "true");
+ } else if (type == VARIABLE_STR) {
+ _type = StringVar;
+ _val_str = xml.get_attr("value");
+ } else if (type == VARIABLE_STR_SEL) {
+ _type = StrSel;
+ _val_str = xml.get_attr("value");
+ } else if (type == VARIABLE_XML) {
+ _type = XMLVar;
+ if (xml.children().empty())
+ throw String("variable missing XML value");
+ else
+ _val_xml = xml.children().front();
+ } else if (type == VARIABLE_LIST_INT) {
+ _type = ListInt;
+ for (list<XMLObject>::const_iterator
+ iter = xml.children().begin() ;
+ iter != xml.children().end() ;
+ iter++)
+ {
+ const XMLObject& node = *iter;
+
+ if (node.tag() == VARIABLE_LISTENTRY)
+ _val_list_int.push_back(utils::to_long(node.get_attr("value").c_str()));
+ }
+ } else if (type == VARIABLE_LIST_STR) {
+ _type = ListStr;
+ for (list<XMLObject>::const_iterator
+ iter = xml.children().begin() ;
+ iter != xml.children().end() ;
+ iter++)
+ {
+ const XMLObject& node = *iter;
+
+ if (node.tag() == VARIABLE_LISTENTRY)
+ _val_list_str.push_back(node.get_attr("value"));
+ }
+ } else if (type == VARIABLE_LIST_XML) {
+ _type = ListXML;
+ for (list<XMLObject>::const_iterator
+ iter = xml.children().begin() ;
+ iter != xml.children().end() ;
+ iter++)
+ {
+ _val_list_XML.push_back(*iter);
+ }
+ } else
+ throw String("invalid variable type");
+}
// integer
-Variable::Variable(const String& name,
- long long value) :
- _name(name),
- _type(Integer),
- _mutable(false)
-{
- set_value(value);
-}
-Variable::Variable(const String& name,
- long long value,
- long long min,
- long long max,
- long long step) :
- _name(name),
- _type(Integer),
- _mutable(true),
- _validator(min, max, step)
+Variable::Variable(const String& name, long long value) :
+ _name(name),
+ _type(Integer),
+ _mutable(false)
+{
+ set_value(value);
+}
+
+Variable::Variable( const String& name,
+ long long value,
+ long long min,
+ long long max,
+ long long step) :
+ _name(name),
+ _type(Integer),
+ _mutable(true),
+ _validator(min, max, step)
{
- set_value(value);
+ set_value(value);
}
-
// integer selector
-Variable::Variable(const String& name,
- long long value,
- const std::list<long long>& valid_values) :
- _name(name),
- _type(IntSel),
- _mutable(true),
- _validator(valid_values)
+Variable::Variable( const String& name,
+ long long value,
+ const std::list<long long>& valid_values) :
+ _name(name),
+ _type(IntSel),
+ _mutable(true),
+ _validator(valid_values)
{
- set_value(value);
+ set_value(value);
}
-
// integer list
-Variable::Variable(const String& name,
- const std::list<long long>& value,
- bool mutabl) :
- _name(name),
- _type(ListInt),
- _mutable(mutabl)
+Variable::Variable( const String& name,
+ const std::list<long long>& value,
+ bool mutabl) :
+ _name(name),
+ _type(ListInt),
+ _mutable(mutabl)
{
- set_value(value);
+ set_value(value);
}
// boolean
-Variable::Variable(const String& name,
- bool value,
- bool mutabl) :
- _name(name),
- _type(Boolean),
- _mutable(mutabl)
+Variable::Variable(const String& name, bool value, bool mutabl) :
+ _name(name),
+ _type(Boolean),
+ _mutable(mutabl)
{
- set_value(value);
+ set_value(value);
}
-
// string
-Variable::Variable(const String& name,
- const String& value) :
- _name(name),
- _type(StringVar),
- _mutable(false)
-{
- set_value(value);
-}
-Variable::Variable(const String& name,
- const String& value,
- long long min_length,
- long long max_length,
- const String& illegal_chars,
- const std::list<String>& reserved_words) :
- _name(name),
- _type(StringVar),
- _mutable(true),
- _validator(min_length,
- max_length,
- illegal_chars,
- reserved_words)
+Variable::Variable(const String& name, const String& value) :
+ _name(name),
+ _type(StringVar),
+ _mutable(false)
+{
+ set_value(value);
+}
+
+Variable::Variable( const String& name,
+ const String& value,
+ long long min_length,
+ long long max_length,
+ const String& illegal_chars,
+ const std::list<String>& reserved_words) :
+ _name(name),
+ _type(StringVar),
+ _mutable(true),
+ _validator(min_length, max_length, illegal_chars, reserved_words)
{
- set_value(value);
+ set_value(value);
}
-
// string selector
-Variable::Variable(const String& name,
- const String& value,
- const std::list<String>& valid_values) :
- _name(name),
- _type(StrSel),
- _mutable(true),
- _validator(valid_values)
+Variable::Variable( const String& name,
+ const String& value,
+ const std::list<String>& valid_values) :
+ _name(name),
+ _type(StrSel),
+ _mutable(true),
+ _validator(valid_values)
{
- set_value(value);
+ set_value(value);
}
-
// string list
-Variable::Variable(const String& name,
- const std::list<String>& value,
- bool mutabl) :
- _name(name),
- _type(ListStr),
- _mutable(mutabl)
+Variable::Variable( const String& name,
+ const std::list<String>& value,
+ bool mutabl) :
+ _name(name),
+ _type(ListStr),
+ _mutable(mutabl)
{
- set_value(value);
+ set_value(value);
}
// XML
-Variable::Variable(const String& name,
- const XMLObject& value) :
- _name(name),
- _type(XMLVar),
- _mutable(false)
+Variable::Variable(const String& name, const XMLObject& value) :
+ _name(name),
+ _type(XMLVar),
+ _mutable(false)
{
- set_value(value);
+ set_value(value);
}
// XML list
-Variable::Variable(const String& name,
- const std::list<XMLObject>& value) :
- _name(name),
- _type(ListXML),
- _mutable(false)
+Variable::Variable(const String& name, const std::list<XMLObject>& value) :
+ _name(name),
+ _type(ListXML),
+ _mutable(false)
{
- set_value(value);
+ set_value(value);
}
Variable::~Variable()
{}
-
-void
+void
Variable::set_conditional_bool_if(const String& bool_name)
{
- if (name() == bool_name)
- throw String("circular conditional: ") + bool_name;
- _cond_bool_if = bool_name;
+ if (name() == bool_name)
+ throw String("circular conditional: ") + bool_name;
+ _cond_bool_if = bool_name;
}
-void
+void
Variable::set_conditional_bool_ifnot(const String& bool_name)
{
- if (name() == bool_name)
- throw String("circular conditional: ") + bool_name;
- _cond_bool_ifnot = bool_name;
+ if (name() == bool_name)
+ throw String("circular conditional: ") + bool_name;
+ _cond_bool_ifnot = bool_name;
}
-
-long long
+long long
Variable::get_int() const
-{
- if (_type != Integer && _type != IntSel)
- throw String("variable ") + name() + " is not of " + VARIABLE_INT + " type";
- return _val_int;
+{
+ if (_type != Integer && _type != IntSel) {
+ throw String("variable ") + name() + " is not of "
+ + VARIABLE_INT + " type";
+ }
+ return _val_int;
}
+
void
Variable::set_value(long long value)
{
- if (_type != Integer && _type != IntSel)
- throw String("variable ") + name() + " is not of " + VARIABLE_INT + " type";
- _validator.validate(value);
- _val_int = value;
+ if (_type != Integer && _type != IntSel) {
+ throw String("variable ") + name() + " is not of "
+ + VARIABLE_INT + " type";
+ }
+ _validator.validate(value);
+ _val_int = value;
}
-bool
+bool
Variable::get_bool() const
{
- if (_type != Boolean)
- throw String("variable ") + name() + " is not of " + VARIABLE_BOOL + " type";
- return _val_bool;
+ if (_type != Boolean) {
+ throw String("variable ") + name() + " is not of "
+ + VARIABLE_BOOL + " type";
+ }
+ return _val_bool;
}
+
void
Variable::set_value(bool value)
{
- if (_type != Boolean)
- throw String("variable ") + name() + " is not of " + VARIABLE_BOOL + " type";
- _validator.validate(value);
- _val_bool = value;
+ if (_type != Boolean) {
+ throw String("variable ") + name() + " is not of "
+ + VARIABLE_BOOL + " type";
+ }
+ _validator.validate(value);
+ _val_bool = value;
}
-String
+String
Variable::get_string() const
{
- if (_type != StringVar && _type != StrSel)
- throw String("variable ") + name() + " is not of " + VARIABLE_STR + " type";
- return _val_str;
+ if (_type != StringVar && _type != StrSel) {
+ throw String("variable ") + name() + " is not of "
+ + VARIABLE_STR + " type";
+ }
+ return _val_str;
}
+
void
Variable::set_value(const String& value)
{
- if (_type != StringVar && _type != StrSel)
- throw String("variable ") + name() + " is not of " + VARIABLE_STR + " type";
- _validator.validate(value);
- _val_str = value;
+ if (_type != StringVar && _type != StrSel) {
+ throw String("variable ") + name() + " is not of "
+ + VARIABLE_STR + " type";
+ }
+ _validator.validate(value);
+ _val_str = value;
}
-XMLObject
+XMLObject
Variable::get_XML() const
-{
- if (_type != XMLVar)
- throw String("variable ") + name() + " is not of " + VARIABLE_XML + " type";
- return _val_xml;
+{
+ if (_type != XMLVar) {
+ throw String("variable ") + name() + " is not of "
+ + VARIABLE_XML + " type";
+ }
+ return _val_xml;
}
+
void
Variable::set_value(const XMLObject& value)
{
- if (_type != XMLVar)
- throw String("variable ") + name() + " is not of " + VARIABLE_XML + " type";
- _validator.validate(value);
- _val_xml = value;
+ if (_type != XMLVar) {
+ throw String("variable ") + name() + " is not of "
+ + VARIABLE_XML + " type";
+ }
+ _validator.validate(value);
+ _val_xml = value;
}
-std::list<long long>
+std::list<long long>
Variable::get_list_int() const
{
- if (_type != ListInt)
- throw String("variable ") + name() + " is not of " + VARIABLE_LIST_INT + " type";
- return _val_list_int;
+ if (_type != ListInt) {
+ throw String("variable ") + name() + " is not of "
+ + VARIABLE_LIST_INT + " type";
+ }
+ return _val_list_int;
}
+
void
Variable::set_value(const std::list<long long>& value)
{
- if (_type != ListInt)
- throw String("variable ") + name() + " is not of " + VARIABLE_LIST_INT + " type";
- _validator.validate(value);
- _val_list_int = value;
+ if (_type != ListInt) {
+ throw String("variable ") + name() + " is not of "
+ + VARIABLE_LIST_INT + " type";
+ }
+ _validator.validate(value);
+ _val_list_int = value;
}
-std::list<String>
+std::list<String>
Variable::get_list_str() const
{
- if (_type != ListStr)
- throw String("variable ") + name() + " is not of " + VARIABLE_LIST_STR + " type";
- return _val_list_str;
+ if (_type != ListStr) {
+ throw String("variable ") + name() + " is not of "
+ + VARIABLE_LIST_STR + " type";
+ }
+ return _val_list_str;
}
+
void
Variable::set_value(const std::list<String>& value)
{
- if (_type != ListStr)
- throw String("variable ") + name() + " is not of " + VARIABLE_LIST_STR + " type";
- _validator.validate(value);
- _val_list_str = value;
+ if (_type != ListStr) {
+ throw String("variable ") + name() + " is not of "
+ + VARIABLE_LIST_STR + " type";
+ }
+ _validator.validate(value);
+ _val_list_str = value;
}
std::list<XMLObject>
Variable::get_list_XML() const
{
- if (_type != ListXML)
- throw String("variable ") + name() + " is not of " + VARIABLE_LIST_XML + " type";
- return _val_list_XML;
+ if (_type != ListXML) {
+ throw String("variable ") + name() + " is not of "
+ + VARIABLE_LIST_XML + " type";
+ }
+ return _val_list_XML;
}
+
void
Variable::set_value(const std::list<XMLObject>& value)
{
- if (_type != ListXML)
- throw String("variable ") + name() + " is not of " + VARIABLE_LIST_XML + " type";
- _validator.validate(value);
- _val_list_XML = value;
+ if (_type != ListXML) {
+ throw String("variable ") + name() + " is not of "
+ + VARIABLE_LIST_XML + " type";
+ }
+ _validator.validate(value);
+ _val_list_XML = value;
}
-bool
+bool
Variable::equal(const Variable& var) const
{
- if (type() != var.type() ||
- name() != var.name() ||
- get_conditional_bool_if() != var.get_conditional_bool_if() ||
- get_conditional_bool_ifnot() != var.get_conditional_bool_ifnot())
- return false;
- switch (var.type()) {
- case Integer:
- case IntSel:
- return get_int() == var.get_int();
- case Boolean:
- return get_bool() == var.get_bool();
- case StringVar:
- case StrSel:
- return get_string() == var.get_string();
- case XMLVar:
- return get_XML() == var.get_XML();
- case ListInt:
- return get_list_int() == var.get_list_int();
- case ListStr:
- return get_list_str() == var.get_list_str();
- default:
- return false;
- }
- return false;
+ if (type() != var.type() ||
+ name() != var.name() ||
+ get_conditional_bool_if() != var.get_conditional_bool_if() ||
+ get_conditional_bool_ifnot() != var.get_conditional_bool_ifnot())
+ {
+ return false;
+ }
+
+ switch (var.type()) {
+ case Integer:
+ case IntSel:
+ return get_int() == var.get_int();
+
+ case Boolean:
+ return get_bool() == var.get_bool();
+
+ case StringVar:
+ case StrSel:
+ return get_string() == var.get_string();
+
+ case XMLVar:
+ return get_XML() == var.get_XML();
+
+ case ListInt:
+ return get_list_int() == var.get_list_int();
+
+ case ListStr:
+ return get_list_str() == var.get_list_str();
+
+ default:
+ return false;
+ }
+ return false;
}
-bool
+bool
Variable::validate() const
{
- return validate(*this);
+ return validate(*this);
}
-bool
+bool
Variable::validate(const Variable& var) const
{
- if (name() != var.name())
- throw String("different variable names");
- if (type() != var.type())
- throw String("invalid variable type");
- if (get_conditional_bool_if() != var.get_conditional_bool_if() ||
- get_conditional_bool_ifnot() != var.get_conditional_bool_ifnot())
- throw String("invalid bool conditional");
-
- switch (var.type()) {
- case Integer:
- case IntSel:
- return _validator.validate(var.get_int());
- case Boolean:
- return _validator.validate(var.get_bool());
- case StringVar:
- case StrSel:
- return _validator.validate(var.get_string());
- case XMLVar:
- return _validator.validate(var.get_XML());
- case ListInt:
- return _validator.validate(var.get_list_int());
- case ListStr:
- return _validator.validate(var.get_list_str());
- default:
- return false;
- }
+ if (name() != var.name())
+ throw String("different variable names");
+ if (type() != var.type())
+ throw String("invalid variable type");
+
+ if (get_conditional_bool_if() != var.get_conditional_bool_if() ||
+ get_conditional_bool_ifnot() != var.get_conditional_bool_ifnot())
+ {
+ throw String("invalid bool conditional");
+ }
+
+ switch (var.type()) {
+ case Integer:
+ case IntSel:
+ return _validator.validate(var.get_int());
+
+ case Boolean:
+ return _validator.validate(var.get_bool());
+
+ case StringVar:
+ case StrSel:
+ return _validator.validate(var.get_string());
+
+ case XMLVar:
+ return _validator.validate(var.get_XML());
+
+ case ListInt:
+ return _validator.validate(var.get_list_int());
+
+ case ListStr:
+ return _validator.validate(var.get_list_str());
+
+ default:
+ return false;
+ }
}
-
XMLObject
Variable::xml() const
{
- XMLObject xml(VARIABLE_TAG);
-
- xml.set_attr("name", name());
- xml.set_attr("mutable", (_mutable)?"true":"false");
-
- int i = 0;
- switch (_type) {
- case Integer:
- xml.set_attr("type", VARIABLE_INT);
- xml.set_attr("value", utils::to_string(_val_int));
- break;
- case IntSel:
- xml.set_attr("type", VARIABLE_INT_SEL);
- xml.set_attr("value", utils::to_string(_val_int));
- break;
- case Boolean:
- xml.set_attr("type", VARIABLE_BOOL);
- xml.set_attr("value", utils::to_string(_val_bool));
- break;
- case StringVar:
- xml.set_attr("type", VARIABLE_STR);
- xml.set_attr("value", _val_str);
- break;
- case StrSel:
- xml.set_attr("type", VARIABLE_STR_SEL);
- xml.set_attr("value", _val_str);
- break;
- case XMLVar:
- xml.set_attr("type", VARIABLE_XML);
- xml.add_child(_val_xml);
- break;
- case ListInt:
- xml.set_attr("type", VARIABLE_LIST_INT);
- i = 0;
- for (list<long long>::const_iterator iter = _val_list_int.begin();
- iter != _val_list_int.end();
- iter++, i++) {
- XMLObject xml_t = XMLObject(VARIABLE_LISTENTRY);
- // xml_t.set_attr("index", utils::to_string(i));
- xml_t.set_attr("value", utils::to_string(*iter));
- xml.add_child(xml_t);
- }
- break;
- case ListStr:
- xml.set_attr("type", VARIABLE_LIST_STR);
- i = 0;
- for (list<String>::const_iterator iter = _val_list_str.begin();
- iter != _val_list_str.end();
- iter++, i++) {
- XMLObject xml_t = XMLObject(VARIABLE_LISTENTRY);
- // xml_t.set_attr("index", utils::to_string(i));
- xml_t.set_attr("value", *iter);
- xml.add_child(xml_t);
- }
- break;
- case ListXML:
- xml.set_attr("type", VARIABLE_LIST_XML);
- i = 0;
- for (list<XMLObject>::const_iterator iter = _val_list_XML.begin();
- iter != _val_list_XML.end();
- iter++, i++) {
- xml.add_child(*iter);
- }
- break;
-
- default:
- throw String("invalid variable type");
- break;
- }
-
- if (_mutable)
- _validator.export_params(xml);
-
- if (!_cond_bool_if.empty())
- xml.set_attr("if_bool", _cond_bool_if);
- if (!_cond_bool_ifnot.empty())
- xml.set_attr("ifnot_bool", _cond_bool_ifnot);
-
- return xml;
-}
-
+ XMLObject xml(VARIABLE_TAG);
+ xml.set_attr("name", name());
+ xml.set_attr("mutable", (_mutable) ? "true" : "false");
+ int i = 0;
+ switch (_type) {
+ case Integer:
+ xml.set_attr("type", VARIABLE_INT);
+ xml.set_attr("value", utils::to_string(_val_int));
+ break;
+
+ case IntSel:
+ xml.set_attr("type", VARIABLE_INT_SEL);
+ xml.set_attr("value", utils::to_string(_val_int));
+ break;
+
+ case Boolean:
+ xml.set_attr("type", VARIABLE_BOOL);
+ xml.set_attr("value", utils::to_string(_val_bool));
+ break;
+
+ case StringVar:
+ xml.set_attr("type", VARIABLE_STR);
+ xml.set_attr("value", _val_str);
+ break;
+
+ case StrSel:
+ xml.set_attr("type", VARIABLE_STR_SEL);
+ xml.set_attr("value", _val_str);
+ break;
+
+ case XMLVar:
+ xml.set_attr("type", VARIABLE_XML);
+ xml.add_child(_val_xml);
+ break;
+
+ case ListInt:
+ xml.set_attr("type", VARIABLE_LIST_INT);
+ i = 0;
+ for (list<long long>::const_iterator
+ iter = _val_list_int.begin() ;
+ iter != _val_list_int.end() ;
+ iter++, i++)
+ {
+ XMLObject xml_t = XMLObject(VARIABLE_LISTENTRY);
+ //xml_t.set_attr("index", utils::to_string(i));
+ xml_t.set_attr("value", utils::to_string(*iter));
+ xml.add_child(xml_t);
+ }
+ break;
+
+ case ListStr:
+ xml.set_attr("type", VARIABLE_LIST_STR);
+
+ i = 0;
+ for (list<String>::const_iterator
+ iter = _val_list_str.begin() ;
+ iter != _val_list_str.end() ;
+ iter++, i++)
+ {
+ XMLObject xml_t = XMLObject(VARIABLE_LISTENTRY);
+ //xml_t.set_attr("index", utils::to_string(i));
+ xml_t.set_attr("value", *iter);
+ xml.add_child(xml_t);
+ }
+ break;
+
+ case ListXML:
+ xml.set_attr("type", VARIABLE_LIST_XML);
+ i = 0;
+ for (list<XMLObject>::const_iterator
+ iter = _val_list_XML.begin() ;
+ iter != _val_list_XML.end() ;
+ iter++, i++)
+ {
+ xml.add_child(*iter);
+ }
+ break;
+
+ default:
+ throw String("invalid variable type");
+ break;
+ }
+
+ if (_mutable)
+ _validator.export_params(xml);
+
+ if (!_cond_bool_if.empty())
+ xml.set_attr("if_bool", _cond_bool_if);
+ if (!_cond_bool_ifnot.empty())
+ xml.set_attr("ifnot_bool", _cond_bool_ifnot);
+ return xml;
+}
-// ##### class Validator #####
+// ##### class Validator #####
// always valid
Validator::Validator() :
- _always_valid(true),
- _integer(false),
- _int_sel(false),
- _string(false),
- _string_sel(false)
+ _always_valid(true),
+ _integer(false),
+ _int_sel(false),
+ _string(false),
+ _string_sel(false)
{}
// integer
-Validator::Validator(long long min,
- long long max,
- long long step) :
- _always_valid(false),
- _integer(true),
- _int_sel(false),
- _string(false),
- _string_sel(false)
-{
- _min = min;
- _max = max;
- _step = step;
+Validator::Validator(long long min, long long max, long long step) :
+ _always_valid(false),
+ _integer(true),
+ _int_sel(false),
+ _string(false),
+ _string_sel(false)
+{
+ _min = min;
+ _max = max;
+ _step = step;
}
// integer selector
Validator::Validator(const std::list<long long>& valid_values) :
- _always_valid(false),
- _integer(false),
- _int_sel(true),
- _string(false),
- _string_sel(false)
+ _always_valid(false),
+ _integer(false),
+ _int_sel(true),
+ _string(false),
+ _string_sel(false)
{
- _valid_ints = valid_values;
+ _valid_ints = valid_values;
}
// string
-Validator::Validator(long long min_length,
- long long max_length,
- const String& illegal_chars,
- const std::list<String>& reserved_words) :
- _always_valid(false),
- _integer(false),
- _int_sel(false),
- _string(true),
- _string_sel(false)
-{
- _min_length = min_length;
- _max_length = max_length;
- _illegal_chars = illegal_chars;
- _reserved_words = reserved_words;
+Validator::Validator( long long min_length,
+ long long max_length,
+ const String& illegal_chars,
+ const std::list<String>& reserved_words) :
+ _always_valid(false),
+ _integer(false),
+ _int_sel(false),
+ _string(true),
+ _string_sel(false)
+{
+ _min_length = min_length;
+ _max_length = max_length;
+ _illegal_chars = illegal_chars;
+ _reserved_words = reserved_words;
}
// string selector
Validator::Validator(const std::list<String>& valid_words) :
- _always_valid(false),
- _integer(false),
- _int_sel(false),
- _string(false),
- _string_sel(true)
+ _always_valid(false),
+ _integer(false),
+ _int_sel(false),
+ _string(false),
+ _string_sel(true)
{
- _valid_words = valid_words;
+ _valid_words = valid_words;
}
Validator::~Validator()
{}
-
-bool
+bool
Validator::validate(long long value) const
{
- if (_always_valid)
- return true;
- else if (_integer) {
- if (value >= _min &&
- value <= _max &&
- value % _step == 0)
- return true;
- else
- return false;
- } else if (_int_sel) {
- if (find(_valid_ints.begin(), _valid_ints.end(), value) == _valid_ints.end())
- return false;
- else
- return true;
- } else
- throw String("not long long");
+ if (_always_valid)
+ return true;
+ else if (_integer) {
+ if (value >= _min && value <= _max && value % _step == 0)
+ return true;
+ else
+ return false;
+ } else if (_int_sel) {
+ if (find(_valid_ints.begin(), _valid_ints.end(), value) == _valid_ints.end())
+ return false;
+ else
+ return true;
+ } else
+ throw String("not long long");
}
-bool
+bool
Validator::validate(const String& value) const
{
- if (_always_valid)
- return true;
- else if (_string) {
- if ((long long) value.size() >= _min_length &&
- (long long) value.size() <= _max_length &&
- value.find_first_of(_illegal_chars) == value.npos &&
- find(_reserved_words.begin(),
- _reserved_words.end(),
- value) == _reserved_words.end())
- return true;
- else
- return false;
- } else if (_string_sel) {
- if (find(_valid_words.begin(), _valid_words.end(), value) == _valid_words.end())
- return false;
- else
- return true;
- } else
- throw String("not string");
+ if (_always_valid)
+ return true;
+ else if (_string) {
+ if ((long long) value.size() >= _min_length &&
+ (long long) value.size() <= _max_length &&
+ value.find_first_of(_illegal_chars) == value.npos &&
+ find(_reserved_words.begin(), _reserved_words.end(), value) ==
+ _reserved_words.end())
+ {
+ return true;
+ } else
+ return false;
+ } else if (_string_sel) {
+ if (find(_valid_words.begin(), _valid_words.end(), value) == _valid_words.end())
+ return false;
+ else
+ return true;
+ } else
+ throw String("not string");
}
-bool
+bool
Validator::validate(bool value) const
{
- if (_always_valid)
- return true;
- else
- return false;
+ if (_always_valid)
+ return true;
+
+ return false;
}
-bool
+bool
Validator::validate(const XMLObject& value) const
{
- if (_always_valid)
- return true;
- else
- return false;
+ if (_always_valid)
+ return true;
+
+ return false;
}
-bool
+bool
Validator::validate(const std::list<long long>& value) const
{
- if (_always_valid)
- return true;
- else
- return false;
+ if (_always_valid)
+ return true;
+
+ return false;
}
-bool
+bool
Validator::validate(const std::list<String>& value) const
{
- if (_always_valid)
- return true;
- else
- return false;
+ if (_always_valid)
+ return true;
+
+ return false;
}
-bool
+bool
Validator::validate(const std::list<XMLObject>& value) const
{
- if (_always_valid)
- return true;
- else
- return false;
-}
-
+ if (_always_valid)
+ return true;
+ return false;
+}
-void
+void
Validator::export_params(XMLObject& xml) const
{
- if (_integer) {
- xml.set_attr("min", utils::to_string(_min));
- xml.set_attr("max", utils::to_string(_max));
- xml.set_attr("step", utils::to_string(_step));
- } else if (_int_sel) {
- for (list<long long>::const_iterator iter = _valid_ints.begin();
- iter != _valid_ints.end();
- iter++) {
- XMLObject entry("listentry");
- entry.set_attr("value", utils::to_string(*iter));
- xml.add_child(entry);
- }
- } else if (_string) {
- xml.set_attr("min_length", utils::to_string(_min_length));
- xml.set_attr("max_length", utils::to_string(_max_length));
- xml.set_attr("illegal_chars", _illegal_chars);
- String reserved;
- for (list<String>::const_iterator iter = _reserved_words.begin();
- iter != _reserved_words.end();
- iter++) {
- if (!reserved.empty())
- reserved += ";";
- reserved += *iter;
- }
- xml.set_attr("reserved_words", reserved);
- } else if (_string_sel) {
- for (list<String>::const_iterator iter = _valid_words.begin();
- iter != _valid_words.end();
- iter++) {
- XMLObject entry("listentry");
- entry.set_attr("value", *iter);
- xml.add_child(entry);
- }
- }
+ if (_integer) {
+ xml.set_attr("min", utils::to_string(_min));
+ xml.set_attr("max", utils::to_string(_max));
+ xml.set_attr("step", utils::to_string(_step));
+ } else if (_int_sel) {
+ for (list<long long>::const_iterator
+ iter = _valid_ints.begin() ;
+ iter != _valid_ints.end() ;
+ iter++)
+ {
+ XMLObject entry("listentry");
+ entry.set_attr("value", utils::to_string(*iter));
+ xml.add_child(entry);
+ }
+ } else if (_string) {
+ xml.set_attr("min_length", utils::to_string(_min_length));
+ xml.set_attr("max_length", utils::to_string(_max_length));
+ xml.set_attr("illegal_chars", _illegal_chars);
+ String reserved;
+ for (list<String>::const_iterator
+ iter = _reserved_words.begin() ;
+ iter != _reserved_words.end() ;
+ iter++)
+ {
+ if (!reserved.empty())
+ reserved += ";";
+ reserved += *iter;
+ }
+ xml.set_attr("reserved_words", reserved);
+ } else if (_string_sel) {
+ for (list<String>::const_iterator
+ iter = _valid_words.begin() ;
+ iter != _valid_words.end() ;
+ iter++)
+ {
+ XMLObject entry("listentry");
+ entry.set_attr("value", *iter);
+ xml.add_child(entry);
+ }
+ }
}
--- conga/ricci/common/XML.cpp 2007/07/27 16:43:47 1.10
+++ conga/ricci/common/XML.cpp 2007/08/31 04:57:37 1.11
@@ -1,5 +1,5 @@
/*
- Copyright Red Hat, Inc. 2005
+ Copyright Red Hat, Inc. 2005-2007
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
@@ -13,7 +13,7 @@
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
+ Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
MA 02139, USA.
*/
/*
@@ -36,242 +36,238 @@
-static String
-escape_chars(const String&);
-static String
-invert_chars(const String&);
-
-
-
+static String escape_chars(const String&);
+static String invert_chars(const String&);
XMLObject::XMLObject(const String& elem_name) :
- _tag(elem_name)
+ _tag(elem_name)
{}
XMLObject::~XMLObject()
{}
-
-bool
+bool
XMLObject::operator== (const XMLObject& obj) const
{
- if (children() != obj.children())
- return false;
- if (tag() != obj.tag())
- return false;
- if (attrs() != obj.attrs())
- return false;
- return true;
+ if (children() != obj.children())
+ return false;
+ if (tag() != obj.tag())
+ return false;
+ if (attrs() != obj.attrs())
+ return false;
+ return true;
}
-bool
+bool
XMLObject::has_attr(const String& attr_name) const
{
- return _attrs.find(attr_name) != _attrs.end();
+ return _attrs.find(attr_name) != _attrs.end();
}
-String
+String
XMLObject::set_attr(const String& attr_name, const String& value)
{
- String ret = _attrs[attr_name];
- _attrs[attr_name] = value;
- return ret;
+ String ret = _attrs[attr_name];
+ _attrs[attr_name] = value;
+ return ret;
}
-String
+String
XMLObject::get_attr(const String& attr_name) const
{
- map<String, String>::const_iterator iter = _attrs.find(attr_name);
- if (iter == _attrs.end())
- return "";
- else
- return iter->second;
+ map<String, String>::const_iterator iter = _attrs.find(attr_name);
+ if (iter == _attrs.end())
+ return "";
+ else
+ return iter->second;
}
XMLObject&
XMLObject::add_child(const XMLObject& child)
{
- _kids.push_back(child);
- return _kids.back();
+ _kids.push_back(child);
+ return _kids.back();
}
-bool
+bool
XMLObject::remove_child(const XMLObject& child)
{
- list<XMLObject>::iterator iter = find(_kids.begin(), _kids.end(), child);
- if (iter == _kids.end())
- return false;
- else {
- _kids.erase(iter);
- return true;
- }
+ list<XMLObject>::iterator iter = find(_kids.begin(), _kids.end(), child);
+ if (iter == _kids.end())
+ return false;
+ else {
+ _kids.erase(iter);
+ return true;
+ }
}
void
XMLObject::generate_xml(String& xml, const String& indent) const
{
- xml += indent + "<" + _tag;
- for (map<String, String>::const_iterator iter = attrs().begin();
- iter != attrs().end();
- iter++) {
- const String& name = iter->first;
- const String value = escape_chars(iter->second);
- xml += " " + name + "=\"" + value + "\"";
- }
- if (children().empty())
- xml += "/>\n";
- else {
- xml += ">\n";
- for (list<XMLObject>::const_iterator iter = children().begin();
- iter != children().end();
- iter++) {
- iter->generate_xml(xml, indent + "\t");
- }
- xml += indent + "</" + _tag + ">\n";
- }
-}
-
-
+ xml += indent + "<" + _tag;
+ for (map<String, String>::const_iterator
+ iter = attrs().begin() ;
+ iter != attrs().end() ;
+ iter++)
+ {
+ const String& name = iter->first;
+ const String value = escape_chars(iter->second);
+ xml += " " + name + "=\"" + value + "\"";
+ }
+ if (children().empty())
+ xml += "/>\n";
+ else {
+ xml += ">\n";
+ for (list<XMLObject>::const_iterator
+ iter = children().begin() ;
+ iter != children().end() ;
+ iter++)
+ {
+ iter->generate_xml(xml, indent + "\t");
+ }
-// *** GLOBAL FUNCTIONS ***
+ xml += indent + "</" + _tag + ">\n";
+ }
+}
+// *** GLOBAL FUNCTIONS ***
static void
_parseXML(XMLObject& parent, xmlNode* children)
{
- for (xmlNode* curr_node = children;
- curr_node;
- curr_node = curr_node->next) {
- if (curr_node->type == XML_ELEMENT_NODE) {
-
- XMLObject me((const char*) curr_node->name);
-
- // attrs
- for (xmlAttr* curr_attr = curr_node->properties;
- curr_attr;
- curr_attr = curr_attr->next) {
- if (curr_attr->type == XML_ATTRIBUTE_NODE) {
- const xmlChar* name = curr_attr->name;
- const xmlChar* value = xmlGetProp(curr_node, name);
- if (!value)
- throw String("xmlGetProp() returned NULL!!!");
- try {
- const String name_str((const char*) name);
- const String value_str = invert_chars((const char*) value);
- me.set_attr(name_str, value_str);
- xmlFree((void*) value);
- } catch ( ... ) {
- xmlFree((void*) value);
- throw;
- }
+ for (xmlNode* curr_node = children; curr_node; curr_node = curr_node->next)
+ {
+ if (curr_node->type == XML_ELEMENT_NODE) {
+ XMLObject me((const char*) curr_node->name);
+
+ // attrs
+ for (xmlAttr* curr_attr = curr_node->properties ;
+ curr_attr ;
+ curr_attr = curr_attr->next)
+ {
+ if (curr_attr->type == XML_ATTRIBUTE_NODE) {
+ const xmlChar* name = curr_attr->name;
+ const xmlChar* value = xmlGetProp(curr_node, name);
+
+ if (!value)
+ throw String("xmlGetProp() returned NULL!!!");
+ try {
+ const String name_str((const char *) name);
+ const String value_str =
+ invert_chars((const char *) value);
+
+ me.set_attr(name_str, value_str);
+ xmlFree((void *) value);
+ } catch ( ... ) {
+ xmlFree((void *) value);
+ throw;
+ }
+ }
+ }
+
+ // kids
+ _parseXML(me, curr_node->children);
+ parent.add_child(me);
+ }
}
- }
-
- // kids
- _parseXML(me, curr_node->children);
-
- parent.add_child(me);
- }
- }
}
-XMLObject
+XMLObject
parseXML(const String& xml)
{
- static bool initialized = false;
- if (!initialized) {
- LIBXML_TEST_VERSION;
- initialized = true;
- }
-
- xmlDoc* doc = xmlReadMemory(xml.c_str(),
- xml.size(),
- "noname.xml",
- NULL,
- XML_PARSE_NONET | XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
- if (!doc)
- throw String("parseXML(): couldn't parse xml");
-
- XMLObject root("if you see this, something wrong happened");
- try {
- _parseXML(root, xmlDocGetRootElement(doc));
- xmlFreeDoc(doc);
- return *(root.children().begin());
- } catch ( ... ) {
- xmlFreeDoc(doc);
- xmlCleanupParser();
- throw String("parseXML(): low memory");
- }
+ static bool initialized = false;
+
+ if (!initialized) {
+ LIBXML_TEST_VERSION;
+ initialized = true;
+ }
+
+ xmlDoc* doc = xmlReadMemory(xml.c_str(),
+ xml.size(),
+ "noname.xml",
+ NULL,
+ XML_PARSE_NONET | XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
+ if (!doc)
+ throw String("parseXML(): couldn't parse xml");
+
+ XMLObject root("if you see this, something wrong happened");
+
+ try {
+ _parseXML(root, xmlDocGetRootElement(doc));
+ xmlFreeDoc(doc);
+ return *(root.children().begin());
+ } catch ( ... ) {
+ xmlFreeDoc(doc);
+ xmlCleanupParser();
+ throw String("parseXML(): low memory");
+ }
}
-String
+String
generateXML(const XMLObject& obj)
{
- String xml("<?xml version=\"1.0\"?>\n");
- obj.generate_xml(xml, "");
-
- // verify xml
- xmlDoc* doc = xmlReadMemory(xml.c_str(),
- xml.size(),
- "noname.xml",
- NULL,
- XML_PARSE_NONET | XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
- if (!doc) {
- // cout << xml << endl;
- throw String("generateXML(): internal error");
- }
- xmlFreeDoc(doc);
-
- return xml;
+ String xml("<?xml version=\"1.0\"?>\n");
+ obj.generate_xml(xml, "");
+
+ // verify xml
+ xmlDoc* doc = xmlReadMemory(xml.c_str(),
+ xml.size(),
+ "noname.xml",
+ NULL,
+ XML_PARSE_NONET | XML_PARSE_NOERROR | XML_PARSE_NOWARNING);
+ if (!doc) {
+ //cout << xml << endl;
+ throw String("generateXML(): internal error");
+ }
+ xmlFreeDoc(doc);
+
+ return xml;
}
-XMLObject
+XMLObject
readXML(const String& filename)
{
- return parseXML(File::open(filename));
+ return parseXML(File::open(filename));
}
-
-
-
String
escape_chars(const String& str)
{
- const String amp_repl ("______AMP_REPLACEMENT_XML_KOJIKOJIKOJIKO______");
- const String lt_repl ("______LT_REPLACEMENT_XML_KOJIKOJIKOJIKO______");
- const String gt_repl ("______GT_REPLACEMENT_XML_KOJIKOJIKOJIKO______");
- const String apos_repl("______APOS_REPLACEMENT_XML_KOJIKOJIKOJIKO______");
- const String quot_repl("______QUOT_REPLACEMENT_XML_KOJIKOJIKOJIKO______");
-
- String ret = utils::replace("&", amp_repl, str);
- ret = utils::replace("<", lt_repl, ret);
- ret = utils::replace(">", gt_repl, ret);
- ret = utils::replace("'", apos_repl, ret);
- ret = utils::replace(""", quot_repl, ret);
-
- ret = utils::replace("&", "&", ret);
- ret = utils::replace("<", "<", ret);
- ret = utils::replace(">", ">", ret);
- ret = utils::replace("'", "'", ret);
- ret = utils::replace("\"", """, ret);
-
- ret = utils::replace(amp_repl, "&", ret);
- ret = utils::replace(lt_repl, "<", ret);
- ret = utils::replace(gt_repl, ">", ret);
- ret = utils::replace(apos_repl, "'", ret);
- ret = utils::replace(quot_repl, """, ret);
-
- return ret;
+ const String amp_repl("______AMP_REPLACEMENT_XML_KOJIKOJIKOJIKO______");
+ const String lt_repl("______LT_REPLACEMENT_XML_KOJIKOJIKOJIKO______");
+ const String gt_repl("______GT_REPLACEMENT_XML_KOJIKOJIKOJIKO______");
+ const String apos_repl("______APOS_REPLACEMENT_XML_KOJIKOJIKOJIKO______");
+ const String quot_repl("______QUOT_REPLACEMENT_XML_KOJIKOJIKOJIKO______");
+
+ String ret = utils::replace("&", amp_repl, str);
+ ret = utils::replace("<", lt_repl, ret);
+ ret = utils::replace(">", gt_repl, ret);
+ ret = utils::replace("'", apos_repl, ret);
+ ret = utils::replace(""", quot_repl, ret);
+
+ ret = utils::replace("&", "&", ret);
+ ret = utils::replace("<", "<", ret);
+ ret = utils::replace(">", ">", ret);
+ ret = utils::replace("'", "'", ret);
+ ret = utils::replace("\"", """, ret);
+
+ ret = utils::replace(amp_repl, "&", ret);
+ ret = utils::replace(lt_repl, "<", ret);
+ ret = utils::replace(gt_repl, ">", ret);
+ ret = utils::replace(apos_repl, "'", ret);
+ ret = utils::replace(quot_repl, """, ret);
+
+ return ret;
}
String
invert_chars(const String& str)
{
- String ret = utils::replace("&", "&", str);
- ret = utils::replace("<", "<", ret);
- ret = utils::replace(">", ">", ret);
- ret = utils::replace("'", "'", ret);
- ret = utils::replace(""", "\"", ret);
- return ret;
+ String ret = utils::replace("&", "&", str);
+ ret = utils::replace("<", "<", ret);
+ ret = utils::replace(">", ">", ret);
+ ret = utils::replace("'", "'", ret);
+ ret = utils::replace(""", "\"", ret);
+ return ret;
}
--- conga/ricci/common/daemon_init.c 2006/03/27 23:15:30 1.1
+++ conga/ricci/common/daemon_init.c 2007/08/31 04:57:37 1.2
@@ -20,16 +20,16 @@
/** @file
* daemon_init function, does sanity checks and calls daemon().
*
- * $Id: daemon_init.c,v 1.1 2006/03/27 23:15:30 kupcevic Exp $
+ * $Id: daemon_init.c,v 1.2 2007/08/31 04:57:37 rmccabe Exp $
*
* Author: Jeff Moyer <moyer at mclinux.com>
*/
/*
* TODO: Clean this up so that only one function constructs the
- * pidfile /var/run/loggerd.PID, and perhaps only one function
- * forms the /proc/PID/ path.
+ * pidfile /var/run/loggerd.PID, and perhaps only one function
+ * forms the /proc/PID/ path.
*
- * Also need to add file locking for the pid file.
+ * Also need to add file locking for the pid file.
*/
#include <stdio.h>
#include <stdlib.h>
--- conga/ricci/common/signals.c 2006/03/06 21:48:04 1.1
+++ conga/ricci/common/signals.c 2007/08/31 04:57:37 1.2
@@ -1,5 +1,5 @@
/*
- Copyright Red Hat, Inc. 2003
+ Copyright Red Hat, Inc. 2003-2007
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
@@ -13,7 +13,7 @@
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
+ Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
MA 02139, USA.
*/
@@ -21,7 +21,6 @@
#include <signal.h>
#include <stdlib.h>
#include <string.h>
-//#include <resgroup.h>
#include "signals.h"
@@ -52,12 +51,12 @@
int
block_signal(int sig)
{
- sigset_t set;
+ sigset_t set;
sigemptyset(&set);
sigaddset(&set, sig);
- return(sigprocmask(SIG_BLOCK, &set, NULL));
+ return (sigprocmask(SIG_BLOCK, &set, NULL));
}
@@ -70,21 +69,20 @@
int
unblock_signal(int sig)
{
- sigset_t set;
+ sigset_t set;
sigemptyset(&set);
sigaddset(&set, sig);
- return(sigprocmask(SIG_UNBLOCK, &set, NULL));
+ return (sigprocmask(SIG_UNBLOCK, &set, NULL));
}
-
int
block_all_signals(void)
{
- sigset_t set;
+ sigset_t set;
sigfillset(&set);
sigdelset(&set, SIGSEGV);
- return(sigprocmask(SIG_BLOCK, &set, NULL));
+ return (sigprocmask(SIG_BLOCK, &set, NULL));
}
--- conga/ricci/common/utils.cpp 2007/08/24 22:05:14 1.9
+++ conga/ricci/common/utils.cpp 2007/08/31 04:57:37 1.10
@@ -1,5 +1,5 @@
/*
- Copyright Red Hat, Inc. 2005
+ Copyright Red Hat, Inc. 2005-2007
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
@@ -13,7 +13,7 @@
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
+ Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
MA 02139, USA.
*/
/*
@@ -33,268 +33,269 @@
using namespace std;
+String
+utils::replace(const String& what, const String& with, const String& in_str)
+{
+ vector<String> v(split(in_str, what));
+ String ret(v[0]);
-
-
-String
-utils::replace(const String& what,
- const String& with,
- const String& in_str)
-{
- vector<String> v(split(in_str, what));
- String ret(v[0]);
- for (vector<String>::size_type i=1;
- i < v.size();
- i++)
- ret += with + v[i];
- return ret;
+ for (vector<String>::size_type i = 1 ; i < v.size() ; i++)
+ ret += with + v[i];
+ return ret;
}
-
-
-String
+String
utils::hash_str(const String& txt)
{
- unsigned char buff[16];
- MD5((const unsigned char*) txt.c_str(), txt.size(), buff);
-
- String hash;
- for (unsigned int i=0; i<sizeof(buff); i++) {
- hash += (char) ('a' + (int) ((buff[i] & 0xf0)>>4));
- hash += (char) ('a' + (int) ((buff[i] & 0x0f)>>4));
- }
- return hash;
-}
-
-
+ unsigned char buff[16];
+ MD5((const unsigned char*) txt.c_str(), txt.size(), buff);
+ String hash;
+ for (size_t i = 0; i < sizeof(buff) ; i++) {
+ hash += (char) ('a' + (int) ((buff[i] & 0xf0) >> 4));
+ hash += (char) ('a' + (int) ((buff[i] & 0x0f) >> 4));
+ }
+ return hash;
+}
-String
+String
utils::lstrip(String str, const String& del)
{
- if (del.empty())
- throw String("empty separator");
-
- while (str.find(del) == 0) {
- str = str.substr(del.size());
- }
-
- return str;
+ if (del.empty())
+ throw String("empty separator");
+
+ while (str.find(del) == 0) {
+ str = str.substr(del.size());
+ }
+
+ return str;
}
-String
+
+String
utils::rstrip(String str, const String& del)
{
- if (del.empty())
- throw String("empty separator");
- if (str.size() < del.size())
- return str;
-
- unsigned int i;
- while (str.rfind(del) == (i = (str.size() - del.size()))) {
- if (str.rfind(del) == str.npos)
- break;
- str = str.substr(0, i);
- }
-
- return str;
+ if (del.empty())
+ throw String("empty separator");
+
+ if (str.size() < del.size())
+ return str;
+
+ unsigned int i;
+ while (str.rfind(del) == (i = (str.size() - del.size()))) {
+ if (str.rfind(del) == str.npos)
+ break;
+ str = str.substr(0, i);
+ }
+
+ return str;
}
-String
+String
utils::lstrip(String str)
{
- while (str.find_first_of(" \n\t") == 0)
- str = str.substr(1);
- return str;
+ while (str.find_first_of(" \n\t") == 0)
+ str = str.substr(1);
+
+ return str;
}
-String
+
+String
utils::rstrip(String str)
{
- unsigned int i;
- while ((i=str.size()) != 0) {
- i--;
- if (str[i] == ' ' || str[i] == '\n' || str[i] == '\t')
- str = str.substr(0, i);
- else
- break;
- }
- return str;
+ unsigned int i;
+
+ while ((i = str.size()) != 0) {
+ i--;
+ if (str[i] == ' ' || str[i] == '\n' || str[i] == '\t')
+ str = str.substr(0, i);
+ else
+ break;
+ }
+
+ return str;
}
-vector<String>
+vector<String>
utils::split(const String& t, const String& del)
{
- if (del.empty())
- throw String("empty separator");
-
- String txt(t);
-
- // merge separators
- if (del == " " || del == "\n") {
- String::size_type i;
- while ((i = txt.find(del+del)) != txt.npos)
- txt.erase(i, del.size());
- }
-
- // split
- vector<String> lines;
- for (String::size_type from=0, to=txt.find(del);
- from != txt.size();
- ) {
- String substr = txt.substr(from, to-from);
- lines.push_back(substr);
- if (to == txt.npos)
- return lines;
- from = to + del.size();
- to = txt.find(del, from);
- }
- lines.push_back(String());
- return lines;
+ if (del.empty())
+ throw String("empty separator");
+
+ String txt(t);
+
+ // merge separators
+ if (del == " " || del == "\n") {
+ String::size_type i;
+ while ((i = txt.find(del + del)) != txt.npos)
+ txt.erase(i, del.size());
+ }
+
+ // split
+ vector<String> lines;
+ for (String::size_type from=0, to = txt.find(del) ; from != txt.size() ;) {
+ String substr = txt.substr(from, to - from);
+ lines.push_back(substr);
+ if (to == txt.npos)
+ return lines;
+ from = to + del.size();
+ to = txt.find(del, from);
+ }
+
+ lines.push_back(String());
+ return lines;
}
-vector<String>
+vector<String>
utils::split(const String& t)
{
- String del(" ");
- String txt(t);
-
- // merge separators
- String::size_type i;
- while ((i = txt.find('\t')) != txt.npos)
- txt[i] = ' ';
- while ((i = txt.find(del+del)) != txt.npos)
- txt.erase(i, del.size());
-
- // split
- vector<String> lines;
- for (String::size_type from=0, to=txt.find(del);
- from != txt.size();
- ) {
- String substr = txt.substr(from, to-from);
- lines.push_back(substr);
- if (to == txt.npos)
- return lines;
- from = to + del.size();
- to = txt.find(del, from);
- }
-
- return lines;
+ String del(" ");
+ String txt(t);
+
+ // merge separators
+ String::size_type i;
+ while ((i = txt.find('\t')) != txt.npos)
+ txt[i] = ' ';
+
+ while ((i = txt.find(del + del)) != txt.npos)
+ txt.erase(i, del.size());
+
+ // split
+ vector<String> lines;
+ for (String::size_type from=0, to = txt.find(del) ; from != txt.size() ;) {
+ String substr = txt.substr(from, to - from);
+ lines.push_back(substr);
+ if (to == txt.npos)
+ return lines;
+ from = to + del.size();
+ to = txt.find(del, from);
+ }
+
+ return lines;
}
String
utils::to_lower(const String& str)
{
- String s;
- for (String::size_type i=0; i<str.size(); i++)
- s.push_back(tolower(str[i]));
- return s;
+ String s;
+
+ for (String::size_type i = 0 ; i < str.size() ; i++)
+ s.push_back(tolower(str[i]));
+ return s;
}
String
utils::to_upper(const String& str)
{
- String s;
- for (String::size_type i=0; i<str.size(); i++)
- s.push_back(toupper(str[i]));
- return s;
-}
-
-
+ String s;
+ for (String::size_type i = 0 ; i < str.size() ; i++)
+ s.push_back(toupper(str[i]));
+ return s;
+}
/*
int
utils::to_int(const String& str)
{
- return atoi(str.c_str());
+ return atoi(str.c_str());
}
*/
+
long long
utils::to_long(const String& str)
{
- return atoll(str.c_str());
+ return atoll(str.c_str());
}
+
float
utils::to_float(const String& str)
{
- float num=0;
-
- sscanf(strip(str).c_str(), "%f", &num);
-
- return num;
+ float num = 0;
+
+ sscanf(strip(str).c_str(), "%f", &num);
+ return num;
}
-String
+String
utils::to_string(int value)
{
- char tmp[100];
- sprintf(tmp, "%d", value);
- return tmp;
+ char tmp[64];
+
+ sprintf(tmp, "%d", value);
+ return tmp;
}
-String
+
+String
utils::to_string(long value)
{
- char tmp[100];
- sprintf(tmp, "%ld", value);
- return tmp;
+ char tmp[64];
+
+ sprintf(tmp, "%ld", value);
+ return tmp;
}
-String
+
+String
utils::to_string(long long value)
{
- char tmp[100];
- sprintf(tmp, "%lld", value);
- return tmp;
+ char tmp[64];
+
+ sprintf(tmp, "%lld", value);
+ return tmp;
}
-String
+
+String
utils::to_string(bool value)
{
- return (value)? "true" : "false";
+ return (value) ? "true" : "false";
}
+int
+utils::execute( const String& path,
+ const std::vector<String>& args,
+ String& out,
+ String& err,
+ int& status,
+ bool caching)
+{
+ String _command = path;
+ for (vector<String>::const_iterator
+ iter = args.begin() ;
+ iter != args.end() ;
+ iter++)
+ {
+ _command += " " + *iter;
+ }
+
+ map<String, exec_cache>::iterator iter = cache.find(_command);
+ if (iter != cache.end() && caching) {
+ exec_cache &c = iter->second;
+ //cout << "exec: " << _command << " cached" << endl;
+
+ out = c.out;
+ err = c.err;
+ status = c.status;
+
+ return c.exec_ret;
+ } else {
+ int ret = ::execute(path, args, out, err, status);
+ //cout << "exec: " << _command << " executed" << endl;
+ exec_cache c(_command, out, err, status, ret);
+
+ if (caching)
+ cache.insert(pair<String, exec_cache>(_command, c));
+
+ out = c.out;
+ err = c.err;
+ status = c.status;
-
-
-
-
-int
-utils::execute(const String& path,
- const std::vector<String>& args,
- String& out,
- String& err,
- int& status,
- bool caching)
-{
- String _command = path;
- for (vector<String>::const_iterator iter = args.begin();
- iter != args.end();
- iter++)
- _command += " " + *iter;
-
- map<String, exec_cache>::iterator iter = cache.find(_command);
- if (iter != cache.end() && caching) {
- exec_cache &c = iter->second;
- // cout << "exec: " << _command << " cached" << endl;
- out = c.out;
- err = c.err;
- status = c.status;
- return c.exec_ret;
- } else {
- int ret = ::execute(path, args, out, err, status);
- // cout << "exec: " << _command << " executed" << endl;
- exec_cache c(_command, out, err, status, ret);
- if (caching)
- cache.insert(pair<String, exec_cache>(_command, c));
- out = c.out;
- err = c.err;
- status = c.status;
- return c.exec_ret;
- }
+ return c.exec_ret;
+ }
}
void
utils::clear_cache()
{
- cache.clear();
+ cache.clear();
}
-std::map<String, exec_cache>
-utils::cache;
+std::map<String, exec_cache> utils::cache;
More information about the Cluster-devel
mailing list