]> git.ozlabs.org Git - ccan/blob - _info
ad01ceae846bc3cb2518a15ee55f733ea0ede16d
[ccan] / _info
1 #include <string.h>
2 #include <stdio.h>
3 #include "config.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/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  *      size_t len;
52  *      char *file;
53  *      struct token_list *tl;
54  *      tok_message_queue mq;
55  *      queue_init(mq, NULL);
56  *
57  *      //grab the file
58  *      if (argc != 2) {
59  *              fprintf(stderr, "Usage: %s source_file\n", argv[0]);
60  *              return 1;
61  *      }
62  *      file = grab_file(NULL, argv[1], &len);
63  *      if (!file)
64  *              err(1, "Could not read file %s", argv[1]);
65  *
66  *      //tokenize the contents
67  *      tl = tokenize(file, file, len, &mq);
68  *
69  *      //print warnings, errors, etc.
70  *      while (queue_count(mq)) {
71  *              struct tok_message msg = dequeue(mq);
72  *              tok_message_print(&msg, tl);
73  *      }
74  *
75  *      //do neat stuff with the token list
76  *      token_list_stats(tl);
77  *
78  *      //free stuff
79  *      talloc_free(file); //implicitly frees tl
80  *      queue_free(mq);
81  *
82  *      return 0;
83  * }
84  *
85  * License: BSD (3 clause)
86  *
87  * Ccanlint:
88  *      // We actually depend on the LGPL dependencies
89  *      license_depends_compat FAIL
90  *      // We don't put the license line in all files.
91  *      license_comment FAIL
92  */
93 int main(int argc, char *argv[])
94 {
95         /* Expect exactly one argument */
96         if (argc != 2)
97                 return 1;
98
99         if (strcmp(argv[1], "depends") == 0) {
100                 printf("ccan/darray\n");
101                 printf("ccan/talloc\n");
102                 return 0;
103         }
104
105         return 1;
106 }