diff options
author | Joursoir <chat@joursoir.net> | 2021-04-03 17:56:02 +0000 |
---|---|---|
committer | Joursoir <chat@joursoir.net> | 2021-04-03 17:56:02 +0000 |
commit | 60271422194afae4a9c89dceaed5c2d53c51ff00 (patch) | |
tree | 82ad4435ce7970887526e4f8a1b8a3a1e647f202 /src/xstdlib | |
parent | 5ccf499a04871129cb188f0ab7c376716a87c823 (diff) | |
download | space-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.cpp | 38 | ||||
-rw-r--r-- | src/xstdlib/xstdlib.hpp | 6 |
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 */ |