Loading...
Searching...
No Matches
ojph_arg.h
Go to the documentation of this file.
//***************************************************************************/
// This software is released under the 2-Clause BSD license, included
// below.
//
// Copyright (c) 2019, Aous Naman
// Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
// Copyright (c) 2019, The University of New South Wales, Australia
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//***************************************************************************/
// This file is part of the OpenJPH software implementation.
// File: ojph_arg.h
// Author: Aous Naman
// Date: 28 August 2019
//***************************************************************************/
#ifndef OJPH_ARG_H
#define OJPH_ARG_H
#include <cstdlib>
#include <cassert>
#include <cstring>
#include "ojph_defs.h"
namespace ojph {
//
class argument {
friend class cli_interpreter;
public:
argument() : arg(NULL), index(0) {}
char *arg;
bool is_valid() { return (arg != NULL); }
private:
int index;
};
//
class cli_interpreter {
public:
cli_interpreter() : argv(NULL), argc(0), avail(avail_store) { }
~cli_interpreter()
{ if (avail != avail_store) delete[] avail; }
void init(int argc, char *argv[]) {
assert(avail == avail_store);
if (argc > 128)
avail = new ui8[((ui32)argc + 7u) >> 3];
memset(avail, 0,
ojph_max(sizeof(avail_store), (size_t)((argc + 7) >> 3)));
this->argv = argv;
this->argc = argc;
for (int i = 0; i < argc; ++i)
avail[i >> 3] = (ui8)(avail[i >> 3] | (ui8)(1 << (i & 7)));
}
argument get_next_value(const argument& current) {
argument t;
int idx = current.index + 1;
if (idx < argc && (avail[idx >> 3] & (1 << (idx & 0x7)))) {
t.arg = argv[idx];
t.index = idx;
}
return t;
}
argument find_argument(const char *str) {
argument t;
for (int index = 1; index < argc; ++index)
if (avail[index >> 3] & (1 << (index & 0x7)))
if (strcmp(str, argv[index]) == 0) {
t.arg = argv[index];
t.index = index;
return t;
}
return t;
}
void release_argument(const argument& arg) {
if (arg.index != 0) {
assert(arg.index < argc);
avail[arg.index >> 3] &= (ui8)(~(1 << (arg.index & 0x7)));
}
}
bool is_exhausted() {
for (int i = 1; i < argc; ++i)
if (avail[i >> 3] & (1 << (i & 0x7)))
return false;
return true;
}
argument get_argument_zero() {
argument t;
t.arg = argv[0];
return t;
}
argument get_next_avail_argument(const argument& arg) {
argument t;
int idx = arg.index + 1;
while (idx < argc && (avail[idx >> 3] & (1 << (idx & 0x7))) == 0)
++idx;
if (idx < argc) {
t.arg = argv[idx];
t.index = idx;
}
return t;
}
void reinterpret(const char *str, int& val) {
argument t = find_argument(str);
if (t.is_valid()) {
argument t2 = get_next_value(t);
if (t2.is_valid()) {
val = atoi(t2.arg);
release_argument(t);
release_argument(t2);
}
}
}
void reinterpret(const char* str, ui32& val) {
argument t = find_argument(str);
if (t.is_valid()) {
argument t2 = get_next_value(t);
if (t2.is_valid()) {
val = (ui32)strtoul(t2.arg, NULL, 10);
release_argument(t);
release_argument(t2);
}
}
}
void reinterpret(const char *str, float& val) {
argument t = find_argument(str);
if (t.is_valid()) {
argument t2 = get_next_value(t);
if (t2.is_valid()) {
val = strtof(t2.arg, NULL);
release_argument(t);
release_argument(t2);
}
}
}
void reinterpret(const char *str, bool& val) {
argument t = find_argument(str);
if (t.is_valid()) {
argument t2 = get_next_value(t);
if (t2.is_valid()) {
if (strcmp(t2.arg, "false") == 0) {
val = false;
release_argument(t);
release_argument(t2);
}
else if (strcmp(t2.arg, "true") == 0) {
val = true;
release_argument(t);
release_argument(t2);
}
}
}
}
bool reinterpret(const char *str) {
argument t = find_argument(str);
if (t.is_valid()) {
release_argument(t);
return true;
}
else
return false;
}
void reinterpret_to_bool(const char *str, int& val) {
argument t = find_argument(str);
if (t.is_valid()) {
argument t2 = get_next_value(t);
if (t2.is_valid()) {
if (strcmp(t2.arg, "false") == 0) {
val = 0;
release_argument(t);
release_argument(t2);
}
else if (strcmp(t2.arg, "true") == 0) {
val = 1;
release_argument(t);
release_argument(t2);
}
}
}
}
void reinterpret(const char *str, char *& val) {
argument t = find_argument(str);
if (t.is_valid()) {
argument t2 = get_next_value(t);
if (t2.is_valid()) {
val = t2.arg;
release_argument(t);
release_argument(t2);
}
}
}
struct arg_inter_base { virtual void operate(const char *) = 0; };
void reinterpret(const char *str, arg_inter_base* fun) {
argument t = find_argument(str);
if (t.is_valid()) {
argument t2 = get_next_value(t);
if (t2.is_valid()) {
fun->operate(t2.arg);
release_argument(t);
release_argument(t2);
}
}
}
private:
char **argv;
int argc;
ui8 avail_store[16];//this should be enough for 128 command line arguments
ui8 *avail;
};
}
#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 &current)
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