_info 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "config.h"
  2. #include <string.h>
  3. #include <stdio.h>
  4. /**
  5. * ccan_tokenizer - A full-text lexer for C source files
  6. *
  7. * ccan_tokenizer generates a list of tokens given the contents of a C source
  8. * or header file.
  9. *
  10. * Example:
  11. *
  12. * #include <ccan/ccan_tokenizer/ccan_tokenizer.h>
  13. * #include <ccan/grab_file/grab_file.h>
  14. * #include <err.h>
  15. *
  16. * static void token_list_stats(const struct token_list *tl) {
  17. * size_t comment=0, white=0, stray=0, code=0, total=0;
  18. * size_t count = 0;
  19. * const struct token *i;
  20. *
  21. * for (i=tl->first; i; i=i->next) {
  22. * size_t size = i->orig_size;
  23. * total += size;
  24. * count++;
  25. *
  26. * if (token_type_is_comment(i->type))
  27. * comment += size;
  28. * else if (i->type == TOK_WHITE)
  29. * white += size;
  30. * else if (i->type == TOK_STRAY)
  31. * stray += size;
  32. * else
  33. * code += size;
  34. * }
  35. *
  36. * printf("Code: %.02f%%\n"
  37. * "White space: %.02f%%\n"
  38. * "Comments: %.02f%%\n",
  39. * (double)code * 100.0 / (double)total,
  40. * (double)white * 100.0 / (double)total,
  41. * (double)comment * 100.0 / (double)total);
  42. * if (stray)
  43. * printf("Stray: %.02f%%\n",
  44. * (double)stray * 100.0 / (double)total);
  45. * printf("Total size: %zu bytes with %zu tokens\n",
  46. * total, count);
  47. * }
  48. *
  49. * int main(int argc, char *argv[]) {
  50. * size_t len;
  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], &len);
  62. * if (!file)
  63. * err(1, "Could not read file %s", argv[1]);
  64. *
  65. * //tokenize the contents
  66. * tl = tokenize(file, file, len, &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. * Ccanlint:
  87. * // We actually depend on the LGPL dependencies
  88. * license_depends_compat FAIL
  89. * // We don't put the license line in all files.
  90. * license_comment FAIL
  91. */
  92. int main(int argc, char *argv[])
  93. {
  94. /* Expect exactly one argument */
  95. if (argc != 2)
  96. return 1;
  97. if (strcmp(argv[1], "depends") == 0) {
  98. printf("ccan/darray\n");
  99. printf("ccan/talloc\n");
  100. return 0;
  101. }
  102. return 1;
  103. }