diff options
author | Antony Kellermann | 2018-05-01 19:27:26 -0400 |
---|---|---|
committer | Antony Kellermann | 2018-05-01 19:27:26 -0400 |
commit | 4095dcd225f63f4d42cc69fb48b6ca7b1cd8bf1f (patch) | |
tree | e847c4d274c379c9a4210145cf2ccb6066b2b5e0 | |
parent | 7711c2e04b801343837d27303f5bf067cc2ae4c0 (diff) | |
download | aur-4095dcd225f63f4d42cc69fb48b6ca7b1cd8bf1f.tar.gz |
Implemented storing portfolio data in structs
-rw-r--r-- | portfolio.c | 64 | ||||
-rw-r--r-- | portfolio.h | 34 |
2 files changed, 97 insertions, 1 deletions
diff --git a/portfolio.c b/portfolio.c index d121cbdfca0f..d54634f6c267 100644 --- a/portfolio.c +++ b/portfolio.c @@ -145,6 +145,59 @@ void portfolio_modify(const char* ticker_name_string, double quantity_shares, do string_destroy(&pString); } +SDA* portfolio_get_data_array(void) { + String* pString = portfolio_file_get_string(); + if (pString == NULL) { + puts("Empty portfolio."); + return NULL; + } + if (strcmp(pString->data, "") == 0) { + puts("Empty portfolio."); + string_destroy(&pString); + return NULL; + } + + Json* jobj = json_tokener_parse(pString->data); + char* password = NULL; + if (jobj == NULL) { //ENCRYPTED PORTFOLIO + password = rc4_getPassword(); + printf("Decrypting portfolio...\n"); + String* temp = rc4_get_crypted_string(pString, password, DECRYPT); + string_destroy(&pString); + pString = temp; + if (pString == NULL) + return NULL; + jobj = json_tokener_parse(pString->data); + if (password != NULL) + free(password); + } + + size_t portfolio_size = json_object_array_length(jobj); + if (portfolio_size == 0) + return NULL; + + SDA* portfolio_data = malloc(sizeof(SDA)); + pointer_alloc_check(portfolio_data); + portfolio_data->length = portfolio_size; + portfolio_data->sec_data = malloc(sizeof(SD*) * portfolio_data->length); + pointer_alloc_check(portfolio_data->sec_data); + + Json* json_index; + SD* tcd_index; + for (size_t i = 0; i < portfolio_data->length; i++) { + portfolio_data->sec_data[i] = malloc(sizeof(SD)); + tcd_index = portfolio_data->sec_data[i]; + pointer_alloc_check(tcd_index); + json_index = json_object_array_get_idx(jobj, i); + strcpy(tcd_index->symbol, json_object_get_string(json_object_object_get(json_index, "Symbol"))); + portfolio_data->sec_data[i]->amount = json_object_get_double(json_object_object_get(json_index, "Shares")); + tcd_index->total_spent = json_object_get_double(json_object_object_get(json_index, "USD_Spent")); + } + json_object_put(jobj); + string_destroy(&pString); + return portfolio_data; +} + void portfolio_print_all(void) { String* pString = portfolio_file_get_string(); if (pString == NULL) { @@ -204,7 +257,7 @@ double* portfolio_print_stock(char* ticker_name_string, Json* current_index) { } else { //if being called directly from main strcpy(symbol, ticker_name_string); pString = portfolio_file_get_string(); - if (pString == NULL || pString->len == 0){ + if (pString == NULL || pString->len == 0) { puts("Empty portfolio."); free(data); if (pString != NULL) @@ -267,4 +320,13 @@ int portfolio_symbol_index(const char* ticker_name_string, Json* jarray) { return i; } return -1; +} + +void sda_destroy(SDA** phSDA) { + SDA* ptr = *phSDA; + for (size_t i = 0; i < ptr->length; i++) + free(ptr->sec_data[i]); + free(ptr->sec_data); + free(ptr); + *phSDA = NULL; }
\ No newline at end of file diff --git a/portfolio.h b/portfolio.h index 8e3d6cfadd8f..eddab25af863 100644 --- a/portfolio.h +++ b/portfolio.h @@ -9,6 +9,28 @@ #define ADD 1 #define SET 2 +struct security_data { + char symbol[32]; + double amount; + double total_spent; + double current_value; + double total_profit; + double total_profit_percent; + double one_day_profit; + double one_day_profit_percent; + double seven_day_profit; + double seven_day_profit_percent; +}; + +typedef struct security_data SD; + +struct security_data_array { + SD** sec_data; + size_t length; // Elements in array +}; + +typedef struct security_data_array SDA; + extern char* portfolio_file; /** @@ -59,6 +81,12 @@ String* portfolio_file_get_string(void); void portfolio_modify(const char* ticker_name_string, double quantity_shares, double usd_spent, int option); /** + * Returns an SDA* containing data from the portfolio. + * @return + */ +SDA* portfolio_get_data_array(void); + +/** * Precondition: portfolio_file has been initialized * Prints to stdout information about every security contained in the portfolio: symbol, number of shares, USD spent, * current value, profit, and 24h profit. Additionally, print a grand total with info from all securities. @@ -89,4 +117,10 @@ double* portfolio_print_stock(char* ticker_name_string, Json* current_index); */ int portfolio_symbol_index(const char* ticker_name_string, Json* jarray); +/** + * Frees all memory associated with a SDA struct and sets the handle to NULL + * @param phSDA + */ +void sda_destroy(SDA** phSDA); + #endif
\ No newline at end of file |