Lumiera  0.pre.03
»edit your freedom«
cmdline.cpp
Go to the documentation of this file.
1 /*
2  Cmdline - abstraction of the usual commandline, a sequence of strings
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
23 
30 #include "include/logging.h"
31 #include "lib/hash-standard.hpp"
32 #include "lib/cmdline.hpp"
33 #include "lib/format-util.hpp"
34 
35 #include <regex>
36 
37 using std::regex;
38 using std::smatch;
39 using std::regex_search;
40 using util::join;
41 using util::noneg;
42 
43 
44 
45 namespace lib {
46 
47 
50  Cmdline::Cmdline (int argc, const char** argv)
51  : vector<string> (noneg(argc-1))
52  {
53  for (int i=1; i<argc; ++i)
54  {
55  ASSERT (argv[i]);
56  (*this)[i-1] = argv[i];
57  }
58  }
59 
60 
64  Cmdline::Cmdline (const string cmdline)
65  {
66  static regex TOKENDEF{"\\S+"};
67  smatch match;
68  string::const_iterator it = cmdline.begin();
69  string::const_iterator end = cmdline.end();
70 
71  while (regex_search(it, end, match, TOKENDEF))
72  {
73  string ss(match[0]);
74  this->push_back(ss);
75  it = match[0].second;
76  }
77 
78  }
79 
80 
82  Cmdline::operator string () const
83  {
84  return join(*this," ");
85  }
86 
87 
88 
89 } // namespace lib
NUM constexpr noneg(NUM val)
cut a numeric value to be >=0
Definition: util.hpp:93
This header is for including and configuring NoBug.
Implementation namespace for support and library code.
Class to encapsulate the typical C-style commandline definition.
Helper to use a single extension point for specialised hash functions.
Collection of small helpers and convenience shortcuts for diagnostics & formatting.