mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 23:04:08 +10:00
added basic read, and an improved HAL
This commit is contained in:
71
hal/hal_file.h
Normal file
71
hal/hal_file.h
Normal file
@@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
|
||||
// Enumeration of all file-related error codes
|
||||
typedef enum hal_file_code {
|
||||
HAL_SUCCESS, // Operation was successful
|
||||
HAL_ERROR_INIT, // Error during initialization
|
||||
HAL_ERROR_ALLOC, // Memory allocation error
|
||||
HAL_ERROR_OPEN, // Error while opening a file
|
||||
HAL_ERROR_INPUT, // Invalid input parameter(s)
|
||||
HAL_ERROR_READ, // Error while reading from a file
|
||||
HAL_ERROR_WRITE, // Error while writing to a file
|
||||
HAL_ERROR_CLOSE, // Error while closing a file
|
||||
HAL_ERROR_RENAME, // Error while renaming a file
|
||||
HAL_ERROR_REMOVE, // Error while removing (deleting) a file
|
||||
HAL_ERROR_TEARDOWN, // Error during teardown
|
||||
HAL_ERROR_MAX, // Maximum error code value (used for range checking)
|
||||
} hal_file_code;
|
||||
|
||||
// Forward declaration of the implementation defined file structure
|
||||
typedef struct hal_file_type hal_file;
|
||||
|
||||
// Structure for different file operation functions
|
||||
typedef struct hal_file_operations {
|
||||
// Function: setup
|
||||
// Initialize the file handling system.
|
||||
// Returns: HAL_SUCCESS on successful initialization, or appropriate error code on failure.
|
||||
hal_file_code (*setup)();
|
||||
|
||||
// Function: open
|
||||
// Open a file with the specified filename and mode.
|
||||
// Returns: HAL_SUCCESS on successful read, or appropriate error code on failure.
|
||||
hal_file_code (*open)(hal_file** file, const char* filename, const char* mode);
|
||||
|
||||
// Function: read
|
||||
// Read data from the file into the provided buffer.
|
||||
// Returns: HAL_SUCCESS on successful read, or appropriate error code on failure.
|
||||
hal_file_code (*read)(hal_file* file, char* buffer, const int size);
|
||||
|
||||
// Function: write
|
||||
// Write the provided message to the file.
|
||||
// Returns: HAL_SUCCESS on successful write, or appropriate error code on failure.
|
||||
hal_file_code (*write)(hal_file* file, const char* message);
|
||||
|
||||
// Function: close
|
||||
// Close the file and release associated resources.
|
||||
// Returns: HAL_SUCCESS on successful close, or appropriate error code on failure.
|
||||
hal_file_code (*close)(hal_file* file);
|
||||
|
||||
|
||||
hal_file_code (*rename)(const char* oldname, const char* newname);
|
||||
|
||||
|
||||
hal_file_code (*remove)(const char* filename);
|
||||
|
||||
// Function: teardown
|
||||
// Perform necessary cleanup and teardown operations for the file handling system.
|
||||
// Returns: HAL_SUCCESS on successful teardown, or appropriate error code on failure.
|
||||
hal_file_code (*teardown)();
|
||||
} hal_file_operations;
|
||||
|
||||
// Global variable to access file operations
|
||||
extern hal_file_operations hal_file_manager;
|
||||
|
||||
// Maximum size of a filename supported by implementation
|
||||
extern const int HAL_MAX_FILENAME_SIZE;
|
||||
|
||||
// Maximum number of files that can be open simultaneously by implementation
|
||||
extern const int HAL_MAX_FILES_OPEN;
|
||||
|
||||
// End-of-File (EOF) indicator value defined by implementation
|
||||
extern const int HAL_EOF;
|
||||
25
hal/makefile
Normal file
25
hal/makefile
Normal file
@@ -0,0 +1,25 @@
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -Wextra -pedantic
|
||||
|
||||
# Library name and source files
|
||||
LIB_NAME = hal.a
|
||||
SRC_FILES = ports/hal_file.c
|
||||
BUILD_DIR = bin
|
||||
|
||||
# Object files derived from source files
|
||||
OBJ_FILES = $(SRC_FILES:.c=.o)
|
||||
|
||||
# Target: build the library
|
||||
all: $(BUILD_DIR)/$(LIB_NAME)
|
||||
|
||||
# Build the library from object files
|
||||
$(LIB_NAME): $(OBJ_FILES)
|
||||
ar rcs $@ $^
|
||||
|
||||
# Compile from object files
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -c $< -o $@
|
||||
|
||||
# Clean up generated files
|
||||
clean:
|
||||
rm -f $(LIB_NAME) $(OBJ_FILES)
|
||||
117
hal/ports/hal_file.c
Normal file
117
hal/ports/hal_file.c
Normal file
@@ -0,0 +1,117 @@
|
||||
#include "hal_file.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct hal_file_type {
|
||||
FILE* fp;
|
||||
} hal_file;
|
||||
|
||||
// Initialization logic, if needed
|
||||
hal_file_code setup() {
|
||||
return HAL_SUCCESS;
|
||||
}
|
||||
|
||||
hal_file_code open(hal_file** file, const char* filename, const char* mode) {
|
||||
if (!file || !filename || !mode) {
|
||||
return HAL_ERROR_INPUT;
|
||||
}
|
||||
|
||||
*file = (hal_file*)malloc(sizeof(hal_file));
|
||||
if (!(*file)) {
|
||||
return HAL_ERROR_ALLOC;
|
||||
}
|
||||
|
||||
(*file)->fp = fopen(filename, mode);
|
||||
if (!(*file)->fp) {
|
||||
free(*file);
|
||||
return HAL_ERROR_OPEN;
|
||||
}
|
||||
|
||||
return HAL_SUCCESS;
|
||||
}
|
||||
|
||||
hal_file_code read(hal_file* file, char* buffer, const int size) {
|
||||
if (!file || !file->fp || !buffer || size <= 0) {
|
||||
return HAL_ERROR_INPUT;
|
||||
}
|
||||
|
||||
scanf()
|
||||
|
||||
if (fgets(buffer, size, file->fp) == NULL) {
|
||||
return HAL_ERROR_READ;
|
||||
}
|
||||
|
||||
return HAL_SUCCESS;
|
||||
}
|
||||
|
||||
hal_file_code write(hal_file* file, const char* message) {
|
||||
if (!file || !file->fp || !message) {
|
||||
return HAL_ERROR_INPUT;
|
||||
}
|
||||
|
||||
if (fputs(message, file->fp) == EOF) {
|
||||
return HAL_ERROR_WRITE;
|
||||
}
|
||||
|
||||
return HAL_SUCCESS;
|
||||
}
|
||||
|
||||
hal_file_code close(hal_file* file) {
|
||||
if (!file || !file->fp) {
|
||||
return HAL_ERROR_INPUT;
|
||||
}
|
||||
|
||||
if (fclose(file->fp) != 0) {
|
||||
return HAL_ERROR_CLOSE;
|
||||
}
|
||||
|
||||
free(file);
|
||||
return HAL_SUCCESS;
|
||||
}
|
||||
|
||||
hal_file_code crename(const char* oldname, const char* newname) {
|
||||
if (!oldname || !newname) {
|
||||
return HAL_ERROR_INPUT;
|
||||
|
||||
}
|
||||
|
||||
if (rename(oldname, newname) != 0) {
|
||||
return HAL_ERROR_RENAME;
|
||||
}
|
||||
|
||||
return HAL_SUCCESS;
|
||||
}
|
||||
|
||||
hal_file_code cremove(const char* filename) {
|
||||
if (!filename) {
|
||||
return HAL_ERROR_INPUT;
|
||||
}
|
||||
|
||||
if (remove(filename) != 0) {
|
||||
return HAL_ERROR_REMOVE;
|
||||
}
|
||||
|
||||
return HAL_SUCCESS;
|
||||
}
|
||||
|
||||
// deletion logic, if needed
|
||||
hal_file_code teardown() {
|
||||
return HAL_SUCCESS;
|
||||
}
|
||||
|
||||
// Expose into global variable
|
||||
hal_file_operations hal_file_manager = {
|
||||
.setup = setup,
|
||||
.open = open,
|
||||
.read = read,
|
||||
.write = write,
|
||||
.close = close,
|
||||
.rename = crename,
|
||||
.remove = cremove,
|
||||
.teardown = teardown
|
||||
};
|
||||
|
||||
const int HAL_MAX_FILENAME_SIZE = FILENAME_MAX;
|
||||
const int HAL_MAX_FILES_OPEN = FOPEN_MAX;
|
||||
const int HAL_EOF = EOF;
|
||||
Reference in New Issue
Block a user