From c66c943d3a2f6c53935b9239917d85585543eff6 Mon Sep 17 00:00:00 2001 From: IlushaShurupov Date: Fri, 7 Jul 2023 00:12:42 +0300 Subject: [PATCH] Utils new Features --- Utils/outdated/Random.cpp | 12 +++ Utils/outdated/Random.hpp | 7 ++ Utils/outdated/file.cpp | 211 ++++++++++++++++++++++++++++++++++++++ Utils/outdated/file.h | 70 +++++++++++++ 4 files changed, 300 insertions(+) create mode 100644 Utils/outdated/Random.cpp create mode 100644 Utils/outdated/Random.hpp create mode 100644 Utils/outdated/file.cpp create mode 100644 Utils/outdated/file.h diff --git a/Utils/outdated/Random.cpp b/Utils/outdated/Random.cpp new file mode 100644 index 0000000..60773d7 --- /dev/null +++ b/Utils/outdated/Random.cpp @@ -0,0 +1,12 @@ + + +#include "Random.hpp" + +#include + +namespace tp { + flt8 randf() { + flt8 r = static_cast(std::rand()) / static_cast(RAND_MAX); + return r; + } +}; \ No newline at end of file diff --git a/Utils/outdated/Random.hpp b/Utils/outdated/Random.hpp new file mode 100644 index 0000000..f4c3c88 --- /dev/null +++ b/Utils/outdated/Random.hpp @@ -0,0 +1,7 @@ +#pragma once + +#include "Env.hpp" + +namespace tp { + flt8 randf(); +}; \ No newline at end of file diff --git a/Utils/outdated/file.cpp b/Utils/outdated/file.cpp new file mode 100644 index 0000000..666b824 --- /dev/null +++ b/Utils/outdated/file.cpp @@ -0,0 +1,211 @@ + +#include "env.h" + +#include "file.h" + +#include +#include +#include +#include +#include + +typedef std::fstream osfstream; + + +#ifdef ENV_OS_ANDROID + +const char* g_android_internal_data_path = "/"; +const int max_len = 512; +char gbuff1[max_len]; +char gbuff2[max_len]; + +// u own output untill next call to this function +const char* get_android_abs_path(const char* path, bool second = false) { + + auto buff = second ? gbuff2 : gbuff1; + + const int dir_len = strlen(g_android_internal_data_path); + const int file_len = strlen(path); + + if (max_len < dir_len + file_len + 2) { + return NULL; + } + + memcpy(buff, g_android_internal_data_path, dir_len); + buff[dir_len] = '/'; + + memcpy(buff + dir_len + 1, path, file_len + 1); + + return buff; +} + +#define PATH(__path__) get_android_abs_path(__path__) +#define PATH2(__path__) get_android_abs_path(__path__, true) + +#else + +#define PATH(__path__) __path__ +#define PATH2(__path__) __path__ + +#endif + +static inline bool file_exists(const char* name) { + struct stat buffer; + return (stat(name, &buffer) == 0); +} + +static tp::ModuleManifest* sModuleDependencies[] = { + &tp::gModuleCommon, + NULL +}; + +namespace tp { + + ModuleManifest tp::gModuleFilesystem = ModuleManifest("FileSystem", NULL, NULL, sModuleDependencies); + + File::File() { + MODULE_SANITY_CHECK(gModuleFilesystem); + + fh = NULL; + } + + File::File(const char* path, osfile_openflags flags) { + open(path, flags); + } + + void File::open(const char* apath, osfile_openflags flags) { + auto path = PATH(apath); + + close(); + + adress = 0; + avl_adress = 0; + fh = new osfstream(); + osfstream& fhref = *((osfstream*) fh); + + switch (flags) { + case SAVE: + fhref.open(path, std::ios::out | std::ios::binary | std::ios::trunc); + break; + + case LOAD: + //if (file_exists(path)) { + fhref.open(path, std::ios::in | std::ios::binary | std::ios::app); + //} + break; + }; + + if (!fhref.is_open()) { + opened = false; + delete& fhref; + return; + } + opened = true; + } + + void File::close() { + if (opened) { + osfstream& fhref = *((osfstream*) fh); + fhref.close(); + opened = false; + delete ((osfstream*) fh); + } + } + + File::~File() { + close(); + if (preloaded) { + free(preloaded); + } + } + + void File::Preload() { + if (!opened) return; + + RelAssert(!preloaded); + alni filesize = size(); + preloaded = malloc(filesize); + + osfstream& fhref = *((osfstream*) fh); + fhref.seekg(0); + fhref.read((char*) preloaded, filesize); + } + + alni File::sizeAllocatedMem() { + alni out = 0; + out += sizeof(osfstream); + out += sizeof(alni) * 2; + out += sizeof(bool) * 2; + return out; + } + + void File::write_bytes(const int1* in, alni size, alni p_adress) { + if (!opened) return; + + if (p_adress == -1) { + p_adress = this->adress; + this->adress += size; + } + + osfstream& fhref = *((osfstream*) fh); + + fhref.seekp(p_adress); + fhref.write(in, size); + } + + void File::read_bytes(int1* in, alni size, alni p_adress) { + if (!opened) return; + + if (p_adress == -1) { + p_adress = this->adress; + adress += size; + } + + osfstream& fhref = *((osfstream*) fh); + + fhref.seekg(p_adress); + fhref.read(in, size); + + if (preloaded) { + char* read_mem = ((char*) preloaded) + p_adress; + memcp(in, read_mem, size); + } else { + osfstream& fhref = *((osfstream*) fh); + fhref.seekg(p_adress); + fhref.read(in, size); + } + } + + void File::fill(uint1 val, alni len) { + if (!opened) return; + + for (int i = 0; i < len; i++) { + write(&val, i); + } + } + + alni File::size() { + if (!opened) return 0; + + osfstream& fhref = *((osfstream*) fh); + + alni out = 0; + fhref.seekg(0, std::ios::beg); + out = fhref.tellg(); + fhref.seekg(0, std::ios::end); + out = fhref.tellg() - out; + return out; + } + + bool File::remove(const char* path) { + return (bool) std::remove(PATH(path)); + } + + bool File::rename(const char* path, const char* path2) { + return (bool)std::rename(PATH(path), PATH2(path2)); + } + + bool File::exists(const char* path) { + return file_exists(PATH(path)); + } +}; \ No newline at end of file diff --git a/Utils/outdated/file.h b/Utils/outdated/file.h new file mode 100644 index 0000000..3e59272 --- /dev/null +++ b/Utils/outdated/file.h @@ -0,0 +1,70 @@ + +#pragma once + +#include "common.h" + +namespace tp { + + extern ModuleManifest gModuleFilesystem; + + enum osfile_openflags { + SAVE, + LOAD, + }; + + class File { + void* fh = NULL; + + public: + alni adress = 0; + bool opened = false; + + alni avl_adress = 0; + + void* preloaded = NULL; + + File(); + + File(const char* path, osfile_openflags flags); + + void open(const char* path, osfile_openflags); + + void write_bytes(const int1* in, alni size, alni adress = -1); + + template + void write(Type* in, alni adress = -1) { + write_bytes((int1*) in, sizeof(Type), adress); + } + + void read_bytes(int1* in, alni size, alni adress = -1); + + template + void read(Type* in, alni adress = -1) { + read_bytes((int1*) in, sizeof(Type), adress); + } + + void write(const char* in1) { + assert(0 && "depricated"); + } + + alni size(); + + void fill(uint1 val, alni len); + + void close(); + + void Preload(); + + ~File(); + + alni sizeAllocatedMem(); + alni sizeUsedMem() { + return sizeAllocatedMem(); + } + + static bool remove(const char* path); + static bool rename(const char* path, const char* path2); + static bool exists(const char* path); + }; + +}; \ No newline at end of file