From a695cce70995bb092af1ccaf237e24913c674ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20A=2E=20Ortega?= Date: Tue, 27 Dec 2016 21:31:24 +0100 Subject: [PATCH] Moving shit to files.c --- src/files.c | 13 +++++++++++++ src/main.c | 47 +++++++++++++++++++++++++---------------------- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/files.c b/src/files.c index 0ef1e3a..3aed3aa 100644 --- a/src/files.c +++ b/src/files.c @@ -1,8 +1,13 @@ #include "files.h" #include #include +#include int inputPrimes(char *file, List *list) { + // Assert safeguards + assert(file != NULL); + assert(list != NULL); + FILE *pFile = fopen(file, "r"); if(pFile == NULL) return 1; mpz_t n; @@ -13,6 +18,10 @@ int inputPrimes(char *file, List *list) { } int outputPrimes(char *file, List *list) { + // Assert safeguards + assert(file != NULL); + assert(list != NULL); + FILE *oFile = fopen(file, "w"); if(oFile == NULL) return 1; printf("Saving primes to `%s'...\n", file); @@ -29,6 +38,10 @@ int outputPrimes(char *file, List *list) { } int exportPrimes(char *file, List *list, int base) { + // Assert safeguards + assert(file != NULL); + assert(list != NULL); + FILE *eFile = fopen(file, "w"); if(eFile == NULL) return 1; printf("Exporting primes to `%s'...\n", file); diff --git a/src/main.c b/src/main.c index 7b61d7c..be5079c 100644 --- a/src/main.c +++ b/src/main.c @@ -5,6 +5,8 @@ #include #include +#include + #include "list.h" #include "files.h" @@ -21,13 +23,12 @@ int main(int argc, char *argv[]) { f_version = false, f_quiet = false; int base = 10; - char *ofile = NULL; - char *ifile = NULL; + char *file = NULL; char *efile = NULL; // Parse commandline arguments int c; - while((c = getopt(argc, argv, "hvqb:o:i:e:")) != -1) { + while((c = getopt(argc, argv, "hvqb:f:e:")) != -1) { switch(c) { case 'h': f_help = true; @@ -47,11 +48,8 @@ int main(int argc, char *argv[]) { exit(1); } break; - case 'o': - ofile = optarg; - break; - case 'i': - ifile = optarg; + case 'f': + file = optarg; break; case 'e': efile = optarg; @@ -69,8 +67,7 @@ int main(int argc, char *argv[]) { puts(" -v print version number of program"); puts(" -q quiet mode"); puts(" -b base in which to print primes between 2 and 62 (default 10)"); - puts(" -o file to save primes to"); - puts(" -i file to read primes from a previous session"); + puts(" -f file in/from which primes are stored and read from in raw format"); puts(" -e export input file to plain text format"); return 0; } else if(f_version) { @@ -82,10 +79,15 @@ int main(int argc, char *argv[]) { run = true; signal(SIGINT, leave); - if(efile != NULL && ifile == NULL) { + if(efile != NULL && file == NULL) { fprintf(stderr, "There must be an input file to export! Use `-h' for help.\n"); return 1; } + bool newFile = true; + if(file != NULL) { + struct stat s; + if(stat(file, &s) == 0) newFile = false; + } int exitCode = 0; @@ -103,7 +105,7 @@ int main(int argc, char *argv[]) { if(efile == NULL) puts("Use Ctrl+C to exit."); - if(ifile == NULL) { + if(newFile) { // Add 2, a known prime to this list mpz_set_ui(num, 2); addToList(&primes, num); @@ -118,15 +120,15 @@ int main(int argc, char *argv[]) { mpz_add_ui(num, num, 1); } else { // Load primes from file - int err = inputPrimes(ifile, &primes); + int err = inputPrimes(file, &primes); if(err == 0) { printf("Loaded %zu primes.\n", primes.end); } else { if(err == 1) - fprintf(stderr, "Failed to open Indivisible file `%s'.\n", ifile); + fprintf(stderr, "Failed to open Indivisible file `%s'.\n", file); else if(err == 2) - fprintf(stderr, "Failed to close Indivisible file `%s'.\n", ifile); + fprintf(stderr, "Failed to close Indivisible file `%s'.\n", file); exitCode = 1; goto releaseMemory; } @@ -181,28 +183,29 @@ nextPrime: printf("Found %zu primes.\n", primes.end); - if(ofile != NULL) { - int err = outputPrimes(ofile, &primes); + if(file != NULL) { + int err = outputPrimes(file, &primes); if(err == 0) { puts("Successfully saved primes."); } else { if(err == 1) - fprintf(stderr, "Failed to open/create file `%s'.\n", ofile); + fprintf(stderr, "Failed to open/create file `%s'.\n", file); else if(err == 2) - fprintf(stderr, "Failed to close file `%s'.\n", ofile); + fprintf(stderr, "Failed to close file `%s'.\n", file); else if(err == 3) - fprintf(stderr, "Failed while writing a prime to `%s'.\n", ofile); + fprintf(stderr, "Failed while writing a prime to `%s'.\n", file); exitCode = 1; goto releaseMemory; } } -releaseMemory: - puts("Clearing memory..."); // Clear GMP variables mpz_clear(halfNum); mpz_clear(num); + +releaseMemory: + puts("Clearing memory..."); // Deinitialize the list deInitList(&primes);