aboutsummarylogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--api.c83
-rw-r--r--graph.c5
-rw-r--r--main.c5
-rw-r--r--portfolio.c20
-rw-r--r--rc4.c20
-rw-r--r--string-tick.c25
-rw-r--r--string-tick.h6
7 files changed, 55 insertions, 109 deletions
diff --git a/api.c b/api.c
index c55c4930a0be..75ad18b77986 100644
--- a/api.c
+++ b/api.c
@@ -2,24 +2,18 @@
Info* api_info_init(void) {
Info* pInfo = malloc(sizeof(Info));
- if (pInfo != NULL) {
- pInfo->name = malloc(64);
- pInfo->symbol = malloc(64);
- if (pInfo->name == NULL || pInfo->symbol == NULL) {
- fprintf(stderr, "malloc() failed\n");
- exit(EXIT_FAILURE);
- }
- pInfo->price = EMPTY;
- pInfo->change_1d = EMPTY;
- pInfo->change_7d = EMPTY;
- pInfo->change_30d = EMPTY;
- pInfo->div_yield = EMPTY;
- pInfo->marketcap = EMPTY;
- pInfo->volume_1d = EMPTY;
- } else {
- fprintf(stderr, "malloc() failed\n");
- exit(EXIT_FAILURE);
- }
+ pointer_alloc_check(pInfo);
+ pInfo->name = malloc(64);
+ pointer_alloc_check(pInfo->name);
+ pInfo->symbol = malloc(64);
+ pointer_alloc_check(pInfo->symbol);
+ pInfo->price = EMPTY;
+ pInfo->change_1d = EMPTY;
+ pInfo->change_7d = EMPTY;
+ pInfo->change_30d = EMPTY;
+ pInfo->div_yield = EMPTY;
+ pInfo->marketcap = EMPTY;
+ pInfo->volume_1d = EMPTY;
return pInfo;
}
@@ -27,10 +21,7 @@ size_t api_string_writefunc(void* ptr, size_t size, size_t nmemb, String* hStrin
String* pString = hString;
size_t new_len = pString->len + size * nmemb;
pString->data = realloc(pString->data, new_len + 1);
- if (pString->data == NULL) {
- fprintf(stderr, "realloc() failed\n");
- exit(EXIT_FAILURE);
- }
+ pointer_alloc_check(pString->data);
memcpy(pString->data + pString->len, ptr, size * nmemb);
pString->data[new_len] = '\0';
pString->len = new_len;
@@ -39,8 +30,6 @@ size_t api_string_writefunc(void* ptr, size_t size, size_t nmemb, String* hStrin
String* api_curl_data(char* url, char* post_field) {
String* pString = string_init();
- if (pString == NULL)
- return NULL;
CURL* curl = curl_easy_init();
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
@@ -94,10 +83,7 @@ double* iex_get_price(const char* ticker_name_string) {
}
Json* jobj = json_tokener_parse(pString->data);
double* ret = malloc(sizeof(double) * 2);
- if (ret == NULL) {
- fprintf(stderr, "malloc() failed\n");
- exit(EXIT_FAILURE);
- }
+ pointer_alloc_check(ret);
const char* price_string = json_object_to_json_string(json_object_object_get(jobj, "latestPrice"));
const char* close_price_string = json_object_to_json_string(json_object_object_get(jobj, "previousClose"));
ret[0] = strtod(price_string, NULL); //Intraday current price
@@ -118,7 +104,7 @@ double* morningstar_get_price(const char* ticker_name_string) {
strftime(yesterday_char, 16, "%Y-%m-%d", ts);
sprintf(morningstar_api_string,
"http://globalquote.morningstar.com/globalcomponent/RealtimeHistoricalStockData.ashx?showVol=true&dtype=his"
- "&f=d&curry=USD&isD=true&isS=true&hasF=true&ProdCode=DIRECT&ticker=%s&range=%s|%s",
+ "&f=d&curry=USD&isD=true&isS=true&hasF=true&ProdCode=DIRECT&ticker=%s&range=%s|%s",
ticker_name_string, yesterday_char, today_char);
String* pString = api_curl_data(morningstar_api_string, NULL);
if (strcmp("null", pString->data) == 0) { //Invalid symbol
@@ -127,10 +113,7 @@ double* morningstar_get_price(const char* ticker_name_string) {
}
Json* jobj = json_tokener_parse(pString->data);
double* ret = malloc(sizeof(double) * 2);
- if (ret == NULL) {
- fprintf(stderr, "malloc() failed\n");
- exit(EXIT_FAILURE);
- }
+ pointer_alloc_check(ret);
Json* datapoints = json_object_object_get(
json_object_array_get_idx(json_object_object_get(jobj, "PriceDataList"), 0), "Datapoints");
size_t size = json_object_array_length(datapoints);
@@ -160,10 +143,7 @@ double* coinmarketcap_get_price(const char* ticker_name_string) {
const char* price = json_object_get_string(usd);
const char* change_1d = json_object_get_string(percent_change_1d);
double* ret = malloc(sizeof(double) * 2);
- if (ret == NULL) {
- fprintf(stderr, "malloc() failed\n");
- exit(EXIT_FAILURE);
- }
+ pointer_alloc_check(ret);
ret[0] = strtod(price, NULL); //Current real-time price
ret[1] = ret[0] - ((strtod(change_1d, NULL) / 100) * ret[0]); //Price 24 hours earlier
string_destroy(&pString);
@@ -193,10 +173,7 @@ double* iex_get_hist_5y(const char* ticker_name_string) {
Json* jobj = json_tokener_parse(pString->data);
size_t len = json_object_array_length(jobj);
double* ret = calloc(len + 1, sizeof(double));
- if (ret == NULL) {
- fprintf(stderr, "malloc() failed\n");
- exit(EXIT_FAILURE);
- }
+ pointer_alloc_check(ret);
Json* temp;
for (int i = 0; i < (int) len; i++) {
temp = json_object_array_get_idx(jobj, (size_t) i);
@@ -218,7 +195,7 @@ double* morningstar_get_hist_5y(const char* ticker_name_string) {
strftime(yesterday_char, 16, "%Y-%m-%d", ts);
sprintf(morningstar_api_string,
"http://globalquote.morningstar.com/globalcomponent/RealtimeHistoricalStockData.ashx?showVol=true&dtype=his"
- "&f=d&curry=USD&isD=true&isS=true&hasF=true&ProdCode=DIRECT&ticker=%s&range=%s|%s",
+ "&f=d&curry=USD&isD=true&isS=true&hasF=true&ProdCode=DIRECT&ticker=%s&range=%s|%s",
ticker_name_string, yesterday_char, today_char);
String* pString = api_curl_data(morningstar_api_string, NULL);
if (strcmp("null", pString->data) == 0) { //Invalid symbol
@@ -230,11 +207,8 @@ double* morningstar_get_hist_5y(const char* ticker_name_string) {
json_object_array_get_idx(json_object_object_get(jobj, "PriceDataList"), 0), "Datapoints");
size_t len = json_object_array_length(datapoints);
double* ret = calloc(len + 1, sizeof(double)); // Must calloc() because some data points don't exist
- if (ret == NULL) {
- fprintf(stderr, "malloc() failed\n");
- exit(EXIT_FAILURE);
- }
- for (int i = 0; i < (int)len; i++)
+ pointer_alloc_check(ret);
+ for (int i = 0; i < (int) len; i++)
ret[i] = json_object_get_double(
json_object_array_get_idx(json_object_array_get_idx(datapoints, (size_t) i), 0));
json_object_put(jobj);
@@ -244,10 +218,7 @@ double* morningstar_get_hist_5y(const char* ticker_name_string) {
void news_print_top_three(const char* ticker_name_string) {
char* url_encoded_string = calloc(128, 1);
- if (url_encoded_string == NULL) {
- fprintf(stderr, "malloc() failed\n");
- exit(EXIT_FAILURE);
- }
+ pointer_alloc_check(url_encoded_string);
for (int i = 0, j = 0; i < 128; i++, j++) { //Replace underscores and spaces with url encoded '%20's
if (ticker_name_string[i] == '_' || ticker_name_string[i] == ' ') {
memcpy(&url_encoded_string[i], "%20", 3);
@@ -266,8 +237,7 @@ void news_print_top_three(const char* ticker_name_string) {
free(url_encoded_string);
String* pString = api_curl_data(news_api_string, NULL);
Json* jobj = json_tokener_parse(pString->data);
- if (strtol(json_object_to_json_string(json_object_object_get(jobj, "totalResults")), NULL,
- 10) > 0)
+ if (strtol(json_object_to_json_string(json_object_object_get(jobj, "totalResults")), NULL, 10) > 0)
json_print_news(jobj);
else printf("No articles. Try a different input.\n");
string_destroy(&pString);
@@ -389,7 +359,7 @@ Info* morningstar_get_info(const char* ticker_name_string) {
strftime(yesterday_char, 16, "%Y-%m-%d", ts);
sprintf(morningstar_api_string,
"http://globalquote.morningstar.com/globalcomponent/RealtimeHistoricalStockData.ashx?showVol=true&dtype=his"
- "&f=d&curry=USD&isD=true&isS=true&hasF=true&ProdCode=DIRECT&ticker=%s&range=%s|%s",
+ "&f=d&curry=USD&isD=true&isS=true&hasF=true&ProdCode=DIRECT&ticker=%s&range=%s|%s",
ticker_name_string, yesterday_char, today_char);
String* pString = api_curl_data(morningstar_api_string, NULL);
if (strcmp("null", pString->data) == 0) { //Invalid symbol
@@ -450,10 +420,7 @@ char* google_shorten_link(const char* url_string) {
"https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyAoMAvMPpc7U8lfrnGMk2ZKl966tU2pppU", post_string);
Json* jobj = json_tokener_parse(pString->data);
char* short_url = malloc(32);
- if (short_url == NULL) {
- fprintf(stderr, "calloc() failed\n");
- exit(EXIT_FAILURE);
- }
+ pointer_alloc_check(short_url);
strcpy(short_url, json_object_to_json_string(json_object_object_get(jobj, "id")));
strip_char(short_url, '\\');
strip_char(short_url, '\"');
diff --git a/graph.c b/graph.c
index c0205eab562a..d63433ae2c51 100644
--- a/graph.c
+++ b/graph.c
@@ -160,10 +160,7 @@ void graph_print(const double* points, struct tm* start_time, int zoom) {
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
- if (points == NULL) {
- fprintf(stderr, "realloc() failed\n");
- exit(EXIT_FAILURE);
- }
+ 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
diff --git a/main.c b/main.c
index 3cdd0d6096a8..c28579642ea0 100644
--- a/main.c
+++ b/main.c
@@ -13,10 +13,7 @@ int main(int argc, char* argv[]) {
char* sym = NULL;
if (argc > 2) {
char* s = malloc(strlen(argv[2]) + 1);
- if (s == NULL) {
- fprintf(stderr, "malloc() failed\n");
- exit(EXIT_FAILURE);
- }
+ pointer_alloc_check(s);
strcpy(s, argv[2]);
strtoupper(s);
sym = s;
diff --git a/portfolio.c b/portfolio.c
index ea7984f607b3..d121cbdfca0f 100644
--- a/portfolio.c
+++ b/portfolio.c
@@ -5,10 +5,7 @@ char* portfolio_file;
void portfolio_file_init(void) {
char* home = getenv("HOME");
char* path = (char*) malloc(strlen(home) + 30);
- if (path == NULL) {
- fprintf(stderr, "malloc() failed\n");
- exit(EXIT_FAILURE);
- }
+ pointer_alloc_check(path);
sprintf(path, "%s/.tick_portfolio.json", home);
portfolio_file = path;
}
@@ -19,11 +16,8 @@ String* portfolio_file_get_string(void) {
return NULL;
String* pString = string_init();
pString->data = realloc(pString->data, 65536);
+ pointer_alloc_check(pString->data);
memset(pString->data, '\0', 65536);
- if (pString->data == NULL) {
- fprintf(stderr, "realloc() failed\n");
- exit(EXIT_FAILURE);
- }
fseek(fp, 0, SEEK_END);
pString->len = (size_t) ftell(fp);
fseek(fp, 0, SEEK_SET);
@@ -136,10 +130,7 @@ void portfolio_modify(const char* ticker_name_string, double quantity_shares, do
pString->len = strlen(json_object_to_json_string(jobj));
pString->data = realloc(pString->data, pString->len + 1);
- if (pString->data == NULL) {
- fprintf(stderr, "malloc() failed\n");
- exit(EXIT_FAILURE);
- }
+ pointer_alloc_check(pString->data);
strcpy(pString->data, json_object_to_json_string(jobj));
if (password != NULL) { // If data must be re-encrypted
@@ -201,10 +192,7 @@ void portfolio_print_all(void) {
double* portfolio_print_stock(char* ticker_name_string, Json* current_index) {
char symbol[32];
double* data = malloc(sizeof(double) * 3);
- if (data == NULL) {
- fprintf(stderr, "malloc() failed\n");
- exit(EXIT_FAILURE);
- }
+ pointer_alloc_check(data);
String* pString = NULL;
char* password = NULL;
Json* jobj = NULL;
diff --git a/rc4.c b/rc4.c
index ab9f59d54321..bd3ecc9d3044 100644
--- a/rc4.c
+++ b/rc4.c
@@ -2,10 +2,7 @@
char* rc4_getPassword(void) {
unsigned char* pass = malloc(PASS_MAX + 1);
- if (pass == NULL) {
- fprintf(stderr, "malloc() failed\n");
- return NULL;
- }
+ pointer_alloc_check(pass);
initscr();
noecho(); // Doesn't echo chars when typed
unsigned int c, i;
@@ -47,10 +44,7 @@ void rc4_key_exchange(int keySchedule[KEY_SCHEDULE_LENGTH], char* key) {
char* rc4_prga(int keySchedule[KEY_SCHEDULE_LENGTH], size_t len) {
int i = 0, j = 0, temp;
char* output = malloc(len);
- if (output == NULL) {
- fprintf(stderr, "malloc() failed\n");
- return NULL;
- }
+ pointer_alloc_check(output);
for (int k = 0; k < (int) len; k++) {
i = (i + 1) % KEY_SCHEDULE_LENGTH;
j = (j + keySchedule[i]) % KEY_SCHEDULE_LENGTH;
@@ -67,7 +61,7 @@ void rc4_execute(char* output, String* pString) {
output[i] ^= pString->data[i];
}
-char* rc4_encode_string(String* pString, char* password){
+char* rc4_encode_string(String* pString, char* password) {
int keySchedule[256];
rc4_key_exchange(keySchedule, password);
char* output = rc4_prga(keySchedule, pString->len);
@@ -106,11 +100,9 @@ String* rc4_get_crypted_string(String* input_pString, char* password, int option
output_pString->len = input_pString->len;
if (option == DECRYPT) {
json_object_put(jobj);
- output_pString->data = realloc(output_pString->data, output_pString->len + 1);// Realloc to add null terminator for json parsing
- if (output_pString->data == NULL) { // Dealing with len is annoying so it's easier to just realloc
- fprintf(stderr, "realloc() failed\n");
- exit(EXIT_FAILURE);
- }
+ // Realloc to add null terminator for json parsing
+ output_pString->data = realloc(output_pString->data, output_pString->len + 1);
+ pointer_alloc_check(output_pString->data); // Dealing with len is annoying so it's easier to just realloc
output_pString->data[output_pString->len] = '\0'; // Null terminate string for parsing
jobj = json_tokener_parse(output_pString->data);
if (jobj == NULL) { // If after decrypting, the portfolio is not JSON formatted,
diff --git a/string-tick.c b/string-tick.c
index 701c87463135..c5cf0ab31e8a 100644
--- a/string-tick.c
+++ b/string-tick.c
@@ -3,18 +3,10 @@
String* string_init(void) {
String* pString = (String*) malloc(sizeof(String));
- if (pString != NULL) {
- pString->len = 0;
- pString->data = (char*) malloc(sizeof(char));
- if (pString->data == NULL) {
- fprintf(stderr, "malloc() failed\n");
- exit(EXIT_FAILURE);
- }
- pString->data[0] = '\0';
- } else {
- fprintf(stderr, "malloc() failed\n");
- exit(EXIT_FAILURE);
- }
+ pointer_alloc_check(pString);
+ pString->len = 0;
+ pString->data = calloc(sizeof(char), 1);
+ pointer_alloc_check(pString->data);
return pString;
}
@@ -40,7 +32,7 @@ char* strip_char(char* string, char c) {
return string;
}
-void string_write_portfolio(String* pString){
+void string_write_portfolio(String* pString) {
FILE* fp = fopen(portfolio_file, "w"); // fprintf %s won't work since there some chars are encoded to '\0', so it
for (int i = 0; i < (int) pString->len; i++) // will be null terminated several times in the middle
fputc(pString->data[i], fp);
@@ -52,4 +44,11 @@ void string_destroy(String** phString) {
free(pString->data);
free(*phString);
*phString = NULL;
+}
+
+void pointer_alloc_check(void* alloced) {
+ if (alloced == NULL) {
+ fprintf(stderr, "alloc failed!\n");
+ exit(EXIT_FAILURE);
+ }
} \ No newline at end of file
diff --git a/string-tick.h b/string-tick.h
index 7f6785a8e970..4680f5e7caf0 100644
--- a/string-tick.h
+++ b/string-tick.h
@@ -53,4 +53,10 @@ void string_write_portfolio(String* pString);
*/
void string_destroy(String** phString);
+/**
+ * If pointer is NULL, print error message and exit the program
+ * @param alloced recently alloced pointer
+ */
+void pointer_alloc_check(void* alloced);
+
#endif \ No newline at end of file