Fix #63: don't use reference arguments in openFileInPackage

This commit is contained in:
Twan van Laarhoven
2020-05-27 22:16:48 +02:00
parent 279fdb0378
commit c9c708dfbe
7 changed files with 17 additions and 21 deletions
+2 -2
View File
@@ -683,10 +683,10 @@ ExprType parseExpr(TokenIterator& input, Script& script, Precedence minPrec) {
// include the file // include the file
// read the entire file, and start at the beginning of it // read the entire file, and start at the beginning of it
String const& filename = token.value; String const& filename = token.value;
auto stream = package_manager.openFileFromPackage(input.package, filename); auto [stream,file_package] = package_manager.openFileFromPackage(input.package, filename);
eat_utf8_bom(*stream); eat_utf8_bom(*stream);
String included_input = read_utf8_line(*stream, true); String included_input = read_utf8_line(*stream, true);
TokenIterator included_tokens(included_input, input.package, false, filename, input.errors); TokenIterator included_tokens(included_input, file_package, false, filename, input.errors);
return parseTopLevel(included_tokens, script); return parseTopLevel(included_tokens, script);
} else { } else {
// variable // variable
+1 -1
View File
@@ -190,7 +190,7 @@ unique_ptr<wxInputStream> Package::openIn(const String& file) {
if (!file.empty() && file.GetChar(0) == _('/')) { if (!file.empty() && file.GetChar(0) == _('/')) {
// absolute path, open file from another package // absolute path, open file from another package
Packaged* p = dynamic_cast<Packaged*>(this); Packaged* p = dynamic_cast<Packaged*>(this);
return package_manager.openFileFromPackage(p, file); return package_manager.openFileFromPackage(p, file).first;
} }
FileInfos::iterator it = files.find(normalize_internal_filename(file)); FileInfos::iterator it = files.find(normalize_internal_filename(file));
if (it == files.end()) { if (it == files.end()) {
+7 -6
View File
@@ -95,7 +95,7 @@ void PackageManager::findMatching(const String& pattern, vector<PackagedP>& out)
} }
} }
unique_ptr<wxInputStream> PackageManager::openFileFromPackage(Packaged*& package, const String& name) { pair<unique_ptr<wxInputStream>,Packaged*> PackageManager::openFileFromPackage(Packaged* package, const String& name) {
if (!name.empty() && name.GetChar(0) == _('/')) { if (!name.empty() && name.GetChar(0) == _('/')) {
// absolute name; break name // absolute name; break name
size_t start = name.find_first_not_of(_("/\\"), 1); // allow "//package/name" from incorrect scripts size_t start = name.find_first_not_of(_("/\\"), 1); // allow "//package/name" from incorrect scripts
@@ -106,17 +106,19 @@ unique_ptr<wxInputStream> PackageManager::openFileFromPackage(Packaged*& package
if (package && !is_substr(name,start,_(":NO-WARN-DEP:"))) { if (package && !is_substr(name,start,_(":NO-WARN-DEP:"))) {
package->requireDependency(p.get()); package->requireDependency(p.get());
} }
package = p.get(); return {p->openIn(name.substr(pos + 1)), p.get()};
return p->openIn(name.substr(pos + 1));
} }
} else if (package) { } else if (package) {
// relative name // relative name
return package->openIn(name); return {package->openIn(name), package};
} }
throw FileNotFoundError(name, _("No package name specified, use '/package/filename'")); throw FileNotFoundError(name, _("No package name specified, use '/package/filename'"));
} }
pair<unique_ptr<wxInputStream>, Packaged*> openFileFromPackage(Packaged* package, const String& name) {
return package_manager.openFileFromPackage(package, name);
}
String PackageManager::openFilenameFromPackage(Packaged*& package, const String& name) { String PackageManager::openFilenameFromPackage(Packaged* package, const String& name) {
if (!name.empty() && name.GetChar(0) == _('/')) { if (!name.empty() && name.GetChar(0) == _('/')) {
// absolute name; break name // absolute name; break name
size_t start = name.find_first_not_of(_("/\\"), 1); // allow "//package/name" from incorrect scripts size_t start = name.find_first_not_of(_("/\\"), 1); // allow "//package/name" from incorrect scripts
@@ -127,7 +129,6 @@ String PackageManager::openFilenameFromPackage(Packaged*& package, const String&
if (package && !is_substr(name,start,_(":NO-WARN-DEP:"))) { if (package && !is_substr(name,start,_(":NO-WARN-DEP:"))) {
package->requireDependency(p.get()); package->requireDependency(p.get());
} }
package = p.get();
return p->absoluteFilename() + _("/") + name.substr(pos + 1); return p->absoluteFilename() + _("/") + name.substr(pos + 1);
} }
} else if (package) { } else if (package) {
+3 -3
View File
@@ -145,15 +145,15 @@ public:
* - tries to open a relative file from the package if the name is "file" * - tries to open a relative file from the package if the name is "file"
* - verifies a dependency from that package if an absolute filename is used * - verifies a dependency from that package if an absolute filename is used
* this is to force people to fill in the dependencies * this is to force people to fill in the dependencies
* Afterwards, package will be set to the package the file is opened from * Returns the opened file and the package it is in
*/ */
unique_ptr<wxInputStream> openFileFromPackage(Packaged*& package, const String& name); pair<unique_ptr<wxInputStream>,Packaged*> openFileFromPackage(Packaged* package, const String& name);
/// Get a filename to open from a package /// Get a filename to open from a package
/** WARNING: this is a bit of a hack, since not all package types support names in this way. /** WARNING: this is a bit of a hack, since not all package types support names in this way.
* It is needed for third party libraries (i.e. hunspell) that load stuff from files. * It is needed for third party libraries (i.e. hunspell) that load stuff from files.
*/ */
String openFilenameFromPackage(Packaged*& package, const String& name); String openFilenameFromPackage(Packaged* package, const String& name);
// --------------------------------------------------- : Packages on disk // --------------------------------------------------- : Packages on disk
-4
View File
@@ -29,10 +29,6 @@ Reader::Reader(wxInputStream& input, Packaged* package, const String& filename,
handleAppVersion(); handleAppVersion();
} }
unique_ptr<wxInputStream> Reader::openIncludedFile() {
return package_manager.openFileFromPackage(package, value);
}
void Reader::handleIgnore(int end_version, const Char* a) { void Reader::handleIgnore(int end_version, const Char* a) {
if (file_app_version < end_version) { if (file_app_version < end_version) {
if (enterBlock(a)) exitBlock(); if (enterBlock(a)) exitBlock();
+3 -3
View File
@@ -16,6 +16,7 @@ template <typename T> class Scriptable;
DECLARE_POINTER_TYPE(Game); DECLARE_POINTER_TYPE(Game);
DECLARE_POINTER_TYPE(StyleSheet); DECLARE_POINTER_TYPE(StyleSheet);
class Packaged; class Packaged;
pair<unique_ptr<wxInputStream>, Packaged*> openFileFromPackage(Packaged* package, const String& name);
// ----------------------------------------------------------------------------- : Reader // ----------------------------------------------------------------------------- : Reader
@@ -171,8 +172,8 @@ private:
template <typename T> template <typename T>
void unknownKey(T& v) { void unknownKey(T& v) {
if (key == _("include_file")) { if (key == _("include_file")) {
auto stream = openIncludedFile(); auto [stream, include_package] = openFileFromPackage(package, value);
Reader sub_reader(*stream, package, value, ignore_invalid); Reader sub_reader(*stream, include_package, value, ignore_invalid);
if (sub_reader.file_app_version == 0) { if (sub_reader.file_app_version == 0) {
// in an included file, use the app version of the parent if there is none // in an included file, use the app version of the parent if there is none
sub_reader.file_app_version = file_app_version; sub_reader.file_app_version = file_app_version;
@@ -184,7 +185,6 @@ private:
} }
} }
void unknownKey(); void unknownKey();
unique_ptr<wxInputStream> openIncludedFile();
}; };
// ----------------------------------------------------------------------------- : After reading hook // ----------------------------------------------------------------------------- : After reading hook
+1 -2
View File
@@ -36,8 +36,7 @@ SpellChecker* SpellChecker::get(const String& language) {
SpellChecker* SpellChecker::get(const String& filename, const String& language) { SpellChecker* SpellChecker::get(const String& filename, const String& language) {
SpellCheckerP& speller = spellers[filename + _(".") + language]; SpellCheckerP& speller = spellers[filename + _(".") + language];
if (!speller) { if (!speller) {
Packaged* package = nullptr; String prefix = package_manager.openFilenameFromPackage(nullptr, filename) + _(".");
String prefix = package_manager.openFilenameFromPackage(package, filename) + _(".");
String local_dir = package_manager.getDictionaryDir(true); String local_dir = package_manager.getDictionaryDir(true);
String global_dir = package_manager.getDictionaryDir(false); String global_dir = package_manager.getDictionaryDir(false);
String aff_path = language + _(".aff"); String aff_path = language + _(".aff");