]> git.ozlabs.org Git - ccan/blob - ccan/cast/_info
isaac, crcsync: acknowledge licensing issues.
[ccan] / ccan / cast / _info
1 #include <string.h>
2 #include "config.h"
3
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
20  *
21  * Example:
22  *      // Given "test" contains "3 t's in 'test string'
23  *      #include <ccan/cast/cast.h>
24  *      #include <stdint.h>
25  *      #include <stdio.h>
26  *
27  *      // Find char @orig in @str, if @repl, replace them.  Return number.
28  *      static size_t find_chars(char *str, char orig, char repl)
29  *      {
30  *              size_t i, count = 0;
31  *              for (i = 0; str[i]; i++) {
32  *                      if (str[i] == orig) {
33  *                              count++;
34  *                              if (repl)
35  *                                      str[i] = repl;
36  *                      }
37  *              }
38  *              return count;
39  *      }
40  *
41  *      // Terrible hash function.
42  *      static uint64_t hash_string(const unsigned char *str)
43  *      {
44  *              size_t i;
45  *              uint64_t hash = 0;
46  *              for (i = 0; str[i]; i++)
47  *                      hash += str[i];
48  *              return hash;
49  *      }
50  *
51  *      int main(int argc, char *argv[])
52  *      {
53  *              uint64_t hash;
54  *
55  *              // find_chars wants a non-const string, but doesn't
56  *              // need it if repl == 0.
57  *              printf("%zu %c's in 'test string'\n",
58  *                     find_chars(cast_const(char *, "test string"),
59  *                                argv[1][0], 0),
60  *                     argv[1][0]);
61  *
62  *              // hash_string wants an unsigned char.
63  *              hash = hash_string(cast_signed(unsigned char *, argv[1]));
64  *
65  *              // Need a long long to hand to printf.
66  *              printf("Hash of '%s' = %llu\n", argv[1],
67  *                     cast_static(unsigned long long, hash));
68  *              return 0;
69  *      }
70  *              
71  */
72 int main(int argc, char *argv[])
73 {
74         /* Expect exactly one argument */
75         if (argc != 2)
76                 return 1;
77
78         if (strcmp(argv[1], "depends") == 0) {
79                 printf("ccan/build_assert\n");
80                 return 0;
81         }
82
83         return 1;
84 }