Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
JArgs.hh
Go to the documentation of this file.
1#ifndef __JEEP__JARGS__
2#define __JEEP__JARGS__
3
4#include <string>
5#include <sstream>
6#include <istream>
7#include <ostream>
8#include <vector>
9
10
11/**
12 * \author mdejong
13 */
14
15namespace JEEP {}
16namespace JPP { using namespace JEEP; }
17
18namespace JEEP {
19
20
21 /**
22 * Data structure to store command line arguments.
23 */
24 class JArgs :
25 public std::vector<std::string>
26 {
27 public:
28 /**
29 * Default constructor.
30 */
32 {}
33
34
35 /**
36 * Constructor.\n
37 * The first argument (if any) is assumed to be the process name (PID).
38 *
39 * \param argc number of command line arguments
40 * \param argv array of command line arguments
41 */
42 JArgs(const int argc, const char* const argv[])
43 {
44 if (argc > 0) {
45
46 PID = argv[0];
47
48 for (int i = 1; i != argc; ++i) {
49 push_back(argv[i]);
50 }
51
52 } else {
53
54 PID = "";
55 }
56 }
57
58
59 /**
60 * Constructor.\n
61 *
62 * \param PID process identifier
63 * \param __begin begin of command line arguments
64 * \param __end end of command line arguments
65 */
66 JArgs(const std::string& PID, JArgs::const_iterator __begin, JArgs::const_iterator __end)
67 {
68 this->PID = PID;
69
70 for (const_iterator i = __begin; i != __end; ++i) {
71 push_back(*i);
72 }
73 }
74
75
76 /**
77 * Constructor.
78 *
79 * \param buffer input
80 */
81 JArgs(const std::string& buffer)
82 {
83 using namespace std;
84
85 istringstream is(buffer);
86
87 PID = "";
88
89 for (string word; is >> word; ) {
90 push_back(word);
91 }
92 }
93
94
95 /**
96 * Convert to string consisting of sequence of tokens separated by given white space character.
97 *
98 * \param ws white space character
99 * \return string
100 */
101 std::string str(const char ws = ' ') const
102 {
103 std::string buffer = PID;
104
105 for (const_iterator i = this->begin(); i != this->end(); ++i) {
106 buffer += ws;
107 buffer += *i;
108 }
109
110 return buffer;
111 }
112
113
114 /**
115 * Convert to character array consisting of sequence of tokens separated by given white space character.
116 *
117 * \param ws white space character
118 * \return character array
119 */
120 const char* c_str(const char ws = ' ') const
121 {
122 static std::string buffer;
123
124 buffer = this->str(ws);
125
126 return buffer.c_str();
127 }
128
129
130 /**
131 * Stream input.
132 *
133 * \param in input stream
134 * \param args args
135 * \return input stream
136 */
137 friend inline std::istream& operator>>(std::istream& in, JArgs& args)
138 {
139 //in >> args.PID;
140
141 for (std::string buffer; in >> buffer; ) {
142 args.push_back(buffer);
143 }
144
145 return in;
146 }
147
148
149 /**
150 * Stream output.
151 *
152 * \param out output stream
153 * \param args args
154 * \return output stream
155 */
156 friend inline std::ostream& operator<<(std::ostream& out, const JArgs& args)
157 {
158 out << args.PID;
159
160 for (JArgs::const_iterator i = args.begin(); i != args.end(); ++i) {
161 out << ' ' << *i;
162 }
163
164 return out;
165 }
166
167 std::string PID;
168 };
169}
170
171#endif
Data structure to store command line arguments.
Definition JArgs.hh:26
friend std::ostream & operator<<(std::ostream &out, const JArgs &args)
Stream output.
Definition JArgs.hh:156
JArgs(const std::string &buffer)
Constructor.
Definition JArgs.hh:81
const char * c_str(const char ws=' ') const
Convert to character array consisting of sequence of tokens separated by given white space character.
Definition JArgs.hh:120
JArgs(const int argc, const char *const argv[])
Constructor.
Definition JArgs.hh:42
friend std::istream & operator>>(std::istream &in, JArgs &args)
Stream input.
Definition JArgs.hh:137
JArgs(const std::string &PID, JArgs::const_iterator __begin, JArgs::const_iterator __end)
Constructor.
Definition JArgs.hh:66
std::string PID
Definition JArgs.hh:167
JArgs()
Default constructor.
Definition JArgs.hh:31
std::string str(const char ws=' ') const
Convert to string consisting of sequence of tokens separated by given white space character.
Definition JArgs.hh:101
General puprpose classes and methods.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).