aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-02-02 19:43:40 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-02-02 19:44:08 +0100
commit98cf44a23e4216c7e2ba156a67eabdf21a224c91 (patch)
tree7f1b437399f6103bba2133cb79633cf87c800309
parentAdd -Wextra flag, fix resulting warnings. (diff)
downloadcalp-98cf44a23e4216c7e2ba156a67eabdf21a224c91.tar.gz
calp-98cf44a23e4216c7e2ba156a67eabdf21a224c91.tar.xz
Broke read_vcalendar of into own file.
-rw-r--r--calendar.c56
-rw-r--r--calendar.h8
-rw-r--r--main.c54
-rw-r--r--trie.inc3
4 files changed, 72 insertions, 49 deletions
diff --git a/calendar.c b/calendar.c
new file mode 100644
index 00000000..f99eb775
--- /dev/null
+++ b/calendar.c
@@ -0,0 +1,56 @@
+#include "calendar.h"
+
+/*
+ * These three are only for some FD hacks.
+ */
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <dirent.h>
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+
+#include "macro.h"
+#include "parse.h"
+
+int read_vcalendar(vcalendar* cal, char* path) {
+
+ DIR* dir = opendir(path);
+ struct dirent* d;
+ while ((d = readdir(dir)) != NULL) {
+
+ /* Check that it's a regular file */
+ if (d->d_type != DT_REG) continue;
+
+ /* Check that we have an ICS file */
+ char *s, *fname;
+ s = fname = d->d_name;
+ while (*(s++) != '.');
+
+ if (strcmp(s, "ics") != 0) continue;
+
+ /* We now assume that it's a good file, and start parsing it */
+
+ int fd = openat(dirfd(dir), fname, O_RDONLY);
+
+ FILE* f = fdopen(fd, "r");
+ if (f == NULL) {
+ fprintf(stderr, "Error opening file [%s], errno = %i\n",
+ fname, errno);
+ exit (1);
+ }
+
+ /* TODO currently the hedaers cal is overwritten each
+ * iteration (not really, since I don't save any headers).
+ * Preferably, a special case is made for vdir structures
+ * which can assume that all headers are equal. */
+ parse_file(f, cal);
+ fclose(f);
+
+ }
+
+ closedir(dir);
+
+ return 0;
+}
diff --git a/calendar.h b/calendar.h
new file mode 100644
index 00000000..e17d9281
--- /dev/null
+++ b/calendar.h
@@ -0,0 +1,8 @@
+#ifndef CALENDAR_H
+#define CALENDAR_H
+
+#include "vcal.h"
+
+int read_vcalendar(vcalendar* cal, char* path);
+
+#endif /* CALENDAR_H */
diff --git a/main.c b/main.c
index 475f9ddc..9ee1288b 100644
--- a/main.c
+++ b/main.c
@@ -1,62 +1,19 @@
-#include <dirent.h>
#include <errno.h>
-/*
- * These three are only for some FD hacks.
- */
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-
-#include <string.h>
-
-#include "parse.h"
+#include "calendar.h"
#include "macro.h"
+#include "vcal.h"
+#include "stdio.h"
int main (int argc, char* argv[argc]) {
if (argc < 2) {
- //puts("Please give a ics file as first argument");
puts("Please give vdir as first argument");
- exit (1);
+ exit (1);
}
SNEW(vcalendar, cal);
- char* dname = argv[1];
- DIR* dir = opendir(dname);
- struct dirent* d;
- int fcount = 0;
- while ((d = readdir(dir)) != NULL) {
-
- /* Check that it's a regular file */
- if (d->d_type != DT_REG) continue;
-
- /* Check that we have an ICS file */
- char *s, *fname;
- s = fname = d->d_name;
- while (*(s++) != '.');
- if (strcmp(s, "ics") != 0) continue;
-
- /* We now assume that it's a good file, and start parsing it */
-
- int fd = openat(dirfd(dir), fname, O_RDONLY);
-
- FILE* f = fdopen(fd, "r");
- if (f == NULL) {
- fprintf(stderr, "Error opening file [%s], errno = %i\n",
- fname, errno);
- exit (1);
- }
-
- printf("%3i | %s\n", fcount++, fname);
- /* TODO currently the hedaers cal is overwritten each
- * iteration (not really, since I don't save any headers).
- * Preferably, a special case is made for vdir structures
- * which can assume that all headers are equal. */
- parse_file(f, &cal);
- fclose(f);
-
- }
+ read_vcalendar(&cal, argv[1]);
printf("\nParsed calendar file containing [%lu] events\n",
cal.n_events);
@@ -64,6 +21,5 @@ int main (int argc, char* argv[argc]) {
printf("%3lu. %s\n", i + 1, get_property(cal.events[i], "SUMMARY")->val.mem);
}
- closedir(dir);
free_vcalendar(&cal);
}
diff --git a/trie.inc b/trie.inc
index b1e0d0ed..532251b3 100644
--- a/trie.inc
+++ b/trie.inc
@@ -75,6 +75,9 @@ int TRIE_PUT(TYPE) ( TRIE(TYPE)* trie, char* key, TYPE* val ) {
return 0;
}
+/*
+ * TODO what happens when I give an invalid key?
+ */
TYPE* TRIE_GET(TYPE) ( TRIE(TYPE)* trie, char* key ) {
TRIE_NODE(TYPE)* n = trie->root->child;
char* subkey = key;