From 14b63ee87dde3688fea7e58fa25b73362117ae11 Mon Sep 17 00:00:00 2001 From: valmat Date: Thu, 10 Apr 2014 13:04:34 +0600 Subject: Fixed problem with duplicate names ini entries. Mentioned https://github.com/CopernicaMarketingSoftware/PHP-CPP/issues/64#issuecomment-39838004 Now in the case of duplication name of ini entriy the new value overrides the old one. Before was incorrect handling of this situation. --- include/ini.h | 15 +++++++++++++-- include/namespace.h | 6 +++--- phpcpp.h | 1 + tests/cpp/main.cpp | 2 ++ zend/includes.h | 1 + zend/ini.cpp | 4 ++-- 6 files changed, 22 insertions(+), 7 deletions(-) diff --git a/include/ini.h b/include/ini.h index 2223bee..9d4cbd9 100644 --- a/include/ini.h +++ b/include/ini.h @@ -116,9 +116,20 @@ namespace Php { * @param int module_number */ void fill(_zend_ini_entry *ini_entry, int module_number); - + /** + * Compare by name + * A predicate that takes two arguments of type Ini. + * Used when adding elements of type Ini in the container std::set + */ + struct Compare + { + int operator()(const std::shared_ptr &s1, const std::shared_ptr &s2) const + { + return s1->_name.compare(s2->_name); + } + }; private: @@ -128,7 +139,7 @@ namespace Php { } // ini entry name - const char* _name; + std::string _name; // ini entry value std::string _value; diff --git a/include/namespace.h b/include/namespace.h index 645eace..468a423 100644 --- a/include/namespace.h +++ b/include/namespace.h @@ -52,7 +52,7 @@ protected: * Ini entry defined by the extension * @var list */ - std::list> _ini_entries; + std::set, Ini::Compare> _ini_entries; public: /** @@ -172,7 +172,7 @@ public: Namespace &add(Ini &&ini) { // and add it to the list of classes - _ini_entries.push_back(std::unique_ptr(new Ini(std::move(ini)))); + _ini_entries.emplace(new Ini(std::move(ini))); // allow chaining return *this; @@ -186,7 +186,7 @@ public: Namespace &add(const Ini &ini) { // and add it to the list of classes - _ini_entries.push_back(std::unique_ptr(new Ini(ini))); + _ini_entries.emplace(new Ini(ini)); // allow chaining return *this; diff --git a/phpcpp.h b/phpcpp.h index ce1beb5..4e9735d 100644 --- a/phpcpp.h +++ b/phpcpp.h @@ -21,6 +21,7 @@ #include #include #include +#include /** * Include all headers files that are related to this library diff --git a/tests/cpp/main.cpp b/tests/cpp/main.cpp index 334398a..2e0a1dc 100644 --- a/tests/cpp/main.cpp +++ b/tests/cpp/main.cpp @@ -147,6 +147,8 @@ extern "C" Php::Ini ini8("ini8", 3.1415926, 6.2831852); Php::Ini ini9("ini9", 2.7182818, 5.4365636, Php::Ini::User); + extension.add(Php::Ini("ini9", 0.333333, 0.777777, Php::Ini::Perdir)); + extension.add(ini8); extension.add(std::move(ini9)); diff --git a/zend/includes.h b/zend/includes.h index e7dece9..4aa5eb0 100644 --- a/zend/includes.h +++ b/zend/includes.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include diff --git a/zend/ini.cpp b/zend/ini.cpp index e6a9888..0c04d6a 100644 --- a/zend/ini.cpp +++ b/zend/ini.cpp @@ -21,8 +21,8 @@ namespace Php { { ini_entry->module_number = module_number; ini_entry->modifiable = static_cast(this->_place); - ini_entry->name = const_cast(this->_name); - ini_entry->name_length = strlen(this->_name)+1; + ini_entry->name = const_cast(this->_name.c_str()); + ini_entry->name_length = this->_name.size()+1; ini_entry->on_modify = OnUpdateString; ini_entry->mh_arg1 = nullptr; #ifdef ZTS -- cgit v1.2.3