From 835a3a974f4f79ca789d6fca6c9745b4f4931870 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 20 Nov 2021 08:36:02 +0100 Subject: Added carr_X_idx(). --- docs/carray_api.md | 2 ++ docs/coption_api.md | 49 ++++++++++---------------- examples/option.c | 94 ------------------------------------------------- examples/option_mkdir.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++ include/stc/carr2.h | 4 +++ include/stc/carr3.h | 4 +++ 6 files changed, 122 insertions(+), 125 deletions(-) delete mode 100644 examples/option.c create mode 100644 examples/option_mkdir.c diff --git a/docs/carray_api.md b/docs/carray_api.md index aa9ea681..eb5123b3 100644 --- a/docs/carray_api.md +++ b/docs/carray_api.md @@ -34,6 +34,7 @@ void carr2_X_del(carr2_X* self); size_t carr2_X_size(carr2_X arr); i_val* carr2_X_data(carr2_X* self); // access storage data const i_val* carr2_X_at(const carr2_X* self, size_t x, size_t y); +size_t carr2_X_idx(const carr2_X* self, size_t x, size_t y); carr2_X_iter carr2_X_begin(const carr2_X* self); carr2_X_iter carr2_X_end(const carr2_X* self); @@ -52,6 +53,7 @@ void carr3_X_del(carr3_X* self); size_t carr3_X_size(carr3_X arr); i_val* carr3_X_data(carr3_X* self); // storage data const i_val* carr3_X_at(const carr3_X* self, size_t x, size_t y, size_t z); +size_t carr3_X_idx(const carr3_X* self, size_t x, size_t y, size_t z); carr3_X_iter carr3_X_begin(const carr3_X* self); carr3_X_iter carr3_X_end(const carr3_X* self); diff --git a/docs/coption_api.md b/docs/coption_api.md index b7eebc43..be0d0978 100644 --- a/docs/coption_api.md +++ b/docs/coption_api.md @@ -39,46 +39,33 @@ int coption_get(coption *opt, int argc, char *argv[], ## Example ```c -#include #include +#include int main(int argc, char *argv[]) { - static coption_long long_options[] = { - {"verbose", coption_no_argument, 'V'}, - {"help", coption_no_argument, 'H'}, - {"add", coption_no_argument, 'a'}, - {"append", coption_no_argument, 'b'}, - {"delete", coption_required_argument, 'd'}, - {"create", coption_required_argument, 'c'}, - {"file", coption_required_argument, 'f'}, - {NULL} + coption_long longopts[] = { + {"foo", coption_no_argument, 'f'}, + {"bar", coption_required_argument, 'b'}, + {"opt", coption_optional_argument, 'o'}, + {0} }; + const char* shortopts = "xy:z::123"; + if (argc == 1) + printf("Usage: program -x -y ARG -z [ARG] -1 -2 -3 --foo --bar ARG --opt [ARG] [ARGUMENTS]\n", argv[0]); coption opt = coption_init(); int c; - while ((c = coption_get(&opt, argc, argv, ":if:lr", long_options)) != -1) { + while ((c = coption_get(&opt, argc, argv, shortopts, longopts)) != -1) { switch (c) { - case 'V': case 'H': - case 'a': case 'b': - case 'd': case 'c': - case 'i': case 'l': - case 'r': - printf("option: %c\n", c); - break; - case 'f': - printf("filename: %s\n", opt.arg); - break; - case ':': - printf("option %s needs a value\n", opt.optstr); - break; - case '?': - printf("unknown option: %s\n", opt.optstr); - break; + case '?': printf("error: unknown option: %s\n", opt.optstr); break; + case ':': printf("error: missing argument for %s\n", opt.optstr); break; + default: printf("option: %c [%s]\n", opt.opt, opt.arg ? opt.arg : ""); break; } } - - for (; opt.ind < argc; ++opt.ind) { - printf("extra arguments: %s\n", argv[opt.ind]); - } + printf("\nNon-option arguments:"); + for (int i = opt.ind; i < argc; ++i) + printf(" %s", argv[i]); + putchar('\n'); +} return 0; } ``` diff --git a/examples/option.c b/examples/option.c deleted file mode 100644 index 308348b0..00000000 --- a/examples/option.c +++ /dev/null @@ -1,94 +0,0 @@ -#include -#include - -// mkdir option parsing. -struct AppArgs { - const char *mode; - bool parent; - bool verbose; - const char *context; - int index; -}; - -int parseArgs(int argc, char *argv[], struct AppArgs *args) -{ - const char *shortopts = "pvm:Z"; - coption_long longopts[] = { - {"mode", coption_required_argument, 'm'}, - {"parent", coption_no_argument, 'p'}, - {"verbose", coption_no_argument, 'v'}, - {"context", coption_optional_argument, 'Z'}, - {"help", coption_no_argument, 'H'}, - {"version", coption_no_argument, 'V'}, - {NULL} - }; - *args = (struct AppArgs){NULL}; - coption opt = coption_init(); - int c; - while ((c = coption_get(&opt, argc, argv, shortopts, longopts)) != -1) { - switch (c) { - case 'm': args->mode = opt.arg; break; - case 'p': args->parent = true; break; - case 'v': args->verbose = true; break; - case 'Z': args->context = opt.arg? opt.arg : "DEFAULT"; break; - case 'H': - printf( "Usage: mkdir [OPTION]... DIRECTORY...\n" - "Create the DIRECTORY(ies), if they do not already exist.\n\n" - "Mandatory arguments to long options are mandatory for short options too.\n" - " -m, --mode=MODE set file mode (as in chmod), not a=rwx - umask\n" - " -p, --parents no error if existing, make parent directories as needed\n" - " -v, --verbose print a message for each created directory\n" - " -Z set SELinux security context of each created directory\n" - " to the default type\n" - " --context[=CTX] like -Z, or if CTX is specified then set the SELinux\n" - " or SMACK security context to CTX\n" - " --help display this help and exit\n" - " --version output version information and exit\n\n" - "GNU coreutils online help: \n" - "Full documentation at: \n" - "or available locally via: info '(coreutils) mkdir invocation'\n" ); - return 1; - case 'V': - printf( "mkdir (GNU coreutils) 8.30\n" - "Copyright (C) 2018 Free Software Foundation, Inc.\n" - "License GPLv3+: GNU GPL version 3 or later .\n" - "This is free software: you are free to change and redistribute it.\n" - "There is NO WARRANTY, to the extent permitted by law.\n" ); - return 1; - case ':': - printf("mkdir: option '%s' requires an argument\n" - "Try 'mkdir --help' for more information.\n", opt.optstr); - return -2; - case '?': - printf("mkdir: invalid option '%s'\n" - "Try 'mkdir --help' for more information.\n", opt.optstr); - return -3; - } - } - - if (opt.ind == argc) { - printf("mkdir: missing operand\n" - "Try 'mkdir --help' for more information.\n"); - return -1; - } - args->index = opt.ind; - return 0; -} - - -int main(int argc, char* argv[]) -{ - struct AppArgs args; - int ret = parseArgs(argc, argv, &args); - if (ret != 0) - return ret < 0? ret : 0; - - printf("mkdir:"); - for (int i=args.index; i < argc; ++i) { - printf(" %s\n", argv[i]); - } - printf(" mode:%s\n parent:%d\n verbose:%d\n context:%s\n", - args.mode? args.mode:"", - args.parent, args.verbose, - args.context? args.context:""); -} \ No newline at end of file diff --git a/examples/option_mkdir.c b/examples/option_mkdir.c new file mode 100644 index 00000000..308348b0 --- /dev/null +++ b/examples/option_mkdir.c @@ -0,0 +1,94 @@ +#include +#include + +// mkdir option parsing. +struct AppArgs { + const char *mode; + bool parent; + bool verbose; + const char *context; + int index; +}; + +int parseArgs(int argc, char *argv[], struct AppArgs *args) +{ + const char *shortopts = "pvm:Z"; + coption_long longopts[] = { + {"mode", coption_required_argument, 'm'}, + {"parent", coption_no_argument, 'p'}, + {"verbose", coption_no_argument, 'v'}, + {"context", coption_optional_argument, 'Z'}, + {"help", coption_no_argument, 'H'}, + {"version", coption_no_argument, 'V'}, + {NULL} + }; + *args = (struct AppArgs){NULL}; + coption opt = coption_init(); + int c; + while ((c = coption_get(&opt, argc, argv, shortopts, longopts)) != -1) { + switch (c) { + case 'm': args->mode = opt.arg; break; + case 'p': args->parent = true; break; + case 'v': args->verbose = true; break; + case 'Z': args->context = opt.arg? opt.arg : "DEFAULT"; break; + case 'H': + printf( "Usage: mkdir [OPTION]... DIRECTORY...\n" + "Create the DIRECTORY(ies), if they do not already exist.\n\n" + "Mandatory arguments to long options are mandatory for short options too.\n" + " -m, --mode=MODE set file mode (as in chmod), not a=rwx - umask\n" + " -p, --parents no error if existing, make parent directories as needed\n" + " -v, --verbose print a message for each created directory\n" + " -Z set SELinux security context of each created directory\n" + " to the default type\n" + " --context[=CTX] like -Z, or if CTX is specified then set the SELinux\n" + " or SMACK security context to CTX\n" + " --help display this help and exit\n" + " --version output version information and exit\n\n" + "GNU coreutils online help: \n" + "Full documentation at: \n" + "or available locally via: info '(coreutils) mkdir invocation'\n" ); + return 1; + case 'V': + printf( "mkdir (GNU coreutils) 8.30\n" + "Copyright (C) 2018 Free Software Foundation, Inc.\n" + "License GPLv3+: GNU GPL version 3 or later .\n" + "This is free software: you are free to change and redistribute it.\n" + "There is NO WARRANTY, to the extent permitted by law.\n" ); + return 1; + case ':': + printf("mkdir: option '%s' requires an argument\n" + "Try 'mkdir --help' for more information.\n", opt.optstr); + return -2; + case '?': + printf("mkdir: invalid option '%s'\n" + "Try 'mkdir --help' for more information.\n", opt.optstr); + return -3; + } + } + + if (opt.ind == argc) { + printf("mkdir: missing operand\n" + "Try 'mkdir --help' for more information.\n"); + return -1; + } + args->index = opt.ind; + return 0; +} + + +int main(int argc, char* argv[]) +{ + struct AppArgs args; + int ret = parseArgs(argc, argv, &args); + if (ret != 0) + return ret < 0? ret : 0; + + printf("mkdir:"); + for (int i=args.index; i < argc; ++i) { + printf(" %s\n", argv[i]); + } + printf(" mode:%s\n parent:%d\n verbose:%d\n context:%s\n", + args.mode? args.mode:"", + args.parent, args.verbose, + args.context? args.context:""); +} \ No newline at end of file diff --git a/include/stc/carr2.h b/include/stc/carr2.h index b961c47e..cda5a3b0 100644 --- a/include/stc/carr2.h +++ b/include/stc/carr2.h @@ -81,6 +81,10 @@ STC_INLINE const _cx_value *_cx_memb(_at)(const _cx_self* self, size_t x, size_t return *self->data + self->ydim*x + y; } +STC_INLINE size_t _cx_memb(_idx)(const _cx_self* self, size_t x, size_t y) { + return self->ydim*x + y; +} + STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->data == other.data) return; _cx_memb(_del)(self); *self = _cx_memb(_clone)(other); diff --git a/include/stc/carr3.h b/include/stc/carr3.h index 3f230204..d8c79c64 100644 --- a/include/stc/carr3.h +++ b/include/stc/carr3.h @@ -83,6 +83,10 @@ STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t x, size_t return **self->data + self->zdim*(self->ydim*x + y) + z; } +STC_INLINE size_t _cx_memb(_idx)(const _cx_self* self, size_t x, size_t y, size_t z) { + return self->zdim*(self->ydim*x + y) + z; +} + STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->data == other.data) return; _cx_memb(_del)(self); *self = _cx_memb(_clone)(other); -- cgit v1.2.3