-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaldo.c
382 lines (350 loc) · 10.2 KB
/
saldo.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ctype.h>
// Function to print help text when --help option is detected
void print_help_text()
{
printf("This program calculates the current saldo for a given month by "
"reading expenses from a configuration file.\n");
printf("Usage: saldo [--help|-h] [--config|-cfg] [--today|-t] [--edit-config|-c] "
"[--edit|-e [day] "
"[expense]] [--add|-a [day] [expense]] [--new-config|--rebuild-config "
"[income] [expenses]] [number]\n\n");
printf("--help, -h:\n");
printf(" Displays the help text.\n");
printf("--config, -cfg ... :\n");
printf(" Use another config instead of ~/.local/share/... one.\n");
printf("--today, -t:\n");
printf(" Show only today and tomorrow saldo.\n");
printf("--edit-config, -c:\n");
printf(" Edit config manually using neovim, vim or nano.\n");
printf("--edit, -e [day] [expense]:\n");
printf(" Edits the expenses for the specified day.\n");
printf("--add, -a [day] [expense]:\n");
printf(" Adds the specified expense to the existing expense for the "
"specified day.\n");
printf("--new-config, --rebuild-config [income] [expenses] (optionally):\n");
printf(" rebuilds config (for example, after starting next month). Be "
"careful!\n");
printf("Without any flag you can write just a number like `saldo 1000' and "
"this will work like `saldo --add [today] 1000'\n\n");
printf(
"If the configuration file is not found, the program will generate one "
"in ~/.local/share/saldo_config.txt.\nThe configuration file contains "
"the income, fixed expenses and expenses for each day of the month.\nThe "
"program will then calculate the saldo for each day based on the daily "
"budget (income - fixed expenses / days in the month).\nThe daily budget "
"and the saldo for each day will be displayed.\nYou can add comments in "
"the config file: after income an expense and betweed days.\nJust start "
"line with '#'.\n");
exit(0);
}
int main(int argc, char *argv[])
{
double income, fixedExpenses, totalBudget, dailyBudget, expenses, saldo;
int month, day, daysInMonth;
char configFilePath[256];
// Check if --config or -cfg flag is passed
if (argc > 1 &&
(strcmp(argv[1], "--config") == 0 || strcmp(argv[1], "-cfg") == 0))
{
sprintf(configFilePath, "%s", argv[2]);
// Shift argv two positions
memmove(argv, argv + 2, (argc - 2) * sizeof(char *));
}
else
{
sprintf(configFilePath, "%s/.local/share/saldo_config.txt", getenv("HOME"));
}
FILE *configFile = fopen(configFilePath, "r+"); // Read and write
// Get the current month
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
month = timeinfo->tm_mon + 1;
int today = timeinfo->tm_mday;
// int yesterday = timeinfo->tm_mday - 1;
int tomorrow = timeinfo->tm_mday + 1;
char monthName[20];
strftime(monthName, 20, "%B", timeinfo);
// Get the number of days in the month
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ||
month == 10 || month == 12)
daysInMonth = 31;
else if (month == 2)
daysInMonth = 28;
else
daysInMonth = 30;
// Print help text if --help option is detected
if (argc > 1 &&
(strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0))
{
print_help_text();
}
if (argc > 3 && (strcmp(argv[1], "--new-config") == 0 ||
strcmp(argv[1], "--rebuild-config") == 0))
{
printf("Rebuilding config...\nDon't forget to edit it\n");
FILE *configFile = fopen(configFilePath, "w");
fprintf(configFile, "income %s\n", argv[2]);
fprintf(configFile, "fixed_expenses %s\n", argv[3]);
fprintf(configFile, "# \n");
for (int i = 1; i <= daysInMonth; i++)
{
fprintf(configFile, "%d 0\n", i);
}
fclose(configFile);
exit(0);
}
if (argc > 1 && (strcmp(argv[1], "--new-config") == 0 ||
strcmp(argv[1], "--rebuild-config") == 0))
{
printf("Rebuilding config...\nDon't forget to edit it\n");
FILE *configFile = fopen(configFilePath, "w");
fprintf(configFile, "income 0\n");
fprintf(configFile, "fixed_expenses 0\n");
fprintf(configFile, "# \n");
for (int i = 1; i <= daysInMonth; i++)
{
fprintf(configFile, "%d 0\n", i);
}
fclose(configFile);
exit(0);
}
// Add option to open neovim to edit config
if (argc > 1 &&
(strcmp(argv[1], "--edit-config") == 0 || strcmp(argv[1], "-c") == 0))
{
// Check if neovim exists
if (system("which nvim") == 0)
{
char command[512];
sprintf(command, "nvim %s", configFilePath);
system(command);
}
// Check if nano exists
else if (system("which nano") == 0)
{
char command[512];
sprintf(command, "vim %s", configFilePath);
system(command);
}
// Check if vi exists
else if (system("which vi") == 0)
{
char command[512];
sprintf(command, "nano %s", configFilePath);
system(command);
}
exit(0);
}
// If --edit option is detected, edit the config file
if (argc > 3 &&
(strcmp(argv[1], "--edit") == 0 || strcmp(argv[1], "-e") == 0))
{
int editDay = atoi(argv[2]);
double editExpense = atof(argv[3]);
// Create a new file
char newConfigFilePath[256];
sprintf(newConfigFilePath, "%s/.local/share/saldotmpconfig.txt",
getenv("HOME"));
FILE *newConfigFile = fopen(newConfigFilePath, "w");
// Find the line
char line[100];
while (fgets(line, sizeof(line), configFile) != NULL)
{
if (atoi(line) == editDay)
{
fprintf(newConfigFile, "%d %.2lf\n", editDay, editExpense);
}
else
{
fprintf(newConfigFile, "%s", line);
}
}
// Close the files
fclose(configFile);
fclose(newConfigFile);
// Rename the new file
rename(newConfigFilePath, configFilePath);
exit(0);
}
// If --add option is detected, add expenses from user input to config file
if (argc > 3 &&
(strcmp(argv[1], "--add") == 0 || strcmp(argv[1], "-a") == 0))
{
int addDay = atoi(argv[2]);
double addExpense = atof(argv[3]);
// Create a new file
char newConfigFilePath[256];
sprintf(newConfigFilePath, "%s/.local/share/saldotmpconfig.txt",
getenv("HOME"));
FILE *newConfigFile = fopen(newConfigFilePath, "w");
// Find the line
char line[100];
while (fgets(line, sizeof(line), configFile) != NULL)
{
if (atoi(line) == addDay)
{
double existingExpense;
sscanf(line, "%d %lf", &addDay, &existingExpense);
addExpense += existingExpense;
fprintf(newConfigFile, "%d %.2lf\n", addDay, addExpense);
}
else
{
fprintf(newConfigFile, "%s", line);
}
}
// Close the files
fclose(configFile);
fclose(newConfigFile);
// Rename the new file
rename(newConfigFilePath, configFilePath);
exit(0);
}
if (argc == 2)
{
if (isdigit(argv[1][0]) || (argv[1][0] == '-' && isdigit(argv[1][1])))
{
double expense = atof(argv[1]);
// Create a new file
char newConfigFilePath[256];
sprintf(newConfigFilePath, "%s/.local/share/saldotmpconfig.txt",
getenv("HOME"));
FILE *newConfigFile = fopen(newConfigFilePath, "w");
// Find the line
char line[100];
while (fgets(line, sizeof(line), configFile) != NULL)
{
if (atoi(line) == today)
{
double existingExpense;
sscanf(line, "%d %lf", &today, &existingExpense);
expense += existingExpense;
fprintf(newConfigFile, "%d %.2lf\n", today, expense);
}
else
{
fprintf(newConfigFile, "%s", line);
}
}
// Close the files
fclose(configFile);
fclose(newConfigFile);
// Rename the new file
rename(newConfigFilePath, configFilePath);
exit(0);
}
}
// Check if the configuration file exists and generate it if not
if (!configFile)
{
printf("No config file found. Generating one now...\nDon't forget to edit "
"it\n");
FILE *configFile = fopen(configFilePath, "w");
fprintf(configFile, "income 0\n");
fprintf(configFile, "fixed_expenses 0\n");
fprintf(configFile, "# \n");
for (int i = 1; i <= daysInMonth; i++)
{
fprintf(configFile, "%d 0\n", i);
}
fclose(configFile);
exit(0);
}
// Print only today saldo or all
if (argc > 1 &&
(strcmp(argv[1], "--today") == 0 || strcmp(argv[1], "-t") == 0))
{
// Read income and fixed expenses from the configuration file
fscanf(configFile, "income %lf\n", &income);
fscanf(configFile, "fixed_expenses %lf\n", &fixedExpenses);
totalBudget = income - fixedExpenses;
dailyBudget = totalBudget / daysInMonth;
saldo = 0;
// Calculate the saldo for each day and print the result
for (day = 1; day <= daysInMonth; day++)
{
char line[128];
if (fgets(line, sizeof(line), configFile) != NULL)
{
if (line[0] == '#')
{
continue;
}
if (sscanf(line, "%d %lf\n", &day, &expenses) != EOF)
{
saldo += dailyBudget - expenses;
}
else
{
saldo += dailyBudget;
}
if (day == today)
{
printf("%02d %s : %.0lf ¤ *\n", day, monthName, saldo);
}
if (day == tomorrow)
{
printf("%02d %s : %.0lf ¤\n", day, monthName, saldo);
}
}
}
fclose(configFile);
return 0;
}
else
{
// Read income and fixed expenses from the configuration file
fscanf(configFile, "income %lf\n", &income);
fscanf(configFile, "fixed_expenses %lf\n", &fixedExpenses);
totalBudget = income - fixedExpenses;
dailyBudget = totalBudget / daysInMonth;
saldo = 0;
// Calculate the saldo for each day and print the result
for (day = 1; day <= daysInMonth; day++)
{
char line[128];
if (fgets(line, sizeof(line), configFile) != NULL)
{
if (line[0] == '#')
{
continue;
}
if (sscanf(line, "%d %lf\n", &day, &expenses) != EOF)
{
saldo += dailyBudget - expenses;
}
else
{
saldo += dailyBudget;
}
// if (day == yesterday)
// {
// printf("On the %d you may spend
//%.2lf\033[0;31m\n", day, saldo);
// }
if (day == today)
{
printf("%02d %s\t%.0lf ¤ *\n", day, monthName, saldo);
// printf("\033[0m");
}
else
{
printf("%02d %s\t%.0lf ¤\n", day, monthName, saldo);
}
}
}
// Display the daily budget
printf("\nYour average daily budget for this month is %.0lf\n", dailyBudget);
printf("You spent %.0lf this month.\n", totalBudget - saldo);
printf("You income are %.0lf this month and fixed expenses are %.0lf.\n", income, fixedExpenses);
fclose(configFile);
return 0;
}
}