Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- From 9d9dbbea0c3a95430c37f2f3d1a3fa693d332b48 Mon Sep 17 00:00:00 2001
- From: Bartosz Kosiorek <gang65@poczta.onet.pl>
- Date: Wed, 3 Jul 2019 15:58:31 +0200
- Subject: [PATCH] tdf#76829 tdf#98291 Add support for custom formatting
- The RVNGDoubleProperty property is supporting only one conversion to
- string. It is %.4f. It print as a floating point with a precision
- of four characters after the decimal point.
- Example:
- Number: 3.14 will be converted to string "3.1400"
- In some libraries (libvisio), there is a need to convert double
- type property to different formats.
- For example with different precision after decimal point, or without
- trailing zeros:
- Example:
- - 3.1
- - 3.140
- This patch adds new method to RVNGProperty:
- virtual RVNGString getStr(const char* format) const;
- It allows to change default format of every property.
- ---
- inc/librevenge/RVNGProperty.h | 1 +
- inc/librevenge/RVNGPropertyListVector.h | 1 +
- src/lib/RVNGProperty.cpp | 71 ++++++++++++++++++++-----
- src/lib/RVNGPropertyListVector.cpp | 5 ++
- src/test/RVNGPropertyListTest.cpp | 33 ++++++++++++
- 5 files changed, 97 insertions(+), 14 deletions(-)
- diff --git a/inc/librevenge/RVNGProperty.h b/inc/librevenge/RVNGProperty.h
- index df56c2cc..d1c5ef7a 100644
- --- a/inc/librevenge/RVNGProperty.h
- +++ b/inc/librevenge/RVNGProperty.h
- @@ -38,6 +38,7 @@ public:
- /** returns the property unit when possible. If not, returns RVNG_UNIT_ERROR */
- virtual RVNGUnit getUnit() const = 0;
- virtual RVNGString getStr() const = 0;
- + virtual RVNGString getStr(const char *) const = 0;
- virtual RVNGProperty *clone() const = 0;
- };
- diff --git a/inc/librevenge/RVNGPropertyListVector.h b/inc/librevenge/RVNGPropertyListVector.h
- index d56f5836..17687a8d 100644
- --- a/inc/librevenge/RVNGPropertyListVector.h
- +++ b/inc/librevenge/RVNGPropertyListVector.h
- @@ -43,6 +43,7 @@ public:
- double getDouble() const;
- RVNGUnit getUnit() const;
- RVNGString getStr() const;
- + RVNGString getStr(const char *) const;
- RVNGProperty *clone() const;
- void append(const RVNGPropertyList &elem);
- diff --git a/src/lib/RVNGProperty.cpp b/src/lib/RVNGProperty.cpp
- index 9d06a181..d8d27094 100644
- --- a/src/lib/RVNGProperty.cpp
- +++ b/src/lib/RVNGProperty.cpp
- @@ -32,13 +32,13 @@ namespace librevenge
- namespace
- {
- -static RVNGString doubleToString(const double value)
- +static RVNGString doubleToString(const double value, const char* format)
- {
- RVNGString tempString;
- if (value < 0.0001 && value > -0.0001)
- - tempString.sprintf("0.0000");
- + tempString.sprintf(format, 0.0);
- else
- - tempString.sprintf("%.4f", value);
- + tempString.sprintf(format, value);
- #ifndef __ANDROID__
- std::string decimalPoint(localeconv()->decimal_point);
- #else
- @@ -77,6 +77,7 @@ public:
- return RVNG_UNIT_ERROR;
- }
- virtual RVNGString getStr() const;
- + virtual RVNGString getStr(const char* format) const;
- virtual RVNGProperty *clone() const;
- private:
- @@ -102,6 +103,7 @@ public:
- return RVNG_UNIT_ERROR;
- }
- virtual RVNGString getStr() const;
- + virtual RVNGString getStr(const char* format) const;
- virtual RVNGProperty *clone() const;
- private:
- @@ -120,6 +122,7 @@ public:
- return RVNG_GENERIC;
- }
- virtual RVNGString getStr() const;
- + virtual RVNGString getStr(const char* format) const;
- virtual RVNGProperty *clone() const;
- private:
- @@ -136,6 +139,7 @@ public:
- return RVNG_UNIT_ERROR;
- }
- virtual RVNGString getStr() const;
- + virtual RVNGString getStr(const char* format) const;
- virtual RVNGProperty *clone() const;
- };
- @@ -151,6 +155,7 @@ public:
- return RVNG_GENERIC;
- }
- virtual RVNGString getStr() const;
- + virtual RVNGString getStr(const char* format) const;
- virtual RVNGProperty *clone() const;
- private:
- @@ -180,6 +185,7 @@ public:
- return RVNG_PERCENT;
- }
- virtual RVNGString getStr() const;
- + virtual RVNGString getStr(const char* format) const;
- virtual RVNGProperty *clone() const;
- };
- @@ -206,6 +212,7 @@ public:
- return RVNG_TWIP;
- }
- virtual RVNGString getStr() const;
- + virtual RVNGString getStr(const char* format) const;
- virtual RVNGProperty *clone() const;
- };
- @@ -215,6 +222,7 @@ public:
- RVNGGenericProperty(const double val);
- ~RVNGGenericProperty() {}
- virtual RVNGString getStr() const;
- + virtual RVNGString getStr(const char* format) const;
- virtual RVNGProperty *clone() const;
- };
- @@ -236,6 +244,14 @@ RVNGString RVNGStringProperty::getStr() const
- return m_str;
- }
- +RVNGString RVNGStringProperty::getStr(const char *format) const
- +{
- + RVNGString str;
- + str.sprintf(format, m_str.cstr());
- + return str;
- +}
- +
- +
- RVNGProperty *RVNGStringProperty::clone() const
- {
- return new RVNGStringProperty(m_str);
- @@ -255,6 +271,11 @@ RVNGString RVNGBinaryDataProperty::getStr() const
- return m_data.getBase64Data();
- }
- +RVNGString RVNGBinaryDataProperty::getStr(const char *) const
- +{
- + return RVNGBinaryDataProperty::getStr();
- +}
- +
- RVNGProperty *RVNGBinaryDataProperty::clone() const
- {
- return new RVNGBinaryDataProperty(m_data);
- @@ -282,6 +303,13 @@ RVNGString RVNGIntProperty::getStr() const
- return str;
- }
- +RVNGString RVNGIntProperty::getStr(const char *format) const
- +{
- + RVNGString str;
- + str.sprintf(format, m_val);
- + return str;
- +}
- +
- RVNGProperty *RVNGIntProperty::clone() const
- {
- return new RVNGIntProperty(m_val);
- @@ -300,6 +328,11 @@ RVNGString RVNGBoolProperty::getStr() const
- return "false";
- }
- +RVNGString RVNGBoolProperty::getStr(const char *) const
- +{
- + return RVNGBoolProperty::getStr();
- +}
- +
- RVNGProperty *RVNGBoolProperty::clone() const
- {
- return new RVNGBoolProperty(getInt() != 0);
- @@ -322,8 +355,12 @@ double RVNGDoubleProperty::getDouble() const
- RVNGString RVNGDoubleProperty::getStr() const
- {
- - RVNGString str = doubleToString(getDouble());
- - return str;
- + return doubleToString(getDouble(), "%.4f");
- +}
- +
- +RVNGString RVNGDoubleProperty::getStr(const char *format) const
- +{
- + return doubleToString(getDouble(), format);
- }
- RVNGProperty *RVNGDoubleProperty::clone() const
- @@ -338,9 +375,7 @@ RVNGInchProperty::RVNGInchProperty(const double val) :
- RVNGString RVNGInchProperty::getStr() const
- {
- - RVNGString str = doubleToString(getDouble());
- - str.append("in");
- - return str;
- + return doubleToString(getDouble(), "%.4fin");
- }
- RVNGProperty *RVNGInchProperty::clone() const
- @@ -355,9 +390,12 @@ RVNGPercentProperty::RVNGPercentProperty(const double val) :
- RVNGString RVNGPercentProperty::getStr() const
- {
- - RVNGString str = doubleToString(getDouble()*100.0);
- - str.append("%");
- - return str;
- + return doubleToString(getDouble()*100.0, "%.4f%%");
- +}
- +
- +RVNGString RVNGPercentProperty::getStr(const char *format) const
- +{
- + return doubleToString(getDouble()*100.0, format);
- }
- RVNGProperty *RVNGPercentProperty::clone() const
- @@ -372,9 +410,7 @@ RVNGPointProperty::RVNGPointProperty(const double val) :
- RVNGString RVNGPointProperty::getStr() const
- {
- - RVNGString str = doubleToString(getDouble());
- - str.append("pt");
- - return str;
- + return doubleToString(getDouble(), "%.4fpt");
- }
- RVNGProperty *RVNGPointProperty::clone() const
- @@ -394,6 +430,13 @@ RVNGString RVNGTwipProperty::getStr() const
- return str;
- }
- +RVNGString RVNGTwipProperty::getStr(const char *format) const
- +{
- + RVNGString str;
- + str.sprintf(format, getInt());
- + return str;
- +}
- +
- RVNGProperty *RVNGTwipProperty::clone() const
- {
- return new RVNGTwipProperty(getDouble());
- diff --git a/src/lib/RVNGPropertyListVector.cpp b/src/lib/RVNGPropertyListVector.cpp
- index 34f7d0e4..786bbba7 100644
- --- a/src/lib/RVNGPropertyListVector.cpp
- +++ b/src/lib/RVNGPropertyListVector.cpp
- @@ -125,6 +125,11 @@ RVNGString RVNGPropertyListVector::getStr() const
- return RVNGString();
- }
- +RVNGString RVNGPropertyListVector::getStr(const char *) const
- +{
- + return RVNGString();
- +}
- +
- RVNGProperty *RVNGPropertyListVector::clone() const
- {
- return new RVNGPropertyListVector(*this);
- diff --git a/src/test/RVNGPropertyListTest.cpp b/src/test/RVNGPropertyListTest.cpp
- index 166217d1..6c223e62 100644
- --- a/src/test/RVNGPropertyListTest.cpp
- +++ b/src/test/RVNGPropertyListTest.cpp
- @@ -60,6 +60,12 @@ void RVNGPropertyListTest::testInsertWithConversion()
- {
- RVNGPropertyList props;
- + // valid string
- + props.insert("string", RVNGString("Very long string"));
- + CPPUNIT_ASSERT_EQUAL(RVNGString("Very long string"), props["string"]->getStr());
- + // Print only first 9 characters of the string
- + CPPUNIT_ASSERT_EQUAL(RVNGString("Very long"), props["string"]->getStr("%.9s"));
- +
- // valid int
- props.insert("int", RVNGString("42"));
- CPPUNIT_ASSERT_EQUAL(42, props["int"]->getInt());
- @@ -79,29 +85,56 @@ void RVNGPropertyListTest::testInsertWithConversion()
- props.insert("double", RVNGString(" 3.14 "));
- CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps);
- CPPUNIT_ASSERT_EQUAL(RVNG_GENERIC, props["double"]->getUnit());
- +
- + // valid double with custom formatting
- + props.insert("double", 3.0, RVNG_GENERIC);
- + CPPUNIT_ASSERT_DOUBLES_EQUAL(3.0, props["double"]->getDouble(), eps);
- + // By default double formatting is "%.4f" (print as a floating point with a precision of four characters after the decimal point)
- + CPPUNIT_ASSERT_EQUAL(RVNGString("3.0000"), props["double"]->getStr());
- + CPPUNIT_ASSERT_EQUAL(RVNGString("3.0"), props["double"]->getStr("%.1f"));
- + CPPUNIT_ASSERT_EQUAL(RVNGString("3"), props["double"]->getStr("%.3g"));
- +
- // valid double with unit
- props.insert("double", RVNGString("3.14pt"));
- CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps);
- CPPUNIT_ASSERT_EQUAL(RVNG_POINT, props["double"]->getUnit());
- + CPPUNIT_ASSERT_EQUAL(RVNGString("3.1400pt"), props["double"]->getStr());
- + CPPUNIT_ASSERT_EQUAL(RVNGString("3.14 points"), props["double"]->getStr("%.4g points"));
- +
- // TODO: is this really expected to work?
- props.insert("double", RVNGString("3.14 pt"));
- CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps);
- CPPUNIT_ASSERT_EQUAL(RVNG_POINT, props["double"]->getUnit());
- + CPPUNIT_ASSERT_EQUAL(RVNGString("3.1400pt"), props["double"]->getStr());
- + CPPUNIT_ASSERT_EQUAL(RVNGString("3.14 pt"), props["double"]->getStr("%.4g pt"));
- +
- props.insert("double", RVNGString("3.14in"));
- CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps);
- CPPUNIT_ASSERT_EQUAL(RVNG_INCH, props["double"]->getUnit());
- + CPPUNIT_ASSERT_EQUAL(RVNGString("3.1400in"), props["double"]->getStr());
- + CPPUNIT_ASSERT_EQUAL(RVNGString("3.14 in"), props["double"]->getStr("%.4g in"));
- +
- props.insert("double", RVNGString("314%"));
- CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps);
- CPPUNIT_ASSERT_EQUAL(RVNG_PERCENT, props["double"]->getUnit());
- + CPPUNIT_ASSERT_EQUAL(RVNGString("314.0000%"), props["double"]->getStr());
- + CPPUNIT_ASSERT_EQUAL(RVNGString("314 %"), props["double"]->getStr("%.4g %%"));
- +
- props.insert("double", RVNGString("3.14*"));
- CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps);
- CPPUNIT_ASSERT_EQUAL(RVNG_TWIP, props["double"]->getUnit());
- + CPPUNIT_ASSERT_EQUAL(RVNGString("3*"), props["double"]->getStr());
- +
- props.insert("double", RVNGString("2.54cm"));
- + // There is not such unit like cm, mm, a.
- + // TODO Should we add support for these units?
- CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, props["double"]->getDouble(), eps);
- CPPUNIT_ASSERT_EQUAL(RVNG_INCH, props["double"]->getUnit());
- +
- props.insert("double", RVNGString("25.4mm"));
- CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, props["double"]->getDouble(), eps);
- CPPUNIT_ASSERT_EQUAL(RVNG_INCH, props["double"]->getUnit());
- +
- // invalid double
- props.insert("double", RVNGString("25.4a"));
- CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, props["double"]->getDouble(), eps);
- --
- 2.17.1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement