aboutsummarylogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.SRCINFO2
-rw-r--r--PKGBUILD2
-rw-r--r--README.md2
-rw-r--r--main.c84
-rw-r--r--portfolio.c19
-rw-r--r--tick.12
6 files changed, 38 insertions, 73 deletions
diff --git a/.SRCINFO b/.SRCINFO
index c9c08ca75eeb..1168136729b3 100644
--- a/.SRCINFO
+++ b/.SRCINFO
@@ -1,6 +1,6 @@
pkgbase = tick
pkgdesc = Command line stock and cryptocurrency portfolio tracker.
- pkgver = 1.10.0
+ pkgver = 1.11.0
pkgrel = 1
url = https://github.com/aokellermann/tick
arch = x86_64
diff --git a/PKGBUILD b/PKGBUILD
index 4690d4a4a3c1..9180c47993b4 100644
--- a/PKGBUILD
+++ b/PKGBUILD
@@ -1,7 +1,7 @@
# Maintainer: Antony Kellermann <aokellermann@gmail.com>
pkgname=tick
-pkgver=1.10.0
+pkgver=1.11.0
pkgrel=1
pkgdesc="Command line stock and cryptocurrency portfolio tracker."
arch=('x86_64')
diff --git a/README.md b/README.md
index 06fb30fb4105..449d0e22ebbb 100644
--- a/README.md
+++ b/README.md
@@ -92,4 +92,4 @@ and portfolio distribution (tried to implement, but IEX API is unpredictable; wa
* Make printouts pretty with ncurses (graph plots with historical prices?)
* Format prices with commas (set locale?)
* Function to add new attributes to portfolio if not found
-* Improve code documentation \ No newline at end of file
+* Maybe replace News API with IEX news endpoint \ No newline at end of file
diff --git a/main.c b/main.c
index 174b8c19b73b..e4a30c84c3a6 100644
--- a/main.c
+++ b/main.c
@@ -1,4 +1,3 @@
-#include "api.h"
#include "portfolio.h"
int main(int argc, char* argv[]) {
@@ -10,11 +9,23 @@ int main(int argc, char* argv[]) {
strcpy(cmd, argv[1]);
strtolower(cmd);
+ char* sym = NULL;
+ if (argc > 2) {
+ char* s = malloc(strlen(argv[2]) + 1);
+ if (s == NULL) {
+ fprintf(stderr, "malloc() failed\n");
+ exit(EXIT_FAILURE);
+ }
+ strcpy(s, argv[2]);
+ strtoupper(s);
+ sym = s;
+ }
+
// Init portfolio path
portfolio_file_init();
// Portfolio modify operation
- int modop = -1;
+ int modop = -1, cryptopt;
// News
if (strcmp(cmd, "news") == 0) {
@@ -24,54 +35,27 @@ int main(int argc, char* argv[]) {
}
//Encrypt/decrypt
- else if (strcmp(cmd, "encrypt") == 0 && argc == 2) {
- String* pString = portfolio_file_get_string();
- if (pString == NULL){
- free(portfolio_file);
- return 0;
- }
- String* encrypted = rc4_get_crypted_string(pString, NULL, ENCRYPT);
- if (encrypted == NULL){
- string_destroy(&pString);
- free(portfolio_file);
- return 0;
- }
- string_write_portfolio(encrypted);
- string_destroy(&pString);
- string_destroy(&encrypted);
- }
- else if (strcmp(cmd, "decrypt") == 0 && argc == 2) {
+ else if ((strcmp(cmd, "encrypt") == 0 || strcmp(cmd, "decrypt") == 0) && argc == 2) {
+ cryptopt = strcmp(cmd, "encrypt") == 0; // 1 if encrypting, 0 if decrypting
String* pString = portfolio_file_get_string();
- if (pString == NULL){
- free(portfolio_file);
- return 0;
- }
- String* decrypted = rc4_get_crypted_string(pString, NULL, DECRYPT);
- if (decrypted == NULL){
+ if (pString != NULL) { // NULL if error opening portfolio
+ String* crypted = rc4_get_crypted_string(pString, NULL, cryptopt);
string_destroy(&pString);
- free(portfolio_file);
- return 0;
+ if (crypted != NULL) { // NULL if password error
+ string_write_portfolio(crypted);
+ string_destroy(&crypted);
+ }
}
- string_write_portfolio(decrypted);
- string_destroy(&pString);
- string_destroy(&decrypted);
}
// Info
- else if (strcmp(cmd, "info") == 0 && argc == 3) {
- char sym[strlen(argv[2]) + 1];
- strcpy(sym, argv[2]);
- strtoupper(sym);
+ else if (strcmp(cmd, "info") == 0 && argc == 3)
api_print_info(sym);
- }
// Check
else if (strcmp(cmd, "check") == 0) {
if (argc < 3) {
portfolio_print_all();
} else {
- char sym[strlen(argv[2]) + 1];
- strcpy(sym, argv[2]);
- strtoupper(sym);
if (strcmp(sym, "ALL") == 0)
portfolio_print_all();
else {
@@ -101,12 +85,7 @@ int main(int argc, char* argv[]) {
else if (strlen(argv[3]) > 16 || strlen(argv[4]) > 16)
printf("Value too large.\n");
else {
- char sym[strlen(argv[2]) + 1];
- strcpy(sym, argv[2]);
- strtoupper(sym);
-
double qty = strtod(argv[3], NULL);
-
size_t ulen = strlen(argv[4]);
char susd[ulen + 1];
strcpy(susd, argv[4]);
@@ -124,21 +103,14 @@ int main(int argc, char* argv[]) {
if (ea)
usd *= qty;
- switch (modop) {
- case ADD:
- portfolio_modify(sym, qty, usd, ADD);
- break;
- case REMOVE:
- portfolio_modify(sym, qty, usd, REMOVE);
- break;
- case SET:
- portfolio_modify(sym, qty, usd, SET);
- break;
- default:
- break;
- }
+ 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);
}
}
free(portfolio_file);
+ free(sym);
return 0;
} \ No newline at end of file
diff --git a/portfolio.c b/portfolio.c
index 02ae948ed5d3..b64dcbb923b0 100644
--- a/portfolio.c
+++ b/portfolio.c
@@ -29,7 +29,6 @@ String* portfolio_file_get_string(void) {
fseek(fp, 0, SEEK_SET);
if (fread(pString->data, sizeof(char), pString->len, fp) != pString->len) {
fclose(fp);
- printf("Returning null\n");
return NULL;
}
fclose(fp);
@@ -61,11 +60,9 @@ void portfolio_modify(const char* ticker_name_string, double quantity_shares, do
String* temp = rc4_get_crypted_string(pString, password, DECRYPT);
string_destroy(&pString);
pString = temp;
- jobj = json_tokener_parse(pString->data);
- if (pString == NULL) {
- free(password);
+ if (pString == NULL)
return;
- }
+ jobj = json_tokener_parse(pString->data);
}
int index = portfolio_symbol_index(ticker_name_string, jobj);
if (index == -1) { //if not already in portfolio
@@ -162,6 +159,8 @@ void portfolio_print_all(void) {
String* temp = rc4_get_crypted_string(pString, password, DECRYPT);
string_destroy(&pString);
pString = temp;
+ if (pString == NULL)
+ return;
jobj = json_tokener_parse(pString->data);
}
int num_symbols = (int) json_object_array_length(jobj);
@@ -185,12 +184,6 @@ void portfolio_print_all(void) {
}
double* portfolio_print_stock(char* ticker_name_string, Json* current_index) {
- /**
- * Values in USD
- * a[0] -- current balance
- * a[1] -- amount spent
- * a[2] -- 1d gain
- */
char symbol[32];
double* data = malloc(sizeof(double) * 3);
if (data == NULL) {
@@ -215,11 +208,11 @@ double* portfolio_print_stock(char* ticker_name_string, Json* current_index) {
String* temp = rc4_get_crypted_string(pString, password, DECRYPT);
string_destroy(&pString);
pString = temp;
- jobj = json_tokener_parse(pString->data);
if (pString == NULL) {
- free(password);
+ free(data);
return NULL;
}
+ jobj = json_tokener_parse(pString->data);
}
int index = portfolio_symbol_index(symbol, jobj);
if (index == -1) {
diff --git a/tick.1 b/tick.1
index 97610cac908c..346340a944c2 100644
--- a/tick.1
+++ b/tick.1
@@ -1,4 +1,4 @@
-.TH TICK "1" "March 2018" "Tick 1.10.0" "User Commands"
+.TH TICK "1" "March 2018" "Tick 1.11.0" "User Commands"
.SH NAME
Tick - Command line stock and cryptocurrency portfolio tracker.