Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
danny-burrows committed Sep 6, 2021
1 parent 7cd65f5 commit fe91be4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
4 changes: 3 additions & 1 deletion include/arg_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#define STR_EQUAL 0

#define VERSION "v0.1.1"
#define VERSION "v0.1.2"

#define HELP "Usage: img_to_txt [OPTION]... [FILE]... \n\
Convert images to text. Either Ascii, Ansi (true colour), \n\
Expand Down Expand Up @@ -69,4 +69,6 @@ typedef struct _FileJob {

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

void free_job_memory(FileJob * job_list);

#endif
10 changes: 10 additions & 0 deletions src/arg_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ static int get_opt_int_param(int argc, char ** argv, int opt) {
return param;
}

void free_job_memory(FileJob * job_list) {
while (job_list != NULL) {
FileJob * next = job_list->nextJob;
free(job_list);
job_list = next;
}
}

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

Expand Down Expand Up @@ -127,5 +135,7 @@ FileJob * arg_parse(int argc, char ** argv, ImageOptions * opts) {
}
if (read_param) opt++;
}

if (jobs == NULL) fprintf(stderr, "No files to convert! (-? for help text)\n");
return jobs;
}
35 changes: 16 additions & 19 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ unsigned char calc_ascii_char(PixelData * p) {
}


int read_and_convert(char * filename, ImageOptions * options) {
int read_and_convert(char * filepath, ImageOptions * options) {
int rwidth, rheight, rchannels;
unsigned char * read_data = stbi_load(filename, &rwidth, &rheight, &rchannels, 3);
unsigned char * read_data = stbi_load(filepath, &rwidth, &rheight, &rchannels, 3);

if (read_data == NULL) {
fprintf(stderr, "Error reading image data!\n\n");
Expand Down Expand Up @@ -95,9 +95,18 @@ int read_and_convert(char * filename, ImageOptions * options) {
}

// Print header if required...
if (options->suppress_header == false) {
printf("Img Type: %s\nTrue Color: %d\nSize: %dx%d\n", OutputModeStr[options->output_mode], options->true_color, desired_width, desired_height);
}
if (options->suppress_header == false)
printf(
"Converting File: %s\n"
"Img Type: %s\n"
"True Color: %d\n"
"Size: %dx%d\n",
filepath,
OutputModeStr[options->output_mode],
options->true_color,
desired_width,
desired_height
);

for (unsigned int d = 0; d < desired_width * desired_height; d++)
{
Expand Down Expand Up @@ -148,15 +157,8 @@ int main(int argc, char ** argv) {
FileJob * jobs = arg_parse(argc, argv, &opts);
if (jobs == NULL) return EXIT_FAILURE;

if (jobs == NULL) {
fprintf(stderr, "No files to convert! (-? for help text)\n");
return EXIT_FAILURE;
}

// Parse file paths and do all jobs.
for (FileJob * read_jobs = jobs; read_jobs != NULL; read_jobs = read_jobs->nextJob) {
if (opts.suppress_header == false) printf("Converting File: %s\n", read_jobs->path);

for (FileJob * read_jobs = jobs; read_jobs != NULL; read_jobs = read_jobs->nextJob) {
int r = read_and_convert(read_jobs->path, &opts);
if (r == -1) {
fprintf(stderr, "Failed to convert image: %s\n", read_jobs->path);
Expand All @@ -165,12 +167,7 @@ int main(int argc, char ** argv) {
}

// Free job memory!
FileJob * f = jobs;
while (f != NULL) {
FileJob * next = f->nextJob;
free(f);
f = next;
}
free_job_memory(jobs);

return EXIT_SUCCESS;
}

0 comments on commit fe91be4

Please sign in to comment.