-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinance_cli.c
276 lines (241 loc) · 5.86 KB
/
finance_cli.c
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "finance.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
const char *help = "\
Usage: finance [--help] <command> [args]\n\
Work on the finance database.\n\
\n\
Commands:\n\
add <date> <amount> <description>: adds a new entry in the database\n\
csv <outputfile>: exports the database to a csv file\n\
show [-l [<limit>]]: Prints the database to stdout\n\
sql <query>: Executes an sql query on the database\
";
const char *usage_add = "\
Usage: finance add <date> <amount> <description>\n\
date: date in YYYY-MM-DD format or now, today or yesterday\n\
amount: float value\n\
description: Descriptions about the transaction\
";
const char *usage_csv = "\
Usage: finance csv <outputfile>\n\
outputfile: the output csv file\
";
const char *usage_show = "\
Usage: finance show [-l [<limit>]] [-s]\n\
-l: limit the amount of rows printed to the last <limit> entries. Default=10\n\
if <limit> isn't specified but -l is, all rows are printed.\n\
-s: also calculate the sum/current balance\
";
const char *usage_sql = "\
Usage: finance sql <query>\n\
<query> any valid sql query\n\
\n\
The structure of the database is:\n\
finance\n\
-----------------------------------\n\
id | date | amount | description\n\
";
int valid_date_format(char** date);
int isNumber(char* number);
int main(int argc, char **argv) {
#ifdef _DEBUG
puts("Debug mode");
#endif /* _DEBUG */
// Prints the help and exit
if (argc < 2) {
printf("%s", help);
return 0;
}
if (!strcmp(argv[1], "--help")) {
if (argc < 3) {
puts(help);
return 0;
}
if (!strcmp(argv[2], "add")) {
puts(usage_add);
return 0;
}
if (!strcmp(argv[2], "csv")) {
puts(usage_csv);
return 0;
}
if (!strcmp(argv[2], "show")) {
puts(usage_show);
return 0;
}
if (!strcmp(argv[2], "sql")) {
puts(usage_sql);
return 0;
}
printf("Unkown command: %s\n", argv[2]);
puts(help);
return 0;
}
// finance add <date> <amount> <description>
if (!strcmp(argv[1], "add")) {
// Check if there are enough arguments
if (argc < 5) {
puts("Not enough arguments.");
printf("%s", usage_add);
return 0;
}
// Check if the date is in a valid format
char *date = argv[2];
if (!valid_date_format(&date)) {
puts("Please enter a valid date format.");
printf("%s\n", usage_add);
return 0;
}
// Executes the transaction
if (open_db()) {
close_db();
return 1;
}
if (add(date, strtof(argv[3], NULL), argv[4])) {
close_db();
return 1;
}
close_db();
return 0;
}
// finance csv <outputfile>
if (!strcmp(argv[1], "csv")) {
if (argc < 3) {
puts("No outputfile specified.");
printf("%s", usage_csv);
return 0;
}
if (open_db()) {
close_db();
return 1;
}
if (export_to_csv(argv[2])) {
close_db();
return 1;
}
close_db();
return 0;
}
// finance show [-l [<limit>]] [-s]
if( !strcmp(argv[1], "show") ) {
int limit = 10;
int calculateSum = 0;
size_t optind;
for (optind = 2; optind < argc && argv[optind][0] == '-'; optind++) {
switch (argv[optind][1]) {
case 'l':
// No limit given
if (optind+1 == argc || argv[optind+1][0] == '-') {
limit = 0;
break;
}
if (!isNumber(argv[optind+1])) {
printf("%s isn't a number.\n", argv[optind+1]);
puts(usage_show);
return 0;
}
limit = atoi(argv[optind + 1]);
optind++;
break;
case 's': calculateSum = 1; break;
default:
printf("Unknown argument %s.\n", argv[optind]);
puts(usage_show);
return 0;
}
}
if (open_db()) {
close_db();
return 1;
}
if (print_db(limit, calculateSum)) {
close_db();
return 1;
}
close_db();
return 0;
}
// finance sql <query>
if (!strcmp(argv[1], "sql")) {
if (argc < 3) {
puts("No query given.");
puts(usage_sql);
return 0;
}
if (open_db()) {
close_db();
return 1;
}
if (execute_query(argv[2], NULL, 0)) {
close_db();
return 1;
}
close_db();
return 0;
}
// Invalid argument
puts("Invalid argument given.");
printf("%s", help);
return 0;
}
int isNumber(char* number) {
int i = 0;
int isDigit = 1;
while (i < strlen(number) && isDigit) {
isDigit = isdigit(number[i]);
i++;
}
return isDigit;
}
int valid_date_format (char** date) {
if (!strcmp(*date, "yesterday")) {
*date = "now\", \"-1 day";
return 1;
}
if (!strcmp(*date, "today")) {
*date = "now";
}
pcre2_code *re;
PCRE2_SPTR pattern;
PCRE2_SPTR subject;
int errornumber;
int rc;
PCRE2_SIZE erroroffset;
PCRE2_SIZE subject_length;
pcre2_match_data *match_data;
pattern = (PCRE2_SPTR)"^\\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1 2]\\d|3[0-1])|now$";
subject = (PCRE2_SPTR) *date;
subject_length = (PCRE2_SIZE)strlen((char*)subject);
re = pcre2_compile(
pattern,
PCRE2_ZERO_TERMINATED,
0,
&errornumber,
&erroroffset,
NULL
);
if (re == NULL) {
PCRE2_UCHAR buffer[256];
pcre2_get_error_message(errornumber, buffer, sizeof(buffer));
printf("PCRE2 compilation failed at offset %d: %s\n", (int)erroroffset, buffer);
return -1;
}
match_data = pcre2_match_data_create_from_pattern(re, NULL);
rc = pcre2_match(re, subject, subject_length, 0, 0, match_data, NULL);
pcre2_match_data_free(match_data);
pcre2_code_free(re);
if (rc < 0) {
if (rc == PCRE2_ERROR_NOMATCH) {
return 0;
}
printf("Matching error %d\n", rc);
return -1;
}
return (subject_length == 10 || subject_length == 3);
}