Made small modifications

This commit is contained in:
2024-02-18 18:09:53 +01:00
parent c9f76746b4
commit 0da7474158
14 changed files with 1150 additions and 1713 deletions

View File

@@ -61,7 +61,7 @@ void Log(LogLevel level, const char* format, ...) {
va_end(args);
}
Status Format(char** _string, const char* fmt, ...) {
Result Format(char** _string, const char* fmt, ...) {
*_string = NULL;
va_list args;
@@ -92,7 +92,41 @@ Status Format(char** _string, const char* fmt, ...) {
return SUCCESS;
}
Status FormatSize(uint64_t size, char** _size_str) {
Result Duplicate(const char* string, char** _duplicate) {
*_duplicate = NULL;
if (string == NULL)
return SUCCESS;
*_duplicate = strdup(string);
if (*_duplicate == NULL) {
Log(LOG_LEVEL_ERROR, "Failed to duplicate the string (%s).", strerror(errno));
return FAILURE;
}
return SUCCESS;
}
Result Substring(const char* string, size_t start, size_t end, char** _substring) {
if (start > end) {
Log(LOG_LEVEL_ERROR, "Invalid substring range.");
return FAILURE;
}
size_t length = end - start;
*_substring = malloc(length + 1);
if (*_substring == NULL) {
Log(LOG_LEVEL_ERROR, "Failed to allocate memory for the substring (%s).", strerror(errno));
return FAILURE;
}
memcpy(*_substring, string + start, length);
(*_substring)[length] = '\0';
return SUCCESS;
}
Result FormatSize(uint64_t size, char** _size_str) {
*_size_str = NULL;
// Determine the unit
@@ -119,7 +153,7 @@ Status FormatSize(uint64_t size, char** _size_str) {
return Format(_size_str, "%.2f %s", value, unit);
}
Status ParseSize(const char* size_str, uint64_t* _size) {
Result ParseSize(const char* size_str, uint64_t* _size) {
*_size = 0;
// Parse the size
@@ -153,7 +187,7 @@ Status ParseSize(const char* size_str, uint64_t* _size) {
return SUCCESS;
}
Status RunExecutable(int* _exit_code, char** _stdout, char** _stderr, const char* executable, ...) {
Result RunExecutable(int* _exit_code, char** _stdout, char** _stderr, const char* executable, ...) {
if (_exit_code != NULL)
*_exit_code = -1;
if (_stdout != NULL)
@@ -301,7 +335,7 @@ Status RunExecutable(int* _exit_code, char** _stdout, char** _stderr, const char
return SUCCESS;
}
Status ReadFileDescriptor(int fd, char** _content) {
Result ReadFileDescriptor(int fd, char** _content) {
*_content = NULL;
// Allocate a buffer for the content
@@ -333,7 +367,7 @@ Status ReadFileDescriptor(int fd, char** _content) {
return SUCCESS;
}
Status WriteFileDescriptor(int fd, const char* content) {
Result WriteFileDescriptor(int fd, const char* content) {
size_t length = strlen(content);
ssize_t bytes_written = 0;
@@ -350,7 +384,7 @@ Status WriteFileDescriptor(int fd, const char* content) {
return SUCCESS;
}
Status ReadFile(const char* path, char** _content) {
Result ReadFile(const char* path, char** _content) {
*_content = NULL;
// Open the file
@@ -361,7 +395,7 @@ Status ReadFile(const char* path, char** _content) {
}
// Read the file
Status status = ReadFileDescriptor(file, _content);
Result status = ReadFileDescriptor(file, _content);
// Close the file
close(file);
@@ -369,7 +403,7 @@ Status ReadFile(const char* path, char** _content) {
return status;
}
Status WriteFile(const char* path, const char* content) {
Result WriteFile(const char* path, const char* content) {
// Open the file
int file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (file == -1) {
@@ -378,7 +412,7 @@ Status WriteFile(const char* path, const char* content) {
}
// Write the content
Status status = WriteFileDescriptor(file, content);
Result status = WriteFileDescriptor(file, content);
// Close the file
close(file);
@@ -386,7 +420,7 @@ Status WriteFile(const char* path, const char* content) {
return status;
}
Status CopyFile(const char* source_path, const char* destination_path) {
Result CopyFile(const char* source_path, const char* destination_path) {
// Open the source file
int source_file = open(source_path, O_RDONLY);
if (source_file == -1) {