OpenJPH
Open-source implementation of JPEG2000 Part-15
Toggle main menu visibility
Loading...
Searching...
No Matches
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
//
66
class
cli_interpreter
{
67
public
:
68
cli_interpreter
() :
argv
(NULL),
argc
(0),
avail
(
avail_store
) { }
69
~cli_interpreter
()
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
[((
ui32
)
argc
+ 7u) >> 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
126
argument
get_argument_zero
() {
127
argument
t;
128
t.
arg
=
argv
[0];
129
return
t;
130
}
131
133
argument
get_next_avail_argument
(
const
argument
& arg) {
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
bool
reinterpret
(
const
char
*str) {
206
argument
t =
find_argument
(str);
207
if
(t.
is_valid
()) {
208
release_argument
(t);
209
return
true
;
210
}
211
else
212
return
false
;
213
}
214
216
void
reinterpret_to_bool
(
const
char
*str,
int
& val) {
217
argument
t =
find_argument
(str);
218
if
(t.
is_valid
()) {
219
argument
t2 =
get_next_value
(t);
220
if
(t2.
is_valid
()) {
221
if
(strcmp(t2.
arg
,
"false"
) == 0) {
222
val = 0;
223
release_argument
(t);
224
release_argument
(t2);
225
}
226
else
if
(strcmp(t2.
arg
,
"true"
) == 0) {
227
val = 1;
228
release_argument
(t);
229
release_argument
(t2);
230
}
231
}
232
}
233
}
234
236
void
reinterpret
(
const
char
*str,
char
*& val) {
237
argument
t =
find_argument
(str);
238
if
(t.
is_valid
()) {
239
argument
t2 =
get_next_value
(t);
240
if
(t2.
is_valid
()) {
241
val = t2.
arg
;
242
release_argument
(t);
243
release_argument
(t2);
244
}
245
}
246
}
247
249
struct
arg_inter_base
{
virtual
void
operate
(
const
char
*) = 0; };
250
252
void
reinterpret
(
const
char
*str,
arg_inter_base
* fun) {
253
argument
t =
find_argument
(str);
254
if
(t.
is_valid
()) {
255
argument
t2 =
get_next_value
(t);
256
if
(t2.
is_valid
()) {
257
fun->
operate
(t2.
arg
);
258
release_argument
(t);
259
release_argument
(t2);
260
}
261
}
262
}
263
264
private
:
265
char
**
argv
;
266
int
argc
;
267
ui8
avail_store
[16];
//this should be enough for 128 command line arguments
268
ui8
*
avail
;
269
};
270
}
271
272
#endif
// !OJPH_ARG_H
ojph::argument
Definition
ojph_arg.h:53
ojph::argument::index
int index
Definition
ojph_arg.h:60
ojph::argument::is_valid
bool is_valid()
Definition
ojph_arg.h:58
ojph::argument::argument
argument()
Definition
ojph_arg.h:56
ojph::argument::arg
char * arg
Definition
ojph_arg.h:57
ojph::argument::cli_interpreter
friend class cli_interpreter
Definition
ojph_arg.h:54
ojph::cli_interpreter::cli_interpreter
cli_interpreter()
Definition
ojph_arg.h:68
ojph::cli_interpreter::init
void init(int argc, char *argv[])
Definition
ojph_arg.h:73
ojph::cli_interpreter::reinterpret_to_bool
void reinterpret_to_bool(const char *str, int &val)
Definition
ojph_arg.h:216
ojph::cli_interpreter::reinterpret
void reinterpret(const char *str, int &val)
Definition
ojph_arg.h:146
ojph::cli_interpreter::avail
ui8 * avail
Definition
ojph_arg.h:268
ojph::cli_interpreter::reinterpret
void reinterpret(const char *str, float &val)
Definition
ojph_arg.h:172
ojph::cli_interpreter::release_argument
void release_argument(const argument &arg)
Definition
ojph_arg.h:110
ojph::cli_interpreter::is_exhausted
bool is_exhausted()
Definition
ojph_arg.h:118
ojph::cli_interpreter::reinterpret
void reinterpret(const char *str, ui32 &val)
Definition
ojph_arg.h:159
ojph::cli_interpreter::find_argument
argument find_argument(const char *str)
Definition
ojph_arg.h:97
ojph::cli_interpreter::reinterpret
void reinterpret(const char *str, bool &val)
Definition
ojph_arg.h:185
ojph::cli_interpreter::get_argument_zero
argument get_argument_zero()
Definition
ojph_arg.h:126
ojph::cli_interpreter::get_next_avail_argument
argument get_next_avail_argument(const argument &arg)
Definition
ojph_arg.h:133
ojph::cli_interpreter::argv
char ** argv
Definition
ojph_arg.h:265
ojph::cli_interpreter::reinterpret
void reinterpret(const char *str, arg_inter_base *fun)
Definition
ojph_arg.h:252
ojph::cli_interpreter::reinterpret
bool reinterpret(const char *str)
Definition
ojph_arg.h:205
ojph::cli_interpreter::~cli_interpreter
~cli_interpreter()
Definition
ojph_arg.h:69
ojph::cli_interpreter::reinterpret
void reinterpret(const char *str, char *&val)
Definition
ojph_arg.h:236
ojph::cli_interpreter::avail_store
ui8 avail_store[16]
Definition
ojph_arg.h:267
ojph::cli_interpreter::argc
int argc
Definition
ojph_arg.h:266
ojph::cli_interpreter::get_next_value
argument get_next_value(const argument ¤t)
Definition
ojph_arg.h:86
ojph
Definition
ojph_img_io.h:52
ojph::ui32
uint32_t ui32
Definition
ojph_defs.h:54
ojph::ui8
uint8_t ui8
Definition
ojph_defs.h:50
ojph_defs.h
ojph_max
#define ojph_max(a, b)
Definition
ojph_defs.h:73
ojph::cli_interpreter::arg_inter_base
Definition
ojph_arg.h:249
ojph::cli_interpreter::arg_inter_base::operate
virtual void operate(const char *)=0
src
core
openjph
ojph_arg.h
Generated by
1.18.0