🚧 Implement file reader
This commit is contained in:
parent
afa9522e78
commit
d7dcd97c75
4 changed files with 105 additions and 15 deletions
|
@ -4,23 +4,66 @@
|
|||
#include <errno.h>
|
||||
//for strerror()
|
||||
#include <string.h>
|
||||
//for malloc
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "fileReader.h"
|
||||
#include "fileUtilities.h"
|
||||
|
||||
void initFileReader(char path[], unsigned long offset, unsigned long length) {
|
||||
//Opens the given file at the given address
|
||||
//Returns a function that will read into memory and return that memory.
|
||||
//minimum of two integers
|
||||
#define min(a,b) \
|
||||
({ __typeof__ (a) _a = (a); \
|
||||
__typeof__ (b) _b = (b); \
|
||||
_a < _b ? _a : _b; })
|
||||
|
||||
FILE *fileFrom = NULL;
|
||||
char* fileName = path;
|
||||
|
||||
fileFrom = fopen(fileName, "r");
|
||||
if (fileFrom == NULL) {
|
||||
fprintf(stderr, "Could not open file %s for reading: %s\n", fileName, strerror(errno));
|
||||
//error();
|
||||
//Opens the given file at the given offset
|
||||
void initFileReader(char path[], unsigned long offset) {
|
||||
file.name = path;
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#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
46
src/fileUtilities.h
Normal 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);
|
||||
}
|
|
@ -13,10 +13,11 @@ int main(int argc, unsigned char *argv[]) {
|
|||
printf("Hello World!\n");
|
||||
|
||||
//Initialise file reader
|
||||
initFileReader("1234", 0UL, 10UL);
|
||||
initFileReader("1234", 0UL);
|
||||
|
||||
//Skip header (30 bytes + additional length)
|
||||
readBytes(30UL);
|
||||
char* fileHeader = getBytes(30UL);
|
||||
printf(fileHeader);
|
||||
|
||||
//Initialise decryption (pass decryption keys)
|
||||
|
||||
|
|
Loading…
Reference in a new issue