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 <stdio.h>
|
||||
#include <gmp.h>
|
||||
#include <assert.h>
|
||||
|
||||
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);
|
||||
|
47
src/main.c
47
src/main.c
@ -5,6 +5,8 @@
|
||||
#include <unistd.h>
|
||||
#include <gmp.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
|
||||
#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> base in which to print primes between 2 and 62 (default 10)");
|
||||
puts(" -o <file> file to save primes to");
|
||||
puts(" -i <file> file to read primes from a previous session");
|
||||
puts(" -f <file> file in/from which primes are stored and read from in raw format");
|
||||
puts(" -e <file> 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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user