Plugins agregados
This commit is contained in:
53
libultraship/include/ship/utils/filesystemtools/Directory.h
Normal file
53
libultraship/include/ship/utils/filesystemtools/Directory.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
#if __has_include(<filesystem>)
|
||||
#include <filesystem>
|
||||
namespace fs = std::filesystem;
|
||||
#else
|
||||
#include <experimental/filesystem>
|
||||
namespace fs = std::experimental::filesystem;
|
||||
#endif
|
||||
|
||||
#undef GetCurrentDirectory
|
||||
#undef CreateDirectory
|
||||
|
||||
class Directory {
|
||||
public:
|
||||
#ifndef PATH_HACK
|
||||
static std::string GetCurrentDirectory() {
|
||||
return fs::current_path().string();
|
||||
}
|
||||
#endif
|
||||
|
||||
static bool Exists(const fs::path& path) {
|
||||
return fs::exists(path);
|
||||
}
|
||||
|
||||
// Stupid hack because of Windows.h
|
||||
static void MakeDirectory(const std::string& path) {
|
||||
CreateDirectory(path);
|
||||
}
|
||||
|
||||
static void CreateDirectory(const std::string& path) {
|
||||
try {
|
||||
fs::create_directories(path);
|
||||
} catch (...) {}
|
||||
}
|
||||
|
||||
static std::vector<std::string> ListFiles(const std::string& dir) {
|
||||
std::vector<std::string> lst;
|
||||
|
||||
if (Exists(dir)) {
|
||||
for (auto& p : fs::recursive_directory_iterator(dir)) {
|
||||
if (!p.is_directory())
|
||||
lst.push_back(p.path().generic_string());
|
||||
}
|
||||
}
|
||||
|
||||
return lst;
|
||||
}
|
||||
};
|
||||
80
libultraship/include/ship/utils/filesystemtools/DiskFile.h
Normal file
80
libultraship/include/ship/utils/filesystemtools/DiskFile.h
Normal file
@@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "Path.h"
|
||||
#include "Directory.h"
|
||||
|
||||
class DiskFile {
|
||||
public:
|
||||
static bool Exists(const fs::path& filePath) {
|
||||
std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
||||
return file.good();
|
||||
}
|
||||
|
||||
static std::vector<uint8_t> ReadAllBytes(const fs::path& filePath) {
|
||||
std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
||||
|
||||
if (!file)
|
||||
return std::vector<uint8_t>();
|
||||
|
||||
int32_t fileSize = (int32_t)file.tellg();
|
||||
file.seekg(0);
|
||||
char* data = new char[fileSize];
|
||||
file.read(data, fileSize);
|
||||
std::vector<uint8_t> result = std::vector<uint8_t>(data, data + fileSize);
|
||||
delete[] data;
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
static std::string ReadAllText(const fs::path& filePath) {
|
||||
std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
||||
int32_t fileSize = (int32_t)file.tellg();
|
||||
file.seekg(0);
|
||||
char* data = new char[fileSize + 1];
|
||||
memset(data, 0, fileSize + 1);
|
||||
file.read(data, fileSize);
|
||||
std::string str = std::string((const char*)data);
|
||||
delete[] data;
|
||||
|
||||
return str;
|
||||
};
|
||||
|
||||
static std::vector<std::string> ReadAllLines(const fs::path& filePath) {
|
||||
std::string text = ReadAllText(filePath);
|
||||
std::vector<std::string> lines = StringHelper::Split(text, "\n");
|
||||
|
||||
return lines;
|
||||
};
|
||||
|
||||
static void WriteAllBytes(const fs::path& filePath, const std::vector<uint8_t>& data) {
|
||||
std::ofstream file(filePath, std::ios::binary);
|
||||
file.write((char*)data.data(), data.size());
|
||||
};
|
||||
|
||||
static void WriteAllBytes(const std::string& filePath, const std::vector<char>& data) {
|
||||
if (!Directory::Exists(Path::GetDirectoryName(filePath))) {
|
||||
Directory::MakeDirectory(Path::GetDirectoryName(filePath).string());
|
||||
}
|
||||
|
||||
std::ofstream file(filePath, std::ios::binary);
|
||||
file.write((char*)data.data(), data.size());
|
||||
};
|
||||
|
||||
static void WriteAllBytes(const std::string& filePath, const char* data, int dataSize) {
|
||||
std::ofstream file(filePath, std::ios::binary);
|
||||
file.write((char*)data, dataSize);
|
||||
};
|
||||
|
||||
static void WriteAllText(const fs::path& filePath, const std::string& text) {
|
||||
if (!Directory::Exists(Path::GetDirectoryName(filePath))) {
|
||||
Directory::MakeDirectory(Path::GetDirectoryName(filePath).string());
|
||||
}
|
||||
std::ofstream file(filePath, std::ios::out);
|
||||
file.write(text.c_str(), text.size());
|
||||
}
|
||||
};
|
||||
98
libultraship/include/ship/utils/filesystemtools/FileHelper.h
Normal file
98
libultraship/include/ship/utils/filesystemtools/FileHelper.h
Normal file
@@ -0,0 +1,98 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "PathHelper.h"
|
||||
#include "Directory.h"
|
||||
|
||||
namespace Ship {
|
||||
class FileHelper {
|
||||
public:
|
||||
static bool Exists(const fs::path& filePath) {
|
||||
std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
||||
return file.good();
|
||||
}
|
||||
|
||||
static std::vector<uint8_t> ReadAllBytes(const fs::path& filePath) {
|
||||
std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
||||
|
||||
if (!file) {
|
||||
return std::vector<uint8_t>();
|
||||
}
|
||||
|
||||
int32_t fileSize = (int32_t)file.tellg();
|
||||
file.seekg(0);
|
||||
char* data = nullptr;
|
||||
std::vector<uint8_t> result;
|
||||
|
||||
try {
|
||||
data = new char[fileSize];
|
||||
file.read(data, fileSize);
|
||||
result = std::vector<uint8_t>(data, data + fileSize);
|
||||
} catch (const std::exception& e) {
|
||||
delete[] data;
|
||||
throw e;
|
||||
}
|
||||
|
||||
delete[] data;
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
static std::string ReadAllText(const fs::path& filePath) {
|
||||
std::ifstream file(filePath, std::ios::in | std::ios::binary | std::ios::ate);
|
||||
int32_t fileSize = (int32_t)file.tellg();
|
||||
file.seekg(0);
|
||||
char* data = nullptr;
|
||||
std::string str;
|
||||
|
||||
try {
|
||||
data = new char[fileSize + 1];
|
||||
memset(data, 0, fileSize + 1);
|
||||
file.read(data, fileSize);
|
||||
str = std::string((const char*)data);
|
||||
} catch (const std::exception& e) {
|
||||
delete[] data;
|
||||
throw e;
|
||||
}
|
||||
|
||||
delete[] data;
|
||||
|
||||
return str;
|
||||
};
|
||||
|
||||
static std::vector<std::string> ReadAllLines(const fs::path& filePath) {
|
||||
std::string text = ReadAllText(filePath);
|
||||
std::vector<std::string> lines = StringHelper::Split(text, "\n");
|
||||
|
||||
return lines;
|
||||
};
|
||||
|
||||
static void WriteAllBytes(const fs::path& filePath, const std::vector<uint8_t>& data) {
|
||||
std::ofstream file(filePath, std::ios::binary);
|
||||
file.write((char*)data.data(), data.size());
|
||||
};
|
||||
|
||||
static void WriteAllBytes(const std::string& filePath, const std::vector<char>& data) {
|
||||
if (!Directory::Exists(PathHelper::GetDirectoryName(filePath))) {
|
||||
Directory::MakeDirectory(PathHelper::GetDirectoryName(filePath).string());
|
||||
}
|
||||
|
||||
std::ofstream file(filePath, std::ios::binary);
|
||||
file.write((char*)data.data(), data.size());
|
||||
};
|
||||
|
||||
static void WriteAllBytes(const std::string& filePath, const char* data, int dataSize) {
|
||||
std::ofstream file(filePath, std::ios::binary);
|
||||
file.write((char*)data, dataSize);
|
||||
};
|
||||
|
||||
static void WriteAllText(const fs::path& filePath, const std::string& text) {
|
||||
std::ofstream file(filePath, std::ios::out);
|
||||
file.write(text.c_str(), text.size());
|
||||
}
|
||||
};
|
||||
} // namespace Ship
|
||||
46
libultraship/include/ship/utils/filesystemtools/Path.h
Normal file
46
libultraship/include/ship/utils/filesystemtools/Path.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "../StringHelper.h"
|
||||
|
||||
#if __has_include(<filesystem>)
|
||||
#include <filesystem>
|
||||
namespace fs = std::filesystem;
|
||||
#else
|
||||
#include <experimental/filesystem>
|
||||
namespace fs = std::experimental::filesystem;
|
||||
#endif
|
||||
|
||||
class Path {
|
||||
public:
|
||||
static std::string GetFileName(const fs::path& input) {
|
||||
// https://en.cppreference.com/w/cpp/filesystem/path/filename
|
||||
return input.filename().string();
|
||||
};
|
||||
|
||||
static std::string GetFileNameWithoutExtension(const fs::path& input) {
|
||||
// https://en.cppreference.com/w/cpp/filesystem/path/stem
|
||||
return input.stem().string();
|
||||
};
|
||||
|
||||
static std::string GetFileNameExtension(const std::string& input) {
|
||||
return input.substr(input.find_last_of("."), input.length());
|
||||
};
|
||||
|
||||
static fs::path GetPath(const std::string& input) {
|
||||
std::vector<std::string> split = StringHelper::Split(input, "/");
|
||||
fs::path output;
|
||||
|
||||
for (std::string str : split) {
|
||||
if (str.find_last_of(".") == std::string::npos)
|
||||
output /= str;
|
||||
}
|
||||
|
||||
return output;
|
||||
};
|
||||
|
||||
static fs::path GetDirectoryName(const fs::path& path) {
|
||||
return path.parent_path();
|
||||
};
|
||||
};
|
||||
49
libultraship/include/ship/utils/filesystemtools/PathHelper.h
Normal file
49
libultraship/include/ship/utils/filesystemtools/PathHelper.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "../StringHelper.h"
|
||||
|
||||
#if __has_include(<filesystem>)
|
||||
#include <filesystem>
|
||||
namespace fs = std::filesystem;
|
||||
#else
|
||||
#include <experimental/filesystem>
|
||||
namespace fs = std::experimental::filesystem;
|
||||
#endif
|
||||
|
||||
namespace Ship {
|
||||
class PathHelper {
|
||||
public:
|
||||
static std::string GetFileName(const fs::path& input) {
|
||||
// https://en.cppreference.com/w/cpp/filesystem/path/filename
|
||||
return input.filename().string();
|
||||
};
|
||||
|
||||
static std::string GetFileNameWithoutExtension(const fs::path& input) {
|
||||
// https://en.cppreference.com/w/cpp/filesystem/path/stem
|
||||
return input.stem().string();
|
||||
};
|
||||
|
||||
static std::string GetFileNameExtension(const std::string& input) {
|
||||
return input.substr(input.find_last_of("."), input.length());
|
||||
};
|
||||
|
||||
static fs::path GetPath(const std::string& input) {
|
||||
std::vector<std::string> split = StringHelper::Split(input, "/");
|
||||
fs::path output;
|
||||
|
||||
for (std::string str : split) {
|
||||
if (str.find_last_of(".") == std::string::npos) {
|
||||
output /= str;
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
};
|
||||
|
||||
static fs::path GetDirectoryName(const fs::path& path) {
|
||||
return path.parent_path();
|
||||
};
|
||||
};
|
||||
} // namespace Ship
|
||||
Reference in New Issue
Block a user