Compare commits

..

2 commits

Author SHA1 Message Date
Mineplay5780
02090f42da fix(file): made some small fixes for tests and file functionality on linux 2025-12-20 21:39:48 +01:00
Mineplay5780
562eab114b docs(file): added commend to all file functions 2025-12-20 20:53:39 +01:00
3 changed files with 105 additions and 2 deletions

View file

@ -45,19 +45,86 @@ struct cosms_core_file {
#endif #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); 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); 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); 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); 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); 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); 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); 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); 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); const char *cosms_core_file_error_string(CosmsCoreFileError error);
#endif #endif /* COSMS_CORE_FILE */

View file

@ -21,6 +21,9 @@
#include <errno.h> #include <errno.h>
#endif #endif
/**
* @brief Opens file in specified mode.
*/
CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode) { CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode) {
file->mode = mode; file->mode = mode;
@ -114,6 +117,9 @@ CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
/**
* @brief Closes an open file.
*/
CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file) { CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file) {
#if defined(__GNUC__) #if defined(__GNUC__)
int error = close(file->native_file); int error = close(file->native_file);
@ -138,6 +144,9 @@ CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file) {
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
/**
* @brief Gets the size of an opened file.
*/
CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size) { CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size) {
#if defined(__GNUC__) #if defined(__GNUC__)
struct stat file_stat; struct stat file_stat;
@ -167,6 +176,9 @@ CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsign
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
/**
* @brief Try's to reads specified number of bytes.
*/
CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_read, unsigned int *bytes_read) { CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_read, unsigned int *bytes_read) {
if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) { if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) {
return COSMS_CORE_FILE_INVALID_OPERATION; return COSMS_CORE_FILE_INVALID_OPERATION;
@ -191,6 +203,9 @@ CosmsCoreFileError cosms_core_file_read(struct cosms_core_file *file, char *buff
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
/**
* @brief Reads the whole file with support for reading large files.
*/
CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char **buffer, unsigned long long *bytes_read) { CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char **buffer, unsigned long long *bytes_read) {
if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) { if ((file->mode & COSMS_CORE_FILE_MODE_READ) == 0) {
return COSMS_CORE_FILE_INVALID_OPERATION; return COSMS_CORE_FILE_INVALID_OPERATION;
@ -270,6 +285,9 @@ CosmsCoreFileError cosms_core_file_read_all(struct cosms_core_file *file, char *
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
/**
* @brief Try's to write specified number of bytes.
*/
CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_write, unsigned int *bytes_written) { CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buffer, unsigned int bytes_to_write, unsigned int *bytes_written) {
if ((file->mode & COSMS_CORE_FILE_MODE_WRITE) == 0) { if ((file->mode & COSMS_CORE_FILE_MODE_WRITE) == 0) {
return COSMS_CORE_FILE_INVALID_OPERATION; return COSMS_CORE_FILE_INVALID_OPERATION;
@ -297,6 +315,9 @@ CosmsCoreFileError cosms_core_file_write(struct cosms_core_file *file, char *buf
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
/**
* @brief Writes the full buffer to the file with support for writting large files.
*/
CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char *buffer, unsigned long long bytes_to_write) { CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char *buffer, unsigned long long bytes_to_write) {
if ((file->mode & COSMS_CORE_FILE_MODE_WRITE) == 0) { if ((file->mode & COSMS_CORE_FILE_MODE_WRITE) == 0) {
return COSMS_CORE_FILE_INVALID_OPERATION; return COSMS_CORE_FILE_INVALID_OPERATION;
@ -347,6 +368,9 @@ CosmsCoreFileError cosms_core_file_write_all(struct cosms_core_file *file, char
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
/**
* @brief Deletes specified file from system.
*/
CosmsCoreFileError cosms_core_file_delete(const char *path) { CosmsCoreFileError cosms_core_file_delete(const char *path) {
#if defined(__GNUC__) #if defined(__GNUC__)
if (unlink(path) == -1) { if (unlink(path) == -1) {
@ -387,6 +411,9 @@ CosmsCoreFileError cosms_core_file_delete(const char *path) {
return COSMS_CORE_FILE_OK; return COSMS_CORE_FILE_OK;
} }
/**
* @brief Converts error code to a string that can be used for printing/logging errors.
*/
const char *cosms_core_file_error_string(CosmsCoreFileError error) { const char *cosms_core_file_error_string(CosmsCoreFileError error) {
switch(error) { switch(error) {
case COSMS_CORE_FILE_OK: case COSMS_CORE_FILE_OK:

View file

@ -9,15 +9,24 @@
implementation \ implementation \
} }
/**
* @brief Creates a list to export so it can be imported into the main test list.
*/
#define COSMS_CORE_TEST_EXPORT(name, ...) static const struct cosms_core_test cosms_core_test_##name[] = { \ #define COSMS_CORE_TEST_EXPORT(name, ...) static const struct cosms_core_test cosms_core_test_##name[] = { \
__VA_ARGS__, \ __VA_ARGS__, \
0 \ 0 \
} }
/**
* @brief Creates a list of lists from exported tests so all tests can be easly executed.
*/
#define COSMS_CORE_TEST_START static const struct cosms_core_test *cosms_core_test_tests[] = { #define COSMS_CORE_TEST_START static const struct cosms_core_test *cosms_core_test_tests[] = {
#define COSMS_CORE_TEST_IMPORT(name) cosms_core_test_##name #define COSMS_CORE_TEST_IMPORT(name) cosms_core_test_##name
#define COSMS_CORE_TEST_END }; #define COSMS_CORE_TEST_END };
/**
* @brief Runs all tests specified in the cosms_core_test_tests array;
*/
#define COSMS_CORE_TEST_RUN() \ #define COSMS_CORE_TEST_RUN() \
for (unsigned long long index = 0; index < sizeof(cosms_core_test_tests) / sizeof(cosms_core_test_tests[0]); index += 1) { \ for (unsigned long long index = 0; index < sizeof(cosms_core_test_tests) / sizeof(cosms_core_test_tests[0]); index += 1) { \
unsigned long long test_index = 0; \ unsigned long long test_index = 0; \
@ -42,4 +51,4 @@ struct cosms_core_test {
const char *(*function)(void); const char *(*function)(void);
}; };
#endif #endif /* COSMS_CORE_TEST */