[PATCH] xmllite/writer: Implement WriteCData()
Nikolay Sivov
nsivov at codeweavers.com
Tue Mar 15 11:54:46 CDT 2016
Signed-off-by: Nikolay Sivov <nsivov at codeweavers.com>
---
dlls/xmllite/tests/writer.c | 67 +++++++++++++++++++++++++++++++++++++++++++++
dlls/xmllite/writer.c | 51 ++++++++++++++++++++++++++++++++--
2 files changed, 115 insertions(+), 3 deletions(-)
diff --git a/dlls/xmllite/tests/writer.c b/dlls/xmllite/tests/writer.c
index a55957e..6f2232e 100644
--- a/dlls/xmllite/tests/writer.c
+++ b/dlls/xmllite/tests/writer.c
@@ -814,6 +814,72 @@ static void test_WriteComment(void)
IStream_Release(stream);
}
+static void test_WriteCData(void)
+{
+ static const WCHAR closeW[] = {']',']','>',0};
+ static const WCHAR close2W[] = {'a',']',']','>','b',0};
+ static const WCHAR aW[] = {'a',0};
+ static const WCHAR bW[] = {'b',0};
+ IXmlWriter *writer;
+ IStream *stream;
+ HGLOBAL hglobal;
+ HRESULT hr;
+ char *ptr;
+
+ hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
+ ok(hr == S_OK, "got 0x%08x\n", hr);
+
+ hr = pCreateXmlWriter(&IID_IXmlWriter, (void**)&writer, NULL);
+ ok(hr == S_OK, "Expected S_OK, got %08x\n", hr);
+
+ hr = IXmlWriter_SetProperty(writer, XmlWriterProperty_OmitXmlDeclaration, TRUE);
+ ok(hr == S_OK, "got 0x%08x\n", hr);
+
+ hr = IXmlWriter_WriteCData(writer, aW);
+ ok(hr == E_UNEXPECTED, "got 0x%08x\n", hr);
+
+ hr = IXmlWriter_SetOutput(writer, (IUnknown*)stream);
+ ok(hr == S_OK, "got 0x%08x\n", hr);
+
+ hr = IXmlWriter_WriteStartElement(writer, NULL, bW, NULL);
+ ok(hr == S_OK, "got 0x%08x\n", hr);
+
+ hr = IXmlWriter_WriteCData(writer, aW);
+ ok(hr == S_OK, "got 0x%08x\n", hr);
+
+ hr = IXmlWriter_WriteCData(writer, NULL);
+ ok(hr == S_OK, "got 0x%08x\n", hr);
+
+ hr = IXmlWriter_WriteCData(writer, closeW);
+ ok(hr == S_OK, "got 0x%08x\n", hr);
+
+ hr = IXmlWriter_WriteCData(writer, close2W);
+ ok(hr == S_OK, "got 0x%08x\n", hr);
+
+ hr = IXmlWriter_Flush(writer);
+ ok(hr == S_OK, "got 0x%08x\n", hr);
+
+ hr = GetHGlobalFromStream(stream, &hglobal);
+ ok(hr == S_OK, "got 0x%08x\n", hr);
+
+ ptr = GlobalLock(hglobal);
+ ok(ptr != NULL, "got %p\n", ptr);
+
+ ok(!strncmp(ptr,
+ "<b>"
+ "<![CDATA[a]]>"
+ "<![CDATA[]]>"
+ "<![CDATA[]]]]>"
+ "<![CDATA[>]]>"
+ "<![CDATA[a]]]]>"
+ "<![CDATA[>b]]>", 84), "got %s\n", ptr);
+
+ GlobalUnlock(hglobal);
+
+ IXmlWriter_Release(writer);
+ IStream_Release(stream);
+}
+
START_TEST(writer)
{
if (!init_pointers())
@@ -829,4 +895,5 @@ START_TEST(writer)
test_bom();
test_writeenddocument();
test_WriteComment();
+ test_WriteCData();
}
diff --git a/dlls/xmllite/writer.c b/dlls/xmllite/writer.c
index 766d335..3e74389 100644
--- a/dlls/xmllite/writer.c
+++ b/dlls/xmllite/writer.c
@@ -556,13 +556,58 @@ static HRESULT WINAPI xmlwriter_WriteAttributeString(IXmlWriter *iface, LPCWSTR
return E_NOTIMPL;
}
-static HRESULT WINAPI xmlwriter_WriteCData(IXmlWriter *iface, LPCWSTR pwszText)
+static void write_cdata_section(xmlwriteroutput *output, const WCHAR *data, int len)
+{
+ static const WCHAR cdataopenW[] = {'<','!','[','C','D','A','T','A','['};
+ static const WCHAR cdatacloseW[] = {']',']','>'};
+ write_output_buffer(output, cdataopenW, ARRAY_SIZE(cdataopenW));
+ if (data)
+ write_output_buffer(output, data, len);
+ write_output_buffer(output, cdatacloseW, ARRAY_SIZE(cdatacloseW));
+}
+
+static HRESULT WINAPI xmlwriter_WriteCData(IXmlWriter *iface, LPCWSTR data)
{
xmlwriter *This = impl_from_IXmlWriter(iface);
+ int len;
- FIXME("%p %s\n", This, wine_dbgstr_w(pwszText));
+ TRACE("%p %s\n", This, debugstr_w(data));
- return E_NOTIMPL;
+ switch (This->state)
+ {
+ case XmlWriterState_Initial:
+ return E_UNEXPECTED;
+ case XmlWriterState_ElemStarted:
+ writer_close_starttag(This);
+ break;
+ case XmlWriterState_DocClosed:
+ return WR_E_INVALIDACTION;
+ default:
+ ;
+ }
+
+ len = data ? strlenW(data) : 0;
+
+ if (!len)
+ write_cdata_section(This->output, NULL, 0);
+ else {
+ static const WCHAR cdatacloseW[] = {']',']','>',0};
+ while (len) {
+ const WCHAR *str = strstrW(data, cdatacloseW);
+ if (str) {
+ str += 2;
+ write_cdata_section(This->output, data, str - data);
+ len -= str - data;
+ data = str;
+ }
+ else {
+ write_cdata_section(This->output, data, len);
+ break;
+ }
+ }
+ }
+
+ return S_OK;
}
static HRESULT WINAPI xmlwriter_WriteCharEntity(IXmlWriter *iface, WCHAR wch)
--
2.7.0
More information about the wine-patches
mailing list