-
Notifications
You must be signed in to change notification settings - Fork 298
/
FileRepository.cpp
361 lines (316 loc) · 11.1 KB
/
FileRepository.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <sofa/helper/system/FileRepository.h>
#include <sofa/helper/system/SetDirectory.h>
#include <sys/types.h>
#include <sys/stat.h>
#if defined(WIN32)
#include <windows.h>
#include <direct.h>
#else
#include <unistd.h>
#endif
#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#elif __has_include(<experimental/filesystem>)
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
error "Missing the <filesystem> header."
#endif
#include <iterator>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <sofa/helper/logging/Messaging.h>
#include <sofa/helper/Utils.h>
#include <sofa/helper/system/FileSystem.h>
using sofa::helper::system::FileSystem;
#ifdef WIN32
#define ON_WIN32 true
#else
#define ON_WIN32 false
#endif // WIN32
namespace sofa::helper::system
{
// replacing every occurences of "//" by "/"
std::string cleanPath( const std::string& path )
{
std::string p = path;
size_t pos = p.find("//");
const size_t len = p.length();
while( pos != std::string::npos )
{
if ( pos == (len-1))
p.replace( pos, 2, "");
else
p.replace(pos,2,"/");
pos = p.find("//");
}
return p;
}
// Initialize PluginRepository and DataRepository
#ifdef WIN32
FileRepository PluginRepository(
"SOFA_PLUGIN_PATH",
{
Utils::getSofaPathTo("bin"),
Utils::getSofaPathTo("plugins"),
Utils::getSofaPathTo("collections"),
Utils::getExecutableDirectory(),
}
);
#else
FileRepository PluginRepository(
"SOFA_PLUGIN_PATH",
{
Utils::getSofaPathTo("plugins"),
Utils::getSofaPathTo("collections"),
Utils::getSofaPathTo("lib"),
}
);
#endif
FileRepository DataRepository(
"SOFA_DATA_PATH",
{
Utils::getSofaPathTo("share/sofa"),
Utils::getSofaPathTo("share/sofa/examples")
},
{
{ Utils::getSofaPathTo("etc/sofa.ini"), {"SHARE_DIR", "EXAMPLES_DIR"} }
}
);
FileRepository::FileRepository(const char* envVar, const std::vector<std::string> & paths, const fileKeysMap& iniFilesAndKeys) {
if (envVar != nullptr && envVar[0]!='\0')
{
const char* envpath = getenv(envVar);
if (envpath != nullptr && envpath[0]!='\0')
addFirstPath(envpath);
}
for (const auto & path : paths) {
if (!path.empty()) {
size_t p0 = 0;
while (p0 < path.size()) {
size_t p1 = path.find(entrySeparator(), p0);
if (p1 == std::string::npos) p1 = path.size();
if (p1 > p0 + 1) {
std::string p = path.substr(p0, p1 - p0);
addLastPath(SetDirectory::GetRelativeFromProcess(p.c_str()));
}
p0 = p1 + 1;
}
}
}
if ( !iniFilesAndKeys.empty() )
{
for ( const auto &iniFileAndKeys : iniFilesAndKeys )
{
const std::string& file = iniFileAndKeys.first;
const std::list<std::string>& keys = iniFileAndKeys.second;
std::map<std::string, std::string> iniFileLines = Utils::readBasicIniFile(file);
for ( const auto &iniFileLine : iniFileLines )
{
const std::string& lineKey = iniFileLine.first;
const std::string& lineDir = iniFileLine.second;
if ( std::find(keys.begin(), keys.end(), lineKey) == keys.end() )
{
// The key on this line is not one of the keys searched in this file
continue;
}
const std::string& absoluteDir = SetDirectory::GetRelativeFromProcess(lineDir.c_str());
if ( FileSystem::exists(absoluteDir) && FileSystem::isDirectory(absoluteDir) )
{
addFirstPath(absoluteDir);
}
}
}
}
}
FileRepository::~FileRepository()
{
}
std::string FileRepository::cleanPath(const std::string& path)
{
msg_deprecated("FileRepository::cleanPath") << "Use FileSystem::cleanPath instead.";
return FileSystem::cleanPath(path);
}
void FileRepository::addFirstPath(const std::string& p)
{
// replacing every occurences of "//" by "/"
std::string path = FileSystem::cleanPath(p);
std::vector<std::string> entries;
size_t p0 = 0;
while ( p0 < path.size() )
{
size_t p1 = path.find(entrySeparator(),p0);
if (p1 == std::string::npos) p1 = path.size();
if (p1>p0+1)
{
entries.push_back(path.substr(p0,p1-p0));
}
p0 = p1+1;
}
vpath.insert(vpath.begin(), entries.begin(), entries.end());
}
void FileRepository::addLastPath(const std::string& p)
{
// replacing every occurences of "//" by "/"
std::string path = FileSystem::cleanPath(p);
std::vector<std::string> entries;
size_t p0 = 0;
while ( p0 < path.size() )
{
size_t p1 = path.find(entrySeparator(),p0);
if (p1 == std::string::npos) p1 = path.size();
if (p1>p0+1)
{
entries.push_back(path.substr(p0,p1-p0));
}
p0 = p1+1;
}
vpath.insert(vpath.end(), entries.begin(), entries.end());
}
void FileRepository::removePath(const std::string& path)
{
std::vector<std::string> entries;
size_t p0 = 0;
while ( p0 < path.size() )
{
size_t p1 = path.find(entrySeparator(),p0);
if (p1 == std::string::npos) p1 = path.size();
if (p1>p0+1)
{
entries.push_back(path.substr(p0,p1-p0));
}
p0 = p1+1;
}
for(std::vector<std::string>::iterator it=entries.begin();
it!=entries.end(); ++it)
{
vpath.erase( find(vpath.begin(), vpath.end(), *it) );
}
}
void FileRepository::clear()
{
vpath.clear();
}
std::string FileRepository::getFirstPath()
{
if (vpath.size() > 0)
return vpath.front();
else return "";
}
bool FileRepository::findFileIn(std::string& filename, const std::string& path)
{
if (filename.empty()) return false; // no filename
const std::string newfname = SetDirectory::GetRelativeFromDir(filename.c_str(), path.c_str());
const fs::path p = fs::u8path(newfname);
if (fs::exists(p))
{
// File found
filename = newfname;
return true;
}
return false;
}
bool FileRepository::findFile(std::string& filename, const std::string& basedir, std::ostream* errlog)
{
if (filename.empty()) return false; // no filename
if (!directAccessProtocolPrefix.empty() && filename.substr(0, directAccessProtocolPrefix.size()) == directAccessProtocolPrefix)
return true;
std::string currentDir = SetDirectory::GetCurrentDir();
if (!basedir.empty())
{
currentDir = SetDirectory::GetRelativeFromDir(basedir.c_str(),currentDir.c_str());
}
if (findFileIn(filename, currentDir)) return true;
if (SetDirectory::IsAbsolute(filename)) return false; // absolute file path
if (filename.substr(0,2)=="./" || filename.substr(0,3)=="../")
{
// update filename with current dir
filename = SetDirectory::GetRelativeFromDir(filename.c_str(), currentDir.c_str());
return false; // local file path
}
for (std::vector<std::string>::const_iterator it = vpath.begin(); it != vpath.end(); ++it)
if (findFileIn(filename, *it)) return true;
if (errlog)
{
// hack to use logging rather than directly writing in std::cerr/std::cout
// @todo do something cleaner
std::stringstream tmplog;
tmplog << "File "<<filename<<" NOT FOUND in "<<basedir;
for (std::vector<std::string>::const_iterator it = vpath.begin(); it != vpath.end(); ++it)
tmplog << ':'<<*it;
if( errlog==&std::cerr || errlog==&std::cout)
msg_error("FileRepository") << tmplog.str();
else
(*errlog)<<tmplog.str()<<std::endl;
}
return false;
}
bool FileRepository::findFileFromFile(std::string& filename, const std::string& basefile, std::ostream* errlog)
{
return findFile(filename, SetDirectory::GetParentDir(basefile.c_str()), errlog);
}
void FileRepository::print()
{
for (std::vector<std::string>::const_iterator it = vpath.begin(); it != vpath.end(); ++it)
std::cout << *it << std::endl;
}
const std::string FileRepository::getPathsJoined()
{
std::ostringstream imploded;
std::string delim = ":";
#ifdef WIN32
delim = ";";
#endif
std::copy(vpath.begin(), vpath.end(), std::ostream_iterator<std::string>(imploded, delim.c_str()));
std::string implodedStr = imploded.str();
implodedStr = implodedStr.substr(0, implodedStr.size()-1); // remove trailing separator
return implodedStr;
}
std::string FileRepository::relativeToPath(std::string path, std::string refPath)
{
std::replace(path.begin(),path.end(),'\\' , '/' );
std::replace(refPath.begin(),refPath.end(),'\\' , '/' );
std::transform(refPath.begin(), refPath.end(), refPath.begin(), ::tolower );
std::string tmppath=path;
std::transform(tmppath.begin(), tmppath.end(), tmppath.begin(), ::tolower );
const std::string::size_type loc = tmppath.find( refPath, 0 );
if (loc != std::string::npos)
{
path = path.substr(refPath.size());
while(!path.empty() && (path.front() == '/' || path.front() == '\\' ))
{
path = path.substr(1);
}
}
return path;
}
const std::string FileRepository::getTempPath() const
{
return fs::temp_directory_path().string();
}
} // namespace sofa::helper::system