adds a code sample for UcxMap

Sat, 12 May 2018 14:50:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Sat, 12 May 2018 14:50:09 +0200
changeset 298
fffe3a16a3de
parent 297
ba760f2195c3
child 299
e7dfcf229625

adds a code sample for UcxMap

docs/src/modules.md file | annotate | diff | comparison | revisions
     1.1 --- a/docs/src/modules.md	Sat May 12 14:13:53 2018 +0200
     1.2 +++ b/docs/src/modules.md	Sat May 12 14:50:09 2018 +0200
     1.3 @@ -279,6 +279,70 @@
     1.4  chaining with linked lists. Similarly to the list module, we provide a
     1.5  `UCX_MAP_FOREACH` macro to conveniently iterate through the key/value pairs.
     1.6  
     1.7 +### Parsing command line options
     1.8 +
     1.9 +Assume you want to parse command line options and record them within a map.
    1.10 +One way to do this is shown by the following code sample:
    1.11 +```C
    1.12 +    UcxMap* options = ucx_map_new(16);
    1.13 +    const char *NOARG = "";
    1.14 +    
    1.15 +    char *option = NULL;
    1.16 +    char optchar = 0;
    1.17 +    for(int i=1;i<argc;i++) {
    1.18 +        char *arg = argv[i];
    1.19 +        size_t len = strlen(arg);
    1.20 +        if(len > 1 && arg[0] == '-') {
    1.21 +            if(option) {
    1.22 +                fprintf(stderr,
    1.23 +                        "Missing argument for option -%c\n", optchar);
    1.24 +                return 1;
    1.25 +            }
    1.26 +            for(int c=1;c<len;c++) {
    1.27 +                switch(arg[c]) {
    1.28 +                    default: {
    1.29 +                        fprintf(stderr, "Unknown option -%c\n\n", arg[c]);
    1.30 +                        return 1;
    1.31 +                    }
    1.32 +                    case 'v': {
    1.33 +                        ucx_map_cstr_put(options, "verbose", NOARG);
    1.34 +                        break;
    1.35 +                    }
    1.36 +                    case 'o': {
    1.37 +                        option = "output";
    1.38 +                        optchar = 'o';
    1.39 +                        break;
    1.40 +                    }
    1.41 +                }
    1.42 +            }
    1.43 +        } else if(option) {
    1.44 +            ucx_map_cstr_put(options, option, arg);
    1.45 +            option = NULL;
    1.46 +        } else {
    1.47 +            /* ... handle argument that is not an option ... */
    1.48 +        }
    1.49 +    }
    1.50 +    if(option) {
    1.51 +        fprintf(stderr,
    1.52 +                "Missing argument for option -%c\n", optchar);
    1.53 +        return 1;
    1.54 +    }
    1.55 +```
    1.56 +With the following loop, you can access the previously recorded options:
    1.57 +```C
    1.58 +    UcxMapIterator iter = ucx_map_iterator(options);
    1.59 +    char *arg;
    1.60 +    UCX_MAP_FOREACH(optkey, arg, iter) {
    1.61 +        char* opt = optkey.data;
    1.62 +        if (*arg) {
    1.63 +            printf("%s = %s\n", opt, arg);
    1.64 +        } else {
    1.65 +            printf("%s active\n", opt);
    1.66 +        }
    1.67 +    }
    1.68 +```
    1.69 +Don't forget to call `ucx_map_free()`, when you are done with the map.
    1.70 +
    1.71  ## Memory Pool
    1.72  
    1.73  *Header file:* [mempool.h](api/mempool_8h.html)  

mercurial