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
|
#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 {
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(" -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)
{
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);
}
}
// print any entry containing the search string
void *print_matches(FILE *fp, const char *sel)
{
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);
}
}
// return one single entry matching the search string from the first character
entry_t *get_entry(FILE *fp, const char *sel)
{
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);
}
return res;
}
int main(int argc, char **argv)
{
FILE *fp = fopen(filename, "rb");
if (fp == NULL) {
fprintf(stderr, "No such file\n");
exit(EXIT_FAILURE);
}
mode_e mode = PRINT_MODE;
int opt;
while ((opt = getopt(argc, argv, "sae")) != -1) {
switch (opt) {
case 's': mode = SEARCH_MODE; break;
case 'a': mode = APPEND_MODE; break;
case 'e': mode = EDIT_MODE; break;
default:
print_help();
fclose(fp);
exit(EXIT_FAILURE);
}
}
if (mode == PRINT_MODE) {
print_all(fp);
fclose(fp);
exit(EXIT_SUCCESS);
}
if (optind >= argc) {
fprintf(stderr, "ERROR: missing argument\n");
print_help();
fclose(fp);
exit(EXIT_FAILURE);
}
size_t buffer_size = strlen(argv[optind]) + 1;
char *selection = (char*)malloc(buffer_size);
if (selection == NULL) {
fprintf(stderr, "Failed to allocate memory for buffer!\n");
exit(EXIT_FAILURE);
}
strncpy(selection, argv[optind], buffer_size - 1);
selection[buffer_size - 1] = '\0';
switch (mode) {
case SEARCH_MODE:
print_matches(fp, selection);
break;
case APPEND_MODE:
case EDIT_MODE:
default:
print_help();
break;
}
free(selection);
fclose(fp);
return 0;
}
|