#ifndef PORTFOLIO_H #define PORTFOLIO_H #include #include "api.h" #include "rc4.h" #define REMOVE 0 #define ADD 1 #define SET 2 #define SORT_ALPHA 0 #define SORT_VALUE 1 #define SORT_PROFIT 2 #define SORT_PROFIT_1D 3 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; }; 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; /** * Postcondition: the char* portfolio_file (the "portfolio") is allocated on the heap and contains the * path "$HOME/.tick_portfolio.json" */ void portfolio_file_init(void); /** * Precondition: portfolio_file has been initialized * @return pointer to a allocated String containing the contents of the portfolio or NULL on error opening * or reading portfolio */ String* portfolio_file_get_string(void); /** * Precondition: portfolio_file has been initialized * Postcondition: the portoflio has been modified according to the functions arguments * * The portfolio is formatted in JSON. Each security is an object in an array. Each object contains the following * keys: "Symbol" (string), "Shares" (double), and "USD_Spent" (double). * * If the portfolio is empty, creates a new JSON array with it's only index being a JSON object containing * ticker_name_string, quantity_shares, and usd_spent as it's attributes. * * If the security is NOT already in the portfolio, checks if ticker_name_string is a valid security. If it is, * appends to the JSON array a new object containing the keys specified above. * * If the security IS already in the portfolio, modifies the values "Shares" and "USD_Spent" according to * the parameter option. SET will set the values to the parameters quantity_shares and usd_spent, REMOVE will * subtract the parameters from the values already in the portfolio, and ADD will add the parameters to the values * already in the portfolio. * * Adding or removing when quantity_shares and usd_spent are both 0 will abort the function, as well as trying to * remove more than is already contained in the portfolio. Negative numbers aren't allowed either. * * If the portfolio is encrypted, ask for the password to decrypt it. If invalid password, abort the function. If * valid password, perform the functions specified above. After this, re-encrypt the portfolio using the already * given password. * * After all this, overwrite the portfolio with the (encrypted or not) created/modified JSON array. * * @param ticker_name_string string containing the symbol of the security to add or name of the cryptocurrency * @param quantity_shares the number of shares to modify the portfolio by * @param usd_spent the amount of USD to modify the portfolio by * @param option SET, REMOVE, or ADD */ void portfolio_modify(const char* ticker_name_string, double quantity_shares, double usd_spent, int option); /** * Returns an SDA array containing data from the portfolio. * length is the number of securities in the portfolio * sec_data is an array of SD pointers to SD structs with the fields symbol, amount, and total_spent populated. * The rest of the values are uninitialized * @return SDA* */ SDA* portfolio_get_data_array(void); /** * Initializes the rest of the fields in an SD struct after symbol, amount, and total_spent have already been stored * @param sec_data pointer to SD struct */ void portfolio_store_api_data(SD* sec_data); /** * Sorts the SDA array based on the SORT mode. * SORT_ALPHA will sort the array lexicographically from least to greatest * SORT_VALUE, SORT_PROFIT, and SORT_PROFIT_1D will sort the array from greatest to least * @param sda_data array to sort * @param SORT mode to sort */ void portfolio_sort(SDA* sda_data, int SORT); /** * 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. */ void portfolio_print_all(int SORT); /** * Prints to stdout information about a specific security contained in the portfolio: symbol, number of shares, USD spent, * current value, profit, and 24h profit. * @param ticker_name_string the security to print */ void portfolio_print_stock(char* ticker_name_string); /** * Goes through the given JSON array until the JSON object at the given index's key "Symbol" contains * the string ticker_name_string, then returns that index number. * @param ticker_name_string the security to return the index of * @param jarray the JSON array * @return -1 if not found, the index number otherwise */ 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