'using namespace std;' added.

This commit is contained in:
Roland Jax
2014-11-22 20:42:11 +01:00
parent b593af967a
commit 4a1033122c
33 changed files with 709 additions and 646 deletions
+29 -27
View File
@@ -22,6 +22,8 @@
#include <iomanip>
#include <cstdlib>
using namespace std;
Appl& Appl::Instance(const bool command)
{
static Appl instance(command);
@@ -64,7 +66,7 @@ void Appl::addOption(const char* name, const char* shortname, OptVal optval,
void Appl::parseArgs(int argc, char* argv[])
{
std::vector<std::string> _argv(argv, argv + argc);
vector<string> _argv(argv, argv + argc);
m_argv = _argv;
// walk through all arguments
@@ -74,7 +76,7 @@ void Appl::parseArgs(int argc, char* argv[])
if (_argv[i].rfind("--") == 0 && _argv[i].size() > 2) {
// is next item an added argument?
if (i+1 < argc && _argv[i+1].rfind("-", 0) == std::string::npos) {
if (i+1 < argc && _argv[i+1].rfind("-", 0) == string::npos) {
if (checkOption(_argv[i].substr(2), _argv[i+1]) == false)
printHelp();
}
@@ -90,7 +92,7 @@ void Appl::parseArgs(int argc, char* argv[])
for (size_t j = 1; j < _argv[i].size(); j++) {
// only last charater could have an argument
if (i+1 < argc && _argv[i+1].rfind("-", 0) == std::string::npos
if (i+1 < argc && _argv[i+1].rfind("-", 0) == string::npos
&& j+1 == _argv[i].size()) {
if (checkOption(_argv[i].substr(j,1), _argv[i+1]) == false)
printHelp();
@@ -108,7 +110,7 @@ void Appl::parseArgs(int argc, char* argv[])
if (m_needCommand == true) {
for (int i = 1; i < argc; i++) {
if (_argv[i].rfind("-", 0) != std::string::npos) {
if (_argv[i].rfind("-", 0) != string::npos) {
i++;
continue;
}
@@ -116,14 +118,14 @@ void Appl::parseArgs(int argc, char* argv[])
}
if (m_arguments.size() == 0) {
std::cerr << std::endl << "command needed" << std::endl;
cerr << endl << "command needed" << endl;
printHelp();
}
}
}
bool Appl::checkOption(const std::string& option, const std::string& value)
bool Appl::checkOption(const string& option, const string& value)
{
if (strcmp(option.c_str(), "settings") == 0)
printSettings();
@@ -139,8 +141,8 @@ bool Appl::checkOption(const std::string& option, const std::string& value)
// need this option and argument?
if (o_it->optiontype == ot_mandatory && value.size() == 0) {
std::cerr << std::endl << "option requires an argument '"
<< option << "'" << std::endl;
cerr << endl << "option requires an argument '"
<< option << "'" << endl;
return false;
}
@@ -153,11 +155,11 @@ bool Appl::checkOption(const std::string& option, const std::string& value)
}
}
std::cerr << std::endl << "unknown option '" << option << "'" << std::endl;
cerr << endl << "unknown option '" << option << "'" << endl;
return false;
}
void Appl::setOptVal(const char* option, const std::string value, DataType datatype)
void Appl::setOptVal(const char* option, const string value, DataType datatype)
{
switch (datatype) {
case dt_bool:
@@ -182,69 +184,69 @@ void Appl::setOptVal(const char* option, const std::string value, DataType datat
void Appl::printVersion()
{
std::cerr << m_version << std::endl;
cerr << m_version << endl;
exit(EXIT_SUCCESS);
}
void Appl::printHelp()
{
std::cerr << std::endl << "Usage:" << std::endl << " "
cerr << endl << "Usage:" << endl << " "
<< m_argv[0].substr(m_argv[0].find_last_of("/\\") + 1) << " [OPTIONS...]" ;
if (m_needCommand == true)
std::cerr << " COMMAND {ARGS...}" << std::endl << std::endl;
cerr << " COMMAND {ARGS...}" << endl << endl;
else
std::cerr << std::endl << std::endl;
cerr << endl << endl;
for (o_it = m_opts.begin(); o_it < m_opts.end(); o_it++) {
if (strcmp(o_it->name, "__text_only__") == 0)
std::cerr << o_it->description << std::endl;
cerr << o_it->description << endl;
else {
const char* c = (strlen(o_it->shortname) == 1) ? o_it->shortname : " ";
std::cerr << ((strcmp(c, " ") == 0) ? " " : "-") << c
cerr << ((strcmp(c, " ") == 0) ? " " : "-") << c
<< " | --" << o_it->name
<< "\t" << o_it->description
<< std::endl;
<< endl;
}
}
std::cerr << " | --settings\n-v | --version\n-h | --help" << std::endl << std::endl;
cerr << " | --settings\n-v | --version\n-h | --help" << endl << endl;
exit(EXIT_SUCCESS);
}
void Appl::printSettings()
{
std::cerr << std::endl << "Settings:" << std::endl << std::endl;
cerr << endl << "Settings:" << endl << endl;
for (o_it = m_opts.begin(); o_it < m_opts.end(); o_it++) {
if (strcmp(o_it->name, "__text_only__") == 0)
continue;
const char* c = (strlen(o_it->shortname) == 1) ? o_it->shortname : " ";
std::cerr << ((strcmp(c, " ") == 0) ? " " : "-") << c
cerr << ((strcmp(c, " ") == 0) ? " " : "-") << c
<< " | --" << o_it->name
<< " = ";
if (o_it->datatype == dt_bool) {
if (getOptVal<bool>(o_it->name) == true)
std::cerr << "yes" << std::endl;
cerr << "yes" << endl;
else
std::cerr << "no" << std::endl;
cerr << "no" << endl;
}
else if (o_it->datatype == dt_int) {
std::cerr << getOptVal<int>(o_it->name) << std::endl;
cerr << getOptVal<int>(o_it->name) << endl;
}
else if (o_it->datatype == dt_long) {
std::cerr << getOptVal<long>(o_it->name) << std::endl;
cerr << getOptVal<long>(o_it->name) << endl;
}
else if (o_it->datatype == dt_float) {
std::cerr << getOptVal<float>(o_it->name) << std::endl;
cerr << getOptVal<float>(o_it->name) << endl;
}
else if (o_it->datatype == dt_string) {
std::cerr << getOptVal<const char*>(o_it->name) << std::endl;
cerr << getOptVal<const char*>(o_it->name) << endl;
}
}
std::cerr << std::endl;
cerr << endl;
exit(EXIT_SUCCESS);
}
+11 -9
View File
@@ -25,6 +25,8 @@
#include <map>
#include <vector>
using namespace std;
/** \file appl.h */
/** the available data types. */
@@ -178,7 +180,7 @@ public:
* @param num number of interested argument.
* @return string value.
*/
std::string getArg(const int num) const { return m_arguments[num]; }
string getArg(const int num) const { return m_arguments[num]; }
private:
/** private constructor - singleton pattern */
@@ -187,15 +189,15 @@ private:
Appl& operator=(const Appl&);
/** application options */
std::vector<opt_t> m_opts;
std::vector<opt_t>::const_iterator o_it;
vector<opt_t> m_opts;
vector<opt_t>::const_iterator o_it;
/** map option - value */
std::map<const char*, OptVal> m_optvals;
std::map<const char*, OptVal>::iterator ov_it;
map<const char*, OptVal> m_optvals;
map<const char*, OptVal>::iterator ov_it;
/** given arguments */
std::vector<std::string> m_argv;
vector<string> m_argv;
/** application version string */
const char* m_version;
@@ -204,14 +206,14 @@ private:
bool m_needCommand;
/** arguments (argument 0 = command) string */
std::vector<std::string> m_arguments;
vector<string> m_arguments;
/**
* @brief checks the passed parameter if this is a valid option.
* @param option to check.
* @param value to save if paramter is a valid option.
*/
bool checkOption(const std::string& option, const std::string& value);
bool checkOption(const string& option, const string& value);
/**
* @brief save the passed value to option.
@@ -219,7 +221,7 @@ private:
* @param value to save.
* @param datatype of given option.
*/
void setOptVal(const char* option, const std::string value, DataType datatype);
void setOptVal(const char* option, const string value, DataType datatype);
/**
* @brief print application version.
+6 -4
View File
@@ -26,6 +26,8 @@
#include <sys/stat.h>
#include <fcntl.h>
using namespace std;
Daemon& Daemon::Instance()
{
static Daemon instance;
@@ -44,7 +46,7 @@ void Daemon::run(const char* pidfile)
pid = fork();
if (pid < 0) {
std::cerr << "daemon fork() failed." << std::endl;
cerr << "daemon fork() failed." << endl;
exit(EXIT_FAILURE);
}
@@ -62,14 +64,14 @@ void Daemon::run(const char* pidfile)
// Create a new SID for the child process and
// detach the process from the parent (normally a shell)
if (setsid() < 0) {
std::cerr << "daemon setsid() failed." << std::endl;
cerr << "daemon setsid() failed." << endl;
exit(EXIT_FAILURE);
}
// Change the current working directory. This prevents the current
// directory from being locked; hence not being able to remove it.
if (chdir("/tmp") < 0) { //DAEMON_WORKDIR
std::cerr << "daemon chdir() failed." << std::endl;
cerr << "daemon chdir() failed." << endl;
exit(EXIT_FAILURE);
}
@@ -80,7 +82,7 @@ void Daemon::run(const char* pidfile)
// write pidfile and try to lock it
if (pidfile_open() == false) {
std::cerr << "can't open pidfile: %s" << m_pidfile << std::endl;
cerr << "can't open pidfile: %s" << m_pidfile << endl;
exit(EXIT_FAILURE);
}
+19 -17
View File
@@ -28,6 +28,8 @@
#include <sys/time.h>
#include <unistd.h>
using namespace std;
/** static char array with logging area names */
static const char* AreaNames[Size_of_Areas] = { "bas", "net", "bus", "cyc" };
@@ -37,19 +39,19 @@ static const char* LevelNames[Size_of_Level] = { "error", "event", "trace", "deb
/** inline function of log2 */
inline double Log2(double n) { return log(n) / log(2); }
int calcAreas(const std::string areas)
int calcAreas(const string areas)
{
int m_areas = 0;
// prepare data
std::string token;
std::istringstream stream(areas);
std::vector<std::string> cmd;
string token;
istringstream stream(areas);
vector<string> cmd;
while (std::getline(stream, token, ',') != 0)
while (getline(stream, token, ',') != 0)
cmd.push_back(token);
for (std::vector<std::string>::iterator it = cmd.begin() ; it != cmd.end(); ++it)
for (vector<string>::iterator it = cmd.begin() ; it != cmd.end(); ++it)
for (int i = 0; i < Size_of_Areas; i++) {
if (strcasecmp("ALL", it->c_str()) == 0)
return (pow(2, (int)Size_of_Areas) - 1);
@@ -61,7 +63,7 @@ int calcAreas(const std::string areas)
return m_areas;
}
int calcLevel(const std::string level)
int calcLevel(const string level)
{
int m_level = event;
for (int i = 0; i < Size_of_Level; i++)
@@ -72,7 +74,7 @@ int calcLevel(const std::string level)
}
LogMessage::LogMessage(const int area, const int level, const std::string text, const bool running)
LogMessage::LogMessage(const int area, const int level, const string text, const bool running)
: m_area(area), m_level(level), m_text(text), m_running(running)
{
char time[24];
@@ -87,7 +89,7 @@ LogMessage::LogMessage(const int area, const int level, const std::string text,
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec/1000);
m_time = std::string(time);
m_time = string(time);
}
@@ -122,23 +124,23 @@ void* LogSink::run()
void LogConsole::write(const LogMessage& message) const
{
std::cout << message.getTime() << " ["
cout << message.getTime() << " ["
<< AreaNames[(int)Log2(message.getArea())] << " "
<< LevelNames[message.getLevel()] << "] "
<< message.getText() << std::endl;
<< message.getText() << endl;
}
void LogFile::write(const LogMessage& message) const
{
std::fstream file(m_file.c_str(), std::ios::out | std::ios::app);
fstream file(m_file.c_str(), ios::out | ios::app);
if (file.is_open() == true) {
file << message.getTime() << " ["
<< AreaNames[(int)Log2(message.getArea())] << " "
<< LevelNames[message.getLevel()] << "] "
<< message.getText() << std::endl;
<< message.getText() << endl;
file.close();
}
}
@@ -160,7 +162,7 @@ Logger::~Logger()
Logger& Logger::operator+=(LogSink* sink)
{
sinkCI_t itEnd = m_sinks.end();
sinkCI_t it = std::find(m_sinks.begin(), itEnd, sink);
sinkCI_t it = find(m_sinks.begin(), itEnd, sink);
if (it == itEnd)
m_sinks.push_back(sink);
@@ -171,7 +173,7 @@ Logger& Logger::operator+=(LogSink* sink)
Logger& Logger::operator-=(const LogSink* sink)
{
sinkCI_t itEnd = m_sinks.end();
sinkCI_t it = std::find(m_sinks.begin(), itEnd, sink);
sinkCI_t it = find(m_sinks.begin(), itEnd, sink);
if (it == itEnd)
return (*this);
@@ -183,7 +185,7 @@ Logger& Logger::operator-=(const LogSink* sink)
return (*this);
}
void Logger::log(const int area, const int level, const std::string& data, ...)
void Logger::log(const int area, const int level, const string& data, ...)
{
if (m_running == true) {
char* tmp;
@@ -191,7 +193,7 @@ void Logger::log(const int area, const int level, const std::string& data, ...)
va_start(ap, data);
if (vasprintf(&tmp, data.c_str(), ap) != -1) {
std::string buffer(tmp);
string buffer(tmp);
m_logQueue.add(new LogMessage(area, level, buffer));
}
+13 -11
View File
@@ -28,6 +28,8 @@
#include <vector>
#include <cstdarg>
using namespace std;
/** \file logger.h */
/** available types for all subsystems */
@@ -50,10 +52,10 @@ enum LevelType {
};
/** global function to get calculate logging areas */
int calcAreas(const std::string areas);
int calcAreas(const string areas);
/** global function to get calculate logging level */
int calcLevel(const std::string level);
int calcLevel(const string level);
/**
* @brief class which describes a logging message itself.
@@ -69,7 +71,7 @@ public:
* @param text the logging message.
* @param running the status of logging subsystem.
*/
LogMessage(const int area, const int level, const std::string text, const bool running=true);
LogMessage(const int area, const int level, const string text, const bool running=true);
/**
* @brief get the logging area.
@@ -87,7 +89,7 @@ public:
* @brief get the logging text.
* @return the logging text.
*/
std::string getText() const { return (m_text.c_str()); }
string getText() const { return (m_text.c_str()); }
/**
* @brief status of logging subsystem.
@@ -99,7 +101,7 @@ public:
* @brief get the logging timestamp.
* @return the logging timestamp.
*/
std::string getTime() const { return (m_time.c_str()); }
string getTime() const { return (m_time.c_str()); }
private:
/** the logging area */
@@ -109,13 +111,13 @@ private:
int m_level;
/** the logging message */
std::string m_text;
string m_text;
/** true if this instance is running */
bool m_running;
/** the logging timestamp */
std::string m_time;
string m_time;
};
@@ -232,7 +234,7 @@ public:
private:
/** the logging file */
std::string m_file;
string m_file;
/**
* @brief write the logging message to specific log file.
@@ -281,7 +283,7 @@ public:
* @param text the logging message.
* @param ... possible 'variable argument lists'.
*/
void log(const int area, const int level, const std::string& text, ...);
void log(const int area, const int level, const string& text, ...);
/**
* @brief returns the sink at the specified index.
@@ -308,8 +310,8 @@ private:
Logger& operator=(const Logger&);
/** typedefs for a vector of type LogSink* */
typedef std::vector<LogSink*> sink_t;
typedef std::vector<LogSink*>::iterator sinkCI_t;
typedef vector<LogSink*> sink_t;
typedef vector<LogSink*>::iterator sinkCI_t;
/** vector of available logging sinks */
sink_t m_sinks;
+3 -1
View File
@@ -24,6 +24,8 @@
#include <netdb.h>
#include <string.h>
using namespace std;
TCPSocket::TCPSocket(int sfd, struct sockaddr_in* address) : m_sfd(sfd)
{
char ip[17];
@@ -41,7 +43,7 @@ bool TCPSocket::isValid()
}
TCPSocket* TCPClient::connect(const std::string& server, const int& port)
TCPSocket* TCPClient::connect(const string& server, const int& port)
{
struct sockaddr_in address;
int ret;
+7 -5
View File
@@ -23,6 +23,8 @@
#include <unistd.h>
#include <string>
using namespace std;
/**
* @brief class for low level tcp socket operations. (open, close, send, receive).
*/
@@ -65,7 +67,7 @@ public:
* @brief returns the ip address.
* @return the ip address.
*/
std::string getIP() const { return m_ip; }
string getIP() const { return m_ip; }
/**
* @brief returns the file descriptor.
@@ -87,7 +89,7 @@ private:
int m_port;
/** ip address of tcp socket */
std::string m_ip;
string m_ip;
/**
* @brief private constructor, limited access only for friend classes.
@@ -111,7 +113,7 @@ public:
* @param port the tcp port.
* @return pointer to an opened tcp socket.
*/
TCPSocket* connect(const std::string& server, const int& port);
TCPSocket* connect(const string& server, const int& port);
};
@@ -127,7 +129,7 @@ public:
* @param port the tcp port.
* @param address the ip address.
*/
TCPServer(const int port, const std::string address)
TCPServer(const int port, const string address)
: m_lfd(0), m_port(port), m_address(address), m_listening(false) {}
/**
@@ -161,7 +163,7 @@ private:
int m_port;
/** listening tcp socket ip address */
std::string m_address;
string m_address;
/** true if object is already listening */
bool m_listening;
+3 -1
View File
@@ -23,6 +23,8 @@
#include <list>
#include <pthread.h>
using namespace std;
/**
* @brief queue class template for all kinds data types with exclusiv lock.
*/
@@ -117,7 +119,7 @@ public:
private:
/** the queue itself */
std::list<T> m_queue;
list<T> m_queue;
/** mutex variable for exclusive lock */
pthread_mutex_t m_mutex;