#ifndef __JEEP__JARGS__ #define __JEEP__JARGS__ #include #include #include #include #include /** * \author mdejong */ namespace JEEP {} namespace JPP { using namespace JEEP; } namespace JEEP { /** * Data structure to store command line arguments. */ class JArgs : public std::vector { public: /** * Default constructor. */ JArgs() {} /** * Constructor.\n * The first argument (if any) is assumed to be the process name (PID). * * \param argc number of command line arguments * \param argv array of command line arguments */ JArgs(const int argc, const char* const argv[]) { if (argc > 0) { PID = argv[0]; for (int i = 1; i != argc; ++i) { push_back(argv[i]); } } else { PID = ""; } } /** * Constructor.\n * * \param PID process identifier * \param __begin begin of command line arguments * \param __end end of command line arguments */ JArgs(const std::string& PID, JArgs::const_iterator __begin, JArgs::const_iterator __end) { this->PID = PID; for (const_iterator i = __begin; i != __end; ++i) { push_back(*i); } } /** * Constructor. * * \param buffer input */ JArgs(const std::string& buffer) { using namespace std; istringstream is(buffer); PID = ""; for (string word; is >> word; ) { push_back(word); } } /** * Convert to string consisting of sequence of tokens separated by given white space character. * * \param ws white space character * \return string */ std::string str(const char ws = ' ') const { std::string buffer = PID; for (const_iterator i = this->begin(); i != this->end(); ++i) { buffer += ws; buffer += *i; } return buffer; } /** * Convert to character array consisting of sequence of tokens separated by given white space character. * * \param ws white space character * \return character array */ const char* c_str(const char ws = ' ') const { static std::string buffer; buffer = this->str(ws); return buffer.c_str(); } /** * Stream input. * * \param in input stream * \param args args * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JArgs& args) { //in >> args.PID; for (std::string buffer; in >> buffer; ) { args.push_back(buffer); } return in; } /** * Stream output. * * \param out output stream * \param args args * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JArgs& args) { out << args.PID; for (JArgs::const_iterator i = args.begin(); i != args.end(); ++i) { out << ' ' << *i; } return out; } std::string PID; }; } #endif