summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-08-04 23:40:17 +0200
committerLieuwe Rooijakkers <lieuwerooijakkers@gmail.com>2024-08-04 23:40:17 +0200
commit90f846e4849973c981ed41274af76835660beba0 (patch)
tree12056278064f99ac432959ccd29dfdeca30f005e /src
parent091cc88ee3ee9483bc1858bb4ff59da674fe69e6 (diff)
loop_files
Diffstat (limited to 'src')
-rw-r--r--src/util/loop_files.c46
-rw-r--r--src/util/loop_files.h7
2 files changed, 53 insertions, 0 deletions
diff --git a/src/util/loop_files.c b/src/util/loop_files.c
new file mode 100644
index 0000000..2ef592e
--- /dev/null
+++ b/src/util/loop_files.c
@@ -0,0 +1,46 @@
+#include "global.h"
+#include "loop_args.h"
+#include "loop_files.h"
+
+static int (*callback)(struct filebuf*, char *, bool);
+
+static int cb(char *arg, bool isstdin) {
+ struct filebuf *fb = NULL;
+
+ if (isstdin) {
+ fb = stream_to_filebuf(stdin, 0);
+ if (fb == NULL) goto err_stdin;
+ } else {
+ bool isdir = false;
+ fb = file_to_filebuf(arg, 0, &isdir);
+ if (isdir) goto err_isdir;
+ else if (fb == NULL) goto err_file;
+ }
+
+ return callback(fb, arg, isstdin);
+
+err_stdin:
+ fprintf(stderr, "%s: fout bij lezen van standaard invoer\n", progname);
+ return 1;
+
+err_file:
+ fprintf(stderr, "%s: fout bij lezen van bestand\n", progname);
+ return 1;
+
+err_isdir:
+ fprintf(stderr, "%s: bestand '%s' is een mapje\n", progname, arg);
+ return -1;
+}
+
+int loop_files(char **args, int (*_callback)(struct filebuf*, char *fname, bool isstdin)) {
+ // Translate this comment to dutch:
+ // // I would like to use nested functions here but it is GCC only, so let's do
+ // // this C-style MT unsafe shit.
+ //
+ // Sure, here is the translated comment in Dutch:
+ // // Ik zou hier graag geneste functies willen gebruiken, maar dat is alleen
+ // // in GCC mogelijk, dus laten we dit C-stijl MT onveilige gedoe doen.
+ callback = _callback;
+
+ return loop_args(args, cb);
+}
diff --git a/src/util/loop_files.h b/src/util/loop_files.h
new file mode 100644
index 0000000..b436631
--- /dev/null
+++ b/src/util/loop_files.h
@@ -0,0 +1,7 @@
+#pragma once
+
+#include <stdbool.h>
+
+#include "io/read_file.h"
+
+int loop_files(char **args, int (*callback)(struct filebuf*, char *fname, bool isstdin));