]> git.ozlabs.org Git - ccan/blob - ccan/ccan_tokenizer/_info
base64: fix for unsigned chars (e.g. ARM).
[ccan] / ccan / ccan_tokenizer / _info
1 #include "config.h"
2 #include <string.h>
3 #include <stdio.h>
4
5 /**
6  * ccan_tokenizer - A full-text lexer for C source files
7  *
8  * ccan_tokenizer generates a list of tokens given the contents of a C source
9  * or header file.
10  *
11  * Example:
12  *
13  * #include <ccan/ccan_tokenizer/ccan_tokenizer.h>
14  * #include <ccan/tal/grab_file/grab_file.h>
15  * #include <err.h>
16  *
17  * static void token_list_stats(const struct token_list *tl) {
18  *      size_t comment=0, white=0, stray=0, code=0, total=0;
19  *      size_t count = 0;
20  *      const struct token *i;
21  *
22  *      for (i=tl->first; i; i=i->next) {
23  *              size_t size = i->orig_size;
24  *              total += size;
25  *              count++;
26  *
27  *              if (token_type_is_comment(i->type))
28  *                      comment += size;
29  *              else if (i->type == TOK_WHITE)
30  *                      white += size;
31  *              else if (i->type == TOK_STRAY)
32  *                      stray += size;
33  *              else
34  *                      code += size;
35  *      }
36  *
37  *      printf("Code:        %.02f%%\n"
38  *             "White space: %.02f%%\n"
39  *             "Comments:    %.02f%%\n",
40  *             (double)code    * 100.0 / (double)total,
41  *             (double)white   * 100.0 / (double)total,
42  *             (double)comment * 100.0 / (double)total);
43  *      if (stray)
44  *              printf("Stray:       %.02f%%\n",
45  *                      (double)stray * 100.0 / (double)total);
46  *      printf("Total size:  %zu bytes with %zu tokens\n",
47  *              total, count);
48  * }
49  *
50  * int main(int argc, char *argv[]) {
51  *      char *file;
52  *      struct token_list *tl;
53  *      tok_message_queue mq;
54  *      queue_init(mq, NULL);
55  *
56  *      //grab the file
57  *      if (argc != 2) {
58  *              fprintf(stderr, "Usage: %s source_file\n", argv[0]);
59  *              return 1;
60  *      }
61  *      file = grab_file(NULL, argv[1]);
62  *      if (!file)
63  *              err(1, "Could not read file %s", argv[1]);
64  *
65  *      //tokenize the contents
66  *      tl = tokenize(file, file, strlen(file), &mq);
67  *
68  *      //print warnings, errors, etc.
69  *      while (queue_count(mq)) {
70  *              struct tok_message msg = dequeue(mq);
71  *              tok_message_print(&msg, tl);
72  *      }
73  *
74  *      //do neat stuff with the token list
75  *      token_list_stats(tl);
76  *
77  *      //free stuff
78  *      talloc_free(file); //implicitly frees tl
79  *      queue_free(mq);
80  *
81  *      return 0;
82  * }
83  *
84  * License: BSD (3 clause)
85  */
86 int main(int argc, char *argv[])
87 {
88         /* Expect exactly one argument */
89         if (argc != 2)
90                 return 1;
91
92         if (strcmp(argv[1], "depends") == 0) {
93                 printf("ccan/darray\n");
94                 printf("ccan/talloc\n");
95                 return 0;
96         }
97
98         if (strcmp(argv[1], "ccanlint") == 0) {
99                 /* We actually depend on the LGPL dependencies */
100                 printf("license_depends_compat FAIL\n");
101                 /* We don't put the license line in all files. */
102                 printf("license_comment FAIL\n");
103                 return 0;
104         }
105
106         return 1;
107 }