15 Commits
v0.6 ... v0.7

Author SHA1 Message Date
8b35c5aea3 Fixed title for v0.7 2016-12-27 22:53:22 +01:00
1264edc8c8 We're going to release v0.7 first. 2016-12-27 22:52:06 +01:00
63aa8e14cb Fixed the damn exporting for once.
Now I'm checking to see if it was actually worth a damn to have raw
files in the first place (-_-;).
2016-12-27 22:34:34 +01:00
f2eb3e869e Fixed the file issue...
but the export issue still remains -_- I hate seg faults.
2016-12-27 21:47:16 +01:00
a695cce709 Moving shit to files.c 2016-12-27 21:31:24 +01:00
096cb2eb16 Moving file I/O to own file. Everything works except export. 2016-12-26 18:50:51 +01:00
1783b16024 Use raw I/O for file saving. 2016-12-26 16:51:51 +01:00
c602b5fe1b Added OpenMP to CI. 2016-12-26 16:35:52 +01:00
6b2411e860 Now using OpenMP for parallel programming and next version is v1.0 2016-12-26 15:57:28 +01:00
6c2f96416b Read and write primes to and from file. 2016-12-26 15:56:56 +01:00
fa3f2dd2b1 New goal before paralellization 2016-12-22 16:31:24 +01:00
591ee92971 Cast is unnecessary in C. 2016-12-15 15:16:04 +01:00
803c6f9e06 `size_t' makes this safeguard unnecessary. 2016-12-14 23:42:20 +01:00
4390fca3ef Added comments. 2016-12-14 23:38:22 +01:00
2629c12f1a Set goal for v1.0 2016-12-14 22:26:43 +01:00
8 changed files with 208 additions and 56 deletions

View File

@ -4,7 +4,7 @@ build:
stage: build
# Install dependencies
before_script:
- apt update && apt -y install cmake libgmp-dev
- apt update && apt -y install cmake libgmp-dev libgomp1
# Build the project
script:
- cd build/

View File

@ -27,3 +27,6 @@ Change Log
- Allow user to choose base in which the prime numbers are printed.
- Give option for primes to be saved to a file upon exit.
- Free memory and leave instead of emergency exit.
- v0.7: Data Saving
- Allow user to save found primes to be loaded later.
- User can save and read primes in raw output.

View File

@ -14,16 +14,18 @@ set(CMAKE_MODULE_PATH
${CMAKE_SOURCE_DIR}/cmake/)
find_package(GMP REQUIRED)
find_package(OpenMP REQUIRED)
include_directories(
${GMP_INCLUDE_DIR})
set(SRCS
src/main.c
src/files.c
src/list.c)
# Define the C flags.
set(CMAKE_C_FLAGS "-std=gnu99 -Wall -Wextra -Werror -Wfatal-errors -Wmissing-declarations -pedantic-errors")
set(CMAKE_C_FLAGS "-std=gnu99 ${OpenMP_C_FLAGS} -Wall -Wextra -Werror -Wfatal-errors -Wmissing-declarations -pedantic-errors")
set(CMAKE_C_FLAGS_DEBUG "-g -O0")
set(CMAKE_C_FLAGS_RELEASE "-O3")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g -O3")

View File

@ -9,6 +9,7 @@ Building
There are multiple dependencies to install before compiling the project:
- CMake
- GMP
- OpenMP
Once the dependencies are installed you can compile by running the following from the root directory of the project:
```bash

61
src/files.c Normal file
View File

@ -0,0 +1,61 @@
#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;
mpz_init(n);
while(mpz_inp_raw(n, pFile) != 0) addToList(list, n);
if(fclose(pFile) != 0) return 2;
return 0;
}
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);
puts("0%");
for(size_t i = 0; i < list->end; ++i) {
if(mpz_out_raw(oFile, list->list[i]) == 0) return 3;
if(i == list->end / 4) puts("25%");
else if(i == list->end / 2) puts("50%");
else if(i == list->end * 3 / 4) puts("75%");
}
puts("100%");
if(fclose(oFile) != 0) return 2;
return 0;
}
int exportPrimes(char *file, List *list, int base) {
// Assert safeguards
assert(file != NULL);
assert(list != NULL);
assert(list->list != NULL);
FILE *eFile = fopen(file, "w");
if(eFile == NULL) return 1;
printf("Exporting primes to `%s'...\n", file);
puts("0%");
for(size_t i = 0; i < list->end; ++i) {
if(mpz_out_str(eFile, base, list->list[i]) == 0) return 3;
fprintf(eFile, "\n");
if(i == list->end / 4) puts("25%");
else if(i == list->end / 2) puts("50%");
else if(i == list->end * 3 / 4) puts("75%");
}
puts("100%");
if(fclose(eFile) != 0) return 2;
puts("Finished exporting primes.");
return 0;
}

