]> git.ozlabs.org Git - ccan/blob - ccan/short_types/_info
Short types
[ccan] / ccan / short_types / _info
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.h"
4
5 /**
6  * short_types - shorter names for standard integer types
7  *
8  * "C is a Spartan language, and so should your naming be."
9  *      -- Linus Torvalds
10  *
11  * The short_types header provides for convenient abbreviations for the
12  * posixly-damned uint32_t types.  It also provides be32/le32 for explicitly
13  * annotating types of specific endian.
14  *
15  * Include this header, if only to stop people using these identifiers
16  * for other things!
17  *
18  * Example:
19  *      #include <stdint.h>
20  *      #include <string.h>
21  *      #include <stdio.h>
22  *      #include <ccan/short_types/short_types.h>
23  *      
24  *      // Print nonsensical numerical comparison of POSIX vs. short_types. 
25  *      #define stringify_1(x)  #x
26  *      #define stringify(x)    stringify_1(x)
27  *      
28  *      static void evaluate(size_t size, const char *posix, const char *sht,
29  *                           unsigned int *posix_total, unsigned int *sht_total,
30  *                           unsigned int *size_total)
31  *      {
32  *              printf("\t%ssigned %s: POSIX %i%%, short %i%%\n",
33  *                     sht[0] == 'u' ? "un" : "",
34  *                     sht+1,
35  *                     strlen(posix)*100 / size,
36  *                     strlen(sht)*100 / size);
37  *              *posix_total += strlen(posix);
38  *              *sht_total += strlen(sht);
39  *              *size_total += size;
40  *      }
41  *
42  *      #define EVALUATE(psx, short, pt, st, t)                         \
43  *              evaluate(sizeof(psx), stringify(psx), stringify(sht), pt, st, t)
44  *
45  *      int main(void)
46  *      {
47  *              unsigned int posix_total = 0, sht_total = 0, size_total = 0;
48  *
49  *              printf("Comparing size of type vs size of name:\n");
50  *
51  *              EVALUATE(uint8_t, u8, &posix_total, &sht_total, &size_total);
52  *              EVALUATE(int8_t, s8, &posix_total, &sht_total, &size_total);
53  *              EVALUATE(uint16_t, u16, &posix_total, &sht_total, &size_total);
54  *              EVALUATE(int16_t, s16, &posix_total, &sht_total, &size_total);
55  *              EVALUATE(uint32_t, u32, &posix_total, &sht_total, &size_total);
56  *              EVALUATE(int32_t, s32, &posix_total, &sht_total, &size_total);
57  *              EVALUATE(uint64_t, u64, &posix_total, &sht_total, &size_total);
58  *              EVALUATE(int64_t, s64, &posix_total, &sht_total, &size_total);
59  *
60  *              printf("Conclusion:\n"
61  *                     "\tPOSIX is %u%% LESS efficient than binary.\n"
62  *                     "\tshort_types.h is %u%% MORE efficient than binary.\n",
63  *                     (posix_total - size_total) * 100 / size_total,
64  *                     (size_total - sht_total) * 100 / size_total);
65  *              return 0;
66  *      }
67  *
68  * Licence: LGPL (2 or any later version)
69  */
70 int main(int argc, char *argv[])
71 {
72         if (argc != 2)
73                 return 1;
74
75         if (strcmp(argv[1], "depends") == 0) {
76                 return 0;
77         }
78
79         return 1;
80 }