123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- #ifdef HAVE_CONFIG_H
- #include "config.h"
- #endif /* HAVE_CONFIG_H */
- #include <stdlib.h>
- #include <stdio.h>
- #include <assert.h>
- #include <string.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <dlfcn.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/uio.h>
- #include <errno.h>
- #if WITH_INTERNAL_GETOPT
- #include "libc/getopt.h"
- #else
- #ifdef HAVE_GETOPT_H
- #include <getopt.h>
- #endif
- #endif
- #if HAVE_LOCALE_H
- #include <locale.h>
- #endif
- #if HAVE_LANGINFO_CODESET
- #include <langinfo.h>
- #endif
- #include <faux/faux.h>
- #include <faux/str.h>
- #include <faux/list.h>
- #include <faux/file.h>
- #ifndef VERSION
- #define VERSION 1.0.0
- #endif
- #define QUOTE(t) #t
- #define version(v) printf("%s\n", v)
- // Binary mode: Number of binary bytes per line. We want 80 syms in row but each
- // byte occupies 4 syms within string so 80 / 4 = 20.
- #define BIN_BYTES_PER_LINE 20
- // Command line options */
- struct opts_s {
- bool_t debug;
- bool_t binary;
- faux_list_t *file_list;
- };
- typedef struct opts_s opts_t;
- static opts_t *opts_parse(int argc, char *argv[]);
- static void opts_free(opts_t *opts);
- static void help(int status, const char *argv0);
- int main(int argc, char *argv[]) {
- opts_t *opts = NULL; // Command line options
- faux_list_node_t *iter = NULL;
- char *fn = NULL; // Text file
- unsigned int total_errors = 0; // Sum of all errors
- unsigned int file_num = 0; // Number of file
- #if HAVE_LOCALE_H
- // Set current locale
- setlocale(LC_ALL, "");
- #endif
- // Parse command line options
- opts = opts_parse(argc, argv);
- if (!opts) {
- fprintf(stderr, "Error: Can't parse command line options\n");
- return -1;
- }
- // Main loop. Iterate through the list of shared objects
- iter = faux_list_head(opts->file_list);
- while ((fn = faux_list_each(&iter))) {
- faux_file_t *f = NULL;
- char *buf = NULL;
- char *var_name = NULL;
- file_num++;
- f = faux_file_open(fn, O_RDONLY, 0);
- if (!f) {
- fprintf(stderr, "Error: Can't open file \"%s\"\n", fn);
- total_errors++;
- continue;
- }
- printf("\n");
- printf("// File \"%s\"\n", fn);
- var_name = opts->binary ? "bin" : "txt";
- printf("const char *%s%u =\n", var_name, file_num);
- // Binary mode
- if (opts->binary) {
- ssize_t bytes_readed = 0;
- size_t total_bytes = 0;
- buf = faux_malloc(BIN_BYTES_PER_LINE);
- assert(buf);
- if (!buf) {
- fprintf(stderr, "Error: Memory problems\n");
- break;
- }
- do {
- char *escaped_str = NULL;
- bytes_readed = faux_file_read_block(f, buf,
- BIN_BYTES_PER_LINE);
- if (bytes_readed < 0) {
- fprintf(stderr, "Error: Can't open "
- "file \"%s\"\n", fn);
- total_errors++;
- break;
- }
- if (0 == bytes_readed) // EOF
- break;
- total_bytes += bytes_readed;
- escaped_str = faux_str_c_bin(buf, bytes_readed);
- if (escaped_str)
- printf("\t\"%s\"\n", escaped_str);
- faux_str_free(escaped_str);
- } while (BIN_BYTES_PER_LINE == bytes_readed);
- faux_free(buf);
- if (0 == total_bytes) // Empty file
- printf("\t\"\"\n");
- // Text mode
- } else {
- bool_t eof = BOOL_FALSE;
- unsigned int line_num = 0;
- while ((buf = faux_file_getline_raw(f))) {
- char *escaped_str = NULL;
- line_num++;
- escaped_str = faux_str_c_esc(buf);
- faux_str_free(buf);
- if (escaped_str)
- printf("\t\"%s\"\n", escaped_str);
- faux_str_free(escaped_str);
- }
- eof = faux_file_eof(f);
- if (!eof) { // File reading was interrupted before EOF
- fprintf(stderr, "Error: File \"%s\" reading was "
- "interrupted before EOF\n", fn);
- total_errors++;
- } // Continue normal operations
- if (0 == line_num) // Empty file is not error
- printf("\t\"\"\n");
- }
- printf(";\n");
- faux_file_close(f);
- }
- opts_free(opts);
- if (total_errors > 0)
- return -1;
- return 0;
- }
- /** @brief Frees allocated opts_t structure
- *
- * @param [in] opts Allocated opts_t structure.
- */
- static void opts_free(opts_t *opts) {
- assert(opts);
- if (!opts)
- return;
- faux_list_free(opts->file_list);
- faux_free(opts);
- }
- /** @brief Allocates new opts_t structure
- *
- * Allocates structure that stores parse command line options.
- *
- * @return Allocated and initialized opts_t structure.
- * @warning The returned opts_t structure must be freed later by opts_free().
- */
- static opts_t *opts_new(void) {
- opts_t *opts = NULL;
- opts = faux_zmalloc(sizeof(*opts));
- assert(opts);
- if (!opts)
- return NULL;
- opts->debug = BOOL_FALSE;
- opts->binary = BOOL_FALSE;
- // Members of list are static strings from argv so don't free() it
- opts->file_list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
- (faux_list_cmp_fn)strcmp, NULL, NULL);
- if (!opts->file_list) {
- opts_free(opts);
- return NULL;
- }
- return opts;
- }
- /** @brief Parse command line options
- *
- * Function allocates opts_t structure, parses command line options and
- * fills opts_t structure.
- *
- * @param [in] argc Standard argc argument.
- * @param [in] argv Standard argv argument.
- * @return Filled opts_t structure with parsed command line options.
- * @warning The returned opts_t structure must be freed later by opts_free().
- */
- static opts_t *opts_parse(int argc, char *argv[]) {
- opts_t *opts = NULL;
- static const char *shortopts = "hvdbt";
- #ifdef HAVE_GETOPT_LONG
- static const struct option longopts[] = {
- {"help", 0, NULL, 'h'},
- {"version", 0, NULL, 'v'},
- {"debug", 0, NULL, 'd'},
- {"text", 0, NULL, 't'},
- {"binary", 0, NULL, 'b'},
- {NULL, 0, NULL, 0}
- };
- #endif
- opts = opts_new();
- if (!opts)
- return NULL;
- optind = 1;
- while (1) {
- int opt;
- #ifdef HAVE_GETOPT_LONG
- opt = getopt_long(argc, argv, shortopts, longopts, NULL);
- #else
- opt = getopt(argc, argv, shortopts);
- #endif
- if (-1 == opt)
- break;
- switch (opt) {
- case 'd':
- opts->debug = BOOL_TRUE;
- break;
- case 't':
- opts->binary = BOOL_FALSE;
- break;
- case 'b':
- opts->binary = BOOL_TRUE;
- break;
- case 'h':
- help(0, argv[0]);
- exit(0);
- break;
- case 'v':
- version(VERSION);
- exit(0);
- break;
- default:
- help(-1, argv[0]);
- exit(-1);
- break;
- }
- }
- if (optind < argc) {
- int i = 0;
- for (i = optind; i < argc; i++)
- faux_list_add(opts->file_list, argv[i]);
- } else {
- help(-1, argv[0]);
- exit(-1);
- }
- return opts;
- }
- /** @brief Prints help
- *
- * @param [in] status If status is not '0' consider help printing as a reaction
- * to error and print appropriate message. If status is '0' then print general
- * help information.
- * @param [in] argv0 The argv[0] argument i.e. programm name
- */
- static void help(int status, const char *argv0) {
- const char *name = NULL;
- if (!argv0)
- return;
- // Find the basename
- name = strrchr(argv0, '/');
- if (name)
- name++;
- else
- name = argv0;
- if (status != 0) {
- fprintf(stderr, "Try `%s -h' for more information.\n",
- name);
- } else {
- printf("Usage: %s [options] <txt_file> [txt_file] ...\n", name);
- printf("Converts text/binary files to C-strings.\n");
- printf("Options:\n");
- printf("\t-v, --version\tPrint version.\n");
- printf("\t-h, --help\tPrint this help.\n");
- printf("\t-d, --debug\tDebug mode.\n");
- printf("\t-t, --text\tText mode conversion (Default).\n");
- printf("\t-d, --debug\tBinary mode conversion.\n");
- }
- }
|