36
src/files.h Normal file
View File

@ -0,0 +1,36 @@
/**
* @file files.h
* @author Deathsbreed <deathsbreed@themusicinnoise.net>
* @brief Functions to deal with file I/O of primes.
* @details Functions that input, output, and export primes from/to files.
*/
#pragma once
#include "list.h"
/**
* @brief Load primes from an Indivisible file into a List.
* @param file File to input primes from.
* @param list List to load primes into.
* @returns If 0 then load was successful, if 1 then failed to open,
* if 2 failed to close.
*/
int inputPrimes(char *file, List *list);
/**
* @brief Output primes from a List into an Indivisible file.
* @param file File to output primes to.
* @param list List to read primes from.
* @returns If 0 then load was successful, if 1 then failed to open,
* if 2 failed to close, if 3 failed when writing.
*/
int outputPrimes(char *file, List *list);
/**
* @brief Export primes from a List to a plain text file.
* @param file File to output primes as plain text to.
* @param list List to read primes from.
* @param base The base in which the primes will be written.
* @returns If 0 then load was successful, if 1 then failed to open,
* if 2 failed to close, if 3 failed when writing.
*/
int exportPrimes(char *file, List *list, int base);

View File

@ -27,16 +27,12 @@ void deInitList(List *restrict l) {
void addToList(List *l, mpz_t n) {
if(l->end == l->size) {
l->size += BLOCK_SIZE;
if(l->size == 0) {
fprintf(stderr, "`l->size' has overflowed!\n");
exit(1);
}
void *tmp = realloc(l->list, sizeof(mpz_t) * l->size);
if(!tmp) {
fprintf(stderr, "Failed to allocate more memory to list!\n");
exit(1);
}
l->list = (mpz_t*)tmp;
l->list = tmp;
}
mpz_init(l->list[l->end]);
mpz_set(l->list[l->end++], n);

View File

@ -5,9 +5,12 @@
#include <unistd.h>
#include <gmp.h>
#include "list.h"
#include <sys/stat.h>
#define VERSION "v0.6"
#include "list.h"
#include "files.h"
#define VERSION "v0.7"
static bool run;
@ -15,14 +18,17 @@ void printUsage(char *progName);
void leave();
int main(int argc, char *argv[]) {
// Variables for argument parsing
bool f_help = false,
f_version = false,
f_quiet = false;
int base = 10;
char *file = NULL;
char *efile = NULL;
// Parse commandline arguments
int c;
while((c = getopt(argc, argv, "hvqb:f:")) != -1) {
while((c = getopt(argc, argv, "hvqb:f:e:")) != -1) {
switch(c) {
case 'h':
f_help = true;
@ -37,7 +43,7 @@ int main(int argc, char *argv[]) {
base = atoi(optarg);
if(base < 2 || base > 62) {
fprintf(stderr,
"Invalid base `%d'. Base must be between 2 and 62\n",
"Invalid base `%d'. Base must be between 2 and 62.\n",
base);
exit(1);
}
@ -45,19 +51,24 @@ int main(int argc, char *argv[]) {
case 'f':
file = optarg;
break;
case 'e':
efile = optarg;
break;
default:
printUsage(argv[0]);
exit(1);
return 1;
}
}
// Act based on which flags were used
if(f_help) {
printUsage(argv[0]);
puts(" -h print this help information");
puts(" -v print version number of program");
puts(" -q quiet mode");
puts(" -b <base> base in which to print primes between 2 and 62");
puts(" -f <file> file to save primes to");
puts(" -b <base> base in which to print primes between 2 and 62 (default 10)");
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) {
printf("Indivisible %s\n", VERSION);
@ -68,9 +79,17 @@ int main(int argc, char *argv[]) {
run = true;
signal(SIGINT, leave);
if(f_quiet) {
puts("Use Ctrl+C (SIGINT) to exit.");
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;
// Primes we've found
List primes;
@ -80,22 +99,63 @@ int main(int argc, char *argv[]) {
mpz_t num;
mpz_init(num);
// Add 2, a known prime to this list
mpz_set_ui(num, 2);
addToList(&primes, num);
if(!f_quiet) {
if(mpz_out_str(stdout, base, num) == 0) {
fprintf(stderr, "Could not print to `stdout'!\n");
goto releaseMemory;
}
printf("\n");
}
mpz_add_ui(num, num, 1);
// Variable for half `num'
mpz_t halfNum;
mpz_init(halfNum);
if(efile == NULL) puts("Use Ctrl+C to exit.");
if(newFile) {
// Add 2, a known prime to this list
mpz_set_ui(num, 2);
addToList(&primes, num);
if(!f_quiet) {
if(mpz_out_str(stdout, base, num) == 0) {
fprintf(stderr, "Could not print to `stdout'!\n");
exitCode = 1;
goto releaseMemory;
}
printf("\n");
}
mpz_add_ui(num, num, 1);
} else {
// Load primes from file
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", file);
else if(err == 2)
fprintf(stderr, "Failed to close Indivisible file `%s'.\n", file);
exitCode = 1;
goto releaseMemory;
}
/**
* Yes, I realize there's a -1 here, I don't know why but it won't
* work if it's not there, so don't change it unless necessary.
*/
mpz_set(num, primes.list[primes.end-1]);
}
if(efile != NULL) {
int err = exportPrimes(efile, &primes, base);
if(err == 0) {
puts("Successfully exported primes.");
} else {
if(err == 1)
fprintf(stderr, "Failed to open/create plain text file `%s'.\n", efile);
else if(err == 2)
fprintf(stderr, "Failed to close plain text file `%s'.\n", efile);
else if(err == 3)
fprintf(stderr, "Failed to write prime to plain text file `%s'.\n", efile);
exitCode = 1;
}
goto releaseMemory;
}
do {
// Calculate half of `num'
mpz_fdiv_q_ui(halfNum, num, 2);
@ -114,6 +174,7 @@ int main(int argc, char *argv[]) {
if(!f_quiet) {
if(mpz_out_str(stdout, base, num) == 0) {
fprintf(stderr, "Could not print to `stdout'!\n");
exitCode = 1;
goto releaseMemory;
}
printf("\n");
@ -125,47 +186,39 @@ nextPrime:
} while(run);
printf("Found %zu primes.\n", primes.end);
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", file);
else if(err == 2)
fprintf(stderr, "Failed to close file `%s'.\n", file);
else if(err == 3)
fprintf(stderr, "Failed while writing a prime to `%s'.\n", file);
exitCode = 1;
goto releaseMemory;
}
}
// Clear GMP variables
mpz_clear(halfNum);
mpz_clear(num);
if(file != NULL) {
FILE *outFile = fopen(file, "w+");
if(outFile == NULL) {
fprintf(stderr, "Failed create file `%s'.\n", file);
goto releaseMemory;
}
printf("Writing primes to `%s'...\n", file);
puts("0%");
for(size_t i = 0; i < primes.end; ++i) {
if(mpz_out_str(outFile, base, primes.list[i]) == 0) {
fprintf(stderr, "Error occurred while writing to file `%s'.\n", file);
goto releaseMemory;
}
fprintf(outFile, "\n");
if(i == primes.end / 4) puts("25%");
else if(i == primes.end / 2) puts("50%");
else if(i == primes.end * 3 / 4) puts("75%");
}
if(fclose(outFile) != 0) {
fprintf(stderr, "Failed to close file `%s'.\n", file);
goto releaseMemory;
}
puts("100%");
puts("Finished writing primes.");
}
releaseMemory:
puts("Clearing memory...");
// Deinitialize the list
deInitList(&primes);
puts("Exit successful.");
return 0;
return exitCode;
}
void printUsage(char *progName) {
printf("%s [options...]\n", progName);
printf("%s [OPTIONS]\n", progName);
}
void leave() { run = false; }