metaforce/hecl/lib/ProjectPath.cpp

344 lines
11 KiB
C++
Raw Normal View History

2016-03-04 23:02:44 +00:00
#include "hecl/hecl.hpp"
#include "hecl/Database.hpp"
2015-06-09 22:19:59 +00:00
#include <regex>
2016-03-04 23:02:44 +00:00
namespace hecl
2015-06-09 22:19:59 +00:00
{
static const SystemRegex regGLOB(_S("\\*"), SystemRegex::ECMAScript|SystemRegex::optimize);
2015-07-18 04:35:01 +00:00
static const SystemRegex regPATHCOMP(_S("[/\\\\]*([^/\\\\]+)"), SystemRegex::ECMAScript|SystemRegex::optimize);
2015-06-09 22:19:59 +00:00
static const SystemRegex regDRIVELETTER(_S("^([^/]*)/"), SystemRegex::ECMAScript|SystemRegex::optimize);
2015-10-06 01:49:23 +00:00
static SystemString CanonRelPath(const SystemString& path)
{
2015-07-18 04:35:01 +00:00
/* Tokenize Path */
std::vector<SystemString> comps;
2016-03-04 23:02:44 +00:00
hecl::SystemRegexMatch matches;
2015-07-18 04:35:01 +00:00
SystemString in = path;
2015-08-05 01:54:35 +00:00
SanitizePath(in);
2015-07-19 20:52:22 +00:00
for (; std::regex_search(in, matches, regPATHCOMP) ; in = matches.suffix())
2015-06-09 22:19:59 +00:00
{
2015-07-18 04:35:01 +00:00
const SystemString& match = matches[1];
if (!match.compare(_S(".")))
continue;
else if (!match.compare(_S("..")))
2015-07-16 02:03:38 +00:00
{
2015-07-18 04:35:01 +00:00
if (comps.empty())
{
/* Unable to resolve outside project */
2016-03-04 23:02:44 +00:00
LogModule.report(logvisor::Fatal, _S("Unable to resolve outside project root in %s"), path.c_str());
2015-07-18 04:35:01 +00:00
return _S(".");
}
comps.pop_back();
continue;
2015-07-16 02:03:38 +00:00
}
2015-07-18 04:35:01 +00:00
comps.push_back(match);
2015-06-09 22:19:59 +00:00
}
2015-07-18 04:35:01 +00:00
/* Emit relative path */
if (comps.size())
{
auto it = comps.begin();
SystemString retval = *it;
for (++it ; it != comps.end() ; ++it)
{
2015-10-06 01:49:23 +00:00
if ((*it).size())
{
retval += _S('/');
retval += *it;
}
2015-07-18 04:35:01 +00:00
}
return retval;
}
2015-07-22 19:14:50 +00:00
return _S(".");
2015-06-09 22:19:59 +00:00
}
2015-10-06 01:49:23 +00:00
static SystemString CanonRelPath(const SystemString& path, const ProjectRootPath& projectRoot)
{
/* Absolute paths not allowed; attempt to make project-relative */
if (IsAbsolute(path))
return CanonRelPath(projectRoot.getProjectRelativeFromAbsolute(path));
return CanonRelPath(path);
}
2015-09-30 06:23:07 +00:00
void ProjectPath::assign(Database::Project& project, const SystemString& path)
2015-09-29 21:50:07 +00:00
{
2015-09-30 06:23:07 +00:00
m_proj = &project;
2015-10-06 01:49:23 +00:00
m_relPath = CanonRelPath(path);
2015-09-30 06:23:07 +00:00
m_absPath = project.getProjectRootPath().getAbsolutePath() + _S('/') + m_relPath;
2015-09-29 21:50:07 +00:00
SanitizePath(m_relPath);
SanitizePath(m_absPath);
#if HECL_UCS2
m_utf8AbsPath = WideToUTF8(m_absPath);
m_utf8RelPath = WideToUTF8(m_relPath);
m_hash = Hash(m_utf8RelPath);
#else
m_hash = Hash(m_relPath);
2015-09-29 21:50:07 +00:00
#endif
}
#if HECL_UCS2
2015-09-30 06:23:07 +00:00
void ProjectPath::assign(Database::Project& project, const std::string& path)
2015-09-29 21:50:07 +00:00
{
2015-09-30 06:23:07 +00:00
m_proj = &project;
2015-09-29 21:50:07 +00:00
std::wstring wpath = UTF8ToWide(path);
2015-10-12 04:38:49 +00:00
m_relPath = CanonRelPath(wpath);
2015-09-30 06:23:07 +00:00
m_absPath = project.getProjectRootPath().getAbsolutePath() + _S('/') + m_relPath;
2015-09-29 21:50:07 +00:00
SanitizePath(m_relPath);
SanitizePath(m_absPath);
m_hash = Hash(m_relPath);
m_utf8AbsPath = WideToUTF8(m_absPath);
m_utf8RelPath = WideToUTF8(m_relPath);
}
#endif
2015-07-28 23:54:54 +00:00
void ProjectPath::assign(const ProjectPath& parentPath, const SystemString& path)
2015-06-09 22:19:59 +00:00
{
2015-09-30 06:23:07 +00:00
m_proj = parentPath.m_proj;
2015-10-06 01:49:23 +00:00
m_relPath = CanonRelPath(parentPath.m_relPath + _S('/') + path);
2015-09-30 06:23:07 +00:00
m_absPath = m_proj->getProjectRootPath().getAbsolutePath() + _S('/') + m_relPath;
2015-08-05 01:54:35 +00:00
SanitizePath(m_relPath);
SanitizePath(m_absPath);
2015-06-10 02:40:03 +00:00
#if HECL_UCS2
m_utf8AbsPath = WideToUTF8(m_absPath);
2015-07-22 19:14:50 +00:00
m_utf8RelPath = WideToUTF8(m_relPath);
m_hash = Hash(m_utf8RelPath);
#else
m_hash = Hash(m_relPath);
2015-06-10 02:40:03 +00:00
#endif
2015-06-09 22:19:59 +00:00
}
2015-07-22 19:14:50 +00:00
#if HECL_UCS2
2015-07-28 23:54:54 +00:00
void ProjectPath::assign(const ProjectPath& parentPath, const std::string& path)
2015-07-22 19:14:50 +00:00
{
2015-10-12 04:38:49 +00:00
m_proj = parentPath.m_proj;
2015-09-29 21:50:07 +00:00
std::wstring wpath = UTF8ToWide(path);
2015-10-12 04:38:49 +00:00
m_relPath = CanonRelPath(parentPath.m_relPath + _S('/') + wpath);
2015-09-30 06:23:07 +00:00
m_absPath = m_proj->getProjectRootPath().getAbsolutePath() + _S('/') + m_relPath;
2015-08-05 01:54:35 +00:00
SanitizePath(m_relPath);
SanitizePath(m_absPath);
2015-07-22 19:14:50 +00:00
m_hash = Hash(m_relPath);
m_utf8AbsPath = WideToUTF8(m_absPath);
m_utf8RelPath = WideToUTF8(m_relPath);
}
#endif
2015-09-30 06:23:07 +00:00
ProjectPath ProjectPath::getCookedPath(const Database::DataSpecEntry& spec) const
{
2015-10-04 04:35:18 +00:00
ProjectPath woExt = getWithExtension(nullptr, true);
return ProjectPath(m_proj->getProjectCookedPath(spec), woExt.getRelativePath());
2015-09-30 06:23:07 +00:00
}
2015-11-21 01:13:06 +00:00
ProjectPath::Type ProjectPath::getPathType() const
2015-06-09 22:19:59 +00:00
{
if (std::regex_search(m_absPath, regGLOB))
2015-11-21 01:13:06 +00:00
return Type::Glob;
2015-07-22 19:14:50 +00:00
Sstat theStat;
2016-03-04 23:02:44 +00:00
if (hecl::Stat(m_absPath.c_str(), &theStat))
2015-11-21 01:13:06 +00:00
return Type::None;
2015-06-09 22:19:59 +00:00
if (S_ISDIR(theStat.st_mode))
2015-11-21 01:13:06 +00:00
return Type::Directory;
2015-06-09 22:19:59 +00:00
if (S_ISREG(theStat.st_mode))
2015-11-21 01:13:06 +00:00
return Type::File;
return Type::None;
2015-06-09 22:19:59 +00:00
}
2015-06-10 23:34:14 +00:00
Time ProjectPath::getModtime() const
{
2015-07-22 19:14:50 +00:00
Sstat theStat;
2015-06-11 04:55:06 +00:00
time_t latestTime = 0;
if (std::regex_search(m_absPath, regGLOB))
{
2015-10-04 04:35:18 +00:00
std::vector<ProjectPath> globResults;
2015-09-30 06:23:07 +00:00
getGlobResults(globResults);
for (ProjectPath& path : globResults)
2015-06-11 04:55:06 +00:00
{
2016-03-04 23:02:44 +00:00
if (!hecl::Stat(path.getAbsolutePath().c_str(), &theStat))
2015-06-11 04:55:06 +00:00
{
if (S_ISREG(theStat.st_mode) && theStat.st_mtime > latestTime)
latestTime = theStat.st_mtime;
}
}
}
2016-03-04 23:02:44 +00:00
if (!hecl::Stat(m_absPath.c_str(), &theStat))
2015-06-11 04:55:06 +00:00
{
if (S_ISREG(theStat.st_mode))
{
return Time(theStat.st_mtime);
}
else if (S_ISDIR(theStat.st_mode))
{
2016-03-04 23:02:44 +00:00
hecl::DirectoryEnumerator de(m_absPath);
for (const hecl::DirectoryEnumerator::Entry& ent : de)
2015-06-11 04:55:06 +00:00
{
2016-03-04 23:02:44 +00:00
if (!hecl::Stat(ent.m_path.c_str(), &theStat))
2015-06-11 04:55:06 +00:00
{
if (S_ISREG(theStat.st_mode) && theStat.st_mtime > latestTime)
latestTime = theStat.st_mtime;
}
}
return Time(latestTime);
}
}
2016-03-04 23:02:44 +00:00
LogModule.report(logvisor::Fatal, _S("invalid path type for computing modtime in '%s'"), m_absPath.c_str());
2015-06-11 04:55:06 +00:00
return Time();
2015-06-10 23:34:14 +00:00
}
2015-09-30 06:23:07 +00:00
static void _recursiveGlob(Database::Project& proj,
2015-10-04 04:35:18 +00:00
std::vector<ProjectPath>& outPaths,
2015-06-09 22:19:59 +00:00
size_t level,
const SystemRegexMatch& pathCompMatches,
const SystemString& itStr,
bool needSlash)
{
if (level >= pathCompMatches.size())
return;
SystemString comp = pathCompMatches.str(level);
if (!std::regex_search(comp, regGLOB))
{
SystemString nextItStr = itStr;
if (needSlash)
nextItStr += _S('/');
nextItStr += comp;
2015-09-30 06:23:07 +00:00
_recursiveGlob(proj, outPaths, level+1, pathCompMatches, nextItStr, true);
2015-06-09 22:19:59 +00:00
return;
}
/* Compile component into regex */
SystemRegex regComp(comp, SystemRegex::ECMAScript);
2016-03-04 23:02:44 +00:00
hecl::DirectoryEnumerator de(itStr);
for (const hecl::DirectoryEnumerator::Entry& ent : de)
2015-06-09 22:19:59 +00:00
{
if (std::regex_search(ent.m_name, regComp))
2015-06-09 22:19:59 +00:00
{
SystemString nextItStr = itStr;
if (needSlash)
nextItStr += '/';
nextItStr += ent.m_name;
2015-06-09 22:19:59 +00:00
2016-03-04 23:02:44 +00:00
hecl::Sstat theStat;
2016-02-17 03:36:06 +00:00
if (Stat(nextItStr.c_str(), &theStat))
2015-06-09 22:19:59 +00:00
continue;
if (ent.m_isDir)
2015-09-30 06:23:07 +00:00
_recursiveGlob(proj, outPaths, level+1, pathCompMatches, nextItStr, true);
else
2015-09-30 06:23:07 +00:00
outPaths.emplace_back(proj, nextItStr);
2015-06-09 22:19:59 +00:00
}
}
}
2015-10-14 23:06:47 +00:00
void ProjectPath::getDirChildren(std::map<SystemString, ProjectPath>& outPaths) const
2015-09-30 06:23:07 +00:00
{
2016-03-04 23:02:44 +00:00
hecl::DirectoryEnumerator de(m_absPath);
for (const hecl::DirectoryEnumerator::Entry& ent : de)
outPaths[ent.m_name] = ProjectPath(*this, ent.m_name);
}
2015-09-30 06:23:07 +00:00
2016-03-04 23:02:44 +00:00
hecl::DirectoryEnumerator ProjectPath::enumerateDir() const
{
2016-03-04 23:02:44 +00:00
return hecl::DirectoryEnumerator(m_absPath);
2015-09-30 06:23:07 +00:00
}
2015-10-04 04:35:18 +00:00
void ProjectPath::getGlobResults(std::vector<ProjectPath>& outPaths) const
2015-06-09 22:19:59 +00:00
{
#if _WIN32
2015-07-22 19:14:50 +00:00
SystemString itStr;
2015-06-09 22:19:59 +00:00
SystemRegexMatch letterMatch;
if (m_absPath.compare(0, 2, _S("//")))
itStr = _S("\\\\");
else if (std::regex_search(m_absPath, letterMatch, regDRIVELETTER))
if (letterMatch[1].str().size())
itStr = letterMatch[1];
#else
SystemString itStr = _S("/");
#endif
SystemRegexMatch pathCompMatches;
if (std::regex_search(m_absPath, pathCompMatches, regPATHCOMP))
2015-09-30 06:23:07 +00:00
_recursiveGlob(*m_proj, outPaths, 1, pathCompMatches, itStr, false);
}
ProjectRootPath SearchForProject(const SystemString& path)
{
ProjectRootPath testRoot(path);
SystemString::const_iterator begin = testRoot.getAbsolutePath().begin();
SystemString::const_iterator end = testRoot.getAbsolutePath().end();
while (begin != end)
{
SystemString testPath(begin, end);
SystemString testIndexPath = testPath + _S("/.hecl/beacon");
Sstat theStat;
2016-03-04 23:02:44 +00:00
if (!hecl::Stat(testIndexPath.c_str(), &theStat))
2015-09-30 06:23:07 +00:00
{
if (S_ISREG(theStat.st_mode))
{
2016-03-04 23:02:44 +00:00
FILE* fp = hecl::Fopen(testIndexPath.c_str(), _S("rb"));
2015-09-30 06:23:07 +00:00
if (!fp)
continue;
char magic[4];
size_t readSize = fread(magic, 1, 4, fp);
fclose(fp);
if (readSize != 4)
continue;
2016-03-04 23:02:44 +00:00
static const hecl::FourCC hecl("HECL");
if (hecl::FourCC(magic) != hecl)
2015-09-30 06:23:07 +00:00
continue;
return ProjectRootPath(testPath);
}
}
while (begin != end && *(end-1) != _S('/') && *(end-1) != _S('\\'))
--end;
if (begin != end)
--end;
}
return ProjectRootPath();
2015-06-09 22:19:59 +00:00
}
2015-09-30 06:23:07 +00:00
ProjectRootPath SearchForProject(const SystemString& path, SystemString& subpathOut)
2015-06-11 04:55:06 +00:00
{
ProjectRootPath testRoot(path);
SystemString::const_iterator begin = testRoot.getAbsolutePath().begin();
SystemString::const_iterator end = testRoot.getAbsolutePath().end();
while (begin != end)
{
SystemString testPath(begin, end);
2015-06-12 09:08:49 +00:00
SystemString testIndexPath = testPath + _S("/.hecl/beacon");
2015-07-22 19:14:50 +00:00
Sstat theStat;
2016-03-04 23:02:44 +00:00
if (!hecl::Stat(testIndexPath.c_str(), &theStat))
2015-06-11 04:55:06 +00:00
{
if (S_ISREG(theStat.st_mode))
{
2016-03-04 23:02:44 +00:00
FILE* fp = hecl::Fopen(testIndexPath.c_str(), _S("rb"));
2015-06-11 04:55:06 +00:00
if (!fp)
continue;
char magic[4];
size_t readSize = fread(magic, 1, 4, fp);
fclose(fp);
if (readSize != 4)
continue;
2016-03-04 23:02:44 +00:00
if (hecl::FourCC(magic) != FOURCC('HECL'))
2015-06-11 04:55:06 +00:00
continue;
2015-09-30 06:23:07 +00:00
ProjectRootPath newRootPath = ProjectRootPath(testPath);
SystemString::const_iterator origEnd = testRoot.getAbsolutePath().end();
while (end != origEnd && *end != _S('/') && *end != _S('\\'))
++end;
subpathOut.assign(end, origEnd);
return newRootPath;
2015-06-11 04:55:06 +00:00
}
}
2015-06-12 04:02:23 +00:00
while (begin != end && *(end-1) != _S('/') && *(end-1) != _S('\\'))
--end;
2015-07-22 19:14:50 +00:00
if (begin != end)
--end;
2015-06-11 04:55:06 +00:00
}
2015-09-30 06:23:07 +00:00
return ProjectRootPath();
2015-06-11 04:55:06 +00:00
}
2015-06-09 22:19:59 +00:00
}