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