faux-file2c.c 5.9 KB

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