aboutsummarylogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main.c12
-rw-r--r--portfolio.c84
-rw-r--r--portfolio.h12
3 files changed, 24 insertions, 84 deletions
diff --git a/main.c b/main.c
index c28579642ea0..ee8bb9a2134b 100644
--- a/main.c
+++ b/main.c
@@ -53,17 +53,9 @@ int main(int argc, char* argv[]) {
// Check
else if (strcmp(cmd, "check") == 0) {
- if (argc < 3) {
+ if (argc == 2 || strcmp(sym, "ALL") == 0)
portfolio_print_all();
- } else {
- if (strcmp(sym, "ALL") == 0)
- portfolio_print_all();
- else {
- double* data = portfolio_print_stock(sym, NULL);
- if (data != NULL)
- free(data);
- }
- }
+ else portfolio_print_stock(sym);
}
// Portfolio
diff --git a/portfolio.c b/portfolio.c
index 5f277fb6f7cb..0be56535129e 100644
--- a/portfolio.c
+++ b/portfolio.c
@@ -226,8 +226,8 @@ void portfolio_print_all(void) {
sec_data = sda_data->sec_data[i];
portfolio_store_api_data(sec_data);
printf("%8.2lf %6s %8.2lf %8.2lf %8.2lf (%6.2lf%%) %8.2lf (%6.2lf%%)\n", sec_data->amount, sec_data->symbol,
- sec_data->current_value, sec_data->total_spent, sec_data->total_profit, sec_data->total_profit_percent,
- sec_data->one_day_profit, sec_data->one_day_profit_percent);
+ sec_data->current_value, sec_data->total_spent, sec_data->total_profit, sec_data->total_profit_percent,
+ sec_data->one_day_profit, sec_data->one_day_profit_percent);
total_owned += sec_data->current_value;
total_spent += sec_data->total_spent;
total_profit_1d += sec_data->one_day_profit;
@@ -238,72 +238,26 @@ void portfolio_print_all(void) {
sda_destroy(&sda_data);
}
-double* portfolio_print_stock(char* ticker_name_string, Json* current_index) {
- char symbol[32];
- double* data = malloc(sizeof(double) * 3);
- pointer_alloc_check(data);
- String* pString = NULL;
- char* password = NULL;
- Json* jobj = NULL;
- if (current_index != NULL) { //if being called from portfolio_print_all
- strcpy(symbol, json_object_get_string(json_object_object_get(current_index, "Symbol")));
- strip_char(symbol, '\"');
- data[0] = json_object_get_double(json_object_object_get(current_index, "Shares"));
- data[1] = json_object_get_double(json_object_object_get(current_index, "USD_Spent"));
- } else { //if being called directly from main
- strcpy(symbol, ticker_name_string);
- pString = portfolio_file_get_string();
- if (pString == NULL || pString->len == 0) {
- puts("Empty portfolio.");
- free(data);
- if (pString != NULL)
- string_destroy(&pString);
- return NULL;
- }
- jobj = json_tokener_parse(pString->data);
- 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) {
- free(data);
- return NULL;
- }
- jobj = json_tokener_parse(pString->data);
- }
- int index = portfolio_symbol_index(symbol, jobj);
- if (index == -1) {
- printf("You do not have %s in your portfolio.\n", symbol);
- return data;
+void portfolio_print_stock(char* ticker_name_string) {
+ SDA* sda_data = portfolio_get_data_array();
+ SD* sec_data = NULL;
+ for (size_t i = 0; i < sda_data->length; i++) {
+ if (strcmp(sda_data->sec_data[i]->symbol, ticker_name_string) == 0) {
+ sec_data = sda_data->sec_data[i];
+ break;
}
- current_index = json_object_array_get_idx(jobj, (size_t) index);
- printf(" AMOUNT SYMBOL VALUE SPENT PROFIT (%%) 24H (%%)\n");
}
- data[0] = json_object_get_double(json_object_object_get(current_index, "Shares"));
- data[1] = json_object_get_double(json_object_object_get(current_index, "USD_Spent"));
- data[2] = 0;
- double* ticker_data, ticker_current_price_usd = 1, ticker_1d_price_usd, ticker_1d_percent_change = 0;
- if (strcmp(symbol, "USD$") != 0) {
- ticker_data = api_get_current_price(symbol);
- ticker_current_price_usd = ticker_data[0];
- ticker_1d_price_usd = ticker_data[1];
- free(ticker_data);
- data[2] = ((ticker_current_price_usd - ticker_1d_price_usd) * data[0]);
- ticker_1d_percent_change = 100 * (ticker_current_price_usd / ticker_1d_price_usd - 1);
- data[0] *= ticker_current_price_usd;
+ if (sec_data == NULL) {
+ printf("Your portfolio does not contain any %s.\n", ticker_name_string);
+ sda_destroy(&sda_data);
+ return;
}
- printf("%8.2lf %6s %8.2lf %8.2lf %8.2lf (%6.2lf%%) %8.2lf (%6.2lf%%)\n",
- data[0] / ticker_current_price_usd, symbol, data[0], data[1], data[0] - data[1],
- (100 * (data[0] - data[1])) / data[1],
- data[2], ticker_1d_percent_change);
- if (password != NULL)
- free(password);
- json_object_put(jobj);
- if (pString != NULL)
- string_destroy(&pString);
- return data;
+ portfolio_store_api_data(sec_data);
+ printf(" AMOUNT SYMBOL VALUE SPENT PROFIT (%%) 24H (%%)\n");
+ printf("%8.2lf %6s %8.2lf %8.2lf %8.2lf (%6.2lf%%) %8.2lf (%6.2lf%%)\n", sec_data->amount, sec_data->symbol,
+ sec_data->current_value, sec_data->total_spent, sec_data->total_profit, sec_data->total_profit_percent,
+ sec_data->one_day_profit, sec_data->one_day_profit_percent);
+ sda_destroy(&sda_data);
}
int portfolio_symbol_index(const char* ticker_name_string, Json* jarray) {
diff --git a/portfolio.h b/portfolio.h
index 00b71d190557..3d8f8405f3f9 100644
--- a/portfolio.h
+++ b/portfolio.h
@@ -100,17 +100,11 @@ void portfolio_store_api_data(SD* sec_data);
void portfolio_print_all(void);
/**
- * Precondition: portfolio_file has been initialized
- * Prints to stdout information about every security contained in the portfolio: symbol, number of shares, USD spent,
+ * 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 or NULL is passing in JSON index
- * @param current_index JSON index to print or NULL if passing in security's name or symbol
- * @return double array containing the indices:
- * [0] -- current balance
- * [1] -- amount spent
- * [2] -- 24h gain
+ * @param ticker_name_string the security to print
*/
-double* portfolio_print_stock(char* ticker_name_string, Json* current_index);
+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