Skip to content

Commit

Permalink
work with env variable in entity
Browse files Browse the repository at this point in the history
  • Loading branch information
ctapmex committed Sep 30, 2024
1 parent d482973 commit c1dc37a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
23 changes: 21 additions & 2 deletions src/colorer/utils/Environment.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "colorer/utils/Environment.h"
#include <regex>
#ifdef WIN32
#include <windows.h>
#include <cwchar>
#else
#include <regex>
#endif

namespace colorer {
fs::path Environment::to_filepath(const UnicodeString* str)
{
Expand Down Expand Up @@ -159,4 +159,23 @@ UnicodeString Environment::getAbsolutePath(const UnicodeString& basePath, const
newPath.append(relPath);
return newPath;
}

UnicodeString Environment::expandSpecialEnvironment(const UnicodeString& path)
{
COLORER_LOG_DEBUG("expand system environment for '%'", path);
std::smatch matcher;
std::string result;
auto text = UStr::to_stdstr(&path);
static const std::regex env_re {R"--(\$([[:alpha:]]\w*)\b)--"};
while (std::regex_search(text, matcher, env_re)) {
result += matcher.prefix().str();
auto a = getOSVariable(matcher[1].str().c_str());
result += UStr::to_stdstr(a);
text = matcher.suffix().str();
}
result += text;

COLORER_LOG_DEBUG("result of expand '%'", result);
return UnicodeString(result.c_str());
}
} // namespace colorer
2 changes: 2 additions & 0 deletions src/colorer/utils/Environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Environment
const UnicodeString& extension);
static bool isRegularFile(const UnicodeString* basePath, const UnicodeString* relPath, UnicodeString& fullPath);
static UnicodeString getAbsolutePath(const UnicodeString& basePath, const UnicodeString& relPath);

static UnicodeString expandSpecialEnvironment(const UnicodeString& path);
};

} // namespace colorer
Expand Down
11 changes: 9 additions & 2 deletions src/colorer/xml/libxml2/LibXmlReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,17 @@ xmlParserInputPtr LibXmlReader::xmlMyExternalEntityLoader(const char* URL, const
// путем склейки пути от текущего файла и указанного в external entity.
// При этом в параметрах функции или в контексте нет ни пути до исходного файла, ни до файла в entity

const UnicodeString string_url(URL);
UnicodeString string_url(URL);

if (!is_full_path && string_url.startsWith(u"env:")) {
const auto exp = colorer::Environment::expandSpecialEnvironment(string_url);
string_url = UnicodeString(exp, 4);
}
#ifdef COLORER_FEATURE_ZIPINPUTSOURCE
if (string_url.startsWith(jar) || current_file->startsWith(jar)) {
if (!is_full_path) {
string_url = colorer::Environment::expandSpecialEnvironment(string_url);
}
auto paths = LibXmlInputSource::getFullPathFromPathJar(string_url, is_full_path ? nullptr : current_file.get());
is_full_path = false;
xmlParserInputPtr ret = nullptr;
Expand All @@ -156,7 +163,7 @@ xmlParserInputPtr LibXmlReader::xmlMyExternalEntityLoader(const char* URL, const
}
#endif
is_full_path = false;
xmlParserInputPtr ret = xmlNewInputFromFile(ctxt, URL);
xmlParserInputPtr ret = xmlNewInputFromFile(ctxt, UStr::to_stdstr(&string_url).c_str());

return ret;
}
Expand Down

0 comments on commit c1dc37a

Please sign in to comment.