_info 2.8 KB

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