linuxinstall/src/utils.h

136 lines
7.5 KiB
C
Executable File

#pragma once
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#define ERROR_BUFFER_SIZE 4096
/// @brief The global error buffer. It can be accessed using the error() function.
extern char _ERROR_BUFFER[];
/// @brief The level of importance of a log message.
typedef enum {
/// @brief A debug message. These messages are used for debugging purposes, and are not meant to be seen by the user.
LOG_LEVEL_DEBUG,
/// @brief An info message. These messages are used to inform the user about the progress of the program.
LOG_LEVEL_INFO,
/// @brief A warning message. These messages are used to warn the user about potential issues.
LOG_LEVEL_WARNING,
/// @brief An error message. These messages are used to inform the user that a critical error has occurred.
LOG_LEVEL_ERROR,
} log_level_t;
/// @brief The minimum log level of the program.
extern log_level_t _LOG_LEVEL;
/// @brief The result of an operation.
typedef int result_t;
/// @brief Sets the log level of the program.
/// @param level The log level to set.
void set_log_level(log_level_t level);
/// @brief Logs a message with the given level, and the given format string and arguments.
/// @param level The level of the message.
/// @param format The format string to use for the message.
/// @param ... The arguments to use for the format string.
void log_message(log_level_t level, const char* format, ...);
/// @brief Returns a generic success result.
/// @return The success result.
result_t success(void);
/// @brief Returns a generic failure result, with an error message formatted using the given format string and arguments. The error message is stored in a global buffer, and can be retrieved using the error() function.
/// @param format The format string to use for the error message.
/// @param ... The arguments to use for the format string.
/// @return The failure result.
result_t failure(const char* format, ...);
/// @brief Returns the error message from the last failure result.
/// @return The error message from the last failure result.
const char* error(void);
/// @brief Formats a string using the given format string and arguments, and stores the result in the given string pointer.
/// @param _str The string pointer to store the resulting string in. The caller is responsible for freeing the string.
/// @param format The format string to use for the string.
/// @param ... The arguments to use for the format string.
/// @return The result of the operation.
result_t format(char** _str, const char* format, ...);
/// @brief Extracts a substring from the given string, starting at the given index and with the given length, and stores the resulting string in the given string pointer.
/// @param _str The string pointer to store the resulting string in. The caller is responsible for freeing the string.
/// @param str The string to extract the substring from.
/// @param start The index to start the substring at.
/// @param length The length of the substring. If the length goes beyond the end of the string, the substring will be truncated to the end of the string.
/// @return The result of the operation.
result_t substring(char** _str, const char* str, size_t start, size_t length);
/// @brief Formats a size in bytes to a human-readable string, and stores the resulting string in the given string pointer.
/// @param _str The string pointer to store the resulting string in. The caller is responsible for freeing the string.
/// @param size The size in bytes to format.
/// @return The result of the operation.
result_t format_size(char** _str, uint64_t size);
/// @brief Parses a size from the given string, and stores the resulting integer in the given size pointer.
/// @param _size The size pointer to store the resulting integer in.
/// @param str The string to parse the size from.
/// @return The result of the operation.
result_t parse_size(uint64_t* _size, const char* str);
/// @brief Executes the given command, and stores the resulting exit code in the given exit code pointer, and the standard output and standard error in the given string pointers.
/// @param _exit_code The exit code pointer to store the resulting exit code in.
/// @param _stdoutbuf The standard output string pointer to store the resulting string in. The caller is responsible for freeing the string. This parameter can be NULL if the standard output is not needed.
/// @param _stderrbuf The standard error string pointer to store the resulting string in. The caller is responsible for freeing the string. This parameter can be NULL if the standard error is not needed.
/// @param working_directory The working directory to execute the command in. If this parameter is NULL, the command will be executed in the current working directory.
/// @param executable The command to execute.
/// @param ... The arguments to pass to the command. The last argument must be NULL. No need to pass the executable as the first argument.
/// @return The result of the operation.
result_t execute(int* _exit_code, char** _stdoutbuf, char** _stderrbuf, const char* working_directory, const char* executable, ...);
// @brief Reads the given file descriptor, and stores the resulting string in the given string pointer. The string can contain null characters.
// @param _str The string pointer to store the resulting string in. The caller is responsible for freeing the string.
// @param fd The file descriptor to read from.
// @return The result of the operation.
result_t read_fd(char** _str, int fd);
// @brief Writes the given string to the given file descriptor.
// @param fd The file descriptor to write to.
// @param str The string to write.
// @return The result of the operation.
result_t write_fd(int fd, const char* str);
/// @brief Reads the given file, and stores the resulting string in the given string pointer. The string can contain null characters.
/// @param _str The string pointer to store the resulting string in. The caller is responsible for freeing the string.
/// @param path The path to the file to read.
/// @return The result of the operation.
result_t read_file(char** _str, const char* path);
/// @brief Writes the given string to the given file.
/// @param path The path to the file to write to.
/// @param str The string to write.
/// @return The result of the operation.
result_t write_file(const char* path, const char* str);
/// @brief Copies the given file to the given destination.
/// @param src The path to the file to copy.
/// @param dst The path to the destination to copy the file to.
/// @return The result of the operation.
result_t copy_file(const char* src, const char* dst);
/// @brief Lists the files in the given directory, and stores the resulting array in the given string array pointer. A filter can be provided to only include files that match the filter.
/// @param _files The string array pointer to store the resulting array in. The caller is responsible for freeing the strings and the array.
/// @param path The path to the directory to list the files in.
/// @param filter The filter to use to only include files that match the filter. This parameter can be NULL if no filter is needed.
/// @return The result of the operation.
result_t list_files(char*** _files, const char* path, bool (*filter)(const char*));
/// @brief Calculates the MD5 checksum of the given file, and stores the resulting hash in the given string pointer.
/// @param _md5 The string pointer to store the resulting hash in. The caller is responsible for freeing the string.
/// @param path The path to the file to calculate the MD5 checksum of.
/// @return The result of the operation.
result_t md5sum(char** _md5, const char* path);