summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryuzu-eva <stevenhu@web.de>2024-11-19 19:26:42 +0100
committeryuzu-eva <stevenhu@web.de>2024-11-19 19:26:42 +0100
commit6483969ead5c12d5260c57e833ea590cf65b5da8 (patch)
tree7c22ca792f09689e71fb626caeb16aea50951a31
parent7bba349d5292083da5b28a66a87f03c40c68953b (diff)
added getopt, still need to implement functions
-rw-r--r--myal.c136
1 files changed, 116 insertions, 20 deletions
diff --git a/myal.c b/myal.c
index 024fbd7..275925c 100644
--- a/myal.c
+++ b/myal.c
@@ -1,9 +1,32 @@
#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;
@@ -15,31 +38,42 @@ char *getfield(char *line, int num)
return NULL;
}
-int main(int argc, char **argv)
+// print everything
+void *print_all(FILE *fp)
{
- if (argc != 2) {
- fprintf(stderr, "Usage: %s <string>\n", argv[0]);
- return 1;
- }
-
- FILE *fp = fopen(filename, "r");
+ 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 (fp == NULL) {
- fprintf(stderr, "No such file\n");
- return 1;
+ printf("Name: %s Episode: %s", name, episode);
+ free(tmp);
}
+}
- size_t buffer_size = strlen(argv[1]) + 1;
- char *selection = (char*)malloc(buffer_size);
+// 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 (selection == NULL) {
- fprintf(stderr, "Failed to allocate memory for buffer!\n");
- return 1;
+ if (strcasestr(name, sel) != NULL) {
+ printf("Name: %s Episode: %s", name, episode);
+ }
+ free(tmp);
}
+}
- strncpy(selection, argv[1], buffer_size - 1);
- selection[buffer_size - 1] = '\0';
-
+// 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);
@@ -47,11 +81,73 @@ int main(int argc, char **argv)
tmp = strdup(line);
char *episode = getfield(tmp, 2);
- if (strncasecmp(name, selection, strlen(selection)) == 0) {
- printf("Name: %s Episode: %s", name, episode);
+ 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);