aboutsummaryrefslogtreecommitdiffstats
path: root/src/xstdlib
diff options
context:
space:
mode:
authorJoursoir <chat@joursoir.net>2021-04-03 17:56:02 +0000
committerJoursoir <chat@joursoir.net>2021-04-03 17:56:02 +0000
commit60271422194afae4a9c89dceaed5c2d53c51ff00 (patch)
tree82ad4435ce7970887526e4f8a1b8a3a1e647f202 /src/xstdlib
parent5ccf499a04871129cb188f0ab7c376716a87c823 (diff)
downloadspace-simulator-60271422194afae4a9c89dceaed5c2d53c51ff00.tar.gz
space-simulator-60271422194afae4a9c89dceaed5c2d53c51ff00.tar.bz2
space-simulator-60271422194afae4a9c89dceaed5c2d53c51ff00.zip
add routine for read file
Diffstat (limited to 'src/xstdlib')
-rw-r--r--src/xstdlib/xstdlib.cpp38
-rw-r--r--src/xstdlib/xstdlib.hpp6
2 files changed, 44 insertions, 0 deletions
diff --git a/src/xstdlib/xstdlib.cpp b/src/xstdlib/xstdlib.cpp
new file mode 100644
index 0000000..4264e68
--- /dev/null
+++ b/src/xstdlib/xstdlib.cpp
@@ -0,0 +1,38 @@
+#include <stdio.h>
+
+#include "xstdlib.hpp"
+
+char *xfread(const char *path, const char *mode)
+{
+ char *source = 0;
+ size_t file_size, bytes_read;
+ FILE *file;
+
+ file = fopen(path, mode);
+ if(!file)
+ return 0;
+
+ do {
+ if(fseek(file, 0, SEEK_END) < 0)
+ break;
+
+ file_size = ftell(file);
+ if(file_size < 0)
+ break;
+
+ if(fseek(file, 0, SEEK_SET) < 0)
+ break;
+
+ source = new char[file_size + 1];
+ bytes_read = fread(source, 1, file_size, file);
+ if(bytes_read != file_size) {
+ delete[] source;
+ source = 0;
+ }
+ source[file_size] = '\0';
+ } while(0);
+
+ fclose(file);
+ return source;
+}
+
diff --git a/src/xstdlib/xstdlib.hpp b/src/xstdlib/xstdlib.hpp
new file mode 100644
index 0000000..7a2c704
--- /dev/null
+++ b/src/xstdlib/xstdlib.hpp
@@ -0,0 +1,6 @@
+#ifndef ENGINE_XSTDLIB_H
+#define ENGINE_XSTDLIB_H
+
+char *xfread(const char *path, const char *mode);
+
+#endif /* ENGINE_XSTDLIB_H */