aboutsummarylogtreecommitdiffstats
path: root/portfolio.c
blob: a3ce051e06c2f93ac702bd7791f369ae76697e07 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "portfolio.h"

void portfolio_file_init() {
    char* home = getenv("HOME");
    char* path = (char*) malloc(strlen(home) + 21);
    memcpy(path, home, strlen(home) + 1);
    memcpy(&path[strlen(home)], "/.tick_portfolio", 17);
    portfolio_file = path;
}

void portfolio_modify(char* ticker_name_string, double quantity_shares, double usd_spent, FILE* fp, int option){
    if (portfolio_contains(ticker_name_string, fp)) {
        long position = ftell(fp);
        fseek(fp, 0, SEEK_END);
        size_t length = (size_t) ftell(fp);
        char* beginning = (char*) calloc(length, sizeof(char));
        char* end = (char*) calloc(length, sizeof(char));
        fseek(fp, 0, SEEK_SET);
        for (int i = 0; i < (int) position; i++)
            beginning[i] = (char) fgetc(fp);
        fseek(fp, position, SEEK_SET);
        double new_quantity_shares;
        double new_usd_spent;
        if (option == ADD) {
            new_quantity_shares = get_next_val(fp) + quantity_shares;
            new_usd_spent = get_next_val(fp) + usd_spent;
        } else if (option == REMOVE) {
            new_quantity_shares = get_next_val(fp) - quantity_shares;
            new_usd_spent = get_next_val(fp) - usd_spent;
        } else {
            new_quantity_shares = quantity_shares;
            new_usd_spent = usd_spent;
            get_next_val(fp);
            get_next_val(fp);
        }
        char c;
        for (int i = 0;; i++) {
            c = (char) fgetc(fp);
            if (feof(fp))
                break;
            end[i] = c;
        }
        if (strlen(end) > 0 && end[strlen(end) - 1] == '\n')
            end[strlen(end) - 1] = '\0';
        remove(portfolio_file);
        fp = fopen(portfolio_file, "w");
        if (new_quantity_shares >= 0 && new_usd_spent >= 0) {
            fprintf(fp, "%s%lf %lf", beginning, new_quantity_shares, new_usd_spent);
            if (strlen(end) != 0)
                fprintf(fp, "\n%s", end);
            if (option == ADD)
                printf("Added ");
            else if (option == REMOVE)
                printf("Removed ");
            else {
                printf("Set %s to %lf\n", ticker_name_string, quantity_shares);
                return;
            }
            printf("%lf %s.\n", quantity_shares, ticker_name_string);
        } else printf("You cannot remove more %s than you have!\n", ticker_name_string);
        free(beginning);
        free(end);
        fclose(fp);
    } else {
        if (iex_get_current_price(ticker_name_string) == -1)
            return;
        if (option == REMOVE) {
            printf("You don't have any %s to remove!\n", ticker_name_string);
            return;
        }
        fseek(fp, 0, SEEK_END);
        if (ftell(fp) != 0)
            fprintf(fp, "\n");
        fprintf(fp, "%s %lf %lf", ticker_name_string, quantity_shares, usd_spent);
        if (option == ADD)
            printf("Added ");
        else {
            printf("Set %s to %lf\n", ticker_name_string, quantity_shares);
            return;
        }
        printf("%lf %s.\n", quantity_shares, ticker_name_string);
    }
}

double portfolio_get_quantity_shares(char* ticker_name_string, FILE* fp) {
    if (portfolio_contains(ticker_name_string, fp))
        return get_next_val(fp);
    return 0;
}

double portfolio_get_usd_spent(char* ticker_name_string, FILE* fp) {
    if (portfolio_contains(ticker_name_string, fp)) {
        get_next_val(fp);
        return get_next_val(fp);
    }
    return 0;
}

void portfolio_print_all(FILE* fp) {
    char* str = (char*) calloc(64, sizeof(char));
    char* ticker_name_string = (char*) calloc(32, sizeof(char));
    double* data;
    double total_owned = 0, total_spent = 0;
    int i;
    char c;
    while (fgets(str, 63, fp) != NULL) {
        i = 0;
        while ((c = str[i]) != ' ') {
            ticker_name_string[i] = c;
            i++;
        }
        data = portfolio_print_stock(ticker_name_string, fp);
        if (data != NULL) {
            total_owned += data[0];
            total_spent += data[1];
        }
        memset(str, '\0', 64);
        memset(ticker_name_string, '\0', 32);
        free(data);
    }
    printf("\nTotals: Value: $%8.2lf. Expenditure: $%8.2lf. Profit: %6.2lf (%4.2lf%%)\n",
           total_owned, total_spent, total_owned - total_spent, (100 * (total_owned - total_spent)) / total_spent);
    free(str);
    free(ticker_name_string);
}

double* portfolio_print_stock(char* ticker_name_string, FILE* fp){
    double ticker_price_usd = iex_get_current_price(ticker_name_string);
    double* a = malloc(sizeof(double) * 2);
    if (portfolio_get_usd_spent(ticker_name_string, fp)) {
        a[0] = portfolio_get_quantity_shares(ticker_name_string, fp) * ticker_price_usd;
        a[1] = portfolio_get_usd_spent(ticker_name_string, fp);
        if (a[0] == 0 && a[1] == 0)
            printf("Your portfolio does not contain %s\n", ticker_name_string);
        else
            printf("%8.4lf %5s. Value: $%8.2lf. Expenditure: $%8.2lf. Profit: %6.2lf (%4.2lf%%)\n",
                   a[0] / ticker_price_usd, ticker_name_string, a[0], a[1], a[0] - a[1], (100 * (a[0] - a[1])) / a[1]);
    } else{
        free(a);
        return NULL;
    }
    return a;
}

int portfolio_contains(char* ticker_name_string, FILE* fp) {
    fseek(fp, 0, SEEK_SET);
    char* temp = (char*) calloc(16, sizeof(char)), c;
    int i = 0;
    while (1) {
        c = (char) fgetc(fp);
        if (feof(fp)) {
            fseek(fp, 0, 0);
            free(temp);
            return 0;
        }
        if (c == ' ') {
            if (strcmp(ticker_name_string, temp) == 0) {
                free(temp);
                return 1;
            } else {
                while (fgetc(fp) != '\n') {
                    if (feof(fp))
                        break;
                }
                memset(temp, '\0', 16);
                i = 0;
            }
        } else {
            temp[i] = c;
            i++;
        }
    }
}

double get_next_val(FILE* fp) {
    char* temp = (char*) calloc(32, sizeof(char)), c;
    int i = 0;
    while ((c = (char) fgetc(fp)) != ' ' && c != '\n') {
        if (feof(fp))
            break;
        temp[i] = c;
        i++;
    }
    double val = strtod(temp, NULL);
    free(temp);
    return val;
}