Moving shit to files.c
This commit is contained in:
parent
096cb2eb16
commit
a695cce709
13
src/files.c
13
src/files.c
@ -1,8 +1,13 @@
|
|||||||
#include "files.h"
|
#include "files.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <gmp.h>
|
#include <gmp.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
int inputPrimes(char *file, List *list) {
|
int inputPrimes(char *file, List *list) {
|
||||||
|
// Assert safeguards
|
||||||
|
assert(file != NULL);
|
||||||
|
assert(list != NULL);
|
||||||
|
|
||||||
FILE *pFile = fopen(file, "r");
|
FILE *pFile = fopen(file, "r");
|
||||||
if(pFile == NULL) return 1;
|
if(pFile == NULL) return 1;
|
||||||
mpz_t n;
|
mpz_t n;
|
||||||
@ -13,6 +18,10 @@ int inputPrimes(char *file, List *list) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int outputPrimes(char *file, List *list) {
|
int outputPrimes(char *file, List *list) {
|
||||||
|
// Assert safeguards
|
||||||
|
assert(file != NULL);
|
||||||
|
assert(list != NULL);
|
||||||
|
|
||||||
FILE *oFile = fopen(file, "w");
|
FILE *oFile = fopen(file, "w");
|
||||||
if(oFile == NULL) return 1;
|
if(oFile == NULL) return 1;
|
||||||
printf("Saving primes to `%s'...\n", file);
|
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) {
|
int exportPrimes(char *file, List *list, int base) {
|
||||||
|
// Assert safeguards
|
||||||
|
assert(file != NULL);
|
||||||
|
assert(list != NULL);
|
||||||
|
|
||||||
FILE *eFile = fopen(file, "w");
|
FILE *eFile = fopen(file, "w");
|
||||||
if(eFile == NULL) return 1;
|
if(eFile == NULL) return 1;
|
||||||
printf("Exporting primes to `%s'...\n", file);
|
printf("Exporting primes to `%s'...\n", file);
|
||||||
|
47
src/main.c
47
src/main.c
@ -5,6 +5,8 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <gmp.h>
|
#include <gmp.h>
|
||||||
|
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include "list.h"
|
#include "list.h"
|
||||||
#include "files.h"
|
#include "files.h"
|
||||||
|
|
||||||
@ -21,13 +23,12 @@ int main(int argc, char *argv[]) {
|
|||||||
f_version = false,
|
f_version = false,
|
||||||
f_quiet = false;
|
f_quiet = false;
|
||||||
int base = 10;
|
int base = 10;
|
||||||
char *ofile = NULL;
|
char *file = NULL;
|
||||||
char *ifile = NULL;
|
|
||||||
char *efile = NULL;
|
char *efile = NULL;
|
||||||
|
|
||||||
// Parse commandline arguments
|
// Parse commandline arguments
|
||||||
int c;
|
int c;
|
||||||
while((c = getopt(argc, argv, "hvqb:o:i:e:")) != -1) {
|
while((c = getopt(argc, argv, "hvqb:f:e:")) != -1) {
|
||||||
switch(c) {
|
switch(c) {
|
||||||
case 'h':
|
case 'h':
|
||||||
f_help = true;
|
f_help = true;
|
||||||
@ -47,11 +48,8 @@ int main(int argc, char *argv[]) {
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'o':
|
case 'f':
|
||||||
ofile = optarg;
|
file = optarg;
|
||||||
break;
|
|
||||||
case 'i':
|
|
||||||
ifile = optarg;
|
|
||||||
break;
|
break;
|
||||||
case 'e':
|
case 'e':
|
||||||
efile = optarg;
|
efile = optarg;
|
||||||
@ -69,8 +67,7 @@ int main(int argc, char *argv[]) {
|
|||||||
puts(" -v print version number of program");
|
puts(" -v print version number of program");
|
||||||
puts(" -q quiet mode");
|
puts(" -q quiet mode");
|
||||||
puts(" -b <base> base in which to print primes between 2 and 62 (default 10)");
|
puts(" -b <base> base in which to print primes between 2 and 62 (default 10)");
|
||||||
puts(" -o <file> file to save primes to");
|
puts(" -f <file> file in/from which primes are stored and read from in raw format");
|
||||||
puts(" -i <file> file to read primes from a previous session");
|
|
||||||
puts(" -e <file> export input file to plain text format");
|
puts(" -e <file> export input file to plain text format");
|
||||||
return 0;
|
return 0;
|
||||||
} else if(f_version) {
|
} else if(f_version) {
|
||||||
@ -82,10 +79,15 @@ int main(int argc, char *argv[]) {
|
|||||||
run = true;
|
run = true;
|
||||||
signal(SIGINT, leave);
|
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");
|
fprintf(stderr, "There must be an input file to export! Use `-h' for help.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
bool newFile = true;
|
||||||
|
if(file != NULL) {
|
||||||
|
struct stat s;
|
||||||
|
if(stat(file, &s) == 0) newFile = false;
|
||||||
|
}
|
||||||
|
|
||||||
int exitCode = 0;
|
int exitCode = 0;
|
||||||
|
|
||||||
@ -103,7 +105,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
if(efile == NULL) puts("Use Ctrl+C to exit.");
|
if(efile == NULL) puts("Use Ctrl+C to exit.");
|
||||||
|
|
||||||
if(ifile == NULL) {
|
if(newFile) {
|
||||||
// Add 2, a known prime to this list
|
// Add 2, a known prime to this list
|
||||||
mpz_set_ui(num, 2);
|
mpz_set_ui(num, 2);
|
||||||
addToList(&primes, num);
|
addToList(&primes, num);
|
||||||
@ -118,15 +120,15 @@ int main(int argc, char *argv[]) {
|
|||||||
mpz_add_ui(num, num, 1);
|
mpz_add_ui(num, num, 1);
|
||||||
} else {
|
} else {
|
||||||
// Load primes from file
|
// Load primes from file
|
||||||
int err = inputPrimes(ifile, &primes);
|
int err = inputPrimes(file, &primes);
|
||||||
|
|
||||||
if(err == 0) {
|
if(err == 0) {
|
||||||
printf("Loaded %zu primes.\n", primes.end);
|
printf("Loaded %zu primes.\n", primes.end);
|
||||||
} else {
|
} else {
|
||||||
if(err == 1)
|
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)
|
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;
|
exitCode = 1;
|
||||||
goto releaseMemory;
|
goto releaseMemory;
|
||||||
}
|
}
|
||||||
@ -181,28 +183,29 @@ nextPrime:
|
|||||||
|
|
||||||
printf("Found %zu primes.\n", primes.end);
|
printf("Found %zu primes.\n", primes.end);
|
||||||
|
|
||||||
if(ofile != NULL) {
|
if(file != NULL) {
|
||||||
int err = outputPrimes(ofile, &primes);
|
int err = outputPrimes(file, &primes);
|
||||||
if(err == 0) {
|
if(err == 0) {
|
||||||
puts("Successfully saved primes.");
|
puts("Successfully saved primes.");
|
||||||
} else {
|
} else {
|
||||||
if(err == 1)
|
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)
|
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)
|
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;
|
exitCode = 1;
|
||||||
goto releaseMemory;
|
goto releaseMemory;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
releaseMemory:
|
|
||||||
puts("Clearing memory...");
|
|
||||||
// Clear GMP variables
|
// Clear GMP variables
|
||||||
mpz_clear(halfNum);
|
mpz_clear(halfNum);
|
||||||
mpz_clear(num);
|
mpz_clear(num);
|
||||||
|
|
||||||
|
releaseMemory:
|
||||||
|
puts("Clearing memory...");
|
||||||
// Deinitialize the list
|
// Deinitialize the list
|
||||||
deInitList(&primes);
|
deInitList(&primes);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user