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