faux-file2c.c 7.0 KB

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