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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
const char *filename = "./anime.csv";
typedef struct {
char name[80];
char episode[5];
} entry_t;
typedef enum {
MATCH_MODE,
SEARCH_MODE,
APPEND_MODE,
EDIT_MODE,
PRINT_MODE
} mode_e;
void print_help()
{
printf("usage: myal [-s <name>] [-ae <name> <episode>] \n");
printf(" Prints all entries by default \n");
printf(" -m <name> Prints all anime containing the given string \n");
printf(" -s <name> Search for a specific anime by name \n");
printf(" -a <name> <number> Append new anime and episode to file \n");
printf(" -e <name> <number> Edit the episode number of an anime \n");
printf("\n");
}
char *getfield(char *line, int num)
{
char *tok;
for (tok = strtok(line, ","); tok && *tok; tok = strtok(NULL, ",")) {
if (!--num) {
return tok;
}
}
return NULL;
}
// print everything
void print_all()
{
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "ERROR: no such file\n");
exit(EXIT_FAILURE);
}
char line[100];
while (fgets(line, sizeof(line), fp)) {
char *tmp = strdup(line);
char *name = getfield(tmp, 1);
tmp = strdup(line);
char *episode = getfield(tmp, 2);
printf("Name: %s Episode: %s", name, episode);
free(tmp);
}
fclose(fp);
}
// print any entry containing the search string
void print_matches(const char *sel)
{
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "ERROR: no such file\n");
exit(EXIT_FAILURE);
}
char line[100];
while (fgets(line, sizeof(line), fp)) {
char *tmp = strdup(line);
char *name = getfield(tmp, 1);
tmp = strdup(line);
char *episode = getfield(tmp, 2);
if (strcasestr(name, sel) != NULL) {
printf("Name: %s Episode: %s", name, episode);
}
free(tmp);
}
fclose(fp);
}
// return one single entry matching the search string from the first character
entry_t *get_entry(const char *sel)
{
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
fprintf(stderr, "ERROR: no such file\n");
exit(EXIT_FAILURE);
}
entry_t *res = malloc(sizeof(entry_t));
char line[100];
while (fgets(line, sizeof(line), fp)) {
char *tmp = strdup(line);
char *name = getfield(tmp, 1);
tmp = strdup(line);
char *episode = getfield(tmp, 2);
if (strncasecmp(name, sel, strlen(sel)) == 0) {
strcpy(res->name, name);
strcpy(res->episode, episode);
break;
}
free(tmp);
}
fclose(fp);
return res;
}
// append new entry to the end of the csv
void append_entry(const char *name, const char *ep)
{
FILE *fp = fopen(filename, "a");
if (fp == NULL) {
fprintf(stderr, "ERROR: no such file\n");
exit(EXIT_FAILURE);
}
entry_t *entry = malloc(sizeof(entry_t));
strncpy(entry->name, name, sizeof(entry->name));
strncpy(entry->episode, ep, sizeof(entry->episode));
fprintf(fp, "%s,%s\n", entry->name, entry->episode);
}
// edit the episode number of an anime
void edit_entry(const char *name, const char *new_ep)
{
FILE *fp_old = fopen(filename, "r");
FILE *fp_new = fopen("temp.csv", "w");
if (fp_old == NULL || fp_new == NULL) {
fprintf(stderr, "ERROR: no such file\n");
exit(EXIT_FAILURE);
}
char line[100];
while (fgets(line, sizeof(line), fp_old)) {
char *tmp = strdup(line);
char *old_name = getfield(tmp, 1);
tmp = strdup(line);
char *old_ep = getfield(tmp, 2);
if (strcasestr(old_name, name) != NULL) {
fprintf(fp_new, "%s,%s\n", old_name, new_ep);
} else {
fprintf(fp_new, "%s,%s", old_name, old_ep);
}
free(tmp);
}
remove(filename);
rename("temp.csv", filename);
fclose(fp_old);
fclose(fp_new);
}
int main(int argc, char **argv)
{
mode_e mode = PRINT_MODE;
int opt;
entry_t *entry;
while ((opt = getopt(argc, argv, "msae")) != -1) {
switch (opt) {
case 'm': mode = MATCH_MODE; break;
case 's': mode = SEARCH_MODE; break;
case 'a': mode = APPEND_MODE; break;
case 'e': mode = EDIT_MODE; break;
default:
print_help();
exit(EXIT_FAILURE);
}
}
if (optind >= argc && mode != PRINT_MODE) {
fprintf(stderr, "ERROR: missing argument\n");
print_help();
exit(EXIT_FAILURE);
}
if (optind == 1 && argc == 2) {
fprintf(stderr, "ERROR: no option for argument given\n");
print_help();
exit(EXIT_FAILURE);
}
switch (mode) {
case MATCH_MODE:
print_matches(argv[optind]);
break;
case SEARCH_MODE:
// maybe useful for sanitizing input, but not needed for now
//
// size_t buffer_size = strlen(argv[optind]) + 1;
// char *selection = (char*)malloc(buffer_size);
// if (selection == NULL) {
// fprintf(stderr, "ERROR: failed to allocate memory for buffer\n");
// exit(EXIT_FAILURE);
// }
// strncpy(selection, argv[optind], buffer_size - 1);
// selection[buffer_size - 1] = '\0';
// [...]
// free(selection);
entry = get_entry(argv[optind]);
if (entry->name[0] == '\0') {
printf("Entry not found\n");
break;
}
printf("Name: %s Episode: %s", entry->name, entry->episode);
break;
case APPEND_MODE:
append_entry(argv[optind], argv[optind+1]);
break;
case EDIT_MODE:
edit_entry(argv[optind], argv[optind+1]);
break;
case PRINT_MODE:
print_all();
break;
default:
print_help();
break;
}
return 0;
}
|