Advertisement
Guest User

tdf#98291 librevenge: Add support for custom formatting

a guest
Jul 3rd, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.75 KB | None | 0 0
  1. From 9d9dbbea0c3a95430c37f2f3d1a3fa693d332b48 Mon Sep 17 00:00:00 2001
  2. From: Bartosz Kosiorek <gang65@poczta.onet.pl>
  3. Date: Wed, 3 Jul 2019 15:58:31 +0200
  4. Subject: [PATCH] tdf#76829 tdf#98291 Add support for custom formatting
  5.  
  6. The RVNGDoubleProperty property is supporting only one conversion to
  7. string. It is %.4f. It print as a floating point with a precision
  8. of four characters after the decimal point.
  9.  
  10. Example:
  11.  Number: 3.14 will be converted to string "3.1400"
  12.  
  13. In some libraries (libvisio), there is a need to convert double
  14. type property to different formats.
  15.  
  16. For example with different precision after decimal point, or without
  17. trailing zeros:
  18. Example:
  19.  - 3.1
  20.  - 3.140
  21.  
  22. This patch adds new method to RVNGProperty:
  23.    virtual RVNGString getStr(const char* format) const;
  24.  
  25. It allows to change default format of every property.
  26. ---
  27.  inc/librevenge/RVNGProperty.h           |  1 +
  28.  inc/librevenge/RVNGPropertyListVector.h |  1 +
  29.  src/lib/RVNGProperty.cpp                | 71 ++++++++++++++++++++-----
  30.  src/lib/RVNGPropertyListVector.cpp      |  5 ++
  31.  src/test/RVNGPropertyListTest.cpp       | 33 ++++++++++++
  32.  5 files changed, 97 insertions(+), 14 deletions(-)
  33.  
  34. diff --git a/inc/librevenge/RVNGProperty.h b/inc/librevenge/RVNGProperty.h
  35. index df56c2cc..d1c5ef7a 100644
  36. --- a/inc/librevenge/RVNGProperty.h
  37. +++ b/inc/librevenge/RVNGProperty.h
  38. @@ -38,6 +38,7 @@ public:
  39.     /** returns the property unit when possible. If not, returns RVNG_UNIT_ERROR */
  40.     virtual RVNGUnit getUnit() const = 0;
  41.     virtual RVNGString getStr() const = 0;
  42. +   virtual RVNGString getStr(const char *) const = 0;
  43.     virtual RVNGProperty *clone() const = 0;
  44.  };
  45.  
  46. diff --git a/inc/librevenge/RVNGPropertyListVector.h b/inc/librevenge/RVNGPropertyListVector.h
  47. index d56f5836..17687a8d 100644
  48. --- a/inc/librevenge/RVNGPropertyListVector.h
  49. +++ b/inc/librevenge/RVNGPropertyListVector.h
  50. @@ -43,6 +43,7 @@ public:
  51.     double getDouble() const;
  52.     RVNGUnit getUnit() const;
  53.     RVNGString getStr() const;
  54. +   RVNGString getStr(const char *) const;
  55.     RVNGProperty *clone() const;
  56.  
  57.     void append(const RVNGPropertyList &elem);
  58. diff --git a/src/lib/RVNGProperty.cpp b/src/lib/RVNGProperty.cpp
  59. index 9d06a181..d8d27094 100644
  60. --- a/src/lib/RVNGProperty.cpp
  61. +++ b/src/lib/RVNGProperty.cpp
  62. @@ -32,13 +32,13 @@ namespace librevenge
  63.  namespace
  64.  {
  65.  
  66. -static RVNGString doubleToString(const double value)
  67. +static RVNGString doubleToString(const double value, const char* format)
  68.  {
  69.     RVNGString tempString;
  70.     if (value < 0.0001 && value > -0.0001)
  71. -       tempString.sprintf("0.0000");
  72. +       tempString.sprintf(format, 0.0);
  73.     else
  74. -       tempString.sprintf("%.4f", value);
  75. +       tempString.sprintf(format, value);
  76.  #ifndef __ANDROID__
  77.     std::string decimalPoint(localeconv()->decimal_point);
  78.  #else
  79. @@ -77,6 +77,7 @@ public:
  80.         return RVNG_UNIT_ERROR;
  81.     }
  82.     virtual RVNGString getStr() const;
  83. +   virtual RVNGString getStr(const char* format) const;
  84.     virtual RVNGProperty *clone() const;
  85.  
  86.  private:
  87. @@ -102,6 +103,7 @@ public:
  88.         return RVNG_UNIT_ERROR;
  89.     }
  90.     virtual RVNGString getStr() const;
  91. +   virtual RVNGString getStr(const char* format) const;
  92.     virtual RVNGProperty *clone() const;
  93.  
  94.  private:
  95. @@ -120,6 +122,7 @@ public:
  96.         return RVNG_GENERIC;
  97.     }
  98.     virtual RVNGString getStr() const;
  99. +   virtual RVNGString getStr(const char* format) const;
  100.     virtual RVNGProperty *clone() const;
  101.  
  102.  private:
  103. @@ -136,6 +139,7 @@ public:
  104.         return RVNG_UNIT_ERROR;
  105.     }
  106.     virtual RVNGString getStr() const;
  107. +   virtual RVNGString getStr(const char* format) const;
  108.     virtual RVNGProperty *clone() const;
  109.  };
  110.  
  111. @@ -151,6 +155,7 @@ public:
  112.         return RVNG_GENERIC;
  113.     }
  114.     virtual RVNGString getStr() const;
  115. +   virtual RVNGString getStr(const char* format) const;
  116.     virtual RVNGProperty *clone() const;
  117.  
  118.  private:
  119. @@ -180,6 +185,7 @@ public:
  120.         return RVNG_PERCENT;
  121.     }
  122.     virtual RVNGString getStr() const;
  123. +   virtual RVNGString getStr(const char* format) const;
  124.     virtual RVNGProperty *clone() const;
  125.  };
  126.  
  127. @@ -206,6 +212,7 @@ public:
  128.         return RVNG_TWIP;
  129.     }
  130.     virtual RVNGString getStr() const;
  131. +   virtual RVNGString getStr(const char* format) const;
  132.     virtual RVNGProperty *clone() const;
  133.  };
  134.  
  135. @@ -215,6 +222,7 @@ public:
  136.     RVNGGenericProperty(const double val);
  137.     ~RVNGGenericProperty() {}
  138.     virtual RVNGString getStr() const;
  139. +   virtual RVNGString getStr(const char* format) const;
  140.     virtual RVNGProperty *clone() const;
  141.  };
  142.  
  143. @@ -236,6 +244,14 @@ RVNGString RVNGStringProperty::getStr() const
  144.     return m_str;
  145.  }
  146.  
  147. +RVNGString RVNGStringProperty::getStr(const char *format) const
  148. +{
  149. +   RVNGString str;
  150. +   str.sprintf(format, m_str.cstr());
  151. +   return str;
  152. +}
  153. +
  154. +
  155.  RVNGProperty *RVNGStringProperty::clone() const
  156.  {
  157.     return new RVNGStringProperty(m_str);
  158. @@ -255,6 +271,11 @@ RVNGString RVNGBinaryDataProperty::getStr() const
  159.     return m_data.getBase64Data();
  160.  }
  161.  
  162. +RVNGString RVNGBinaryDataProperty::getStr(const char *) const
  163. +{
  164. +   return RVNGBinaryDataProperty::getStr();
  165. +}
  166. +
  167.  RVNGProperty *RVNGBinaryDataProperty::clone() const
  168.  {
  169.     return new RVNGBinaryDataProperty(m_data);
  170. @@ -282,6 +303,13 @@ RVNGString RVNGIntProperty::getStr() const
  171.     return str;
  172.  }
  173.  
  174. +RVNGString RVNGIntProperty::getStr(const char *format) const
  175. +{
  176. +   RVNGString str;
  177. +   str.sprintf(format, m_val);
  178. +   return str;
  179. +}
  180. +
  181.  RVNGProperty *RVNGIntProperty::clone() const
  182.  {
  183.     return new RVNGIntProperty(m_val);
  184. @@ -300,6 +328,11 @@ RVNGString RVNGBoolProperty::getStr() const
  185.         return "false";
  186.  }
  187.  
  188. +RVNGString RVNGBoolProperty::getStr(const char *) const
  189. +{
  190. +   return RVNGBoolProperty::getStr();
  191. +}
  192. +
  193.  RVNGProperty *RVNGBoolProperty::clone() const
  194.  {
  195.     return new RVNGBoolProperty(getInt() != 0);
  196. @@ -322,8 +355,12 @@ double RVNGDoubleProperty::getDouble() const
  197.  
  198.  RVNGString RVNGDoubleProperty::getStr() const
  199.  {
  200. -   RVNGString str = doubleToString(getDouble());
  201. -   return str;
  202. +   return doubleToString(getDouble(), "%.4f");
  203. +}
  204. +
  205. +RVNGString RVNGDoubleProperty::getStr(const char *format) const
  206. +{
  207. +   return doubleToString(getDouble(), format);
  208.  }
  209.  
  210.  RVNGProperty *RVNGDoubleProperty::clone() const
  211. @@ -338,9 +375,7 @@ RVNGInchProperty::RVNGInchProperty(const double val) :
  212.  
  213.  RVNGString RVNGInchProperty::getStr() const
  214.  {
  215. -   RVNGString str = doubleToString(getDouble());
  216. -   str.append("in");
  217. -   return str;
  218. +   return doubleToString(getDouble(), "%.4fin");
  219.  }
  220.  
  221.  RVNGProperty *RVNGInchProperty::clone() const
  222. @@ -355,9 +390,12 @@ RVNGPercentProperty::RVNGPercentProperty(const double val) :
  223.  
  224.  RVNGString RVNGPercentProperty::getStr() const
  225.  {
  226. -   RVNGString str = doubleToString(getDouble()*100.0);
  227. -   str.append("%");
  228. -   return str;
  229. +   return doubleToString(getDouble()*100.0, "%.4f%%");
  230. +}
  231. +
  232. +RVNGString RVNGPercentProperty::getStr(const char *format) const
  233. +{
  234. +   return doubleToString(getDouble()*100.0, format);
  235.  }
  236.  
  237.  RVNGProperty *RVNGPercentProperty::clone() const
  238. @@ -372,9 +410,7 @@ RVNGPointProperty::RVNGPointProperty(const double val) :
  239.  
  240.  RVNGString RVNGPointProperty::getStr() const
  241.  {
  242. -   RVNGString str = doubleToString(getDouble());
  243. -   str.append("pt");
  244. -   return str;
  245. +   return doubleToString(getDouble(), "%.4fpt");
  246.  }
  247.  
  248.  RVNGProperty *RVNGPointProperty::clone() const
  249. @@ -394,6 +430,13 @@ RVNGString RVNGTwipProperty::getStr() const
  250.     return str;
  251.  }
  252.  
  253. +RVNGString RVNGTwipProperty::getStr(const char *format) const
  254. +{
  255. +   RVNGString str;
  256. +   str.sprintf(format, getInt());
  257. +   return str;
  258. +}
  259. +
  260.  RVNGProperty *RVNGTwipProperty::clone() const
  261.  {
  262.     return new RVNGTwipProperty(getDouble());
  263. diff --git a/src/lib/RVNGPropertyListVector.cpp b/src/lib/RVNGPropertyListVector.cpp
  264. index 34f7d0e4..786bbba7 100644
  265. --- a/src/lib/RVNGPropertyListVector.cpp
  266. +++ b/src/lib/RVNGPropertyListVector.cpp
  267. @@ -125,6 +125,11 @@ RVNGString RVNGPropertyListVector::getStr() const
  268.     return RVNGString();
  269.  }
  270.  
  271. +RVNGString RVNGPropertyListVector::getStr(const char *) const
  272. +{
  273. +   return RVNGString();
  274. +}
  275. +
  276.  RVNGProperty *RVNGPropertyListVector::clone() const
  277.  {
  278.     return new RVNGPropertyListVector(*this);
  279. diff --git a/src/test/RVNGPropertyListTest.cpp b/src/test/RVNGPropertyListTest.cpp
  280. index 166217d1..6c223e62 100644
  281. --- a/src/test/RVNGPropertyListTest.cpp
  282. +++ b/src/test/RVNGPropertyListTest.cpp
  283. @@ -60,6 +60,12 @@ void RVNGPropertyListTest::testInsertWithConversion()
  284.  {
  285.     RVNGPropertyList props;
  286.  
  287. +   // valid string
  288. +   props.insert("string", RVNGString("Very long string"));
  289. +   CPPUNIT_ASSERT_EQUAL(RVNGString("Very long string"), props["string"]->getStr());
  290. +   // Print only first 9 characters of the string
  291. +   CPPUNIT_ASSERT_EQUAL(RVNGString("Very long"), props["string"]->getStr("%.9s"));
  292. +
  293.     // valid int
  294.     props.insert("int", RVNGString("42"));
  295.     CPPUNIT_ASSERT_EQUAL(42, props["int"]->getInt());
  296. @@ -79,29 +85,56 @@ void RVNGPropertyListTest::testInsertWithConversion()
  297.     props.insert("double", RVNGString("  3.14 "));
  298.     CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps);
  299.     CPPUNIT_ASSERT_EQUAL(RVNG_GENERIC, props["double"]->getUnit());
  300. +
  301. +   // valid double with custom formatting
  302. +   props.insert("double", 3.0, RVNG_GENERIC);
  303. +   CPPUNIT_ASSERT_DOUBLES_EQUAL(3.0, props["double"]->getDouble(), eps);
  304. +   // By default double formatting is "%.4f" (print as a floating point with a precision of four characters after the decimal point)
  305. +   CPPUNIT_ASSERT_EQUAL(RVNGString("3.0000"), props["double"]->getStr());
  306. +   CPPUNIT_ASSERT_EQUAL(RVNGString("3.0"), props["double"]->getStr("%.1f"));
  307. +   CPPUNIT_ASSERT_EQUAL(RVNGString("3"), props["double"]->getStr("%.3g"));
  308. +
  309.     // valid double with unit
  310.     props.insert("double", RVNGString("3.14pt"));
  311.     CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps);
  312.     CPPUNIT_ASSERT_EQUAL(RVNG_POINT, props["double"]->getUnit());
  313. +   CPPUNIT_ASSERT_EQUAL(RVNGString("3.1400pt"), props["double"]->getStr());
  314. +   CPPUNIT_ASSERT_EQUAL(RVNGString("3.14 points"), props["double"]->getStr("%.4g points"));
  315. +
  316.     // TODO: is this really expected to work?
  317.     props.insert("double", RVNGString("3.14  pt"));
  318.     CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps);
  319.     CPPUNIT_ASSERT_EQUAL(RVNG_POINT, props["double"]->getUnit());
  320. +   CPPUNIT_ASSERT_EQUAL(RVNGString("3.1400pt"), props["double"]->getStr());
  321. +   CPPUNIT_ASSERT_EQUAL(RVNGString("3.14 pt"), props["double"]->getStr("%.4g pt"));
  322. +
  323.     props.insert("double", RVNGString("3.14in"));
  324.     CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps);
  325.     CPPUNIT_ASSERT_EQUAL(RVNG_INCH, props["double"]->getUnit());
  326. +   CPPUNIT_ASSERT_EQUAL(RVNGString("3.1400in"), props["double"]->getStr());
  327. +   CPPUNIT_ASSERT_EQUAL(RVNGString("3.14 in"), props["double"]->getStr("%.4g in"));
  328. +
  329.     props.insert("double", RVNGString("314%"));
  330.     CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps);
  331.     CPPUNIT_ASSERT_EQUAL(RVNG_PERCENT, props["double"]->getUnit());
  332. +   CPPUNIT_ASSERT_EQUAL(RVNGString("314.0000%"), props["double"]->getStr());
  333. +   CPPUNIT_ASSERT_EQUAL(RVNGString("314 %"), props["double"]->getStr("%.4g %%"));
  334. +
  335.     props.insert("double", RVNGString("3.14*"));
  336.     CPPUNIT_ASSERT_DOUBLES_EQUAL(3.14, props["double"]->getDouble(), eps);
  337.     CPPUNIT_ASSERT_EQUAL(RVNG_TWIP, props["double"]->getUnit());
  338. +   CPPUNIT_ASSERT_EQUAL(RVNGString("3*"), props["double"]->getStr());
  339. +
  340.     props.insert("double", RVNGString("2.54cm"));
  341. +   // There is not such unit like cm, mm, a.
  342. +   // TODO Should we add support for these units?
  343.     CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, props["double"]->getDouble(), eps);
  344.     CPPUNIT_ASSERT_EQUAL(RVNG_INCH, props["double"]->getUnit());
  345. +
  346.     props.insert("double", RVNGString("25.4mm"));
  347.     CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, props["double"]->getDouble(), eps);
  348.     CPPUNIT_ASSERT_EQUAL(RVNG_INCH, props["double"]->getUnit());
  349. +
  350.     // invalid double
  351.     props.insert("double", RVNGString("25.4a"));
  352.     CPPUNIT_ASSERT_DOUBLES_EQUAL(0.0, props["double"]->getDouble(), eps);
  353. --
  354. 2.17.1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement