diff options
author | Antony Kellermann | 2018-06-27 11:03:06 -0400 |
---|---|---|
committer | Antony Kellermann | 2018-06-27 11:03:06 -0400 |
commit | f7da647a9210ad48535b62528f308010841db0f5 (patch) | |
tree | 972b1cdafb760f85814053050ef316745e260c34 | |
parent | 91076c0a4c3374d81ff38edf9e78d48a01ab601f (diff) | |
download | aur-f7da647a9210ad48535b62528f308010841db0f5.tar.gz |
Changed functions to file_get_string and string_write_file
-rw-r--r-- | string-tick.c | 32 | ||||
-rw-r--r-- | string-tick.h | 14 |
2 files changed, 40 insertions, 6 deletions
diff --git a/string-tick.c b/string-tick.c index 47166012241b..323e2fc37c8a 100644 --- a/string-tick.c +++ b/string-tick.c @@ -1,5 +1,4 @@ #include "string-tick.h" -#include "portfolio.h" String* string_init(void) { String* pString = (String*) malloc(sizeof(String)); @@ -45,8 +44,35 @@ char* strip_tags(char* string) { return string; } -void string_write_portfolio(String* pString) { - FILE* fp = fopen(portfolio_file, "w"); +String* file_get_string(char* file_name) { + struct stat file_info; + if (stat(file_name, &file_info)) // If called from portfolio_modify_string, file should exist (possibly size 0) + RETNULL_MSG("File doesn't exist."); + + if (file_info.st_size == 0) // Return new String if new file + return string_init(); + + FILE* fp = fopen(file_name, "r"); + if (fp == NULL) // If file exists, but cannot be opened, usually because of permissions + RETNULL_MSG("Error opening file.") + + String* pString = string_init(); + pString->len = (size_t) file_info.st_size; + pString->data = realloc(pString->data, pString->len + 1); // Alloc with file size + pointer_alloc_check(pString->data); + pString->data[pString->len] = '\0'; + if (fread(pString->data, sizeof(char), pString->len, fp) != pString->len) { // read file and return NULL if error + fclose(fp); + string_destroy(&pString); + RETNULL_MSG("Error reading file.") + } + + fclose(fp); + return pString; +} + +void string_write_file(String* pString, char* file_name) { + FILE* fp = fopen(file_name, "w"); if (fp == NULL) RET_MSG("Error opening file.") if (fwrite(pString->data, sizeof(char), pString->len, fp) != pString->len) diff --git a/string-tick.h b/string-tick.h index 93909c54fc73..f18347e53eec 100644 --- a/string-tick.h +++ b/string-tick.h @@ -25,6 +25,7 @@ #include <stdlib.h> #include <string.h> #include <ctype.h> +#include <sys/stat.h> typedef struct string { char* data; @@ -34,7 +35,7 @@ typedef struct string { typedef struct json_object Json; /** - * Creates and returns a String object with len 1 and no data + * Creates and returns a String object with len 0 and data allocated 1 byte null terminated. * @return STRING object */ String* string_init(void); @@ -67,10 +68,17 @@ char* strip_char(char* string, char c); char* strip_tags(char* string); /** - * Overwrites the portfolio with the given pString + * Returns the contents of a file in a String + * @param file_name path to file + * @return String* + */ +String* file_get_string(char* file_name); + +/** + * Writes a String to a file * @param pString the String to overwrite with */ -void string_write_portfolio(String* pString); +void string_write_file(String* pString, char* file_name); /** * Destroys String object and frees memory. Points the String to NULL. |