Utils new Features
This commit is contained in:
parent
4a2ab6f5d0
commit
c66c943d3a
4 changed files with 300 additions and 0 deletions
12
Utils/outdated/Random.cpp
Normal file
12
Utils/outdated/Random.cpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
|
||||
|
||||
#include "Random.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace tp {
|
||||
flt8 randf() {
|
||||
flt8 r = static_cast<flt8>(std::rand()) / static_cast<flt8>(RAND_MAX);
|
||||
return r;
|
||||
}
|
||||
};
|
||||
7
Utils/outdated/Random.hpp
Normal file
7
Utils/outdated/Random.hpp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "Env.hpp"
|
||||
|
||||
namespace tp {
|
||||
flt8 randf();
|
||||
};
|
||||
211
Utils/outdated/file.cpp
Normal file
211
Utils/outdated/file.cpp
Normal file
|
|
@ -0,0 +1,211 @@
|
|||
|
||||
#include "env.h"
|
||||
|
||||
#include "file.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <sys/stat.h>
|
||||
|
||||
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<uint1>(&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));
|
||||
}
|
||||
};
|
||||
70
Utils/outdated/file.h
Normal file
70
Utils/outdated/file.h
Normal file
|
|
@ -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 <typename Type>
|
||||
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 <typename Type>
|
||||
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);
|
||||
};
|
||||
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue