🚧 Implement file reader

This commit is contained in:
C-3PO 2018-07-24 17:07:47 +02:00
parent afa9522e78
commit d7dcd97c75
Signed by: c3po
GPG key ID: 62993C4BB4D86F24
4 changed files with 105 additions and 15 deletions

View file

@ -4,23 +4,66 @@
#include <errno.h> #include <errno.h>
//for strerror() //for strerror()
#include <string.h> #include <string.h>
//for malloc
#include <stdlib.h>
#include "fileReader.h" #include "fileReader.h"
#include "fileUtilities.h"
void initFileReader(char path[], unsigned long offset, unsigned long length) { //minimum of two integers
//Opens the given file at the given address #define min(a,b) \
//Returns a function that will read into memory and return that memory. ({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
FILE *fileFrom = NULL;
char* fileName = path;
fileFrom = fopen(fileName, "r"); //Opens the given file at the given offset
if (fileFrom == NULL) { void initFileReader(char path[], unsigned long offset) {
fprintf(stderr, "Could not open file %s for reading: %s\n", fileName, strerror(errno)); file.name = path;
//error();
file.filePointer = fopen(file.name, "r");
if (file.filePointer == NULL) {
fprintf(stderr, "Could not open file %s for reading: %s\n", file.name, strerror(errno));
exit(1);
} }
//get file size
fseek(file.filePointer, 0L, SEEK_END);
file.size = ftell(file.filePointer);
//seek to offset
if (offset != 0UL) {
fseek(file.filePointer, offset, SEEK_SET);
} else {
rewind(file.filePointer);
}
file.offset = offset;
} }
unsigned char* readBytes(unsigned long numBytes) {
//TODO //Reads the given amount of bytes from the file and returns them. Automatically opens next file if EOF is reached.
char* getBytes(unsigned long numBytes) {
char* output = malloc(numBytes);
if (output == NULL) {
fprintf(stderr, "Could not allocate %lu bytes.\n", numBytes);
exit(1);
}
char* bufferPosition = output;
unsigned long remainingBytes = numBytes;
//As long as we still need to read bytes
while (remainingBytes > 0) {
//Read as many bytes as we can from the current file
const unsigned long availableBytes = min(remainingBytes, file.size - file.offset);
readBytesIntoBuffer(bufferPosition, availableBytes);
bufferPosition += availableBytes;
remainingBytes -= availableBytes;
//If we've reached end of file, close file and open next file
if (file.offset == file.size) {
openNextFile();
}
}
return output;
} }

View file

@ -1,5 +1,5 @@
#pragma once #pragma once
void initFileReader(char path[], unsigned long offset, unsigned long length); void initFileReader(char path[], unsigned long offset);
unsigned char* readBytes(unsigned long numBytes); char* getBytes(unsigned long numBytes);

46
src/fileUtilities.h Normal file
View file

@ -0,0 +1,46 @@
#pragma once
#include "fileReader.h"
//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) {
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

@ -13,10 +13,11 @@ int main(int argc, unsigned char *argv[]) {
printf("Hello World!\n"); printf("Hello World!\n");
//Initialise file reader //Initialise file reader
initFileReader("1234", 0UL, 10UL); initFileReader("1234", 0UL);
//Skip header (30 bytes + additional length) //Skip header (30 bytes + additional length)
readBytes(30UL); char* fileHeader = getBytes(30UL);
printf(fileHeader);
//Initialise decryption (pass decryption keys) //Initialise decryption (pass decryption keys)