linuxinstall/src/utils.h

90 lines
3.7 KiB
C
Raw Normal View History

2024-02-15 19:42:22 +01:00
#pragma once
#include <stdbool.h>
#include <stdint.h>
2024-02-17 14:21:14 +01:00
#include <sys/types.h>
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
2024-02-15 19:42:22 +01:00
typedef enum {
SUCCESS,
FAILURE,
OUT_OF_MEMORY,
} Result;
typedef enum {
LOG_LEVEL_DEBUG,
LOG_LEVEL_WARNING,
LOG_LEVEL_ERROR,
2024-02-15 19:42:22 +01:00
} LogLevel;
extern LogLevel log_level;
2024-02-15 19:42:22 +01:00
/// @brief Sets the log level.
/// @param level The log level to set.
void set_log_level(LogLevel level);
2024-02-15 19:42:22 +01:00
/// @brief Logs a message.
/// @param level The log level.
/// @param format The format string.
/// @param ... The format arguments.
void log_message(LogLevel level, const char* format, ...);
2024-02-15 19:42:22 +01:00
/// @brief Formats a string.
2024-02-17 01:20:20 +01:00
/// @param out_string The pointer to the output string. The caller is responsible for freeing the memory.
/// @param fmt The format string.
/// @param ... The format arguments.
/// @return The result of the operation.
2024-02-17 01:20:20 +01:00
Result format(char** out_string, const char* fmt, ...);
/// @brief Runs an external executable with the given arguments, and waits for it to finish, then returns the exit code and the output.
/// @param out_exit_code The pointer to the output exit code. This argument can be NULL if the exit code is not needed.
/// @param out_stdout The pointer to the output stdout string. This argument can be NULL if the output is not needed. The caller is responsible for freeing the memory.
/// @param out_stderr The pointer to the output stderr string. This argument can be NULL if the output is not needed. The caller is responsible for freeing the memory.
/// @param executable The path of the executable to run.
/// @param ... The arguments of the executable. The last argument must be NULL. Note: No need to include the executable path in the arguments.
/// @return The result of the operation.
Result run_executable(int* out_exit_code, char** out_stdout, char** out_stderr, const char* executable, ...);
/// @brief Reads the contents of a file descriptor into a string.
/// @param fd The file descriptor to read from.
/// @param out_string The pointer to the output string. The caller is responsible for freeing the memory.
/// @return The result of the operation.
2024-02-17 12:34:24 +01:00
Result read_fd(int fd, char** out_string);
/// @brief Reads the contents of a file into a string.
/// @param path The path of the file to read.
/// @param out_string The pointer to the output string. The caller is responsible for freeing the memory.
/// @return The result of the operation.
Result read_file(const char* path, char** out_string);
/// @brief Writes the given string to a file descriptor.
/// @param fd The file descriptor to write to.
/// @param string The string to write.
/// @return The result of the operation.
Result write_fd(int fd, const char* string);
/// @brief Writes the given string to a file.
/// @param path The path of the file to write.
/// @param string The string to write.
/// @return The result of the operation.
Result write_file(const char* path, const char* string);
2024-02-17 14:21:14 +01:00
/// @brief Copies a file.
/// @param src_path The path of the source file.
/// @param dst_path The path of the destination file.
/// @param mode The mode of the destination file.
/// @return The result of the operation.
Result copy_file(const char* src_path, const char* dst_path, mode_t mode);
/// @brief Converts a size in bytes to a human-readable string.
/// @param size The size in bytes.
/// @param out_string The pointer to the output string. The caller is responsible for freeing the memory.
/// @return The result of the operation.
2024-02-17 14:21:14 +01:00
Result format_size(uint64_t size, char** out_string);
2024-02-17 14:38:52 +01:00
/// @brief Parses a size from a human-readable string.
/// @param string The string to parse.
/// @param out_size The pointer to the output size.
/// @return The result of the operation.
Result parse_size(const char* string, uint64_t* out_size);