openMSX
FileContext.cc
Go to the documentation of this file.
1 #include "FileContext.hh"
2 #include "FileOperations.hh"
3 #include "FileException.hh"
4 #include "StringOp.hh"
5 #include "serialize.hh"
6 #include "serialize_stl.hh"
7 #include "openmsx.hh"
8 #include "stl.hh"
9 #include <algorithm>
10 #include <cassert>
11 
12 using std::string;
13 using std::vector;
14 
15 namespace openmsx {
16 
17 const string USER_DIRS = "{{USER_DIRS}}";
18 const string USER_OPENMSX = "{{USER_OPENMSX}}";
19 const string USER_DATA = "{{USER_DATA}}";
20 const string SYSTEM_DATA = "{{SYSTEM_DATA}}";
21 
22 
23 static string subst(string_ref path, string_ref before, string_ref after)
24 {
25  assert(path.starts_with(before));
26  return after + path.substr(before.size());
27 }
28 
29 static vector<string> getPathsHelper(const vector<string>& input)
30 {
31  vector<string> result;
32  for (auto& s : input) {
34  result.push_back(subst(s, USER_OPENMSX,
36  } else if (StringOp::startsWith(s, USER_DATA)) {
37  result.push_back(subst(s, USER_DATA,
39  } else if (StringOp::startsWith(s, SYSTEM_DATA)) {
40  result.push_back(subst(s, SYSTEM_DATA,
42  } else if (s == USER_DIRS) {
43  // Nothing. Keep USER_DIRS for isUserContext()
44  } else {
45  result.push_back(s);
46  }
47  }
48  return result;
49 }
50 
51 static string resolveHelper(const vector<string>& pathList,
52  string_ref filename)
53 {
54  PRT_DEBUG("Context: " << filename);
55  string filepath = FileOperations::expandCurrentDirFromDrive(filename);
56  filepath = FileOperations::expandTilde(filepath);
57  if (FileOperations::isAbsolutePath(filepath)) {
58  // absolute path, don't resolve
59  return filepath;
60  }
61 
62  for (auto& p : pathList) {
63  string name = FileOperations::join(p, filename);
64  name = FileOperations::expandTilde(name);
65  PRT_DEBUG("Context: try " << name);
66  if (FileOperations::exists(name)) {
67  return name;
68  }
69  }
70  // not found in any path
71  throw FileException(filename + " not found in this context");
72 }
73 
74 const string FileContext::resolve(string_ref filename) const
75 {
76  vector<string> pathList = getPathsHelper(paths);
77  string result = resolveHelper(pathList, filename);
78  assert(FileOperations::expandTilde(result) == result);
79  return result;
80 }
81 
82 const string FileContext::resolveCreate(string_ref filename) const
83 {
84  string result;
85  vector<string> pathList = getPathsHelper(savePaths);
86  try {
87  result = resolveHelper(pathList, filename);
88  } catch (FileException&) {
89  string path = pathList.front();
90  try {
92  } catch (FileException& e) {
93  PRT_DEBUG(e.getMessage());
94  (void)&e; // Prevent warning
95  }
96  result = FileOperations::join(path, filename);
97  }
98  assert(FileOperations::expandTilde(result) == result);
99  return result;
100 }
101 
102 vector<string> FileContext::getPaths() const
103 {
104  return getPathsHelper(paths);
105 }
106 
108 {
109  return contains(paths, USER_DIRS);
110 }
111 
113 
114 static string backSubstSymbols(const string& path)
115 {
116  string systemData = FileOperations::getSystemDataDir();
117  if (StringOp::startsWith(path, systemData)) {
118  return subst(path, systemData, SYSTEM_DATA);
119  }
120  string userData = FileOperations::getSystemDataDir();
121  if (StringOp::startsWith(path, userData)) {
122  return subst(path, userData, SYSTEM_DATA);
123  }
124  string userDir = FileOperations::getUserOpenMSXDir();
125  if (StringOp::startsWith(path, userDir)) {
126  return subst(path, userDir, USER_OPENMSX);
127  }
128  // TODO USER_DIRS (not needed ATM)
129  return path;
130 }
131 
133  string_ref hwDescr,
134  string_ref userName)
135 {
136  paths = { backSubstSymbols(FileOperations::expandTilde(path)) };
138  USER_OPENMSX, "persistent", hwDescr, userName) };
139 }
140 
142 {
143  paths = { USER_DATA, SYSTEM_DATA };
144  savePaths = { USER_DATA };
145 }
146 
148 {
149  paths = { SYSTEM_DATA, USER_DATA }; // first system dir
150 }
151 
153 {
154  paths = { "", USER_DIRS };
155  if (!savePath.empty()) {
157  USER_OPENMSX, "persistent", savePath) };
158  }
159 }
160 
162 {
163  paths.emplace_back();
164  paths.emplace_back(USER_OPENMSX + '/' + subDir);
165 }
166 
168 {
169  paths = { "" };
170 }
171 
172 
173 template<typename Archive>
174 void FileContext::serialize(Archive& ar, unsigned /*version*/)
175 {
176  ar.serialize("paths", paths);
177  ar.serialize("savePaths", savePaths);
178 }
180 
181 } // namespace openmsx
bool isAbsolutePath(string_ref path)
Checks whether it's a absolute path or not.
const std::string resolveCreate(string_ref filename) const
Definition: FileContext.cc:82
const string USER_DIRS
Definition: FileContext.cc:17
const string USER_OPENMSX
Definition: FileContext.cc:18
void mkdirp(string_ref path_)
Acts like the unix command "mkdir -p".
UserDataFileContext(string_ref subdir)
Definition: FileContext.cc:161
UserFileContext(string_ref savePath="")
Definition: FileContext.cc:152
string getSystemDataDir()
Get system directory.
bool contains(ITER first, ITER last, const VAL &val)
Check if a range contains a given value, using linear search.
Definition: stl.hh:95
bool starts_with(string_ref x) const
Definition: string_ref.cc:136
std::vector< std::string > getPaths() const
Definition: FileContext.cc:102
string join(string_ref part1, string_ref part2)
Join two paths.
This class implements a subset of the proposal for std::string_ref (proposed for the next c++ standar...
Definition: string_ref.hh:18
string expandCurrentDirFromDrive(string_ref path)
Get the current directory of the specified drive Linux: just return an empty string.
size_type size() const
Definition: string_ref.hh:55
const std::string resolve(string_ref filename) const
Definition: FileContext.cc:74
bool startsWith(string_ref total, string_ref part)
Definition: StringOp.cc:241
bool isUserContext() const
Definition: FileContext.cc:107
std::vector< std::string > savePaths
Definition: FileContext.hh:23
const std::string & getMessage() const
Definition: MSXException.hh:14
string getUserDataDir()
Get the openMSX data dir in the user's home directory.
const string SYSTEM_DATA
Definition: FileContext.cc:20
ConfigFileContext(string_ref path, string_ref hwDescr, string_ref userName)
Definition: FileContext.cc:132
bool exists(string_ref filename)
Does this file (directory) exists?
const string & getUserOpenMSXDir()
Get the openMSX dir in the user's home directory.
#define INSTANTIATE_SERIALIZE_METHODS(CLASS)
Definition: serialize.hh:802
void serialize(Archive &ar, unsigned version)
Definition: FileContext.cc:174
const string USER_DATA
Definition: FileContext.cc:19
string expandTilde(string_ref path)
Expand the '~' character to the users home directory.
std::vector< std::string > paths
Definition: FileContext.hh:22
string_ref substr(size_type pos, size_type n=npos) const
Definition: string_ref.cc:52
bool empty() const
Definition: string_ref.hh:56
#define PRT_DEBUG(mes)
Definition: openmsx.hh:69