faux-file2c.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif /* HAVE_CONFIG_H */
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <dlfcn.h>
  11. #include <sys/types.h>
  12. #include <sys/wait.h>
  13. #include <sys/uio.h>
  14. #include <errno.h>
  15. #if WITH_INTERNAL_GETOPT
  16. #include "libc/getopt.h"
  17. #else
  18. #ifdef HAVE_GETOPT_H
  19. #include <getopt.h>
  20. #endif
  21. #endif
  22. #if HAVE_LOCALE_H
  23. #include <locale.h>
  24. #endif
  25. #if HAVE_LANGINFO_CODESET
  26. #include <langinfo.h>
  27. #endif
  28. #include <faux/faux.h>
  29. #include <faux/str.h>
  30. #include <faux/list.h>
  31. #include <faux/file.h>
  32. #ifndef VERSION
  33. #define VERSION 1.0.0
  34. #endif
  35. #define QUOTE(t) #t
  36. #define version(v) printf("%s\n", v)
  37. // Command line options */
  38. struct opts_s {
  39. int debug;
  40. faux_list_t *file_list;
  41. };
  42. typedef struct opts_s opts_t;
  43. static opts_t *opts_parse(int argc, char *argv[]);
  44. static void opts_free(opts_t *opts);
  45. static void help(int status, const char *argv0);
  46. int main(int argc, char *argv[]) {
  47. opts_t *opts = NULL; // Command line options
  48. faux_list_node_t *iter = NULL;
  49. char *fn = NULL; // Text file
  50. unsigned int total_errors = 0; // Sum of all errors
  51. unsigned int file_num = 0; // Number of file
  52. #if HAVE_LOCALE_H
  53. // Set current locale
  54. setlocale(LC_ALL, "");
  55. #endif
  56. // Parse command line options
  57. opts = opts_parse(argc, argv);
  58. if (!opts) {
  59. fprintf(stderr, "Error: Can't parse command line options\n");
  60. return -1;
  61. }
  62. // Main loop. Iterate through the list of shared objects
  63. iter = faux_list_head(opts->file_list);
  64. while ((fn = faux_list_each(&iter))) {
  65. faux_file_t *f = NULL;
  66. char *buf = NULL;
  67. bool_t eof = BOOL_FALSE;
  68. unsigned int line_num = 0;
  69. file_num++;
  70. f = faux_file_open(fn, O_RDONLY, 0);
  71. if (!f) {
  72. fprintf(stderr, "Error: Can't open file \"%s\"\n", fn);
  73. total_errors++;
  74. continue;
  75. }
  76. printf("\n");
  77. printf("// File \"%s\"\n", fn);
  78. printf("const char *txt%u =\n", file_num);
  79. while ((buf = faux_file_getline_raw(f))) {
  80. char *escaped_str = NULL;
  81. line_num++;
  82. escaped_str = faux_str_c_esc(buf);
  83. faux_str_free(buf);
  84. if (escaped_str)
  85. printf("\t\"%s\"\n", escaped_str);
  86. faux_str_free(escaped_str);
  87. }
  88. eof = faux_file_eof(f);
  89. if (!eof) { // File reading was interrupted before EOF
  90. fprintf(stderr, "Error: File \"%s\" reading was "
  91. "interrupted before EOF\n", fn);
  92. total_errors++;
  93. } // Continue normal operations
  94. if (0 == line_num) // Empty file is not error
  95. printf("\t\"\"\n");
  96. printf(";\n");
  97. faux_file_close(f);
  98. }
  99. opts_free(opts);
  100. if (total_errors > 0)
  101. return -1;
  102. return 0;
  103. }
  104. /** @brief Frees allocated opts_t structure
  105. *
  106. * @param [in] opts Allocated opts_t structure.
  107. */
  108. static void opts_free(opts_t *opts) {
  109. assert(opts);
  110. if (!opts)
  111. return;
  112. faux_list_free(opts->file_list);
  113. faux_free(opts);
  114. }
  115. /** @brief Allocates new opts_t structure
  116. *
  117. * Allocates structure that stores parse command line options.
  118. *
  119. * @return Allocated and initialized opts_t structure.
  120. * @warning The returned opts_t structure must be freed later by opts_free().
  121. */
  122. static opts_t *opts_new(void) {
  123. opts_t *opts = NULL;
  124. opts = faux_zmalloc(sizeof(*opts));
  125. assert(opts);
  126. if (!opts)
  127. return NULL;
  128. opts->debug = BOOL_FALSE;
  129. // Members of list are static strings from argv so don't free() it
  130. opts->file_list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  131. (faux_list_cmp_fn)strcmp, NULL, NULL);
  132. if (!opts->file_list) {
  133. opts_free(opts);
  134. return NULL;
  135. }
  136. return opts;
  137. }
  138. /** @brief Parse command line options
  139. *
  140. * Function allocates opts_t structure, parses command line options and
  141. * fills opts_t structure.
  142. *
  143. * @param [in] argc Standard argc argument.
  144. * @param [in] argv Standard argv argument.
  145. * @return Filled opts_t structure with parsed command line options.
  146. * @warning The returned opts_t structure must be freed later by opts_free().
  147. */
  148. static opts_t *opts_parse(int argc, char *argv[]) {
  149. opts_t *opts = NULL;
  150. static const char *shortopts = "hvd";
  151. #ifdef HAVE_GETOPT_LONG
  152. static const struct option longopts[] = {
  153. {"help", 0, NULL, 'h'},
  154. {"version", 0, NULL, 'v'},
  155. {"debug", 0, NULL, 'd'},
  156. {NULL, 0, NULL, 0}
  157. };
  158. #endif
  159. opts = opts_new();
  160. if (!opts)
  161. return NULL;
  162. optind = 1;
  163. while (1) {
  164. int opt;
  165. #ifdef HAVE_GETOPT_LONG
  166. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  167. #else
  168. opt = getopt(argc, argv, shortopts);
  169. #endif
  170. if (-1 == opt)
  171. break;
  172. switch (opt) {
  173. case 'd':
  174. opts->debug = BOOL_TRUE;
  175. break;
  176. case 'h':
  177. help(0, argv[0]);
  178. exit(0);
  179. break;
  180. case 'v':
  181. version(VERSION);
  182. exit(0);
  183. break;
  184. default:
  185. help(-1, argv[0]);
  186. exit(-1);
  187. break;
  188. }
  189. }
  190. if (optind < argc) {
  191. int i = 0;
  192. for (i = optind; i < argc; i++)
  193. faux_list_add(opts->file_list, argv[i]);
  194. } else {
  195. help(-1, argv[0]);
  196. exit(-1);
  197. }
  198. return opts;
  199. }
  200. /** @brief Prints help
  201. *
  202. * @param [in] status If status is not '0' consider help printing as a reaction
  203. * to error and print appropriate message. If status is '0' then print general
  204. * help information.
  205. * @param [in] argv0 The argv[0] argument i.e. programm name
  206. */
  207. static void help(int status, const char *argv0) {
  208. const char *name = NULL;
  209. if (!argv0)
  210. return;
  211. // Find the basename
  212. name = strrchr(argv0, '/');
  213. if (name)
  214. name++;
  215. else
  216. name = argv0;
  217. if (status != 0) {
  218. fprintf(stderr, "Try `%s -h' for more information.\n",
  219. name);
  220. } else {
  221. printf("Usage: %s [options] <txt_file> [txt_file] ...\n", name);
  222. printf("Converts text files to C-strings.\n");
  223. printf("Options:\n");
  224. printf("\t-v, --version\tPrint version.\n");
  225. printf("\t-h, --help\tPrint this help.\n");
  226. printf("\t-d, --debug\tDebug mode.\n");
  227. }
  228. }