2020-09-09 00:54:16 -04:00
|
|
|
#include "FileUtil.h"
|
|
|
|
#include <iostream>
|
2020-09-09 18:35:29 -04:00
|
|
|
#include <stdio.h> /* defines FILENAME_MAX */
|
2020-09-10 20:03:31 -04:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <cassert>
|
2020-09-09 00:54:16 -04:00
|
|
|
|
2020-09-09 02:41:45 -04:00
|
|
|
#ifdef _WIN32
|
2020-09-10 06:07:23 -04:00
|
|
|
#include <Windows.h>
|
2020-09-09 02:41:45 -04:00
|
|
|
#else
|
2020-09-09 18:35:29 -04:00
|
|
|
#include <unistd.h>
|
2020-09-12 20:41:12 -04:00
|
|
|
#include <cstring>
|
2020-09-09 02:41:45 -04:00
|
|
|
#endif
|
2020-09-09 00:54:16 -04:00
|
|
|
|
2020-09-10 20:03:31 -04:00
|
|
|
std::string file_util::get_project_path() {
|
2020-09-10 06:07:23 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
char buffer[FILENAME_MAX];
|
|
|
|
GetModuleFileNameA(NULL, buffer, FILENAME_MAX);
|
2020-09-12 20:41:12 -04:00
|
|
|
std::string::size_type pos =
|
|
|
|
std::string(buffer).rfind("jak-project"); // Strip file path down to \jak-project\ directory
|
2020-09-10 17:24:03 -04:00
|
|
|
return std::string(buffer).substr(
|
2020-09-12 20:41:12 -04:00
|
|
|
0, pos + 11); // + 12 to include "\jak-project" in the returned filepath
|
2020-09-10 20:03:31 -04:00
|
|
|
#else
|
|
|
|
// do Linux stuff
|
2020-09-17 21:47:52 -04:00
|
|
|
char buffer[FILENAME_MAX + 1];
|
|
|
|
auto len = readlink("/proc/self/exe", buffer,
|
|
|
|
FILENAME_MAX); // /proc/self acts like a "virtual folder" containing
|
|
|
|
// information about the current process
|
|
|
|
buffer[len] = '\0';
|
2020-09-12 20:41:12 -04:00
|
|
|
std::string::size_type pos =
|
|
|
|
std::string(buffer).rfind("jak-project"); // Strip file path down to /jak-project/ directory
|
2020-09-10 17:24:03 -04:00
|
|
|
return std::string(buffer).substr(
|
2020-09-12 20:41:12 -04:00
|
|
|
0, pos + 11); // + 12 to include "/jak-project" in the returned filepath
|
2020-09-10 06:07:23 -04:00
|
|
|
#endif
|
2020-09-09 02:41:45 -04:00
|
|
|
}
|
2020-09-09 00:54:16 -04:00
|
|
|
|
2020-09-10 20:03:31 -04:00
|
|
|
std::string file_util::get_file_path(const std::vector<std::string>& input) {
|
|
|
|
std::string currentPath = file_util::get_project_path();
|
2020-09-09 02:41:45 -04:00
|
|
|
char dirSeparator;
|
|
|
|
|
2020-09-09 18:35:29 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
dirSeparator = '\\';
|
|
|
|
#else
|
|
|
|
dirSeparator = '/';
|
|
|
|
#endif
|
2020-09-09 02:41:45 -04:00
|
|
|
|
|
|
|
std::string filePath = currentPath;
|
2020-09-10 20:03:31 -04:00
|
|
|
for (int i = 0; i < int(input.size()); i++) {
|
2020-09-09 18:35:29 -04:00
|
|
|
filePath = filePath + dirSeparator + input[i];
|
|
|
|
}
|
2020-09-09 00:54:16 -04:00
|
|
|
|
2020-09-09 02:41:45 -04:00
|
|
|
return filePath;
|
|
|
|
}
|
2020-09-10 20:03:31 -04:00
|
|
|
|
|
|
|
void file_util::write_binary_file(const std::string& name, void* data, size_t size) {
|
|
|
|
FILE* fp = fopen(name.c_str(), "wb");
|
|
|
|
if (!fp) {
|
|
|
|
throw std::runtime_error("couldn't open file " + name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fwrite(data, size, 1, fp) != 1) {
|
|
|
|
throw std::runtime_error("couldn't write file " + name);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_util::write_text_file(const std::string& file_name, const std::string& text) {
|
|
|
|
FILE* fp = fopen(file_name.c_str(), "w");
|
|
|
|
if (!fp) {
|
|
|
|
printf("Failed to fopen %s\n", file_name.c_str());
|
|
|
|
throw std::runtime_error("Failed to open file");
|
|
|
|
}
|
|
|
|
fprintf(fp, "%s\n", text.c_str());
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint8_t> file_util::read_binary_file(const std::string& filename) {
|
|
|
|
auto fp = fopen(filename.c_str(), "rb");
|
|
|
|
if (!fp)
|
2020-09-12 20:41:12 -04:00
|
|
|
throw std::runtime_error("File " + filename +
|
|
|
|
" cannot be opened: " + std::string(strerror(errno)));
|
2020-09-10 20:03:31 -04:00
|
|
|
fseek(fp, 0, SEEK_END);
|
|
|
|
auto len = ftell(fp);
|
|
|
|
rewind(fp);
|
|
|
|
|
|
|
|
std::vector<uint8_t> data;
|
|
|
|
data.resize(len);
|
|
|
|
|
|
|
|
if (fread(data.data(), len, 1, fp) != 1) {
|
|
|
|
throw std::runtime_error("File " + filename + " cannot be read");
|
|
|
|
}
|
2020-09-12 20:41:12 -04:00
|
|
|
fclose(fp);
|
2020-09-10 20:03:31 -04:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string file_util::read_text_file(const std::string& path) {
|
|
|
|
std::ifstream file(path);
|
|
|
|
if (!file.good()) {
|
|
|
|
throw std::runtime_error("couldn't open " + path);
|
|
|
|
}
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << file.rdbuf();
|
|
|
|
return ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool file_util::is_printable_char(char c) {
|
|
|
|
return c >= ' ' && c <= '~';
|
|
|
|
}
|