summaryrefslogtreecommitdiff
path: root/code/core/fileutils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'code/core/fileutils.cpp')
-rw-r--r--code/core/fileutils.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/code/core/fileutils.cpp b/code/core/fileutils.cpp
new file mode 100644
index 0000000..9d080ce
--- /dev/null
+++ b/code/core/fileutils.cpp
@@ -0,0 +1,37 @@
+#include <string.h>
+#include <stdlib.h>
+
+#include "fileutils.h"
+
+// direction is either false=input or true=output and used to overload parameter '-'
+FILE *fd_magic(char *str, bool direction) {
+ static bool usedDir[2] = { false, false };
+
+ // check if stdin/-out is wanted
+ if (strcmp(str, "-") == 0) {
+ // check if we already used this
+ if (usedDir[direction]) {
+ fprintf(stderr, "stdin/-out cannot be used twice\n");
+ exit(-1);
+ }
+
+ // mark that we used it and return in
+ usedDir[direction] = true;
+ return direction ? stdout : stdin;
+ }
+
+ // check if stdout is wanted
+ if (strcmp(str, "0") == 0) {
+ // replace filename and proceed
+ str = "/dev/null";
+ }
+
+ // open file traditionally
+ FILE *fd = fopen(str, direction ? "w" : "r" );
+ if (fd == NULL) {
+ fprintf(stderr, "Failed to open %s\n", str);
+ exit(-1);
+ }
+
+ return fd;
+}
contact: Jan Huwald // Impressum