From 9b2ac923e4bf98ca76441362b1d15e58de4297c8 Mon Sep 17 00:00:00 2001 From: valmat Date: Sun, 13 Apr 2014 21:20:52 +0600 Subject: Class IniValue designed for extracting values from ini entries --- include/ini.h | 133 ++++++++++++++++++++++++++++++++++++ tests/cpp/include/ini_entries/001.h | 16 +++++ tests/cpp/main.cpp | 8 ++- tests/php/dbg.php | 3 +- zend/ini.cpp | 29 ++++++++ 5 files changed, 187 insertions(+), 2 deletions(-) diff --git a/include/ini.h b/include/ini.h index 49587d7..83e287d 100644 --- a/include/ini.h +++ b/include/ini.h @@ -17,6 +17,127 @@ struct _zend_ini_entry; */ namespace Php { +/** + * Class IniValue designed for extracting values from ini entries. + */ +class IniValue +{ +public: + /** + * Constructors for floating point values + * + * @param name Name of the php.ini variable + * @param isorig Is the original value + */ + IniValue(const char *name, const bool isorig) : _name(name), _isorig(isorig) {} + + /** + * Cast to a number + * @return int32_t + */ + operator int16_t () const + { + return (int16_t)numericValue(); + } + + /** + * Cast to a number + * @return int32_t + */ + operator int32_t () const + { + return (int32_t)numericValue(); + } + + /** + * Cast to a number + * @return uint64_t + */ + operator int64_t () const + { + return numericValue(); + } + + /** + * Cast to a boolean + * @return boolean + */ + operator bool () const + { + return (bool)numericValue(); + } + + /** + * Cast to a string + * @return string + */ + operator std::string () const + { + return rawValue(); + } + + /** + * Cast to byte array + * @return const char * + */ + operator const char * () const + { + return rawValue(); + } + + /** + * Cast to a floating point + * @return double + */ + operator double() const; + + +private: + + + /** + * Retrieve the value as number + * + * We force this to be a int64_t because we assume that most + * servers run 64 bits nowadays, and because we use int32_t, int64_t + * almost everywhere, instead of 'long' and on OSX neither of + * these intxx_t types is defined as 'long'... + * + * @return int64_t + */ + int64_t numericValue() const; + + /** + * Get access to the raw buffer for read operationrs. + * @return const char * + */ + const char *rawValue() const; + + /** + * ini entry name + * @var std::string + */ + std::string _name; + + /** + * Is the orig value? + * @var bool + */ + bool _isorig = false; +}; + +/** + * Custom output stream operator + * @param stream + * @param ini_val + * @return ostream + */ +std::ostream &operator<<(std::ostream &stream, const IniValue &ini_val) +{ + return stream << static_cast(ini_val); +} + + /** * Class definition */ @@ -117,6 +238,18 @@ public: */ void fill(struct _zend_ini_entry *ini_entry, int module_number); + + static IniValue get(const char* name) + { + return IniValue(name, false); + } + + static IniValue get_orig(const char* name) + { + return IniValue(name, true); + } + + private: /** * Helper function to convert a boolean to a string diff --git a/tests/cpp/include/ini_entries/001.h b/tests/cpp/include/ini_entries/001.h index 649b0bd..440944c 100644 --- a/tests/cpp/include/ini_entries/001.h +++ b/tests/cpp/include/ini_entries/001.h @@ -13,10 +13,26 @@ namespace TestIniEntries { void iniTest1(Php::Parameters ¶ms) { + /* + .add(Php::Ini(, "valIni1")) + .add(Php::Ini("ini2", "valIni2", "OrigValIni2")) + .add(Php::Ini("ini3", "valIni3", "OrigValIni3", Php::Ini::System)) + .add(Php::Ini("ini4", true, false, Php::Ini::Place::User)) + .add(Php::Ini("ini5", false)); + + Ini::get("ini1") + */ + + Php::out << "Ini::get(ini1) = {{" << Php::Ini::get("ini1") << " | " << Php::Ini::get_orig("ini1") << "}}" << std::endl; + Php::out << "Ini::get(ini2) = {{" << Php::Ini::get("ini2") << " | " << Php::Ini::get_orig("ini2") << "}}" << std::endl; + Php::out << "Ini::get(ini3) = {{" << Php::Ini::get("ini3") << " | " << Php::Ini::get_orig("ini3") << "}}" << std::endl; + Php::out << "Ini::get(ini4) = {{" << Php::Ini::get("ini4") << " | " << Php::Ini::get_orig("ini4") << "}}" << std::endl; + /* Php::out << "extension_for_tests.some_string ={{" << Php::ini_get("extension_for_tests.some_string") << "}}" << std::endl; Php::out << "extension_for_tests.some_string2 ={{" << Php::ini_get("extension_for_tests.some_string2") << "}}" << std::endl; Php::out << "extension_for_tests.some_string3 ={{" << Php::ini_get("extension_for_tests.some_string3") << "}}" << std::endl; Php::out << "extension_for_tests.some_string3 ={{" << Php::ini_get("extension_for_tests.some_bool") << "}}" << std::endl; + */ diff --git a/tests/cpp/main.cpp b/tests/cpp/main.cpp index 2e0a1dc..5fd3d87 100644 --- a/tests/cpp/main.cpp +++ b/tests/cpp/main.cpp @@ -147,12 +147,18 @@ 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(Php::Ini("ini9", 0.333333, 0.777777, Php::Ini::Perdir)); extension.add(ini8); extension.add(std::move(ini9)); extension.add("TestIniEntries\\iniTest1", TestIniEntries::iniTest1); + + extension.onStartup([](){ + Php::out << "Ini::get(ini1) = {{" << Php::Ini::get("ini1") << " | " << Php::Ini::get_orig("ini1") << "}}" << std::endl; + Php::out << "Ini::get(ini2) = {{" << Php::Ini::get("ini2") << " | " << Php::Ini::get_orig("ini2") << "}}" << std::endl; + //Php::out << "Ini::get(ini2) = {{" << Php::ini_get("ini2") << "}}" << std::endl; // <-- Error. Php core not loaded yet. + }); diff --git a/tests/php/dbg.php b/tests/php/dbg.php index 8de0bb8..70e71ef 100644 --- a/tests/php/dbg.php +++ b/tests/php/dbg.php @@ -12,7 +12,8 @@ - +TestIniEntries\iniTest1(); +exit; echo "\x1b[1;31m"; (new ReflectionExtension('extension_for_tests') )->info(); echo "\x1b[0m"; diff --git a/zend/ini.cpp b/zend/ini.cpp index 25f081a..e7d9292 100644 --- a/zend/ini.cpp +++ b/zend/ini.cpp @@ -48,6 +48,35 @@ void Ini::fill(zend_ini_entry *ini_entry, int module_number) ini_entry->displayer = nullptr; } + +/** + * Cast to a number + * @return uint64_t + */ +int64_t IniValue::numericValue() const +{ + return zend_ini_long(const_cast(_name.c_str()), _name.size()+1, _isorig); + +} + +/** + * Get access to the raw buffer for read operationrs. + * @return const char * + */ +const char* IniValue::rawValue() const +{ + return zend_ini_string(const_cast(_name.c_str()), _name.size()+1, _isorig); +} + +/** + * Cast to a floating point + * @return double + */ +IniValue::operator double() const +{ + return zend_ini_double(const_cast(_name.c_str()), _name.size()+1, _isorig); +} + /** * End of namespace */ -- cgit v1.2.3