Grok  7.6.0
ojph_arg.h
Go to the documentation of this file.
1 //***************************************************************************/
2 // This software is released under the 2-Clause BSD license, included
3 // below.
4 //
5 // Copyright (c) 2019, Aous Naman
6 // Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
7 // Copyright (c) 2019, The University of New South Wales, Australia
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are
11 // met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 //***************************************************************************/
32 // This file is part of the OpenJPH software implementation.
33 // File: ojph_arg.h
34 // Author: Aous Naman
35 // Date: 28 August 2019
36 //***************************************************************************/
37 
38 
39 #ifndef OJPH_ARG_H
40 #define OJPH_ARG_H
41 
42 #include <cstdlib>
43 #include <cassert>
44 #include <cstring>
45 
46 #include "ojph_defs.h"
47 
48 namespace ojph {
49 
51  //
53  class argument {
54  friend class cli_interpreter;
55  public:
56  argument() : arg(NULL), index(0) {}
57  char *arg;
58  bool is_valid() { return (arg != NULL); }
59  private:
60  int index;
61  };
62 
64  //
67  public:
70  { if (avail != avail_store) delete[] avail; }
71 
73  void init(int argc, char *argv[]) {
74  assert(avail == avail_store);
75  if (argc > 128)
76  avail = new ui8[(argc + 7) >> 3];
77  memset(avail, 0, ojph_max((int)sizeof(avail_store), (argc + 7) >> 3));
78  this->argv = argv;
79  this->argc = argc;
80  for (int i = 0; i < argc; ++i)
81  avail[i >> 3] |= (ui8)(1 << (i & 7));
82  }
83 
85  argument get_next_value(const argument& current) {
86  argument t;
87  int idx = current.index + 1;
88  if (idx < argc && (avail[idx >> 3] & (1 << (idx & 0x7)))) {
89  t.arg = argv[idx];
90  t.index = idx;
91  }
92  return t;
93  }
94 
96  argument find_argument(const char *str) {
97  argument t;
98  for (int index = 1; index < argc; ++index)
99  if (avail[index >> 3] & (1 << (index & 0x7)))
100  if (strcmp(str, argv[index]) == 0) {
101  t.arg = argv[index];
102  t.index = index;
103  return t;
104  }
105  return t;
106  }
107 
109  void release_argument(const argument& arg) {
110  if (arg.index != 0) {
111  assert(arg.index < argc);
112  avail[arg.index >> 3] &= (ui8)(~(1 << (arg.index & 0x7)));
113  }
114  }
115 
117  bool is_exhausted() {
118  for (int i = 1; i < argc; ++i)
119  if (avail[i >> 3] & (1 << (i & 0x7)))
120  return false;
121  return true;
122  }
123 
126  argument t;
127  t.arg = argv[0];
128  return t;
129  }
130 
133  argument t;
134  int idx = arg.index + 1;
135  while (idx < argc && (avail[idx >> 3] & (1 << (idx & 0x7))) == 0)
136  ++idx;
137  if (idx < argc) {
138  t.arg = argv[idx];
139  t.index = idx;
140  }
141  return t;
142  }
143 
145  void reinterpret(const char *str, int& val) {
146  argument t = find_argument(str);
147  if (t.is_valid()) {
148  argument t2 = get_next_value(t);
149  if (t2.is_valid()) {
150  val = atoi(t2.arg);
151  release_argument(t);
152  release_argument(t2);
153  }
154  }
155  }
156 
158  void reinterpret(const char *str, float& val) {
159  argument t = find_argument(str);
160  if (t.is_valid()) {
161  argument t2 = get_next_value(t);
162  if (t2.is_valid()) {
163  val = strtof(t2.arg, NULL);
164  release_argument(t);
165  release_argument(t2);
166  }
167  }
168  }
169 
171  void reinterpret(const char *str, bool& val) {
172  argument t = find_argument(str);
173  if (t.is_valid()) {
174  argument t2 = get_next_value(t);
175  if (t2.is_valid()) {
176  if (strcmp(t2.arg, "false") == 0) {
177  val = false;
178  release_argument(t);
179  release_argument(t2);
180  }
181  else if (strcmp(t2.arg, "true") == 0) {
182  val = true;
183  release_argument(t);
184  release_argument(t2);
185  }
186  }
187  }
188  }
189 
191  void reinterpret_to_bool(const char *str, int& val) {
192  argument t = find_argument(str);
193  if (t.is_valid()) {
194  argument t2 = get_next_value(t);
195  if (t2.is_valid()) {
196  if (strcmp(t2.arg, "false") == 0) {
197  val = 0;
198  release_argument(t);
199  release_argument(t2);
200  }
201  else if (strcmp(t2.arg, "true") == 0) {
202  val = 1;
203  release_argument(t);
204  release_argument(t2);
205  }
206  }
207  }
208  }
209 
211  void reinterpret(const char *str, char *& val) {
212  argument t = find_argument(str);
213  if (t.is_valid()) {
214  argument t2 = get_next_value(t);
215  if (t2.is_valid()) {
216  val = t2.arg;
217  release_argument(t);
218  release_argument(t2);
219  }
220  }
221  }
222 
224  struct arg_inter_base { virtual void operate(const char *) = 0; };
225 
227  void reinterpret(const char *str, arg_inter_base* fun) {
228  argument t = find_argument(str);
229  if (t.is_valid()) {
230  argument t2 = get_next_value(t);
231  if (t2.is_valid()) {
232  fun->operate(t2.arg);
233  release_argument(t);
234  release_argument(t2);
235  }
236  }
237  }
238 
239  private:
240  char **argv;
241  int argc;
242  ui8 avail_store[16];//this should be enough for 128 command line arguments
244  };
245 }
246 
247 #endif // !OJPH_ARG_H
ojph::argument::arg
char * arg
Definition: ojph_arg.h:57
ojph::argument::index
int index
Definition: ojph_arg.h:60
ojph::cli_interpreter::avail
ui8 * avail
Definition: ojph_arg.h:243
ojph::ui8
uint8_t ui8
Definition: ojph_defs.h:49
ojph::argument::argument
argument()
Definition: ojph_arg.h:56
ojph::cli_interpreter::get_next_avail_argument
argument get_next_avail_argument(const argument &arg)
Definition: ojph_arg.h:132
ojph::cli_interpreter::reinterpret
void reinterpret(const char *str, int &val)
Definition: ojph_arg.h:145
ojph::argument::is_valid
bool is_valid()
Definition: ojph_arg.h:58
ojph::cli_interpreter::reinterpret
void reinterpret(const char *str, bool &val)
Definition: ojph_arg.h:171
ojph::cli_interpreter::get_argument_zero
argument get_argument_zero()
Definition: ojph_arg.h:125
ojph::cli_interpreter::find_argument
argument find_argument(const char *str)
Definition: ojph_arg.h:96
ojph::argument
Definition: ojph_arg.h:53
ojph::cli_interpreter::reinterpret_to_bool
void reinterpret_to_bool(const char *str, int &val)
Definition: ojph_arg.h:191
ojph::cli_interpreter::reinterpret
void reinterpret(const char *str, char *&val)
Definition: ojph_arg.h:211
ojph::cli_interpreter::release_argument
void release_argument(const argument &arg)
Definition: ojph_arg.h:109
ojph_defs.h
ojph::cli_interpreter::arg_inter_base::operate
virtual void operate(const char *)=0
ojph::cli_interpreter::init
void init(int argc, char *argv[])
Definition: ojph_arg.h:73
ojph::cli_interpreter::avail_store
ui8 avail_store[16]
Definition: ojph_arg.h:242
ojph::cli_interpreter::argc
int argc
Definition: ojph_arg.h:241
ojph_max
#define ojph_max(a, b)
Definition: ojph_defs.h:77
ojph::cli_interpreter::is_exhausted
bool is_exhausted()
Definition: ojph_arg.h:117
ojph::cli_interpreter::arg_inter_base
Definition: ojph_arg.h:224
ojph
Definition: ojph_block_decoder.h:44
ojph::cli_interpreter::cli_interpreter
cli_interpreter()
Definition: ojph_arg.h:68
ojph::cli_interpreter::get_next_value
argument get_next_value(const argument &current)
Definition: ojph_arg.h:85
ojph::cli_interpreter::argv
char ** argv
Definition: ojph_arg.h:240
ojph::cli_interpreter::~cli_interpreter
~cli_interpreter()
Definition: ojph_arg.h:69
ojph::cli_interpreter
Definition: ojph_arg.h:66
ojph::cli_interpreter::reinterpret
void reinterpret(const char *str, arg_inter_base *fun)
Definition: ojph_arg.h:227
ojph::cli_interpreter::reinterpret
void reinterpret(const char *str, float &val)
Definition: ojph_arg.h:158