1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
#include "api.h"
#include "portfolio.h"
int main(int argc, char* argv[]) {
if (argc < 2) {
printf("Invalid arguments. Type \"man tick\" for help.\n");
return 0;
}
char cmd[strlen(argv[1]) + 1];
strcpy(cmd, argv[1]);
strtolower(cmd);
// Init portfolio path
portfolio_file_init();
// Portfolio modify operation
int modop = -1;
// News
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");
}
//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) {
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){
string_destroy(&pString);
free(portfolio_file);
return 0;
}
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);
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 {
double* data = portfolio_print_stock(sym, NULL);
if (data != NULL)
free(data);
}
}
}
// Portfolio
else if (strcmp(cmd, "add") == 0)
modop = ADD;
else if (strcmp(cmd, "rm") == 0)
modop = REMOVE;
else if (strcmp(cmd, "set") == 0)
modop = SET;
else printf("Invalid arguments. Type \"man tick\" for help.\n");
// 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");
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]);
strtoupper(susd);
// If last two characters of price are "EA", calculate
// total price with number of units
char ea = 0;
if (ulen > 2 && susd[ulen - 2] == 'E' && susd[ulen - 1] == 'A') {
ea = 1;
susd[ulen - 2] = '\0';
}
double usd = strtod(susd, NULL);
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;
}
}
}
free(portfolio_file);
return 0;
}
|