utf8filenamecheck.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * utf8filenamecheck Check if all the filenames in a fiven folder are UTF-8
  3. * Copyright (C) 2023 Johannes 'Banana' Keßler
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * 2023 Small windows C tool to check if paths in a folder are utf-8 formatted
  20. * This is the windows version
  21. */
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. // https://linux.die.net/man/3/nftw
  27. #define _XOPEN_SOURCE 500
  28. #include <ftw.h>
  29. // https://www.argtable.org
  30. #include <argtable3.h>
  31. // https://github.com/simdutf/simdutf
  32. #include <simdutf8check.h>
  33. /**
  34. * global arg_xxx structs
  35. * https://www.argtable.org/
  36. */
  37. struct arg_lit *verbose, *quiet, *help;
  38. struct arg_file *folderToRead;
  39. struct arg_end *end;
  40. const char *program_version = "1.0";
  41. const char *program_bug_address = "https://://www.bananas-playground.net";
  42. struct cmdArguments {
  43. int quiet, verbose;
  44. char *folder_to_read;
  45. };
  46. struct cmdArguments arguments;
  47. /**
  48. * the callback function for nftw
  49. * https://linux.die.net/man/3/nftw
  50. */
  51. static int nftw_callback(const char *fpath,
  52. const struct stat *sb,
  53. int tflag,
  54. struct FTW *ftwbuf) {
  55. if (strcmp(fpath, ".") == 0 || strcmp(fpath, "..") == 0) {
  56. return 0;
  57. }
  58. if(tflag == FTW_DNR) {
  59. if(!arguments.quiet) printf("Can not read %s", fpath);
  60. }
  61. bool result = validate_utf8_fast(fpath, strlen(fpath));
  62. if(result) {
  63. if(!arguments.quiet) printf("%s Valid OK \n", fpath);
  64. } else {
  65. printf("%s Valid FAILED \n", fpath);
  66. }
  67. // continue
  68. return 0;
  69. }
  70. /**
  71. * the main stuff
  72. */
  73. int main(int argc, char *argv[]) {
  74. /**
  75. * command line argument default values
  76. */
  77. arguments.quiet = 0;
  78. arguments.verbose = 0;
  79. arguments.folder_to_read = ".";
  80. /**
  81. * https://www.argtable.org/
  82. */
  83. void *argtable[] = {
  84. help = arg_litn(NULL, "help", 0, 1, "Display this help and exit"),
  85. quiet = arg_litn("q", "quiet", 0, 1, "Display only false ones"),
  86. verbose = arg_litn("v", "verbose", 0, 1, "Verbose additional output"),
  87. folderToRead = arg_filen(NULL, NULL, "<folder>", 1, 1, "Folder to read"),
  88. end = arg_end(20),
  89. };
  90. /* argtable parsing */
  91. int nerrors;
  92. nerrors = arg_parse(argc,argv,argtable);
  93. /* special case: '--help' takes precedence over error reporting */
  94. if (help->count > 0) {
  95. printf("Usage: utf8check.exe");
  96. arg_print_syntax(stdout, argtable, "\n");
  97. arg_print_glossary(stdout, argtable, " %-25s %s\n");
  98. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  99. return(1);
  100. }
  101. /* If the parser returned any errors then display them and exit */
  102. if (nerrors > 0) {
  103. /* Display the error details contained in the arg_end struct.*/
  104. arg_print_errors(stdout, end, "utf8check.exe");
  105. printf("Try '%s --help' for more information.\n", "utf8check.exe");
  106. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  107. return(1);
  108. }
  109. else {
  110. arguments.quiet = quiet->count;
  111. arguments.verbose = verbose->count;
  112. arguments.folder_to_read = folderToRead->filename[0];
  113. }
  114. if(arguments.verbose) {
  115. printf ("Folder = %s\n"
  116. "Verbose = %s\n"
  117. "Quiet = %s\n\n",
  118. arguments.folder_to_read,
  119. arguments.verbose ? "yes" : "no",
  120. arguments.quiet ? "yes" : "no"
  121. );
  122. }
  123. if (nftw(arguments.folder_to_read, nftw_callback, 15, FTW_PHYS)== -1) {
  124. perror("Reading dir failed");
  125. exit(EXIT_FAILURE);
  126. }
  127. arg_freetable(argtable, sizeof(argtable) / sizeof(argtable[0]));
  128. exit(EXIT_SUCCESS);
  129. }