♻ Move utility functions into separate directory

This commit is contained in:
C-3PO 2018-07-24 17:13:51 +02:00
parent 8b8df10e33
commit 7019271bfe
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
5 changed files with 9 additions and 12 deletions

40
src/utils/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);
}

19
src/utils/fileUtilities.h Normal file
View file

@ -0,0 +1,19 @@
#pragma once
//A structure to store various information needed when reading from a file.
struct FILE_INFO {
char* name;
unsigned long size;
FILE* filePointer;
unsigned long offset;
};
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);
//Closes the currently opened file and opens the next file at its beginning.
void openNextFile();

5
src/utils/min.h Normal file
View file

@ -0,0 +1,5 @@
//minimum of two integers
#define min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })