_info 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <string.h>
  2. #include <stdio.h>
  3. /**
  4. * htable - hash table routines
  5. *
  6. * A hash table is an efficient structure for looking up keys. This version
  7. * grows with usage and allows efficient deletion.
  8. *
  9. * Example:
  10. * #include <ccan/htable/htable.h>
  11. * #include <ccan/hash/hash.h>
  12. * #include <stdio.h>
  13. * #include <err.h>
  14. * #include <string.h>
  15. *
  16. * struct name_to_digit {
  17. * const char *name;
  18. * unsigned int val;
  19. * };
  20. *
  21. * static struct name_to_digit map[] = {
  22. * { "zero", 0},
  23. * { "one", 1 },
  24. * { "two", 2 },
  25. * { "three", 3 },
  26. * { "four", 4 },
  27. * { "five", 5 },
  28. * { "six", 6 },
  29. * { "seven", 7 },
  30. * { "eight", 8 },
  31. * { "nine", 9 }
  32. * };
  33. *
  34. * // Wrapper for rehash function pointer.
  35. * static size_t rehash(const void *e, void *unused)
  36. * {
  37. * return hash_string(((struct name_to_digit *)e)->name);
  38. * }
  39. *
  40. * // Comparison function.
  41. * static bool streq(const void *e, void *string)
  42. * {
  43. * return strcmp(((struct name_to_digit *)e)->name, string) == 0;
  44. * }
  45. *
  46. * // We let them add their own aliases, eg. --alias=v=5
  47. * static void add_alias(struct htable *ht, const char *alias)
  48. * {
  49. * char *eq;
  50. * struct name_to_digit *n;
  51. *
  52. * n = malloc(sizeof(*n));
  53. * n->name = strdup(alias);
  54. *
  55. * eq = strchr(n->name, '=');
  56. * if (!eq || ((n->val = atoi(eq+1)) == 0 && !strcmp(eq+1, "0")))
  57. * errx(1, "Usage: --alias=<name>=<value>");
  58. * *eq = '\0';
  59. * htable_add(ht, hash_string(n->name), n);
  60. * }
  61. *
  62. * int main(int argc, char *argv[])
  63. * {
  64. * struct htable *ht;
  65. * unsigned int i;
  66. * unsigned long val;
  67. *
  68. * if (argc < 2)
  69. * errx(1, "Usage: %s [--alias=<name>=<val>]... <str>...",
  70. * argv[0]);
  71. *
  72. * // Create and populate hash table.
  73. * ht = htable_new(rehash, NULL);
  74. * for (i = 0; i < sizeof(map)/sizeof(map[0]); i++)
  75. * htable_add(ht, hash_string(map[i].name), &map[i]);
  76. *
  77. * // Add any aliases to the hash table.
  78. * for (i = 1; i < argc; i++) {
  79. * if (!strncmp(argv[i], "--alias=", strlen("--alias=")))
  80. * add_alias(ht, argv[i] + strlen("--alias="));
  81. * else
  82. * break;
  83. * }
  84. *
  85. * // Find the other args in the hash table.
  86. * for (val = 0; i < argc; i++) {
  87. * struct name_to_digit *n;
  88. * n = htable_get(ht, hash_string(argv[i]),
  89. * streq, argv[i]);
  90. * if (!n)
  91. * errx(1, "Invalid digit name %s", argv[i]);
  92. * // Append it to the value we are building up.
  93. * val *= 10;
  94. * val += n->val;
  95. * }
  96. * printf("%lu\n", val);
  97. * return 0;
  98. * }
  99. *
  100. * License: LGPL (v2.1 or any later version)
  101. * Author: Rusty Russell <rusty@rustcorp.com.au>
  102. */
  103. int main(int argc, char *argv[])
  104. {
  105. if (argc != 2)
  106. return 1;
  107. if (strcmp(argv[1], "depends") == 0) {
  108. printf("ccan/compiler\n");
  109. return 0;
  110. }
  111. return 1;
  112. }