_info 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "config.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. /**
  5. * cast - routines for safer casting.
  6. *
  7. * Often you want to cast in a limited way, such as removing a const or
  8. * switching between integer types. However, normal casts will work on
  9. * almost any type, making them dangerous when the code changes.
  10. *
  11. * These C++-inspired macros serve two purposes: they make it clear the
  12. * exact reason for the cast, and they also (with some compilers) cause
  13. * errors when misused.
  14. *
  15. * Based on Jan Engelhardt's libHX macros: http://libhx.sourceforge.net/
  16. *
  17. * Author: Jan Engelhardt
  18. * Maintainer: Rusty Russell <rusty@rustcorp.com.au>
  19. * License: LGPL (v2.1 or any later version)
  20. *
  21. * Example:
  22. * // Given "test" output contains "3 t's in 'test string'"
  23. * #include <ccan/cast/cast.h>
  24. * #include <stdint.h>
  25. * #include <stdio.h>
  26. * #include <stdlib.h>
  27. *
  28. * // Find char @orig in @str, if @repl, replace them. Return number.
  29. * static size_t find_chars(char *str, char orig, char repl)
  30. * {
  31. * size_t i, count = 0;
  32. * for (i = 0; str[i]; i++) {
  33. * if (str[i] == orig) {
  34. * count++;
  35. * if (repl)
  36. * str[i] = repl;
  37. * }
  38. * }
  39. * return count;
  40. * }
  41. *
  42. * // Terrible hash function.
  43. * static uint64_t hash_string(const unsigned char *str)
  44. * {
  45. * size_t i;
  46. * uint64_t hash = 0;
  47. * for (i = 0; str[i]; i++)
  48. * hash += str[i];
  49. * return hash;
  50. * }
  51. *
  52. * int main(int argc, char *argv[])
  53. * {
  54. * uint64_t hash;
  55. *
  56. * if (argc != 2) {
  57. * fprintf(stderr, "Needs argument\n"); exit(1);
  58. * }
  59. * // find_chars wants a non-const string, but doesn't
  60. * // need it if repl == 0.
  61. * printf("%zu %c's in 'test string'\n",
  62. * find_chars(cast_const(char *, "test string"),
  63. * argv[1][0], 0),
  64. * argv[1][0]);
  65. *
  66. * // hash_string wants an unsigned char.
  67. * hash = hash_string(cast_signed(unsigned char *, argv[1]));
  68. *
  69. * // Need a long long to hand to printf.
  70. * printf("Hash of '%s' = %llu\n", argv[1],
  71. * cast_static(unsigned long long, hash));
  72. * return 0;
  73. * }
  74. *
  75. */
  76. int main(int argc, char *argv[])
  77. {
  78. /* Expect exactly one argument */
  79. if (argc != 2)
  80. return 1;
  81. if (strcmp(argv[1], "depends") == 0) {
  82. printf("ccan/build_assert\n");
  83. return 0;
  84. }
  85. return 1;
  86. }