2 #include "file_util.hpp"
3 #include "file_data_util.hpp"
7 // Fill in values for a local data directory and a system data directory
8 // The values are stored under the keys local-data-dir and data-dir.
9 // If the is no local-data-dir value, use the directory from master-path.
10 // If there is no directory in master-path, use the current working dir.
11 // FIXME: The case when there is no "/" in the master-path should not
12 // happen since it is an internal option. Unofficially, it can still
13 // be set by the user. This needs to eventually be fixed.
14 void fill_data_dir(const Config * config, String & dir1, String & dir2) {
15 if (config->have("local-data-dir")) {
16 dir1 = config->retrieve("local-data-dir");
17 if (!dir1.empty() && dir1[dir1.size()-1] != '/') dir1 += '/';
19 dir1 = config->retrieve("master-path");
20 size_t pos = dir1.rfind('/');
21 if (pos != String::npos)
26 dir2 = config->retrieve("data-dir");
27 if (dir2[dir2.size()-1] != '/') dir2 += '/';
30 const String & find_file(String & file,
31 const String & dir1, const String & dir2,
32 const String & name, const char * extension)
34 file = dir1 + name + extension;
35 if (file_exists(file)) return dir1;
36 file = dir2 + name + extension;
40 bool find_file(String & file,
41 const String & dir1, const String & dir2,
43 ParmString preext, ParmString ext)
45 bool try_name_only = false;
46 if (name.size() > ext.size()
47 && memcmp(name.c_str() + name.size() - ext.size(),
48 ext, ext.size()) == 0) try_name_only = true;
50 String n = name; n += preext; n += ext;
52 if (file_exists(file)) return true;
54 if (file_exists(file)) return true;
58 if (file_exists(file)) return true;
60 if (file_exists(file)) return true;
64 if (file_exists(file)) return true;
66 if (file_exists(file)) return true;
68 if (try_name_only) {file = dir1 + name;}
69 else {file = dir1 + name; file += preext; file += ext;}