Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code refactor and Improvements #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 60 additions & 24 deletions main.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
Expand All @@ -6,11 +8,13 @@

#include "pes.h"

/* Function to report an error message to stderr */
static void report(const char *fmt, va_list params)
{
vfprintf(stderr, fmt, params);
}

/* Function to print an error message and exit the program */
static void die(const char *fmt, ...)
{
va_list params;
Expand All @@ -24,8 +28,8 @@ static void die(const char *fmt, ...)
int main(int argc, char **argv)
{
double density = 1.0;
int i, outputsize = -1;
const char *output = NULL;
int arg_index, output_size = -1;
const char *output_file = NULL;
struct region region;
struct pes pes = {
.min_x = 65535, .max_x = -65535,
Expand All @@ -34,47 +38,79 @@ int main(int argc, char **argv)
.last = NULL,
};

for (i = 1; i < argc; i++) {
const char *arg = argv[i];
/* Loop through command line arguments */
for (arg_index = 1; arg_index < argc; arg_index++) {
const char *arg = argv[arg_index];

/* Check if argument is a flag */
if (*arg == '-') {
/* Handle flag */
switch (arg[1]) {
case 's':
outputsize = atoi(argv[i+1]);
i++;
/* Set output size */
output_size = atoi(argv[arg_index + 1]);
if (output_size == 0) {
die("Invalid output size\n");
}
arg_index++;
continue;
case 'd':
density = atof(argv[i+1]);
i++;
/* Set density */
density = atof(argv[arg_index + 1]);
if (density == 0.0) {
die("Invalid density\n");
}
arg_index++;
continue;
}
die("Unknown argument '%s'\n", arg);
}

/* Check if this is the input file */
if (!pes.blocks) {
if (read_path(arg, &region))
die("Unable to read file %s (%s)\n", arg, strerror(errno));
/* Open input file */
FILE *input_file = fopen(arg, "r");
if (!input_file) {
die("Unable to open input file %s (%s)\n", arg, strerror(errno));
}

if (parse_pes(&region, &pes) < 0)
die("Unable to parse PES file\n");
continue;
/* Read region data from input file */
if (fread(&region, sizeof(struct region), 1, input_file) != 1) {
die("Unable to read from input file %s (%s)\n", arg, strerror(errno));
}
/* Close input file */
fclose(input_file);

/* Parse PES data from region data */
if (parse_pes(&region, &pes) < 0) {
die("Unable to parse PES file\n");
}
continue;
}

if (!output) {
output = arg;
/* Check if this is the output file */
if (!output_file) {
output_file = arg;
continue;
}

die("Too many arguments (%s)\n", arg);
}
/* Too many arguments */
die("Too many arguments (%s)\n", arg);
}

if (!pes.blocks)
die("Need an input PES file\n");
/* Check if input file was specified */
if (!pes.blocks) {
die("Need an input PES file\n");
}

if (!output)
die("Need a png output file name\n");
/* Check if output file was specified */
if (!output_file) {
die("Need a png output file name\n");
}

output_cairo(&pes, output, outputsize, density);
/* Generate output image using Cairo library */
output_cairo(&pes, output_file, output_size, density);

return 0;
}
return 0;

}