Search for colors.txt in multiple locations, fixes #27

Locations (in order):
* <world path>/colors.txt
* $HOME/.minetest/colors.txt (Linux only)
* <share dir>/colors.txt (Linux only for now, defaults to /usr/local/share/minetest)
* current directory (<< this is the old behavior)
This commit is contained in:
sfan5
2016-08-09 17:31:56 +02:00
parent 73dab34d7c
commit 6f1b8284f4
3 changed files with 77 additions and 15 deletions

View File

@ -1,10 +1,12 @@
#include <cstdlib>
#include <getopt.h>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <sstream>
#include <stdexcept>
#include "cmake_config.h"
#include "TileGenerator.h"
void usage()
@ -31,9 +33,30 @@ void usage()
std::cout << usage_text;
}
std::string search_colors()
bool file_exists(const std::string &path)
{
// TBD
std::ifstream ifs(path.c_str());
return ifs.is_open();
}
std::string search_colors(const std::string &worldpath)
{
if(file_exists(worldpath + "/colors.txt"))
return worldpath + "/colors.txt";
#ifndef _WIN32
char *home = std::getenv("HOME");
if(home) {
std::string check = ((std::string) home) + "/.minetest/colors.txt";
if(file_exists(check))
return check;
}
#endif
if(!(SHAREDIR[0] == '.' || SHAREDIR[0] == '\0') && file_exists(SHAREDIR "/colors.txt"))
return SHAREDIR "/colors.txt";
std::cerr << "Warning: Falling back to using colors.txt from current directory." << std::endl;
return "colors.txt";
}
@ -163,7 +186,8 @@ int main(int argc, char *argv[])
}
}
if(colors == "")
colors = search_colors();
colors = search_colors(input);
std::cerr << "is at " << colors << std::endl;
try {
generator.parseColorsFile(colors);
generator.generate(input, output);