Refactored audiogen tool
This commit is contained in:
12
src/dawntools/util/CMakeLists.txt
Normal file
12
src/dawntools/util/CMakeLists.txt
Normal file
@@ -0,0 +1,12 @@
|
||||
# Copyright (c) 2023 Dominic Msters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
set(D ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
list(APPEND
|
||||
DAWN_TOOL_SOURCES
|
||||
${D}/DawnTool.cpp
|
||||
${D}/File.cpp
|
||||
)
|
@@ -17,9 +17,53 @@
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
std::vector<std::string> DawnTool::getRequiredFlags() {
|
||||
return std::vector<std::string>();
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> DawnTool::getOptionalFlags() {
|
||||
return std::map<std::string, std::string>();
|
||||
}
|
||||
|
||||
int32_t DawnTool::exec(const int32_t argc, const char *argv[]) {
|
||||
// Set up flags
|
||||
flags = this->getOptionalFlags();
|
||||
|
||||
// Parse args
|
||||
for(int32_t i = 0; i < argc; i++) {
|
||||
this->args.push_back(std::string(argv[i]));
|
||||
std::string a(argv[i]);
|
||||
this->args.push_back(a);
|
||||
|
||||
// First arg is the path, we ignore it.
|
||||
if(i == 0) continue;
|
||||
|
||||
// Is this a flag-like arg, as in has "--[name]=[value]"
|
||||
if(a.size() < 5) continue;
|
||||
if(a[0] != '-' || a[1] != '-') continue;
|
||||
|
||||
// Remove --
|
||||
auto flag = a.erase(0, 2);
|
||||
|
||||
// Ensure = is present
|
||||
auto equalPos = flag.find('=');
|
||||
if(equalPos == std::string::npos) continue;
|
||||
|
||||
// Get prefix and val and store
|
||||
auto prefix = flag.substr(0, equalPos);
|
||||
auto val = flag.substr(equalPos + 1);
|
||||
flags[prefix] = val;
|
||||
}
|
||||
|
||||
// Now validate flags
|
||||
auto required = this->getRequiredFlags();
|
||||
auto itReq = required.begin();
|
||||
while(itReq != required.end()) {
|
||||
auto match = flags.find(*itReq);
|
||||
if(match == flags.end()) {
|
||||
std::cout << "Missing required flag \"" + *itReq + "\"!" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
++itReq;
|
||||
}
|
||||
|
||||
return this->start();
|
||||
|
@@ -18,6 +18,10 @@ namespace Dawn {
|
||||
class DawnTool {
|
||||
protected:
|
||||
std::vector<std::string> args;
|
||||
std::map<std::string, std::string> flags;
|
||||
|
||||
virtual std::vector<std::string> getRequiredFlags();
|
||||
virtual std::map<std::string, std::string> getOptionalFlags();
|
||||
|
||||
public:
|
||||
int32_t exec(const int32_t argc, const char *argv[]);
|
||||
|
@@ -7,8 +7,50 @@
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
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 File::mkdirp(std::string path) {
|
||||
std::string buffer;
|
||||
char c;
|
||||
size_t i = 0;
|
||||
bool inFile;
|
||||
bool hasMore;
|
||||
|
||||
inFile = false;
|
||||
hasMore = false;
|
||||
while(c = path[i]) {
|
||||
if((c == '\\' || c == '/') && i > 0) {
|
||||
fileMkdir(buffer.c_str(), 0755);
|
||||
inFile = false;
|
||||
hasMore = false;
|
||||
buffer += FILE_PATH_SEP;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(c == '.') inFile = true;
|
||||
hasMore = true;
|
||||
buffer += c;
|
||||
i++;
|
||||
}
|
||||
|
||||
if(!inFile && hasMore) {
|
||||
fileMkdir(buffer.c_str(), 0755);
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
File::File(std::string filename) {
|
||||
this->filename = fileNormalizeSlashesNew(filename);
|
||||
this->filename = File::normalizeSlashes(filename);
|
||||
}
|
||||
|
||||
bool_t File::open(enum FileMode mode) {
|
||||
@@ -42,6 +84,14 @@ bool_t File::isOpen() {
|
||||
return this->file != nullptr;
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -49,7 +99,7 @@ void File::close() {
|
||||
}
|
||||
|
||||
bool_t File::mkdirp() {
|
||||
fileMkdirp((char*)this->filename.c_str());
|
||||
File::mkdirp(this->filename);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -81,10 +131,15 @@ bool_t File::writeString(std::string in) {
|
||||
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();
|
||||
}
|
||||
|
||||
const char_t *strOut = in.c_str();
|
||||
// TODO: Validate write length.
|
||||
this->length = fwrite(strOut, sizeof(char_t), in.size(), this->file);
|
||||
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;
|
||||
}
|
||||
|
||||
|
@@ -5,9 +5,22 @@
|
||||
|
||||
#pragma once
|
||||
#include "assert/assert.hpp"
|
||||
#include "util/file.hpp"
|
||||
#include "util/mathutils.hpp"
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#include <direct.h>
|
||||
#include <windows.h>
|
||||
#define getcwd _getcwd
|
||||
#define FILE_PATH_SEP '\\'
|
||||
#define fileMkdir(path, perms) _mkdir(path)
|
||||
#elif defined(__GNUC__)
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
#define FILE_PATH_SEP '/'
|
||||
#define fileMkdir(path, perms) mkdir(path, perms)
|
||||
#endif
|
||||
|
||||
#define FILE_BUFFER_SIZE 512
|
||||
|
||||
namespace Dawn {
|
||||
@@ -23,6 +36,9 @@ namespace Dawn {
|
||||
size_t length;
|
||||
|
||||
public:
|
||||
static std::string normalizeSlashes(std::string str);
|
||||
static void mkdirp(std::string path);
|
||||
|
||||
std::string filename;
|
||||
|
||||
/**
|
||||
@@ -47,6 +63,14 @@ namespace Dawn {
|
||||
*/
|
||||
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.
|
||||
@@ -78,6 +102,14 @@ namespace Dawn {
|
||||
*/
|
||||
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 );
|
||||
|
||||
~File();
|
||||
};
|
||||
}
|
@@ -74,4 +74,4 @@ int32_t skipAhead(
|
||||
char *needles, int32_t needleCount
|
||||
);
|
||||
|
||||
std::string fileNormalizeSlashesNew(std::string str);
|
||||
static std::string fileNormalizeSlashesNew(std::string str);
|
Reference in New Issue
Block a user