aboutsummarylogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntony Kellermann2018-05-15 01:21:02 -0400
committerAntony Kellermann2018-05-15 01:21:02 -0400
commit7e25c232216693ebf0a5a91ebe7b75cc63f337b3 (patch)
tree71dd3bb6be645298bd00a97850d2df170aab354d
parentc2d3b996408b785d180ce6e8daf30573bce70b7c (diff)
downloadaur-7e25c232216693ebf0a5a91ebe7b75cc63f337b3.tar.gz
Small optimizations
-rw-r--r--main.c20
-rw-r--r--portfolio.c52
-rw-r--r--string-tick.h5
3 files changed, 36 insertions, 41 deletions
diff --git a/main.c b/main.c
index 3e7248cc668b..ddaf8b490cb5 100644
--- a/main.c
+++ b/main.c
@@ -3,7 +3,7 @@
int main(int argc, char* argv[]) {
if (argc < 2) {
- printf("Invalid arguments. Type \"man tick\" for help.\n");
+ puts("Invalid arguments. Type \"man tick\" for help.");
return 0;
}
char cmd[strlen(argv[1]) + 1];
@@ -31,7 +31,7 @@ int main(int argc, char* argv[]) {
if (strcmp(cmd, "news") == 0) {
if (argc == 3 && strlen(argv[2]) <= 32 && strlen(argv[2]) > 1)
news_print_top_three(argv[2]);
- else printf("Invalid symbol.\n");
+ else puts("Invalid input.");
}
//Encrypt/decrypt
@@ -73,16 +73,16 @@ int main(int argc, char* argv[]) {
else if (strcmp(cmd, "set") == 0)
modop = SET;
- else printf("Invalid arguments. Type \"man tick\" for help.\n");
+ else puts("Invalid arguments. Type \"man tick\" for help.");
// Portfolio Operations
if (modop > -1) {
if (argc != 5)
- printf("Invalid arguments. Type \"man tick\" for help.\n");
- else if (strlen(argv[2]) > 16)
- printf("Invalid symbol.\n");
+ puts("Invalid arguments. Type \"man tick\" for help.");
+ else if (strlen(sym) > 16)
+ puts("Invalid symbol.");
else if (strlen(argv[3]) > 16 || strlen(argv[4]) > 16)
- printf("Value too large.\n");
+ puts("Value too large.");
else {
double qty = strtod(argv[3], NULL);
size_t ulen = strlen(argv[4]);
@@ -102,11 +102,7 @@ int main(int argc, char* argv[]) {
if (ea)
usd *= qty;
- if (modop == REMOVE)
- portfolio_modify(sym, qty, usd, REMOVE);
- else if (modop == ADD)
- portfolio_modify(sym, qty, usd, ADD);
- else portfolio_modify(sym, qty, usd, SET);
+ portfolio_modify(sym, qty, usd, modop);
}
}
free(portfolio_file);
diff --git a/portfolio.c b/portfolio.c
index 3267b80c6cee..b618b7465ab6 100644
--- a/portfolio.c
+++ b/portfolio.c
@@ -57,14 +57,14 @@ String* portfolio_file_get_string(char** password) {
void portfolio_modify(const char* ticker_name_string, double quantity_shares, double usd_spent, int option) {
if (quantity_shares < 0 || usd_spent < 0) // Negative numbers
- RET_MSG("You must use positive values.")
+ RET_MSG("You must use positive values.")
if (option != SET && quantity_shares == 0 && usd_spent == 0) // Adding or removing 0
- RET_MSG("You cannot add or remove values of 0.")
+ RET_MSG("You cannot add or remove values of 0.")
FILE* fp = fopen(portfolio_file, "a"); // Creates empty file if portfolio doesn't exist
if (fp == NULL) // If file exists, but cannot be opened, usually because of permissions
- RET_MSG("Error opening porfolio.")
+ RET_MSG("Error opening porfolio.")
fclose(fp);
char* password = NULL; // If portfolio is encrypted, store password when decrypting for re-encryption
@@ -82,12 +82,12 @@ void portfolio_modify(const char* ticker_name_string, double quantity_shares, do
int index = portfolio_symbol_index(ticker_name_string, jobj);
if (index == -1) { // If security is not already contained in portfolio
if (option == REMOVE) // If trying to remove a security they don't own
- GOTO_CLEAN_MSG("You don't have any of this security to remove")
+ GOTO_CLEAN_MSG("You don't have any of this security to remove")
if (strcmp("USD$", ticker_name_string) != 0) { // Check that the symbol is valid, except if it's USD
double* data = api_get_current_price(ticker_name_string);
if (data == NULL) // If NULL response from APIs, it's invalid
- GOTO_CLEAN_MSG("Invalid symbol.")
+ GOTO_CLEAN_MSG("Invalid symbol.")
else free(data);
}
@@ -117,7 +117,7 @@ void portfolio_modify(const char* ticker_name_string, double quantity_shares, do
current_shares -= quantity_shares;
current_spent -= usd_spent;
if (current_shares < 0 || current_spent < 0) // If you try to remove more than you have
- GOTO_CLEAN_MSG("You don't have enough of this security to remove.")
+ GOTO_CLEAN_MSG("You don't have enough of this security to remove.")
printf("Removed %lf %s bought for %lf to portfolio.\n", quantity_shares, ticker_name_string, usd_spent);
}
@@ -258,12 +258,10 @@ void portfolio_print_all(int SORT) {
SD* sec_data;
char loading_str[32];
pthread_t threads[sda_data->length];
- for (size_t i = 0; i < sda_data->length; i++) { // Create one thread per security to collect API data
- if (pthread_create(&threads[i], NULL, portfolio_store_api_data, sda_data->sec_data[i])) {
- fprintf(stderr, "Error creating thread!\n");
- exit(EXIT_FAILURE);
- }
- }
+ for (size_t i = 0; i < sda_data->length; i++) // Create one thread per security to collect API data
+ if (pthread_create(&threads[i], NULL, portfolio_store_api_data, sda_data->sec_data[i]))
+ EXIT_MSG("Error creating thread!")
+
for (size_t i = 0; i < sda_data->length; i++) {
if (i > 0)
for (size_t j = 0; j < strlen(loading_str); j++)
@@ -271,10 +269,9 @@ void portfolio_print_all(int SORT) {
sprintf(loading_str, "(%d/%d)", (int) i + 1, (int) sda_data->length); // Format: (5/23)
printf("%s", loading_str); // Print loading string
fflush(stdout); // Flush because no newline
- if (pthread_join(threads[i], NULL)) { // Wait for each thread to finish collecting API data
- fprintf(stderr, "Error joining thread!\n");
- exit(EXIT_FAILURE);
- }
+ if (pthread_join(threads[i], NULL)) // Wait for each thread to finish collecting API data
+ EXIT_MSG("Error joining thread!")
+
sec_data = sda_data->sec_data[i];
total_owned += sec_data->current_value; // Add collected values to totals
total_spent += sec_data->total_spent;
@@ -302,23 +299,20 @@ void portfolio_print_stock(const char* ticker_name_string) {
return;
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;
- }
- }
- if (sec_data == NULL) {
- printf("Your portfolio does not contain any %s.\n", ticker_name_string);
- sda_destroy(&sda_data);
- return;
- }
+ size_t i = 0;
+ while (i < sda_data->length && strcmp(sda_data->sec_data[i]->symbol, ticker_name_string) != 0)
+ i++;
+ if (i != sda_data->length)
+ sec_data = sda_data->sec_data[i];
+ else GOTO_CLEAN_MSG("Your portfolio does not contain any of this security.")
+
portfolio_store_api_data(sec_data);
- printf(" AMOUNT SYMBOL VALUE SPENT PROFIT (%%) 24H (%%) 7D (%%)\n");
- printf("%8.2lf %6s %8.2lf %8.2lf %8.2lf (%6.2lf%%) %8.2lf (%6.2lf%%) %8.2lf (%6.2lf%%)\n", sec_data->amount,
+ printf(" AMOUNT SYMBOL VALUE SPENT PROFIT (%%) 24H (%%) 7D (%%)\n"
+ "%8.2lf %6s %8.2lf %8.2lf %8.2lf (%6.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->seven_day_profit, sec_data->seven_day_profit_percent);
+ cleanup:
sda_destroy(&sda_data);
}
diff --git a/string-tick.h b/string-tick.h
index 7d507024dd9d..b06f82b3f97e 100644
--- a/string-tick.h
+++ b/string-tick.h
@@ -16,6 +16,11 @@
goto cleanup;\
}
+#define EXIT_MSG(msg) {\
+ puts(msg);\
+ exit(EXIT_FAILURE);\
+}
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>