_info 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "config.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. /**
  5. * jmap - map from indices to values (based on libJudy)
  6. *
  7. * This provides a convenient wrapper for using JudyL arrays; using
  8. * integers or pointers as an index, Judy arrays provide an efficient
  9. * map to integers or pointers.
  10. *
  11. * You define a struct for your particular index and value types using
  12. * the JMAP_MEMBERS macro, then use the jmap routines to manipulate
  13. * the mapping.
  14. *
  15. * Note: if you use an integer type for the index or value types and
  16. * your compiler doesn't support "typeof", you will get warnings about
  17. * mixing pointers and integers.
  18. *
  19. * Example:
  20. * // Silly example of associating data with arguments by pointer and int.
  21. * #include <string.h>
  22. * #include <stdio.h>
  23. * #include <ccan/jmap/jmap.h>
  24. *
  25. * struct opt_detail {
  26. * bool is_long;
  27. * size_t length; // == 1 if !is_long.
  28. * };
  29. *
  30. * // Define map type for int -> argv.
  31. * struct arg_map {
  32. * JMAP_MEMBERS(int, char *);
  33. * };
  34. * // Define map type for argv -> struct opt_detail *.
  35. * struct opt_map {
  36. * JMAP_MEMBERS(char *, struct opt_detail *);
  37. * };
  38. *
  39. * int main(int argc, char *argv[])
  40. * {
  41. * int i;
  42. * // This map is equivalent to the argv[] array. Silly example.
  43. * struct arg_map *arg = jmap_new(struct arg_map);
  44. * struct opt_map *opt = jmap_new(struct opt_map);
  45. * struct opt_detail *d;
  46. *
  47. * // Note: this is not correct for real parsing!
  48. * for (i = 1; i < argc; i++) {
  49. * jmap_add(arg, i, argv[i]);
  50. * if (argv[i][0] != '-')
  51. * continue;
  52. * d = malloc(sizeof(*d));
  53. * if (argv[i][1] == '-') {
  54. * // --<stuff>
  55. * d->is_long = true;
  56. * d->length = strlen(argv[i]+2);
  57. * } else {
  58. * // -<opt1>
  59. * d->is_long = false;
  60. * d->length = 1;
  61. * }
  62. * jmap_add(opt, argv[i], d);
  63. * }
  64. *
  65. * printf("Found %lu options:\n", jmap_count(opt));
  66. * for (i = jmap_first(arg); i; i = jmap_next(arg,i)) {
  67. * char *a = jmap_get(arg, i);
  68. * d = jmap_get(opt, a);
  69. * printf(" Arg %i ('%s') is a %s of %zu chars\n",
  70. * i, a,
  71. * d == NULL ? "normal arg"
  72. * : d->is_long ? "long opt"
  73. * : "short opt",
  74. * d == NULL ? strlen(a) : d->length);
  75. * // We no longer need it, so free it here.
  76. * free(d);
  77. * }
  78. * jmap_free(opt);
  79. * jmap_free(arg);
  80. * return 0;
  81. * }
  82. * // Given "--help" output contains "Arg 1 ('--help') is a long opt of 4 chars"
  83. * // Given "-h" output contains "Arg 1 ('-h') is a short opt of 1 chars"
  84. * // Given "foo" output contains "Arg 1 ('foo') is a normal arg of 3 chars"
  85. *
  86. * License: LGPL (v2.1 or any later version)
  87. * Author: Rusty Russell <rusty@rustcorp.com.au>
  88. */
  89. int main(int argc, char *argv[])
  90. {
  91. if (argc != 2)
  92. return 1;
  93. if (strcmp(argv[1], "depends") == 0) {
  94. printf("ccan/build_assert\n");
  95. printf("ccan/compiler\n");
  96. printf("ccan/tcon\n");
  97. printf("Judy\n");
  98. return 0;
  99. }
  100. if (strcmp(argv[1], "libs") == 0) {
  101. printf("Judy\n");
  102. return 0;
  103. }
  104. return 1;
  105. }