Tool refactoring
This commit is contained in:
5
.gitignore
vendored
5
.gitignore
vendored
@ -82,6 +82,5 @@ assets/borrowed
|
||||
.vscode*
|
||||
.VSCode*
|
||||
|
||||
/vita/*
|
||||
/tools/*
|
||||
./tools/*
|
||||
/vita
|
||||
/tools
|
@ -6,7 +6,7 @@
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")
|
||||
|
||||
|
17
archive/tools/util/CMakeLists.txt
Normal file
17
archive/tools/util/CMakeLists.txt
Normal file
@ -0,0 +1,17 @@
|
||||
# Copyright (c) 2023 Dominic Msters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
set(D ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
set(
|
||||
DAWN_TOOL_SOURCES
|
||||
${D}/DawnTool.cpp
|
||||
${D}/File.cpp
|
||||
${D}/Language.cpp
|
||||
${D}/CodeGen.cpp
|
||||
|
||||
CACHE INTERNAL
|
||||
${DAWN_CACHE_TARGET}
|
||||
)
|
36
archive/tools/util/string.hpp
Normal file
36
archive/tools/util/string.hpp
Normal file
@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Copyright (c) 2023 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "dawnsharedlibs.hpp"
|
||||
|
||||
/**
|
||||
* Remove all instances of a character from a C-Styled string.
|
||||
*
|
||||
* @param string String to remove characters from.
|
||||
* @param remove Character to remove.
|
||||
*/
|
||||
static inline void stringRemoveAll(char *string, char remove) {
|
||||
size_t len = strlen(string);
|
||||
size_t i, j;
|
||||
|
||||
i = 0;
|
||||
while(i < len) {
|
||||
char c = string[i];
|
||||
if(c != remove) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
j = i + 1;
|
||||
while(j < len) {
|
||||
string[j-1] = string[j];
|
||||
j++;
|
||||
}
|
||||
len--;
|
||||
}
|
||||
}
|
@ -5,11 +5,7 @@
|
||||
|
||||
# Check for build target, or default. This is pretty much not guaranteed.
|
||||
if(NOT DEFINED DAWN_BUILD_TARGET)
|
||||
if(WIN32)
|
||||
set(DAWN_BUILD_TARGET "target-rose-win32-glfw")
|
||||
elseif(UNIX AND NOT APPLE)
|
||||
set(DAWN_BUILD_TARGET "target-rose-linux64-glfw")
|
||||
endif()
|
||||
set(DAWN_BUILD_TARGET "target-tools")
|
||||
endif()
|
||||
|
||||
# Now validate we have a build target for real
|
||||
|
9
cmake/targets/target-rose-win64-glfw/CMakeLists.txt
Normal file
9
cmake/targets/target-rose-win64-glfw/CMakeLists.txt
Normal file
@ -0,0 +1,9 @@
|
||||
# Copyright (c) 2023 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
set(DAWN_BUILDING dawnrose CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
||||
set(DAWN_TARGET_WIN32 true CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
||||
set(DAWN_TARGET_GLFW true CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
||||
set(DAWN_TARGET_NAME "Rose" CACHE INTERNAL ${DAWN_CACHE_TARGET})
|
@ -18,12 +18,12 @@ void SphereMesh::createSphere(
|
||||
// Create vertices
|
||||
int32_t c = 0;
|
||||
for (int32_t i = 0; i <= stacks; i++) {
|
||||
float_t phi = M_PI * i / stacks;
|
||||
float_t phi = MATH_PI * i / stacks;
|
||||
float_t cosPhi = cos(phi);
|
||||
float_t sinPhi = sin(phi);
|
||||
|
||||
for (int32_t j = 0; j <= slices; j++) {
|
||||
float_t theta = 2 * M_PI * j / slices;
|
||||
float_t theta = 2 * MATH_PI * j / slices;
|
||||
float_t cosTheta = cos(theta);
|
||||
float_t sinTheta = sin(theta);
|
||||
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
#include "display/mesh/Mesh.hpp"
|
||||
#include "util/mathutils.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class SphereMesh {
|
||||
|
@ -19,7 +19,7 @@ bool_t CapsuleCollider::performRaycast(
|
||||
|
||||
return raytestCapsule(
|
||||
ray,
|
||||
{
|
||||
(struct PhysicsCapsule){
|
||||
.height = this->height,
|
||||
.radius = this->radius,
|
||||
.origin = this->transform->getWorldPosition()
|
||||
|
@ -30,7 +30,7 @@ void GameCamera::onStart() {
|
||||
if(current != slowTarget) {
|
||||
float_t m = 6.0f;
|
||||
float_t s = delta * 3.0f;
|
||||
current += glm::vec2(.
|
||||
current += glm::vec2(
|
||||
mathClamp<float_t>((slowTarget.x - current.x) * s, -m, m),
|
||||
mathClamp<float_t>((slowTarget.y - current.y) * s, -m, m)
|
||||
);
|
||||
|
@ -19,4 +19,4 @@ set(
|
||||
include(util/CMakeLists.txt)
|
||||
|
||||
# Tools
|
||||
add_subdirectory(tools)
|
||||
add_subdirectory(prefabtool)
|
47
src/dawntools/prefabtool/CMakeLists.txt
Normal file
47
src/dawntools/prefabtool/CMakeLists.txt
Normal file
@ -0,0 +1,47 @@
|
||||
# Copyright (c) 2023 Dominic Msters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Texture Build Tool
|
||||
project(prefabtool VERSION 1.0)
|
||||
add_executable(prefabtool)
|
||||
|
||||
# Sources
|
||||
target_sources(prefabtool
|
||||
PRIVATE
|
||||
${DAWN_SHARED_SOURCES}
|
||||
${DAWN_TOOL_SOURCES}
|
||||
PrefabTool.cpp
|
||||
)
|
||||
|
||||
# Includes
|
||||
target_include_directories(prefabtool
|
||||
PUBLIC
|
||||
${DAWN_SHARED_INCLUDES}
|
||||
${DAWN_TOOL_INCLUDES}
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
|
||||
# Definitions
|
||||
target_compile_definitions(prefabtool
|
||||
PUBLIC
|
||||
DAWN_TOOL_INSTANCE=PrefabTool
|
||||
DAWN_TOOL_HEADER="PrefabTool.hpp"
|
||||
)
|
||||
|
||||
# Libraries
|
||||
target_link_libraries(prefabtool
|
||||
PUBLIC
|
||||
${DAWN_BUILD_HOST_LIBS}
|
||||
)
|
||||
|
||||
# Tool Function
|
||||
function(tool_prefab target in)
|
||||
add_custom_target(${target}
|
||||
COMMAND prefabtool --input="${DAWN_ASSETS_SOURCE_DIR}/${in}"
|
||||
COMMENT "Generating prefab ${target} from ${in}"
|
||||
DEPENDS prefabtool
|
||||
)
|
||||
add_dependencies(${DAWN_TARGET_NAME} ${target})
|
||||
endfunction()
|
28
src/dawntools/prefabtool/PrefabTool.cpp
Normal file
28
src/dawntools/prefabtool/PrefabTool.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "PrefabTool.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
std::vector<std::string> PrefabTool::getRequiredFlags() {
|
||||
return { "input" };
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> PrefabTool::getOptionalFlags() {
|
||||
return std::map<std::string, std::string>();
|
||||
}
|
||||
|
||||
int32_t PrefabTool::start() {
|
||||
File input = File(flags["input"]);
|
||||
if(!input.exists()) {
|
||||
std::cout << "Input file does not exist!" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Input: " << input.filename << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
19
src/dawntools/prefabtool/PrefabTool.hpp
Normal file
19
src/dawntools/prefabtool/PrefabTool.hpp
Normal file
@ -0,0 +1,19 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "util/DawnTool.hpp"
|
||||
#include "util/File.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class PrefabTool : public DawnTool {
|
||||
protected:
|
||||
std::vector<std::string> getRequiredFlags() override;
|
||||
std::map<std::string, std::string> getOptionalFlags() override;
|
||||
|
||||
public:
|
||||
int32_t start();
|
||||
};
|
||||
}
|
@ -5,9 +5,6 @@
|
||||
|
||||
#pragma once
|
||||
#include "dawnsharedlibs.hpp"
|
||||
#include "../../util/file.hpp"
|
||||
#include "../../util/csv.hpp"
|
||||
#include "../../util/xml.hpp"
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
@ -1,26 +1,26 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "file.hpp"
|
||||
#include "File.hpp"
|
||||
|
||||
void fileNormalizeSlashes(char *string) {
|
||||
char c;
|
||||
int i = 0;
|
||||
using namespace Dawn;
|
||||
|
||||
while(c = string[i++]) {
|
||||
if(c != '\\' && c != '/') continue;
|
||||
string[i-1] = FILE_PATH_SEP;
|
||||
std::string File::normalizeSlashes(std::string str) {
|
||||
size_t i = 0;
|
||||
while(i < str.size()) {
|
||||
auto c = str[i];
|
||||
if(c == '\\' || c == '/') str[i] = FILE_PATH_SEP;
|
||||
++i;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
void fileMkdirp(char *path) {
|
||||
char buffer[FILENAME_MAX];
|
||||
void File::mkdirp(std::string path) {
|
||||
std::string buffer;
|
||||
char c;
|
||||
int i = 0;
|
||||
size_t i = 0;
|
||||
bool inFile;
|
||||
bool hasMore;
|
||||
|
||||
@ -28,172 +28,166 @@ void fileMkdirp(char *path) {
|
||||
hasMore = false;
|
||||
while(c = path[i]) {
|
||||
if((c == '\\' || c == '/') && i > 0) {
|
||||
buffer[i] = '\0';
|
||||
fileMkdir(buffer, 0755);
|
||||
fileMkdir(buffer.c_str(), 0755);
|
||||
inFile = false;
|
||||
hasMore = false;
|
||||
buffer[i] = FILE_PATH_SEP;
|
||||
buffer += FILE_PATH_SEP;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(c == '.') inFile = true;
|
||||
hasMore = true;
|
||||
buffer[i] = c;
|
||||
buffer += c;
|
||||
i++;
|
||||
}
|
||||
|
||||
if(!inFile && hasMore) {
|
||||
buffer[i] = '\0';
|
||||
fileMkdir(buffer, 0755);
|
||||
fileMkdir(buffer.c_str(), 0755);
|
||||
}
|
||||
}
|
||||
|
||||
size_t assetReadString(FILE *file, char *buffer) {
|
||||
size_t length;
|
||||
fseek(file, 0, SEEK_END);// Seek to the end
|
||||
length = ftell(file);// Get our current position (the end)
|
||||
fseek(file, 0, SEEK_SET);// Reset the seek
|
||||
if(buffer == NULL) return length;
|
||||
return fread(buffer, 1, length, file);// Read all the bytes
|
||||
//
|
||||
|
||||
File::File(std::string filename) {
|
||||
this->filename = File::normalizeSlashes(filename);
|
||||
}
|
||||
|
||||
int32_t readAhead(
|
||||
char *bufferIn, int32_t start,
|
||||
char *bufferOut,
|
||||
char *needles, int32_t needleCount
|
||||
) {
|
||||
int32_t i = start, k = 0, j;
|
||||
char c;
|
||||
bool needleFound = false;
|
||||
bool_t File::open(enum FileMode mode) {
|
||||
assertNull(this->file);
|
||||
|
||||
if(bufferIn[i] == '\0') return 0;
|
||||
this->mode = mode;
|
||||
this->file = fopen(
|
||||
this->filename.c_str(),
|
||||
mode == FILE_MODE_READ ? "rb" : "wb"
|
||||
);
|
||||
|
||||
while((c = bufferIn[i++]) != '\0') {
|
||||
for(j = 0; j < needleCount; j++) {
|
||||
if(c != needles[j]) continue;
|
||||
needleFound = true;
|
||||
}
|
||||
if(needleFound) break;
|
||||
if(bufferOut != NULL) bufferOut[k] = c;
|
||||
k++;
|
||||
}
|
||||
if(this->file == NULL) return false;
|
||||
|
||||
if(bufferOut != NULL) bufferOut[k] = '\0';
|
||||
return k;
|
||||
}
|
||||
if(mode == FILE_MODE_READ) {
|
||||
fseek(this->file, 0, SEEK_END);
|
||||
this->length = ftell(this->file);
|
||||
fseek(this->file, 0, SEEK_SET);
|
||||
|
||||
int32_t skipAhead(
|
||||
char *bufferIn, int32_t start,
|
||||
char *needles, int32_t needleCount
|
||||
) {
|
||||
char c;
|
||||
int32_t j, k = 0, i = start;
|
||||
bool needleFound;
|
||||
|
||||
while((c = bufferIn[i++]) != '\0') {
|
||||
needleFound = false;
|
||||
for(j = 0; j < needleCount; j++) {
|
||||
if(c != needles[j]) continue;
|
||||
needleFound = true;
|
||||
break;
|
||||
}
|
||||
if(!needleFound) break;
|
||||
k++;
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
void fileGetDirectory(char *file, char* buffer) {
|
||||
char *c, *p;
|
||||
int32_t i;
|
||||
p = strrchr(file, FILE_PATH_SEP);
|
||||
c = file;
|
||||
i = 0;
|
||||
do {
|
||||
buffer[i++] = *c;
|
||||
} while(++c < p);
|
||||
buffer[i] = '\0';
|
||||
}
|
||||
|
||||
bool fileListChildren(
|
||||
char *directory,
|
||||
char *buffer,
|
||||
int32_t *count,
|
||||
uint8_t *types,
|
||||
char **children
|
||||
) {
|
||||
#if defined(_MSC_VER)
|
||||
WIN32_FIND_DATA fdFile;
|
||||
HANDLE hFind = NULL;
|
||||
char sPath[2048];
|
||||
int32_t i;
|
||||
|
||||
// Append wildcard
|
||||
sprintf(sPath, "%s\\*.*", directory);
|
||||
|
||||
// Scan first
|
||||
if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE) {
|
||||
printf("Path not found: [%s]\n", directory);
|
||||
if(this->length <= 0) {
|
||||
this->close();
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
this->length = 0;
|
||||
}
|
||||
|
||||
// Iterate
|
||||
i = 0;
|
||||
do {
|
||||
if(
|
||||
strcmp(fdFile.cFileName, ".") == 0 ||
|
||||
strcmp(fdFile.cFileName, "..") == 0
|
||||
) continue;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Get Full path.
|
||||
sprintf(sPath, "%s\\%s", directory, fdFile.cFileName);
|
||||
bool_t File::isOpen() {
|
||||
return this->file != nullptr;
|
||||
}
|
||||
|
||||
//Is the entity a File or Folder?
|
||||
if(fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
||||
types[i] = FILE_CHILD_TYPE_DIR;
|
||||
bool_t File::exists() {
|
||||
if(this->file != nullptr) return true;
|
||||
FILE *f = fopen(this->filename.c_str(), "rb");
|
||||
if(f == NULL || f == nullptr) return false;
|
||||
fclose(f);
|
||||
return true;
|
||||
}
|
||||
|
||||
void File::close() {
|
||||
assertNotNull(this->file);
|
||||
fclose(this->file);
|
||||
this->file = nullptr;
|
||||
}
|
||||
|
||||
bool_t File::mkdirp() {
|
||||
File::mkdirp(this->filename);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool_t File::readString(std::string *out) {
|
||||
assertNotNull(out);
|
||||
|
||||
if(!this->isOpen()) {
|
||||
if(!this->open(FILE_MODE_READ)) return false;
|
||||
}
|
||||
assertTrue(this->mode == FILE_MODE_READ);
|
||||
out->clear();
|
||||
|
||||
size_t i = 0;
|
||||
char buffer[FILE_BUFFER_SIZE + 1];// +1 for null term
|
||||
while(i != this->length) {
|
||||
size_t amt = mathMin<size_t>(FILE_BUFFER_SIZE, (this->length - i));
|
||||
auto amtRead = fread(buffer, sizeof(char), amt, this->file);
|
||||
if(amtRead != amt) return false;
|
||||
i += amtRead;
|
||||
buffer[amtRead] = '\0';
|
||||
out->append(buffer);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t File::readAhead(char *buffer, size_t max, char needle) {
|
||||
assertNotNull(buffer);
|
||||
assertTrue(max > 0);
|
||||
|
||||
if(!this->isOpen()) {
|
||||
if(!this->open(FILE_MODE_READ)) return 0;
|
||||
}
|
||||
assertTrue(this->mode == FILE_MODE_READ);
|
||||
|
||||
// Buffer
|
||||
size_t pos = ftell(this->file);
|
||||
size_t amountLeftToRead = mathMin<size_t>(max, this->length - pos);
|
||||
char temporary[FILE_BUFFER_SIZE];
|
||||
size_t n = 0;
|
||||
|
||||
while(amountLeftToRead > 0) {
|
||||
size_t toRead = mathMin<size_t>(amountLeftToRead, FILE_BUFFER_SIZE);
|
||||
amountLeftToRead -= toRead;
|
||||
// Read bytes
|
||||
size_t read = fread(temporary, sizeof(char), toRead, this->file);
|
||||
|
||||
// Read error?
|
||||
if(toRead != read) return 0;
|
||||
|
||||
// Did we read the needle?
|
||||
size_t i = 0;
|
||||
while(i < read) {
|
||||
char c = temporary[i++];
|
||||
if(c == needle) {
|
||||
return n;
|
||||
} else {
|
||||
types[i] = FILE_CHILD_TYPE_FILE;
|
||||
buffer[n++] = c;
|
||||
}
|
||||
|
||||
children[i] = buffer + (i * FILE_CHILD_NAME_MAX);
|
||||
strcpy(children[i], fdFile.cFileName);
|
||||
i++;
|
||||
} while(FindNextFile(hFind, &fdFile));
|
||||
|
||||
*count = i;
|
||||
return true;
|
||||
#else
|
||||
struct dirent *de;
|
||||
DIR *dr;
|
||||
int32_t i;
|
||||
|
||||
// Open Dir
|
||||
dr = opendir(directory);
|
||||
if(dr == NULL) {
|
||||
printf("Could not open directory");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Iterate
|
||||
i = 0;
|
||||
while ((de = readdir(dr)) != NULL) {
|
||||
// File or folder?
|
||||
if(de->d_type != DT_REG) continue;
|
||||
|
||||
// Copy into child buffer
|
||||
children[i] = buffer + (i * FILE_CHILD_NAME_MAX);
|
||||
strcpy(children[i], de->d_name);
|
||||
i++;
|
||||
}
|
||||
if(closedir(dr)) return false;
|
||||
|
||||
*count = i;
|
||||
return true;
|
||||
#endif
|
||||
// Needle was not found.
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string fileNormalizeSlashesNew(std::string str) {
|
||||
return str;
|
||||
bool_t File::writeString(std::string in) {
|
||||
if(!this->isOpen()) {
|
||||
if(!this->open(FILE_MODE_WRITE)) return false;
|
||||
}
|
||||
assertTrue(this->mode == FILE_MODE_WRITE);
|
||||
return this->writeRaw((char *)in.c_str(), in.size()) && this->length == in.size();
|
||||
}
|
||||
|
||||
bool_t File::writeRaw(char *data, size_t len) {
|
||||
if(!this->isOpen()) {
|
||||
if(!this->open(FILE_MODE_WRITE)) return false;
|
||||
}
|
||||
assertTrue(this->mode == FILE_MODE_WRITE);
|
||||
this->length = fwrite(data, sizeof(char_t), len, this->file);
|
||||
return true;
|
||||
}
|
||||
|
||||
void File::setPosition(size_t n) {
|
||||
fseek(this->file, 0, SEEK_SET);
|
||||
fseek(this->file, n, SEEK_CUR);
|
||||
}
|
||||
|
||||
File::~File() {
|
||||
if(this->file != nullptr) this->close();
|
||||
}
|
@ -1,17 +1,11 @@
|
||||
/**
|
||||
* Copyright (c) 2021 Dominic Masters
|
||||
*
|
||||
* This software is released under the MIT License.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnsharedlibs.hpp"
|
||||
|
||||
#define FILE_CHILD_TYPE_DIR 0x00
|
||||
#define FILE_CHILD_TYPE_FILE 0x01
|
||||
#define FILE_CHILD_NAME_MAX 512
|
||||
#define FILE_CHILD_COUNT_MAX 64
|
||||
#include "assert/assert.hpp"
|
||||
#include "util/mathutils.hpp"
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#include <direct.h>
|
||||
@ -26,51 +20,121 @@
|
||||
#define FILE_PATH_SEP '/'
|
||||
#define fileMkdir(path, perms) mkdir(path, perms)
|
||||
#endif
|
||||
#include <errno.h>
|
||||
|
||||
void fileNormalizeSlashes(char *string);
|
||||
#define FILE_BUFFER_SIZE 512
|
||||
|
||||
void fileMkdirp(char *path);
|
||||
namespace Dawn {
|
||||
enum FileMode {
|
||||
FILE_MODE_READ,
|
||||
FILE_MODE_WRITE
|
||||
};
|
||||
|
||||
size_t assetReadString(FILE *file, char *buffer);
|
||||
class File {
|
||||
private:
|
||||
enum FileMode mode;
|
||||
|
||||
void fileGetDirectory(char *file, char* buffer);
|
||||
public:
|
||||
static std::string normalizeSlashes(std::string str);
|
||||
static void mkdirp(std::string path);
|
||||
|
||||
bool fileListChildren(
|
||||
char *directory,
|
||||
char *buffer,
|
||||
int32_t *count,
|
||||
uint8_t *types,
|
||||
char **children
|
||||
);
|
||||
std::string filename;
|
||||
size_t length;
|
||||
FILE *file = nullptr;
|
||||
|
||||
/**
|
||||
* Reads ahead to the first instance of the given character you provide.
|
||||
*
|
||||
* @param bufferIn Buffer to scan.
|
||||
* @param start Start position within the buffer to scan from (inclusive).
|
||||
* @param bufferOut Where to write the temporary data that was read ahead.
|
||||
* @param needles Array of characters to scan for.
|
||||
* @param needleCount How many elements are within the needles array.
|
||||
* @return The count of characters skipped.
|
||||
*/
|
||||
int32_t readAhead(
|
||||
char *bufferIn, int32_t start,
|
||||
char *bufferOut,
|
||||
char *needles, int32_t needleCount
|
||||
);
|
||||
/**
|
||||
* Constructs a new File interface class.
|
||||
*
|
||||
* @param filename Filename that you want to interface with.
|
||||
*/
|
||||
File(std::string filename);
|
||||
|
||||
/**
|
||||
* Skips any characters found in the needles.
|
||||
*
|
||||
* @param bufferIn Buffer of chars to read.
|
||||
* @param start Start of the buffer.
|
||||
* @param needles Needles you are trying to skip.
|
||||
* @param needleCount Count of needles in the needles array.
|
||||
* @return The count of chars to skip ahead.
|
||||
*/
|
||||
int32_t skipAhead(
|
||||
char *bufferIn, int32_t start,
|
||||
char *needles, int32_t needleCount
|
||||
);
|
||||
/**
|
||||
* Opens a connection to the file.
|
||||
*
|
||||
* @param mode File mode to use for this interface.
|
||||
* @return True if success, otherwise for failure.
|
||||
*/
|
||||
bool_t open(enum FileMode mode);
|
||||
|
||||
static std::string fileNormalizeSlashesNew(std::string str);
|
||||
/**
|
||||
* Returns whether or not the file connection is currently opened.
|
||||
*
|
||||
* @return True if the connection is open, otherwise if it's not.
|
||||
*/
|
||||
bool_t isOpen();
|
||||
|
||||
/**
|
||||
* Returns whether or not the file exists. Will open the connection if it
|
||||
* does exist.
|
||||
*
|
||||
* @return True if exists, otherwsie if it doesn't.
|
||||
*/
|
||||
bool_t exists();
|
||||
|
||||
/**
|
||||
* Closes the currently open interface to the file. Done automatically
|
||||
* when this object is disposed.
|
||||
*/
|
||||
void close();
|
||||
|
||||
/**
|
||||
* Makes all directories above this file's filename.
|
||||
*
|
||||
* @return True if successful, otherwise false.
|
||||
*/
|
||||
bool_t mkdirp();
|
||||
|
||||
/**
|
||||
* Reads the entire contents of a file to the given output string buffer.
|
||||
* This is a bit dangerous since the length of the file isn't checked
|
||||
* against the memory to be consumed.
|
||||
*
|
||||
* @param out Pointer to a string where the output data will be stored.
|
||||
* @return True if the read was successful, otherwise false.
|
||||
*/
|
||||
bool_t readString(std::string *out);
|
||||
|
||||
/**
|
||||
* Reads ahead from the current position to a specific needle (character).
|
||||
*
|
||||
* @param buffer Buffer to output read chars to.
|
||||
* @param max Max length of the buffer / amount of chars to read ahead.
|
||||
* @param needle The character (needle) to look for.
|
||||
* @return Amount of chars read, or <= 0 on error.
|
||||
*/
|
||||
size_t readAhead(
|
||||
char *buffer,
|
||||
size_t max,
|
||||
char needle
|
||||
);
|
||||
|
||||
/**
|
||||
* Writes the entire contents of a string to a file.
|
||||
*
|
||||
* @param in String to write to this file.
|
||||
* @return True if written successfully, otherwise false.
|
||||
*/
|
||||
bool_t writeString(std::string in);
|
||||
|
||||
/**
|
||||
* Write raw bytes to the file.
|
||||
*
|
||||
* @param data Data to write.
|
||||
* @return True if written successfully, otherwise false.
|
||||
*/
|
||||
bool_t writeRaw(char *data, size_t );
|
||||
|
||||
/**
|
||||
* Set the position of the cursor of the file reader.
|
||||
*
|
||||
* @param pos Position to set.
|
||||
*/
|
||||
void setPosition(size_t pos);
|
||||
|
||||
/**
|
||||
* Destruct the File manager.
|
||||
*/
|
||||
~File();
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user