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
|
#include "quotes.h"
#include "assets.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
char *ASSET_TYPE_NAMES[] = {
FOREACH_ASSET_TYPE(GENERATE_STRING)
};
void asset_free(struct asset *a)
{
free(a->symbol);
free(a);
}
int asset_cmp(const void *a, const void *b)
{
struct asset *aa = *(struct asset **)a;
struct asset *bb = *(struct asset **)b;
if (aa->type > bb->type)
return 1;
else if (aa->type < bb->type)
return -1;
else
if (aa->usd_tot > bb->usd_tot)
return -1;
else if (aa->usd_tot < bb->usd_tot)
return 1;
else
return 0;
}
void add_quotes_to_assets(struct asset **assets, int n_assets,
struct quote **quotes, int n_quotes)
{
for (int i = 0; i < n_assets; ++i) {
struct quote *quote = quote_find(quotes, n_quotes, assets[i]->symbol,
"USD");
if (quote) {
assets[i]->usd = true;
assets[i]->usd_pr = quote->price;
assets[i]->usd_ch = quote->change_percent;
assets[i]->usd_tot = assets[i]->amt * quote->price *
(quote->multiplier ? quote->multiplier : 1);
}
quote = quote_find(quotes, n_quotes, assets[i]->symbol, "BTC");
if (quote) {
assets[i]->btc = true;
assets[i]->btc_pr = quote->price;
assets[i]->btc_ch = quote->change_percent;
assets[i]->btc_tot = assets[i]->amt * quote->price *
(quote->multiplier ? quote->multiplier : 1);
} else if (strcmp(assets[i]->symbol, "BTC") == 0) {
assets[i]->btc = true;
assets[i]->btc_pr = 1;
assets[i]->btc_tot = assets[i]->amt;
}
}
}
int add_totals(struct asset **assets, int n_assets)
{
struct asset **totals = calloc(4, sizeof(*totals));
totals[3] = calloc(1, sizeof(**totals));
for (int i = 0; i < n_assets; ++i) {
if (assets[i]->usd) {
totals[3]->usd = true;
totals[3]->usd_tot += assets[i]->usd_tot;
if (!totals[assets[i]->type])
totals[assets[i]->type] = calloc(1, sizeof(**totals));
totals[assets[i]->type]->usd = true;
totals[assets[i]->type]->usd_tot += assets[i]->usd_tot;
}
if (assets[i]->btc) {
totals[3]->btc = true;
totals[3]->btc_tot += assets[i]->btc_tot;
if (!totals[assets[i]->type])
totals[assets[i]->type] = calloc(1, sizeof(**totals));
totals[assets[i]->type]->btc = true;
totals[assets[i]->type]->btc_tot += assets[i]->btc_tot;
}
}
int c = 0;
for (int i = 0; i < 4; ++i) {
if (totals[i]) {
totals[i]->symbol = strdup(ASSET_TYPE_NAMES[i]);
totals[i]->type = TOTAL;
assets[n_assets + c++] = totals[i];
}
}
free(totals);
return c;
}
|