commit 2690b56f816a3254530223a4889df93c0470a1ad
parent 9cc737a4825fa61d9264c9a5dae5bc535b477e20
Author: Frederic Cambus <fred@statdns.com>
Date: Sat, 23 Jul 2016 13:21:50 +0200
Refactoring bits and columns argument value handling
Diffstat:
M | src/main.c | | | 64 | +++++++++++++++++++++++++++------------------------------------- |
1 file changed, 27 insertions(+), 37 deletions(-)
diff --git a/src/main.c b/src/main.c
@@ -135,9 +135,7 @@ int main(int argc, char *argv[]) {
bool fileIsTundra = false;
int getoptFlag;
- char *bits = NULL;
char *mode = NULL;
- char *columns = NULL;
char *font = NULL;
char *input = NULL, *output = NULL;
@@ -147,7 +145,11 @@ int main(int argc, char *argv[]) {
const char *errstr;
- int32_t int_bits, int_columns;
+ // default to 8 if bits option is not specified
+ int32_t bits = 8;
+
+ // default to 160 if columns option is not specified
+ int32_t columns = 160;
if (pledge("stdio cpath rpath wpath", NULL) == -1) {
err(EXIT_FAILURE, "pledge");
@@ -156,10 +158,24 @@ int main(int argc, char *argv[]) {
while ((getoptFlag = getopt(argc, argv, "b:c:ef:him:o:rsv")) != -1) {
switch(getoptFlag) {
case 'b':
- bits = optarg;
+ // convert numeric command line flags to integer values
+ bits = strtonum(optarg, 8, 9, &errstr);
+
+ if (errstr) {
+ printf("\nInvalid value for bits.\n\n");
+ return EXIT_FAILURE;
+ }
+
break;
case 'c':
- columns = optarg;
+ // convert numeric command line flags to integer values
+ columns = strtonum(optarg, 1, 8192, &errstr);
+
+ if (errstr) {
+ printf("\nInvalid value for columns.\n\n");
+ return EXIT_FAILURE;
+ }
+
break;
case 'e':
listExamples();
@@ -236,37 +252,11 @@ int main(int argc, char *argv[]) {
snprintf(retinaout, retinaLen, "%s%s", outputName, "@2x.png");
}
- // default to 8 if bits option is not specified
- if (bits) {
- // convert numeric command line flags to integer values
- int_bits = strtonum(bits, 8, 9, &errstr);
-
- if (errstr) {
- printf("\nInvalid value for bits.\n\n");
- return EXIT_FAILURE;
- }
- } else {
- int_bits = 8;
- }
-
// default to empty string if mode option is not specified
if (!mode) {
mode = "";
}
- // default to 160 if columns option is not specified
- if (columns) {
- // convert numeric command line flags to integer values
- int_columns = strtonum(columns, 1, 8192, &errstr);
-
- if (errstr) {
- printf("\nInvalid value for columns.\n\n");
- return EXIT_FAILURE;
- }
- } else {
- int_columns = 160;
- }
-
// default to 80x25 font if font option is not specified
if (!font) {
font = "80x25";
@@ -321,11 +311,11 @@ int main(int argc, char *argv[]) {
// create the output file by invoking the appropiate function
if (!strcmp(fext, ".pcb")) {
// params: input, output, font, bits, icecolors
- pcboard(input_file_buffer, input_file_size, outputFile, retinaout, font, int_bits, createRetinaRep);
+ pcboard(input_file_buffer, input_file_size, outputFile, retinaout, font, bits, createRetinaRep);
fileIsPCBoard = true;
} else if (!strcmp(fext, ".bin")) {
// params: input, output, columns, font, bits, icecolors
- binary(input_file_buffer, input_file_size, outputFile, retinaout, int_columns, font, int_bits, icecolors, createRetinaRep);
+ binary(input_file_buffer, input_file_size, outputFile, retinaout, columns, font, bits, icecolors, createRetinaRep);
fileIsBinary = true;
} else if (!strcmp(fext, ".adf")) {
// params: input, output, bits
@@ -334,14 +324,14 @@ int main(int argc, char *argv[]) {
// params: input, output, bits
icedraw(input_file_buffer, input_file_size, outputFile, retinaout, createRetinaRep);
} else if (!strcmp(fext, ".tnd")) {
- tundra(input_file_buffer, input_file_size, outputFile, retinaout, font, int_bits, createRetinaRep);
+ tundra(input_file_buffer, input_file_size, outputFile, retinaout, font, bits, createRetinaRep);
fileIsTundra = true;
} else if (!strcmp(fext, ".xb")) {
// params: input, output, bits
xbin(input_file_buffer, input_file_size, outputFile, retinaout, createRetinaRep);
} else {
// params: input, output, font, bits, icecolors, fext
- ansi(input_file_buffer, input_file_size, outputFile, retinaout, font, int_bits, mode, icecolors, fext, createRetinaRep);
+ ansi(input_file_buffer, input_file_size, outputFile, retinaout, font, bits, mode, icecolors, fext, createRetinaRep);
fileIsANSi = true;
}
@@ -349,13 +339,13 @@ int main(int argc, char *argv[]) {
if (fileIsANSi || fileIsBinary ||
fileIsPCBoard || fileIsTundra) {
printf("Font: %s\n", font);
- printf("Bits: %d\n", int_bits);
+ printf("Bits: %d\n", bits);
}
if (icecolors && (fileIsANSi || fileIsBinary)) {
printf("iCE Colors: enabled\n");
}
if (fileIsBinary) {
- printf("Columns: %d\n", int_columns);
+ printf("Columns: %d\n", columns);
}
}