From 01e37e3e5626551b6e8d261e357afcea1ba7c758 Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Mon, 22 Jun 2020 16:45:03 +0200 Subject: use tools::JsonWriter for dumping property tree Change-Id: I8f55af19ba10b71bd621e69b27000ab7cb565309 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/96677 Tested-by: Jenkins Reviewed-by: Noel Grandin --- tools/qa/cppunit/test_json_writer.cxx | 2 +- tools/source/misc/json_writer.cxx | 61 +++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 3 deletions(-) (limited to 'tools') diff --git a/tools/qa/cppunit/test_json_writer.cxx b/tools/qa/cppunit/test_json_writer.cxx index 6d473e605497..d5c037801067 100644 --- a/tools/qa/cppunit/test_json_writer.cxx +++ b/tools/qa/cppunit/test_json_writer.cxx @@ -48,7 +48,7 @@ void JsonWriterTest::test1() aJson.put("oustring", OUString("val1")); aJson.put("ostring", OString("val2")); aJson.put("charptr", "val3"); - aJson.put("int", 12); + aJson.put("int", static_cast(12)); } std::unique_ptr result(aJson.extractData()); diff --git a/tools/source/misc/json_writer.cxx b/tools/source/misc/json_writer.cxx index be891ef18423..aacf3f709c90 100644 --- a/tools/source/misc/json_writer.cxx +++ b/tools/source/misc/json_writer.cxx @@ -11,6 +11,7 @@ #include #include #include +#include namespace tools { @@ -270,7 +271,7 @@ void JsonWriter::put(const char* pPropName, const char* pPropVal) ++mPos; } -void JsonWriter::put(const char* pPropName, int nPropVal) +void JsonWriter::put(const char* pPropName, sal_Int64 nPropVal) { auto nPropNameLength = strlen(pPropName); auto nWorstCasePropValLength = 32; @@ -285,7 +286,49 @@ void JsonWriter::put(const char* pPropName, int nPropVal) memcpy(mPos, "\": ", 3); mPos += 3; - mPos += sprintf(mPos, "%d", nPropVal); + mPos += sprintf(mPos, "%ld", nPropVal); +} + +void JsonWriter::put(const char* pPropName, double fPropVal) +{ + OString sPropVal = rtl::math::doubleToString(fPropVal, rtl_math_StringFormat_F, 12, '.'); + auto nPropNameLength = strlen(pPropName); + ensureSpace(nPropNameLength + sPropVal.getLength() + 8); + + addCommaBeforeField(); + + *mPos = '"'; + ++mPos; + memcpy(mPos, pPropName, nPropNameLength); + mPos += nPropNameLength; + memcpy(mPos, "\": ", 3); + mPos += 3; + + memcpy(mPos, sPropVal.getStr(), sPropVal.getLength()); + mPos += sPropVal.getLength(); +} + +void JsonWriter::put(const char* pPropName, bool nPropVal) +{ + auto nPropNameLength = strlen(pPropName); + ensureSpace(nPropNameLength + 5 + 8); + + addCommaBeforeField(); + + *mPos = '"'; + ++mPos; + memcpy(mPos, pPropName, nPropNameLength); + mPos += nPropNameLength; + memcpy(mPos, "\": ", 3); + mPos += 3; + + const char* pVal; + if (nPropVal) + pVal = "true"; + else + pVal = "false"; + memcpy(mPos, pVal, strlen(pVal)); + mPos += strlen(pVal); } void JsonWriter::putRaw(const rtl::OStringBuffer& rRawBuf) @@ -349,5 +392,19 @@ OString JsonWriter::extractAsOString() return ret; } +std::string JsonWriter::extractAsStdString() +{ + char* pChar = extractData(); + std::string ret(pChar); + free(pChar); + return ret; +} + +bool JsonWriter::isDataEquals(const std::string& s) +{ + return s.length() == static_cast(mPos - mpBuffer) + && memcmp(s.data(), mpBuffer, s.length()) == 0; +} + } // namespace tools /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ -- cgit