jak-project/common/util/FileUtil.cpp

48 lines
1.5 KiB
C++
Raw Normal View History

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-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-09 02:41:45 -04:00
#endif
2020-09-09 00:54:16 -04:00
std::string FileUtil::GetProjectPath() {
2020-09-10 06:07:23 -04:00
#ifdef _WIN32
char buffer[FILENAME_MAX];
GetModuleFileNameA(NULL, buffer, FILENAME_MAX);
2020-09-10 17:24:03 -04:00
std::string::size_type pos = std::string(buffer).rfind(
"\\jak-project\\"); // Strip file path down to \jak-project\ directory
return std::string(buffer).substr(
0, pos + 12); // + 12 to include "\jak-project" in the returned filepath
2020-09-10 18:30:45 -04:00
#else // do Linux stuff
2020-09-10 06:07:23 -04:00
char buffer[FILENAME_MAX];
readlink("/proc/self/exe", buffer,
FILENAME_MAX); // /proc/self acts like a "virtual folder" containing information about
// the current process
2020-09-10 17:24:03 -04:00
std::string::size_type pos = std::string(buffer).find_last_of(
"/jak-project/"); // Strip file path down to /jak-project/ directory
return std::string(buffer).substr(
0, pos + 12); // + 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-09 18:35:29 -04:00
std::string FileUtil::get_file_path(const std::vector<std::string>& input) {
std::string currentPath = FileUtil::GetProjectPath();
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-09 18:35:29 -04:00
for (int i = 0; i < input.size(); i++) {
filePath = filePath + dirSeparator + input[i];
}
2020-09-09 00:54:16 -04:00
2020-09-09 02:41:45 -04:00
return filePath;
}