From fff56280ea009d490b3abd23b5ad920cdd483908 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Thu, 1 Aug 2019 09:59:14 -0400 Subject: add -q option --- README.md | 15 ++++++++++----- array.c | 16 ++++++++++++---- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 501e107..d15dba0 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,12 @@ translate files to C arrays ## Synopsis -`array [-s] [file...]` +`array [-s] [-q qualifier] [file...]` ## Description -The `array` utility translates its input into C arrays (of type -`unsigned char`) that can then be used (via `#include`) in larger C programs. +The `array` utility translates its input into C arrays (of a qualified type +of `char`) that can then be used (via `#include`) in larger C programs. This is useful if you need to embed a binary (e.g. an image) or text (e.g. a template) file and need to ship a program as a single, self-contained executable. @@ -17,12 +17,17 @@ executable. ## Options The `array` utility conforms to the XBD Utility Syntax Guidelines. It supports -one option: +the following options: `-s` Include an additional identifier in the output indicating the size of the array. See STDOUT below for a detailed description. +`-q` Apply the C type qualifier `qualifier` to the generated array. If + none is provided, the default is `unsigned`. To remove any qualifiers + altogether (e.g. to declare an array of `char`), specify the empty + string (e.g. `-q ''`). + ## Operands The `array` utility supports the following operands: @@ -41,7 +46,7 @@ Input files are treated as raw binary streams, with no requirements on format. ## STDOUT -A definition of a C array of type `unsigned char` and unspecified length, with +A definition of a C array of type qualified `char` and unspecified length, with members initialized to the bytes of the input file. Each array is named with the name of the input file, except all non alphanumeric characters (as identified by the function `isalnum()`) are translated to `_`. If the input diff --git a/array.c b/array.c index 6ad54db..edc99e5 100644 --- a/array.c +++ b/array.c @@ -6,7 +6,7 @@ #define CHARS_PER_LINE 8 -int make_array(const char *path, int include_size) +int make_array(const char *path, const char *qual, int include_size) { FILE *f = stdin; @@ -27,7 +27,10 @@ int make_array(const char *path, int include_size) } id[plen] = '\0'; - printf("unsigned char %s[] = {\n", id); + if (qual && strlen(qual) > 0) { + printf("%s ", qual); + } + printf("char %s[] = {\n", id); int c; int i = 0; @@ -62,15 +65,20 @@ int make_array(const char *path, int include_size) int main(int argc, char *argv[]) { + char *qual = "unsigned"; int include_size = 0; int c; - while ((c = getopt(argc, argv, "s")) != -1) { + while ((c = getopt(argc, argv, "sq:")) != -1) { switch (c) { case 's': include_size = 1; break; + case 'q': + qual = optarg; + break; + default: return 1; } @@ -78,7 +86,7 @@ int main(int argc, char *argv[]) int ret = 0; do { - ret |= make_array(argv[optind++], include_size); + ret |= make_array(argv[optind++], qual, include_size); } while (optind < argc); return ret; -- cgit v1.2.1