From 384859f8c6c8bf10a93ae2f39ebff3fcfacf424f Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 29 May 2010 10:35:38 +0000 Subject: [PATCH] Add wxConfig::Read(float *) overload. This uses Read(double *) but casts the result to float after checking that it is in the correct range. Closes #12100. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@64428 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + include/wx/confbase.h | 4 ++++ interface/wx/config.h | 25 ++++++++++++++++++++++++- src/common/config.cpp | 31 +++++++++++++++++++++++++++++++ tests/config/fileconf.cpp | 21 +++++++++++++++++++++ 5 files changed, 81 insertions(+), 1 deletion(-) diff --git a/docs/changes.txt b/docs/changes.txt index 97b68c46f1..3bec98b420 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -449,6 +449,7 @@ All: - Added wxResourceTranslationsLoader for loading translations from Windows resources. - Added wxMessageQueue::Clear(). +- Added wxConfig::Read(float *) overload (Terry Farnham). Unix: diff --git a/include/wx/confbase.h b/include/wx/confbase.h index 9df8edcc0a..9d948b659f 100644 --- a/include/wx/confbase.h +++ b/include/wx/confbase.h @@ -181,6 +181,10 @@ public: bool Read(const wxString& key, double* val) const; bool Read(const wxString& key, double* val, double defVal) const; + // read a float + bool Read(const wxString& key, float* val) const; + bool Read(const wxString& key, float* val, float defVal) const; + // read a bool bool Read(const wxString& key, bool* val) const; bool Read(const wxString& key, bool* val, bool defVal) const; diff --git a/interface/wx/config.h b/interface/wx/config.h index 258938bf09..3c9feeba3a 100644 --- a/interface/wx/config.h +++ b/interface/wx/config.h @@ -589,10 +589,33 @@ public: */ bool Read(const wxString& key, double* d, double defaultVal) const; + /** - Reads a bool value, returning @true if the value was found. If the + Reads a float value, returning @true if the value was found. + + With the second overload, if the value was not found, @a defaultVal is + used instead. + + Notice that the value is read as a double but must be in a valid range + for floats for the function to return @true. + + @since 2.9.1 + + @beginWxPerlOnly + Not supported by wxPerl. + @endWxPerlOnly + */ + //@{ + bool Read(const wxString& key, float* f) const; + bool Read(const wxString& key, float* f, float defaultVal) const; + //@} + + /** + Reads a float value, returning @true if the value was found. If the value was not found, @a b is not changed. + @since 2.9.1 + @beginWxPerlOnly Not supported by wxPerl. @endWxPerlOnly diff --git a/src/common/config.cpp b/src/common/config.cpp index 488aa3b118..7ecc5d5778 100644 --- a/src/common/config.cpp +++ b/src/common/config.cpp @@ -43,6 +43,7 @@ #include #include #include // for INT_MAX +#include // for FLT_MAX // ---------------------------------------------------------------------------- // global and class static variables @@ -180,6 +181,36 @@ bool wxConfigBase::Read(const wxString& key, int *pi, int defVal) const return r; } +// Read floats as doubles then just type cast it down. +bool wxConfigBase::Read(const wxString& key, float* val) const +{ + wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); + + double temp; + if ( !Read(key, &temp) ) + return false; + + wxCHECK_MSG( fabs(temp) <= FLT_MAX, false, + wxT("float overflow in wxConfig::Read") ); + wxCHECK_MSG( (temp == 0.0) || (fabs(temp) >= FLT_MIN), false, + wxT("float underflow in wxConfig::Read") ); + + *val = static_cast(temp); + + return true; +} + +bool wxConfigBase::Read(const wxString& key, float* val, float defVal) const +{ + wxCHECK_MSG( val, false, wxT("wxConfig::Read(): NULL parameter") ); + + if ( Read(key, val) ) + return true; + + *val = defVal; + return false; +} + // the DoReadXXX() for the other types have implementation in the base class // but can be overridden in the derived ones bool wxConfigBase::DoReadBool(const wxString& key, bool* val) const diff --git a/tests/config/fileconf.cpp b/tests/config/fileconf.cpp index 4a354479b9..b17185d681 100644 --- a/tests/config/fileconf.cpp +++ b/tests/config/fileconf.cpp @@ -82,6 +82,7 @@ private: CPPUNIT_TEST( AddToExistingRoot ); CPPUNIT_TEST( ReadNonExistent ); CPPUNIT_TEST( ReadEmpty ); + CPPUNIT_TEST( ReadFloat ); CPPUNIT_TEST_SUITE_END(); void Path(); @@ -105,6 +106,7 @@ private: void AddToExistingRoot(); void ReadNonExistent(); void ReadEmpty(); + void ReadFloat(); static wxString ChangePath(wxFileConfig& fc, const wxChar *path) @@ -659,5 +661,24 @@ void FileConfigTestCase::ReadEmpty() wxFileConfig fc(sis); } +void FileConfigTestCase::ReadFloat() +{ + static const char *confTest = + "x=1.234\n" + "y=-9876.5432\n" + "z=2e+308\n" + ; + + wxStringInputStream sis(confTest); + wxFileConfig fc(sis); + + float f; + CPPUNIT_ASSERT( fc.Read("x", &f) ); + CPPUNIT_ASSERT_EQUAL( 1.234f, f ); + + CPPUNIT_ASSERT( fc.Read("y", &f) ); + CPPUNIT_ASSERT_EQUAL( -9876.5432f, f ); +} + #endif // wxUSE_FILECONFIG -- 2.45.2