diff --git a/src/fileReader.c b/src/fileReader.c index 2cd5ad4..573ad76 100644 --- a/src/fileReader.c +++ b/src/fileReader.c @@ -1,11 +1,7 @@ -//for printf() -#include -//for errno #include -//for strerror() -#include -//for malloc +#include #include +#include #include "fileReader.h" #include "fileUtilities.h" diff --git a/src/fileUtilities.c b/src/fileUtilities.c new file mode 100644 index 0000000..394594a --- /dev/null +++ b/src/fileUtilities.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +#include "fileUtilities.h" +#include "fileReader.h" + + +//Reads the given amount of bytes from the currently open file into the given buffer. +void readBytesIntoBuffer(char* buffer, long numBytes) { + const size_t result = fread(buffer, 1, numBytes, file.filePointer); + file.offset += result; + + if (result != numBytes) { + fprintf(stderr, "Could not read %lu bytes from file: %s\n", numBytes, strerror(errno)); + exit(1); + } +} + + +//Closes the currently opened file and opens the next file at its beginning. +void openNextFile() { + const int closeResult = fclose(file.filePointer); + if (closeResult != 0) { + fprintf(stderr, "Could not close file: %s\n", strerror(errno)); + exit(1); + } + + //Open next file + const size_t fileNameLength = strlen(file.name); + //We need to transfer carry if necessary (e.g. .z09 to .z10), otherwise just increase last digit + if (file.name[fileNameLength - 1] == '9') { + file.name[fileNameLength - 2] += 1; + file.name[fileNameLength - 1] = 0; + } else { + file.name[fileNameLength - 1] += 1; + } + initFileReader(file.name, 0UL); +} diff --git a/src/fileUtilities.h b/src/fileUtilities.h index 01f5b6d..947ee8a 100644 --- a/src/fileUtilities.h +++ b/src/fileUtilities.h @@ -14,33 +14,8 @@ struct FILE_INFO file; //Reads the given amount of bytes from the currently open file into the given buffer. -void readBytesIntoBuffer(char* buffer, long numBytes) { - const size_t result = fread(buffer, 1, numBytes, file.filePointer); - file.offset += result; - - if (result != numBytes) { - fprintf(stderr, "Could not read %lu bytes from file: %s\n", numBytes, strerror(errno)); - exit(1); - } -} +void readBytesIntoBuffer(char* buffer, long numBytes); //Closes the currently opened file and opens the next file at its beginning. -void openNextFile() { - const int closeResult = fclose(file.filePointer); - if (closeResult != 0) { - fprintf(stderr, "Could not close file: %s\n", strerror(errno)); - exit(1); - } - - //Open next file - const size_t fileNameLength = strlen(file.name); - //We need to transfer carry if necessary (e.g. .z09 to .z10), otherwise just increase last digit - if (file.name[fileNameLength - 1] == '9') { - file.name[fileNameLength - 2] += 1; - file.name[fileNameLength - 1] = 0; - } else { - file.name[fileNameLength - 1] += 1; - } - initFileReader(file.name, 0UL); -} +void openNextFile(); diff --git a/src/main.c b/src/main.c index dab2ccc..0803815 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,5 @@ -//for printf() -#include -//for errno #include -//for strerror() +#include #include //Import our code