From c8df9af2cdf0cc4d5b845d3200be3ffdb4727e87 Mon Sep 17 00:00:00 2001 From: dshaligram Date: Fri, 4 May 2007 07:11:20 +0000 Subject: Allow Mac users to double-click to launch Crawl: - Use argv[0] to figure out where Crawl lives and where to find data files. The Mac Finder sets the working directory to / and the full path to the executable is available in argv[0] when the user double-clicks. - Converted some of the old char*s to std::string. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@1407 c06c8d41-db1a-0410-9941-cceddc491573 --- crawl-ref/source/files.cc | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'crawl-ref/source/files.cc') diff --git a/crawl-ref/source/files.cc b/crawl-ref/source/files.cc index 26f716d6ac..b9c14f74b3 100644 --- a/crawl-ref/source/files.cc +++ b/crawl-ref/source/files.cc @@ -213,6 +213,19 @@ std::vector get_dir_files(const std::string &dirname) return (files); } +std::string get_parent_directory(const std::string &filename) +{ + std::string::size_type pos = filename.rfind(FILE_SEPARATOR); + if (pos != std::string::npos) + return filename.substr(0, pos + 1); +#ifdef ALT_FILE_SEPARATOR + pos = filename.rfind(ALT_FILE_SEPARATOR); + if (pos != std::string::npos) + return filename.substr(0, pos + 1); +#endif + return (""); +} + static bool file_exists(const std::string &name) { FILE *f = fopen(name.c_str(), "r"); @@ -272,9 +285,14 @@ static bool create_dirs(const std::string &dir) return (true); } -std::string datafile_path(const std::string &basename, bool croak_on_fail) +std::string datafile_path(const std::string &basename, + bool croak_on_fail, + bool test_base_path) { - std::string cdir = SysEnv.crawl_dir? SysEnv.crawl_dir : ""; + if (test_base_path && file_exists(basename)) + return (basename); + + std::string cdir = !SysEnv.crawl_dir.empty()? SysEnv.crawl_dir : ""; const std::string rawbases[] = { #ifdef DATA_DIR_PATH @@ -286,7 +304,6 @@ std::string datafile_path(const std::string &basename, bool croak_on_fail) const std::string prefixes[] = { std::string("dat") + FILE_SEPARATOR, - std::string("data") + FILE_SEPARATOR, std::string("docs") + FILE_SEPARATOR, std::string("..")+FILE_SEPARATOR+std::string("docs")+FILE_SEPARATOR, std::string("..") + FILE_SEPARATOR, @@ -307,6 +324,8 @@ std::string datafile_path(const std::string &basename, bool croak_on_fail) } #ifndef DATA_DIR_PATH + if (!SysEnv.crawl_base.empty()) + bases.push_back(SysEnv.crawl_base); bases.push_back(""); #endif -- cgit v1.2.3-54-g00ecf