summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2020-06-22 16:45:03 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2020-06-25 10:16:37 +0200
commit01e37e3e5626551b6e8d261e357afcea1ba7c758 (patch)
treebbd1bd5402b07c44747d7962a9db48f9c04c1de0 /tools
parente318d5d8146d18e2c76e23f2e3c39527f2af9f36 (diff)
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 <noel.grandin@collabora.co.uk>
Diffstat (limited to 'tools')
-rw-r--r--tools/qa/cppunit/test_json_writer.cxx2
-rw-r--r--tools/source/misc/json_writer.cxx61
2 files changed, 60 insertions, 3 deletions
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<sal_Int32>(12));
}
std::unique_ptr<char, o3tl::free_delete> 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 <stdio.h>
#include <cstring>
#include <rtl/strbuf.hxx>
+#include <rtl/math.hxx>
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<size_t>(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: */