pianod2
multisource multiuser scriptable networked music player
parsnip_argv.h
Go to the documentation of this file.
1 
9 #include <config.h>
10 
11 #include <string>
12 #include <vector>
13 
14 namespace Parsnip {
19  class ArgumentVector: public std::vector <std::string> {
20  friend class ArgvCursor;
21  public:
22  using StringType = value_type;
23  private:
27  std::vector <StringType::size_type> remainder_start;
28  public:
29  ArgumentVector (const StringType &command_line);
30 
35  inline StringType remainder (size_type index) const {
36  assert (index >= 0 && index < size());
37  return original.substr (remainder_start [index]);
38  }
39  };
40 
42  class ArgvCursor {
43  const ArgumentVector *argv {nullptr};
44  ArgumentVector::size_type point = 0;
45  public:
46  inline ArgvCursor (const ArgumentVector *the_argv): argv (the_argv) {
47  }
48  inline ArgvCursor (const ArgvCursor &from) = default;
49  inline ArgvCursor (ArgvCursor &&from) = default;
50  inline ArgvCursor &operator =(const ArgvCursor &from) = default;
51  inline ArgvCursor &operator =(ArgvCursor &&from) = default;
52 
55  inline bool isStart() const {
56  return point == 0;
57  }
58 
62  inline bool isEnd() const {
63  return point >= argv->size();
64  }
65 
66  inline ArgvCursor &operator++() {
67  point++;
68  return *this;
69  }
70 
71  inline ArgvCursor operator++(int) {
72  ArgvCursor prior { *this };
73  point++;
74  return prior;
75  }
76 
77  inline ArgvCursor operator+(int value) const {
78  ArgvCursor result { *this };
79  result.point += value;
80  return result;
81  }
82 
83  inline ArgvCursor operator-(int value) const {
84  assert (point > 0);
85  ArgvCursor result { *this };
86  result.point -= value;
87  return result;
88  }
89 
91  inline const ArgumentVector::StringType &value () const {
92  assert (point >= 0 && point < argv->size());
93  return (*argv) [point];
94  }
95 
98  assert (point >= 0 && point < argv->size());
99  return argv->remainder (point);
100  }
101  std::vector <ArgumentVector::StringType> remainingTokens () const;
102  };
103 }
This class lexically analyzes a string, splitting it up into tokens, which are accessed as a vector.
Definition: parsnip_argv.h:19
const StringType original
The original command string.
Definition: parsnip_argv.h:25
std::vector< StringType::size_type > remainder_start
Index of token locations in original string.
Definition: parsnip_argv.h:27
StringType remainder(size_type index) const
Retrieve the raw, untokenized remainder of the string.
Definition: parsnip_argv.h:35
ArgumentVector(const StringType &command_line)
Construct an argument vector by splitting up a string into tokens.
Definition: parsnip_argv.cpp:27
value_type StringType
Definition: parsnip_argv.h:22
An iterator for argument vectors.
Definition: parsnip_argv.h:42
ArgvCursor & operator++()
Definition: parsnip_argv.h:66
ArgumentVector::size_type point
Definition: parsnip_argv.h:44
ArgvCursor(const ArgumentVector *the_argv)
Definition: parsnip_argv.h:46
ArgvCursor & operator=(const ArgvCursor &from)=default
bool isEnd() const
Check if the cursor is at the end of the command line.
Definition: parsnip_argv.h:62
const ArgumentVector::StringType & value() const
Return the current token.
Definition: parsnip_argv.h:91
std::vector< ArgumentVector::StringType > remainingTokens() const
const ArgumentVector * argv
Definition: parsnip_argv.h:43
bool isStart() const
Check if the cursor is at the start of the command line.
Definition: parsnip_argv.h:55
ArgvCursor(const ArgvCursor &from)=default
const ArgumentVector::StringType remainingString() const
Return the raw, unsplit remainder of the command line.
Definition: parsnip_argv.h:97
ArgvCursor(ArgvCursor &&from)=default
ArgvCursor operator++(int)
Definition: parsnip_argv.h:71
ArgvCursor operator-(int value) const
Definition: parsnip_argv.h:83
ArgvCursor operator+(int value) const
Definition: parsnip_argv.h:77
Serialization and parsing library.
Definition: mediaunit.h:26