diff --git a/src/miniargs.cpp b/src/miniargs.cpp new file mode 100644 index 0000000..95a832a --- /dev/null +++ b/src/miniargs.cpp @@ -0,0 +1,54 @@ +#include +#include +#include "miniargs.hpp" +#include + +using namespace CosmoTool; +using namespace std; + +int CosmoTool::parseMiniArgs(int argc, char **argv, MiniArgDesc *desc) +{ + int numMini; + for (numMini = 0; desc[numMini].name != 0; numMini++); + + if ((argc-1) != numMini) + { + cerr << "Usage: "; + for (int i = 0; i < numMini; i++) + { + cerr << '[' << desc[i].name << "] "; + } + cerr << endl; + return 0; + } + + for (int i = 0; i < numMini; i++) + { + switch (desc[i].argType) + { + case MINIARG_STRING: + *((char **)desc[i].data) = strdup(argv[i+1]); + break; + case MINIARG_INT: + *((int *)desc[i].data) = strtol(argv[i+1], NULL, 0); + break; + case MINIARG_DOUBLE: + *((double *)desc[i].data) = strtod(argv[i+1], NULL); + break; + case MINIARG_FLOAT: + *((float *)desc[i].data) = strtod(argv[i+1], NULL); + break; + case MINIARG_DOUBLE_3D_VECTOR: + { + double *d_array = (double *)(desc[i].data); + + if (sscanf(argv[i+1], "(%lg,%lg,%lg)", &d_array[0], &d_array[1], &d_array[2]) != 3) + return 0; + break; + } + } + } + + return 1; +} + diff --git a/src/miniargs.hpp b/src/miniargs.hpp new file mode 100644 index 0000000..b746e55 --- /dev/null +++ b/src/miniargs.hpp @@ -0,0 +1,26 @@ +#ifndef _MAK_MINIARGS_HPP +#define _MAK_MINIARGS_HPP + +namespace CosmoTool +{ + typedef enum + { + MINIARG_NULL, + MINIARG_STRING, + MINIARG_INT, + MINIARG_DOUBLE, + MINIARG_FLOAT, + MINIARG_DOUBLE_3D_VECTOR + } MiniArgType; + + typedef struct + { + const char *name; + void *data; + MiniArgType argType; + } MiniArgDesc; + + int parseMiniArgs(int argc, char **argv, MiniArgDesc *desc); +}; + +#endif