Grok  7.6.6
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,
78  ojph_max(sizeof(avail_store), (size_t)((argc + 7) >> 3)));
79  this->argv = argv;
80  this->argc = argc;
81  for (int i = 0; i < argc; ++i)
82  avail[i >> 3] = (ui8)(avail[i >> 3] | (ui8)(1 << (i & 7)));
83  }
84 
86  argument get_next_value(const argument& current) {
87  argument t;
88  int idx = current.index + 1;
89  if (idx < argc && (avail[idx >> 3] & (1 << (idx & 0x7)))) {
90  t.arg = argv[idx];
91  t.index = idx;
92  }
93  return t;
94  }
95 
97  argument find_argument(const char *str) {
98  argument t;
99  for (int index = 1; index < argc; ++index)
100  if (avail[index >> 3] & (1 << (index & 0x7)))
101  if (strcmp(str, argv[index]) == 0) {
102  t.arg = argv[index];
103  t.index = index;
104  return t;
105  }
106  return t;
107  }
108 
110  void release_argument(const argument& arg) {
111  if (arg.index != 0) {
112  assert(arg.index < argc);
113  avail[arg.index >> 3] &= (ui8)(~(1 << (arg.index & 0x7)));
114  }
115  }
116 
118  bool is_exhausted() {
119  for (int i = 1; i < argc; ++i)
120  if (avail[i >> 3] & (1 << (i & 0x7)))
121  return false;
122  return true;
123  }
124 
127  argument t;
128  t.arg = argv[0];
129  return t;
130  }
131 
134  argument t;
135  int idx = arg.index + 1;
136  while (idx < argc && (avail[idx >> 3] & (1 << (idx & 0x7))) == 0)
137  ++idx;
138  if (idx < argc) {
139  t.arg = argv[idx];
140  t.index = idx;
141  }
142  return t;
143  }
144 
146  void reinterpret(const char *str, int& val) {
147  argument t = find_argument(str);
148  if (t.is_valid()) {
149  argument t2 = get_next_value(t);
150  if (t2.is_valid()) {
151  val = atoi(t2.arg);
152  release_argument(t);
153  release_argument(t2);
154  }
155  }
156  }
157 
159  void reinterpret(const char* str, ui32& val) {
160  argument t = find_argument(str);
161  if (t.is_valid()) {
162  argument t2 = get_next_value(t);
163  if (t2.is_valid()) {
164  val = (ui32)strtoul(t2.arg, NULL, 10);
165  release_argument(t);
166  release_argument(t2);
167  }
168  }
169  }
170 
172  void reinterpret(const char *str, float& val) {
173  argument t = find_argument(str);
174  if (t.is_valid()) {
175  argument t2 = get_next_value(t);
176  if (t2.is_valid()) {
177  val = strtof(t2.arg, NULL);
178  release_argument(t);
179  release_argument(t2);
180  }
181  }
182  }
183 
185  void reinterpret(const char *str, bool& val) {
186  argument t = find_argument(str);
187  if (t.is_valid()) {
188  argument t2 = get_next_value(t);
189  if (t2.is_valid()) {
190  if (strcmp(t2.arg, "false") == 0) {
191  val = false;
192  release_argument(t);
193  release_argument(t2);
194  }
195  else if (strcmp(t2.arg, "true") == 0) {
196  val = true;
197  release_argument(t);
198  release_argument(t2);
199  }
200  }
201  }
202  }
203 
205  void reinterpret_to_bool(const char *str, int& val) {
206  argument t = find_argument(str);
207  if (t.is_valid()) {
208  argument t2 = get_next_value(t);
209  if (t2.is_valid()) {
210  if (strcmp(t2.arg, "false") == 0) {
211  val = 0;
212  release_argument(t);
213  release_argument(t2);
214  }
215  else if (strcmp(t2.arg, "true") == 0) {
216  val = 1;
217  release_argument(t);
218  release_argument(t2);
219  }
220  }
221  }
222  }
223 
225  void reinterpret(const char *str, char *& val) {
226  argument t = find_argument(str);
227  if (t.is_valid()) {
228  argument t2 = get_next_value(t);
229  if (t2.is_valid()) {
230  val = t2.arg;
231  release_argument(t);
232  release_argument(t2);
233  }
234  }
235  }
236 
238  struct arg_inter_base { virtual void operate(const char *) = 0; };
239 
241  void reinterpret(const char *str, arg_inter_base* fun) {
242  argument t = find_argument(str);
243  if (t.is_valid()) {
244  argument t2 = get_next_value(t);
245  if (t2.is_valid()) {
246  fun->operate(t2.arg);
247  release_argument(t);
248  release_argument(t2);
249  }
250  }
251  }
252 
253  private:
254  char **argv;
255  int argc;
256  ui8 avail_store[16];//this should be enough for 128 command line arguments
258  };
259 }
260 
261 #endif // !OJPH_ARG_H
Definition: ojph_arg.h:53
int index
Definition: ojph_arg.h:60
bool is_valid()
Definition: ojph_arg.h:58
argument()
Definition: ojph_arg.h:56
char * arg
Definition: ojph_arg.h:57
Definition: ojph_arg.h:66
cli_interpreter()
Definition: ojph_arg.h:68
void init(int argc, char *argv[])
Definition: ojph_arg.h:73
void reinterpret_to_bool(const char *str, int &val)
Definition: ojph_arg.h:205
void reinterpret(const char *str, int &val)
Definition: ojph_arg.h:146
ui8 * avail
Definition: ojph_arg.h:257
void reinterpret(const char *str, float &val)
Definition: ojph_arg.h:172
void release_argument(const argument &arg)
Definition: ojph_arg.h:110
bool is_exhausted()
Definition: ojph_arg.h:118
void reinterpret(const char *str, ui32 &val)
Definition: ojph_arg.h:159
argument find_argument(const char *str)
Definition: ojph_arg.h:97
void reinterpret(const char *str, bool &val)
Definition: ojph_arg.h:185
argument get_argument_zero()
Definition: ojph_arg.h:126
argument get_next_avail_argument(const argument &arg)
Definition: ojph_arg.h:133
char ** argv
Definition: ojph_arg.h:254
void reinterpret(const char *str, arg_inter_base *fun)
Definition: ojph_arg.h:241
~cli_interpreter()
Definition: ojph_arg.h:69
void reinterpret(const char *str, char *&val)
Definition: ojph_arg.h:225
ui8 avail_store[16]
Definition: ojph_arg.h:256
int argc
Definition: ojph_arg.h:255
argument get_next_value(const argument &current)
Definition: ojph_arg.h:86
Definition: ojph_block_decoder.cpp:49
uint32_t ui32
Definition: ojph_defs.h:54
uint8_t ui8
Definition: ojph_defs.h:50
#define ojph_max(a, b)
Definition: ojph_defs.h:73
Definition: ojph_arg.h:238
virtual void operate(const char *)=0