diff options
| -rw-r--r-- | docs/carray_api.md | 2 | ||||
| -rw-r--r-- | docs/coption_api.md | 49 | ||||
| -rw-r--r-- | examples/option_mkdir.c (renamed from examples/option.c) | 0 | ||||
| -rw-r--r-- | include/stc/carr2.h | 4 | ||||
| -rw-r--r-- | include/stc/carr3.h | 4 |
5 files changed, 28 insertions, 31 deletions
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 <stc/coption.h> #include <stdio.h> +#include <stc/coption.h> 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_mkdir.c index 308348b0..308348b0 100644 --- a/examples/option.c +++ b/examples/option_mkdir.c 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); |
