cosms-core/include/cosms-core/file.h
2025-12-20 20:53:39 +01:00

130 lines
5 KiB
C

/*
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
*
* This software is licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License in the file LICENSE or at
* http://www.apache.org/licenses/LICENSE-2.0
*/
#ifndef COSMS_CORE_FILE
#define COSMS_CORE_FILE
#if defined(_WIN32)
#include <windows.h>
#endif
typedef enum {
COSMS_CORE_FILE_OK = 0,
COSMS_CORE_FILE_NOT_FOUND = -1,
COSMS_CORE_FILE_NO_ACCESS = -2,
COSMS_CORE_FILE_LIMIT_REACHED = -3,
COSMS_CORE_FILE_COULD_NOT_READ_SIZE = -4,
COSMS_CORE_FILE_UNKOWN_ERROR = -5,
COSMS_CORE_FILE_FAILED_TO_ALLOCATE = -6,
COSMS_CORE_FILE_FAILED_TO_READ = -7,
COSMS_CORE_FILE_FAILED_TO_WRITE = -8,
COSMS_CORE_FILE_STILL_OPEN = -9,
COSMS_CORE_FILE_INVALID_FILE = -10,
COSMS_CORE_FILE_INVALID_OPERATION = -11
} CosmsCoreFileError;
#define COSMS_CORE_FILE_LARGE 0x7FFFFFFF
#define COSMS_CORE_FILE_MODE_READ (1u << 0)
#define COSMS_CORE_FILE_MODE_WRITE (1u << 1)
#define COSMS_CORE_FILE_MODE_APPEND (1u << 2)
#define COSMS_CORE_FILE_MODE_CREATE (1u << 3)
struct cosms_core_file {
int mode;
#if defined(__GNUC__)
int native_file;
#elif defined(_WIN32)
HANDLE native_file;
#endif
};
/**
* @brief Opens file in specified mode.
*
* @param file Pointer to file struct (must not be NULL)
* @param path to the file to open
* @param mode to open the file in (read, write, apped, create or a combination of these)
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode);
/**
* @brief Closes an open file.
*
* @param file Pointer to file struct that has been opened (must not be NULL)
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file);
/**
* @brief Gets the size of an opened file.
*
* @param file Pointer to file struct that has been opned (must not be NULL)
* @param size Pointer to the variable to store the size in (must not be NULL)
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size);
/**
* @brief Try's to reads specified number of bytes.
*
* @param file Pointer to file struct that has been opened in read mode (must not be NULL)
* @param buffer to store the file content in (has to be preallocated stack or heap memory)
* @param bytes_to_read from the file.
* @param bytes_read Pointer to the variable to store the amount of bytes that where actually read (can be NULL)
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_read, unsigned int *bytes_read);
/**
* @brief Reads the whole file with support for reading large files.
*
* @param file Pointer to file struct that has been opened in read mode (must not be NULL)
* @param buffer Pointer to buffer to store the file content in (needs to be empty)
* @param bytes_read Pointer to the variable to store the amount of bytes that where actually read (can be NULL)
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char **buffer, unsigned long long *bytes_read);
/**
* @brief Try's to write specified number of bytes.
*
* @param file Pointer to file struct that has been opened in write mode (must not be NULL)
* @param buffer that contains the bytes to write (must not be NULL)
* @param bytes_to_write to the file
* @param bytes_written Pointer to the variable to store the amount of bytes that where actually written (can be NULL)
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_write, unsigned int *bytes_written);
/**
* @brief Writes the full buffer to the file with support for writting large files.
*
* @param file Pointer to file struct that has been opened in write mode (must not be NULL)
* @param buffer that contains the bytes to write (must not be NULL)
* @param bytes_to_write to the file
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char *buffer, unsigned long long bytes_to_write);
/**
* @brief Deletes specified file from system.
*
* @param path to the file to delete
* @return COSMS_CORE_FILE_OK on success, or a negative error code
*/
CosmsCoreFileError cosms_core_file_delete(const char *path);
/**
* @brief Converts error code to a string that can be used for printing/logging errors.
*
* @param error The error code to convert to a string message.
* @return The error as a string on success, or NULL
*/
const char *cosms_core_file_error_string(CosmsCoreFileError error);
#endif /* COSMS_CORE_FILE */