ssn-installer/src/utils/fileUtilities.c

53 lines
1.4 KiB
C

#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(uint8_t* 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);
}
}
//Skips the given amount of bytes in the file
void skipBytes(long numBytes) {
const int result = fseek(file.filePointer, numBytes, SEEK_CUR);
if (result != 0) {
fprintf(stderr, "Could not skip %lu bytes from file: %s\n", numBytes, strerror(errno));
exit(1);
} else {
file.offset += 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);
}