♻ Extract function definitions from header into C file

This commit is contained in:
C-3PO 2018-07-24 17:11:24 +02:00
parent d7dcd97c75
commit 8b8df10e33
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
4 changed files with 45 additions and 37 deletions

View file

@ -1,11 +1,7 @@
//for printf()
#include <stdio.h>
//for errno
#include <errno.h> #include <errno.h>
//for strerror() #include <stdio.h>
#include <string.h>
//for malloc
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "fileReader.h" #include "fileReader.h"
#include "fileUtilities.h" #include "fileUtilities.h"

40
src/fileUtilities.c Normal file
View file

@ -0,0 +1,40 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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);
}

View file

@ -14,33 +14,8 @@ struct FILE_INFO file;
//Reads the given amount of bytes from the currently open file into the given buffer. //Reads the given amount of bytes from the currently open file into the given buffer.
void readBytesIntoBuffer(char* buffer, long numBytes) { 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. //Closes the currently opened file and opens the next file at its beginning.
void openNextFile() { 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);
}

View file

@ -1,8 +1,5 @@
//for printf()
#include <stdio.h>
//for errno
#include <errno.h> #include <errno.h>
//for strerror() #include <stdio.h>
#include <string.h> #include <string.h>
//Import our code //Import our code