Skip to content

Commit

Permalink
Project restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
danny-burrows committed Sep 5, 2021
1 parent 6e81fd1 commit 7cd65f5
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 214 deletions.
72 changes: 72 additions & 0 deletions include/arg_parse.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#ifndef IMG_TO_TXT_ARG_H
#define IMG_TO_TXT_ARG_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#include "options.h"

#define STR_EQUAL 0

#define VERSION "v0.1.1"

#define HELP "Usage: img_to_txt [OPTION]... [FILE]... \n\
Convert images to text. Either Ascii, Ansi (true colour), \n\
or solid background (Ansi true color). \n\
\n\
Note: Long arg (--example) not yet supported. \n\
\n\
Available input image types: \n\
- jpg/jpeg \n\
- png \n\
- bmp \n\
- tga \n\
- hdr \n\
- gif (static) \n\
- pic (not tested yet) \n\
- ppm/pnm/pgm \n\
\n\
Options: \n\
-w <width> Set width and height (rows and cols) of output. \n\
-h <height> \n\
\n\
-o Output original size. (Size of image in text) \n\
\n\
-a Ascii mode. (no ansi colors) \n\
-s Solid mode. (Background colors with no ascii chars) \n\
\n\
-t Use true color for ANSI output. \n\
(Need true-color supported terminal) \n\
\n\
-n Disable standard image squashing that tries to make \n\
image look less elongated by chars being taller than\n\
they are wide. \n\
\n\
-q Quiet mode (suppress header) \n\
\n\
-? Print help message and exit. \n\
\n\
-v Print version and exit. \n\
\n\
Examples: \n\
\n\
img_to_txt -h 100 -w 200 -s some_img.png \n\
\n\
img_to_txt -whqt 30 some_img.png \n\
\n\
img_to_txt some_img.jpg -a \n\
\n\
img_to_txt -q -t -o some_img.bmp \n\
"


typedef struct _FileJob {
char * path;
struct _FileJob * nextJob;
} FileJob;

FileJob * arg_parse(int argc, char ** argv, ImageOptions * opts);

#endif
28 changes: 28 additions & 0 deletions include/options.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef IMG_TO_TXT_OPTIONS_H
#define IMG_TO_TXT_OPTIONS_H

// Some preprocessor magic to generate an enum and string array with the same items.
#define OUTPUT_MODES(MODE) \
MODE(ANSI) \
MODE(SOLID_ANSI) \
MODE(ASCII) \

#define GENERATE_ENUM(ENUM) ENUM,
#define GENERATE_STRING(STRING) #STRING,

enum OutputModes {OUTPUT_MODES(GENERATE_ENUM)};
static const char * OutputModeStr[] = {OUTPUT_MODES(GENERATE_STRING)}; // Strings used for output.

typedef struct {
unsigned int width;
unsigned int height;

enum OutputModes output_mode;

bool original_size;
bool true_color;
bool squashing_enabled;
bool suppress_header;
} ImageOptions;

#endif
4 changes: 2 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ LDIR =./lib

LIBS=-lm

_DEPS = terminal.h
_DEPS = terminal.h options.h arg_parse.h
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

_OBJ = main.o terminal.o
_OBJ = main.o terminal.o arg_parse.o
OBJ = $(patsubst %,$(ODIR)/%,$(_OBJ))


Expand Down
131 changes: 131 additions & 0 deletions src/arg_parse.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include "arg_parse.h"

static void print_version(void) {
printf("img_to_text %s\n", VERSION);
}

static void print_help(void) {
printf("%s\n", HELP);
}

static int get_opt_int_param(int argc, char ** argv, int opt) {
int scan_result;
int param;

if (opt == argc - 1) {
fprintf(stderr, "Not enough arguments to read option parameter!\n");
return -1;
}

scan_result = sscanf( argv[opt + 1], "%d", &param );
if (!scan_result) {
fprintf(stderr, "Couldn't read option parameter!\n");
return -1;
}

#ifdef DEBUG_CONFIG_SET
printf("[DEBUG] Arg: %d\n", param);
#endif

return param;
}

FileJob * arg_parse(int argc, char ** argv, ImageOptions * opts) {
// Returns a pointer to FileJobs array or NULL.

FileJob * jobs = NULL;

// Parse arguments
bool stop_opt = false;
for (int opt = 1; opt < argc; opt++) {

// If not an option then add to job linked list.
if (argv[opt][0] != '-' || stop_opt == true) {
FileJob * job = (FileJob *) malloc(sizeof(FileJob));

if (job == NULL) {
perror("Couldn't allocate memory!");
return NULL;
}

job->path = argv[opt];
job->nextJob = jobs;

jobs = job;
continue;
}

if ( strcmp(argv[opt], "--") == STR_EQUAL ) {
stop_opt = true;
continue;
}

#ifdef DEBUG_CONFIG_SET
printf("[DEBUG] Opt[%d]: %s\n", opt, argv[opt]);
#endif

int width;
int height;
bool read_param = false;
for (size_t c = 1; c < strlen(argv[opt]); c++) {
switch (argv[opt][c]) {

case '?':
print_help();
return NULL;
case 'v':
print_version();
return NULL;

case '-':
//parse_long_arg(argv[opt]);
break;

case 'w':
width = get_opt_int_param(argc, argv, opt);
if (width < 0) {
fprintf(stderr, "[ERR] Width is non-zero!\n");
return NULL;
}
opts->width = (unsigned int)width;
read_param = true;
break;
case 'h':
height = get_opt_int_param(argc, argv, opt);
if (height < 0) {
fprintf(stderr, "[ERR] Height is non-zero!\n");
return NULL;
}
opts->height = (unsigned int)height;
read_param = true;
break;

case 'a':
opts->output_mode = ASCII;
opts->true_color = false;
break;
case 's':
opts->output_mode = SOLID_ANSI;
opts->true_color = true;
break;
case 'o':
opts->original_size = true;
break;
case 't':
opts->true_color = true;
break;
case 'n':
opts->squashing_enabled = false;
break;
case 'q':
opts->suppress_header = true;
break;
default:
printf("Unknown option: %s\n", argv[opt]);
return NULL;
}
}
if (read_param) opt++;
}
return jobs;
}
Loading

0 comments on commit 7cd65f5

Please sign in to comment.