aboutsummaryrefslogtreecommitdiff
path: root/src/utility.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/utility.cc')
-rw-r--r--src/utility.cc54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/utility.cc b/src/utility.cc
new file mode 100644
index 0000000..fb6fe61
--- /dev/null
+++ b/src/utility.cc
@@ -0,0 +1,54 @@
+#include "utility.h"
+
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <cstddef>
+#include <cstdio>
+
+namespace {
+
+const int OpenFlags = O_RDONLY;
+const mode_t OpenMode = S_IRUSR | S_IWUSR;
+
+}
+
+namespace InputXSLT {
+
+std::string readFile(const std::string& path) {
+ int descriptor(
+ open(path.data(), OpenFlags, OpenMode)
+ );
+
+ if ( descriptor == -1 ) {
+ close(descriptor);
+
+ return "io error";
+ } else {
+ struct stat info;
+ fstat(descriptor, &info);
+ const std::size_t size(info.st_size);
+
+ char* const buffer(new char[size]);
+
+ ssize_t readSize(read(
+ descriptor,
+ static_cast<void*const>(buffer),
+ size
+ ));
+
+ close(descriptor);
+
+ std::string content(
+ buffer,
+ readSize
+ );
+
+ delete[] buffer;
+
+ return content;
+ }
+}
+
+}