aboutsummarylogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntony Kellermann2018-09-26 09:08:22 -0400
committerAntony Kellermann2018-09-26 09:08:22 -0400
commitb26a8b08fa9bae9ccdc0bbf37dc3571652d9f7a4 (patch)
tree20558f3b4ea60059ab49e2d876dde4e9e094af91
parente18930b85bb3a82cdd3b4958f534a593ef9e0649 (diff)
downloadaur-b26a8b08fa9bae9ccdc0bbf37dc3571652d9f7a4.tar.gz
Moved graph fill empty function to api
-rw-r--r--api.c52
-rw-r--r--api.h8
-rw-r--r--curses_win.c30
-rw-r--r--curses_win.h10
4 files changed, 47 insertions, 53 deletions
diff --git a/api.c b/api.c
index 11c883ce5047..77cbe41835ac 100644
--- a/api.c
+++ b/api.c
@@ -278,28 +278,29 @@ void* api_alphavantage_store_info(void* vpInfo) {
symbol_info->api_provider = API_PROVIDER_ALPHAVANTAGE;
- size_t len = string_get_num_lines(pString) - 1, idx = 0;
- if (len > 1260) // 5 years
- len = 1260;
+ symbol_info->num_points = (int) string_get_num_lines(pString) - 1;
+ size_t idx = 0;
+ if (symbol_info->num_points > 1260) // 5 years
+ symbol_info->num_points = 1260;
- symbol_info->points = calloc(len + 1, sizeof(double));
+ symbol_info->points = calloc((size_t) symbol_info->num_points, sizeof(double));
pointer_alloc_check(symbol_info->points);
csv_goto_next_line(pString, &idx); // skip columns line
- for (int i = (int) len - 1; i >= 0; i--) {
+ for (int i = symbol_info->num_points - 1; i >= 0; i--) {
for (size_t j = 0; j < 4; j++)
csv_goto_next_value(pString, &idx);
symbol_info->points[i] = csv_read_next_double(pString, &idx);
- if (symbol_info->points[i] == 0 && i < (int) len - 1) // API Error
+ if (symbol_info->points[i] == 0 && i < symbol_info->num_points - 1) // API Error
symbol_info->points[i] = symbol_info->points[i + 1];
csv_goto_next_line(pString, &idx);
}
- symbol_info->price = symbol_info->points[len - 1];
- symbol_info->price_last_close = symbol_info->points[len - 2];
- symbol_info->price_7d = symbol_info->points[len - 6];
- symbol_info->price_30d = symbol_info->points[len - 22];
+ symbol_info->price = symbol_info->points[symbol_info->num_points - 1];
+ symbol_info->price_last_close = symbol_info->points[symbol_info->num_points - 2];
+ symbol_info->price_7d = symbol_info->points[symbol_info->num_points - 6];
+ symbol_info->price_30d = symbol_info->points[symbol_info->num_points - 22];
string_destroy(&pString);
return vpInfo;
}
@@ -529,19 +530,19 @@ void info_store_quote_json(Info* pInfo, const Json* jquote) {
void info_store_chart_json(Info* pInfo, const Json* jchart) {
free(pInfo->points);
- size_t len = json_object_array_length(jchart);
- pInfo->points = calloc(len + 1, sizeof(double));
+ pInfo->num_points = (int) json_object_array_length(jchart);
+ pInfo->points = calloc((size_t) pInfo->num_points, sizeof(double));
pointer_alloc_check(pInfo->points);
- for (size_t i = 0; i < len; i++)
+ for (int i = 0; i < pInfo->num_points; i++)
pInfo->points[i] = json_object_get_double(
json_object_object_get(json_object_array_get_idx(jchart, i), "close"));
if (pInfo->price_last_close == EMPTY) // May be 0 over weekend, so get last close from points array
- pInfo->price_last_close = pInfo->points[len - 1];
- if (len > 5)
- pInfo->price_7d = pInfo->points[len - 5];
- if (len > 21)
- pInfo->price_30d = pInfo->points[len - 21];
- if (len < 25) // 1 month api data
+ pInfo->price_last_close = pInfo->points[pInfo->num_points - 1];
+ if (pInfo->num_points > 5)
+ pInfo->price_7d = pInfo->points[pInfo->num_points - 5];
+ if (pInfo->num_points > 21)
+ pInfo->price_30d = pInfo->points[pInfo->num_points - 21];
+ if (pInfo->num_points < 25) // 1 month api data
pInfo->price_30d = pInfo->points[0];
}
@@ -716,6 +717,19 @@ Info* info_array_find_symbol_recursive(const Info_Array* pInfo_Array, const char
return NULL;
}
+void info_chart_fill_empty(Info* pInfo, int trading_days) {
+ // Realloc for number of trading days
+ pInfo->points = realloc(pInfo->points, sizeof(double) * trading_days);
+ pointer_alloc_check(pInfo->points);
+ // Move points to end
+ int difference = trading_days - pInfo->num_points;
+ memmove(&pInfo->points[difference], pInfo->points, sizeof(double) * pInfo->num_points);
+ for (int i = 0; i < difference; i++) // Initialize newly allocated bytes as EMPTY
+ pInfo->points[i] = EMPTY;
+
+ pInfo->num_points = trading_days;
+}
+
void ref_data_destroy(Ref_Data** phRef_Data) {
if (*phRef_Data == NULL)
return;
diff --git a/api.h b/api.h
index f03fd20aea10..f900436c80d1 100644
--- a/api.h
+++ b/api.h
@@ -391,6 +391,14 @@ int ref_data_get_index_from_symbol_bsearch(const Ref_Data* pRef_Data, const char
Info* info_array_find_symbol_recursive(const Info_Array* pInfo_Array, const char* symbol);
/**
+ * Reallocates pInfo->points with size trading days. Moves all values to end of the array and sets
+ * values not initialized as EMPTY.
+ * @param pInfo the Info to modify
+ * @param trading_days the size to realloc
+ */
+void info_chart_fill_empty(Info* pInfo, int trading_days);
+
+/**
* Destroys Ref_Data object and frees memory. Sets the pointer of the Ref_Data to NULL
* @param phRef_Data the Ref_Data to destroy
*/
diff --git a/curses_win.c b/curses_win.c
index 6293a2935854..1439e838b43d 100644
--- a/curses_win.c
+++ b/curses_win.c
@@ -444,19 +444,12 @@ void graph_printw(WINDOW* window, Info* symbol_info, Info* symbol_info2) {
// Calculate total number of trading days between today and five years ago
int trading_days = (int) ((1.0 / DAYS_TO_BUSINESS_DAYS_RATIO) * seconds / 86400.0);
- int total_data_points = 0, total_data_points2 = 0;
- for (int i = 0; symbol_info->points[i] != '\0'; i++) // Calculate total number of data points in the security. This may not
- total_data_points++; // be equal to trading days if the security is younger than 5 years old
+ // If younger than 5 years, realloc with num of trading days and fill with EMPTY
+ if (trading_days - symbol_info->num_points > 0)
+ info_chart_fill_empty(symbol_info, trading_days);
- if (trading_days - total_data_points > 0) // If younger than 5, realloc with num of trading days and fill with EMPTY
- symbol_info->points = graph_fill_empty(symbol_info->points, total_data_points, trading_days);
-
- if (symbol_info2 != NULL) {
- for (int i = 0; symbol_info2->points[i] != '\0'; i++)
- total_data_points2++;
- if (trading_days - total_data_points2 > 0)
- symbol_info2->points = graph_fill_empty(symbol_info2->points, total_data_points2, trading_days);
- }
+ if (symbol_info2 != NULL && trading_days - symbol_info2->num_points > 0)
+ info_chart_fill_empty(symbol_info2, trading_days);
int ch, zoom = ZOOM_5y;
graph_draw(window, symbol_info, symbol_info2, &start_date, zoom); // Initial graph of 5 year history
@@ -618,15 +611,4 @@ void graph_draw(WINDOW* window, Info* symbol_info, Info* symbol_info2, struct tm
wattroff(window, A_STANDOUT);
waddch(window, ' ');
}
-}
-
-double* graph_fill_empty(double* points, int size, int trading_days) {
- int difference = trading_days - size;
- points = realloc(points, (size_t) sizeof(double) * (trading_days + 1)); // Realloc for number of trading days
- pointer_alloc_check(points);
- points[trading_days] = '\0';
- memmove(&points[difference], points, sizeof(double) * size); // Move points to end
- for (int i = 0; i < difference; i++) // Initialize newly allocated bytes as EMPTY
- points[i] = EMPTY;
- return points;
-}
+} \ No newline at end of file
diff --git a/curses_win.h b/curses_win.h
index 21bd6505208d..661536d11798 100644
--- a/curses_win.h
+++ b/curses_win.h
@@ -148,14 +148,4 @@ void graph_printw(WINDOW* window, Info* symbol_info, Info* symbol_info2);
*/
void graph_draw(WINDOW* window, Info* symbol_info, Info* symbol_info2, struct tm* start_time, int zoom);
-/**
- * Reallocates the given array with size trading days. Moves all values to end of the array and sets
- * values not initialized as EMPTY.
- * @param points the array to realloc
- * @param size the size of points
- * @param trading_days the size to realloc
- * @return the reallocated array
- */
-double* graph_fill_empty(double* points, int size, int trading_days);
-
#endif \ No newline at end of file