diff options
-rw-r--r-- | api.c | 12 | ||||
-rw-r--r-- | api.h | 6 | ||||
-rw-r--r-- | graph.c | 14 | ||||
-rw-r--r-- | portfolio.h | 21 | ||||
-rw-r--r-- | string-tick.c | 8 | ||||
-rw-r--r-- | string-tick.h | 8 |
6 files changed, 24 insertions, 45 deletions
@@ -157,8 +157,9 @@ double* coinmarketcap_get_price(const char* ticker_name_string) { } double* api_get_hist_5y(const char* ticker_name_string) { - double* val; - val = iex_get_hist_5y(ticker_name_string); // First tries IEX + if (strlen(ticker_name_string) > 5) // Cryptocurrency + return NULL; + double* val = iex_get_hist_5y(ticker_name_string); // First tries IEX if (val != NULL) return val; val = morningstar_get_hist_5y(ticker_name_string); // Secondly tries Morningstar @@ -179,11 +180,8 @@ double* iex_get_hist_5y(const char* ticker_name_string) { size_t len = json_object_array_length(jobj); double* api_data = calloc(len + 1, sizeof(double)); pointer_alloc_check(api_data); - Json* temp; - for (int i = 0; i < (int) len; i++) { - temp = json_object_array_get_idx(jobj, (size_t) i); - api_data[i] = json_object_get_double(json_object_object_get(temp, "close")); - } + for (size_t i = 0; i < len; i++) + api_data[i] = json_object_get_double(json_object_object_get(json_object_array_get_idx(jobj, i), "close")); json_object_put(jobj); string_destroy(&pString); return api_data; @@ -16,7 +16,7 @@ #include <json-c/json_tokener.h> #include "string-tick.h" -struct info { +typedef struct info { char* name; // Name of security (ex. Apple Inc.) char* symbol; // Symbol of security (ex. AAPL) double price; // Current price of security in USD (ex. 174.54) @@ -24,9 +24,7 @@ struct info { double div_yield; // Percent dividend yield (ex. 1.46) long marketcap; // Market cap in USD (ex. 890489000000) long volume_1d; // Volume in shares of security (ex. 33812360) -}; - -typedef struct info Info; +} Info; /** * Creates and returns an Info object. name and symbol are allocated 64 bytes. the doubles and long are set to EMPTY @@ -1,17 +1,12 @@ #include "graph.h" -int zoom_months[9], zoom_change_x_months[9]; +int zoom_months[] = {60, 48, 36, 24, 12, 9, 6, 3, 1}, zoom_change_x_months[] = {12, 12, 12, 12, 12, 3, 3, 3, 2}; void graph_main(const char* ticker_name_string) { double* price_data = api_get_hist_5y(ticker_name_string); if (price_data == NULL) // If invalid symbol or cryptocurrency RET_MSG("Invalid symbol.") - int temp[] = {60, 48, 36, 24, 12, 9, 6, 3, 1}; - memcpy(zoom_months, temp, sizeof(zoom_months)); - int temp2[] = {12, 12, 12, 12, 12, 3, 3, 3, 2}; - memcpy(zoom_change_x_months, temp2, sizeof(zoom_change_x_months)); - initscr(); noecho(); // Don't echo keystrokes keypad(stdscr, TRUE); // Enables extra keystrokes @@ -34,11 +29,7 @@ void graph_main(const char* ticker_name_string) { graph_print(price_data, &start_date, zoom); // Initial graph of 5 year history - while (1) { // Main input loop - ch = getch(); - if (ch == 'q') // Quits program on "q" - break; - + while ((ch = getch()) != 'q') { // Main input loop -- end if keypress 'q' if ((ch == KEY_UP && zoom != ZOOM_1m) || (ch == KEY_DOWN && zoom != ZOOM_5y) || (zoom != ZOOM_5y && (ch == KEY_LEFT || ch == KEY_RIGHT))) { // UP / DOWN / LEFT / RIGHT if (ch == KEY_UP) { @@ -76,6 +67,7 @@ void graph_print(const double* points, struct tm* start_time, int zoom) { int cols, rows; getmaxyx(stdscr, rows, cols); cols -= 11; // 10 offset to give space for graph labels + 1 for right side + rows -= 3; // Make space for zoom indicator rows -= rows % ROWS_SPACING; // Round down to multiple of 5 if (cols < 10 || rows < 10) // Exits if the terminal is too small RET_MSG("Terminal not large enough.") diff --git a/portfolio.h b/portfolio.h index 2836e4f11592..3e7f7f1008ad 100644 --- a/portfolio.h +++ b/portfolio.h @@ -17,27 +17,20 @@ #define SORT_PROFIT_1D 3 #define SORT_PROFIT_7D 4 -struct security_data { +typedef 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; -}; + double total_profit, total_profit_percent; + double one_day_profit, one_day_profit_percent; + double seven_day_profit, seven_day_profit_percent; +} SD; -typedef struct security_data SD; - -struct security_data_array { +typedef struct security_data_array { SD** sec_data; size_t length; // Elements in array -}; - -typedef struct security_data_array SDA; +} SDA; extern char* portfolio_file; diff --git a/string-tick.c b/string-tick.c index a65a4d58b31c..55b04aa28e60 100644 --- a/string-tick.c +++ b/string-tick.c @@ -34,6 +34,8 @@ char* strip_char(char* string, char c) { void string_write_portfolio(String* pString) { FILE* fp = fopen(portfolio_file, "w"); + if (fp == NULL) + RET_MSG("Error opening file.") if (fwrite(pString->data, sizeof(char), pString->len, fp) != pString->len) puts("Error writing file."); fclose(fp); @@ -47,8 +49,6 @@ void string_destroy(String** phString) { } void pointer_alloc_check(void* alloced) { - if (alloced == NULL) { - fprintf(stderr, "alloc failed!\n"); - exit(EXIT_FAILURE); - } + if (alloced == NULL) + EXIT_MSG("alloc failed!") }
\ No newline at end of file diff --git a/string-tick.h b/string-tick.h index b06f82b3f97e..71309f7849e3 100644 --- a/string-tick.h +++ b/string-tick.h @@ -17,7 +17,7 @@ } #define EXIT_MSG(msg) {\ - puts(msg);\ + fprintf(stderr, "%s\n", msg);\ exit(EXIT_FAILURE);\ } @@ -26,12 +26,10 @@ #include <string.h> #include <ctype.h> -struct string { +typedef struct string { char* data; size_t len; -}; - -typedef struct string String; +} String; typedef struct json_object Json; |