Created
December 5, 2020 10:52
-
-
Save sp1ritCS/9c81ae26a283ec5102c59a77c7fb5c3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <filesystem> | |
#include <string> | |
#include <queue> | |
#include <stdio.h> | |
#include <sstream> | |
using namespace std; | |
namespace fs = filesystem; | |
string queryRootPath(string custom_path) { | |
queue<string> paths; | |
paths.push(custom_path); | |
// checks if XDG_DATA_HOME exists. If it does, it pushes it to potential paths. | |
char* userdata = getenv("XDG_DATA_HOME"); | |
if (userdata != NULL && userdata != "") { | |
paths.push(string(userdata)); | |
} | |
delete userdata; | |
// checks if XDG_DATA_DIRS is set. If it is, it splits XDG_DATA_DIRS at : and pushes each part of it to potential paths. | |
char* xdg = getenv("XDG_DATA_DIRS"); | |
if (xdg != NULL && xdg != "") { | |
stringstream xdg_paths(xdg); | |
string xdg_path; | |
while (getline(xdg_paths, xdg_path, ':')) { | |
paths.push(xdg_path); | |
} | |
} | |
// checks if HOME env var is unset. if it isn't it pushes ~/.local/share/clatexmath to potential paths. | |
char* home = getenv("HOME"); | |
if (home != NULL && home != "") { | |
char* userdata_fallback; | |
asprintf(&userdata_fallback, "%s/.local/share/clatexmath/", home); | |
paths.push(string(userdata_fallback)); | |
delete userdata_fallback; | |
} | |
paths.push("/usr/share/clatexmath/"); | |
paths.push("/usr/local/share/clatexmath/"); | |
// goes through the list of potential paths. if it finds a path that contains .clatexmath-res_root, it returns it. Otherwise I'll throw an error. | |
while (paths.size() > 0) { | |
fs::path p = paths.front(); | |
p.append(".clatexmath-res_root"); | |
if (fs::exists(p)) { | |
p.remove_filename(); | |
return p.u8string(); | |
} | |
paths.pop(); | |
} | |
throw "cLaTeXMath resource path was not found"; | |
} | |
int main(int argc, char** argv) { | |
string path = queryRootPath("res/"); | |
printf("Found path: %s", path.c_str()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment