TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / xmllite / writer.c
blobfa0155375a1adc33d668a50f6d76dceb3a9572ca
1 /*
2 * IXmlWriter implementation
4 * Copyright 2011 Alistair Leslie-Hughes
5 * Copyright 2014 Nikolay Sivov for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define COBJMACROS
23 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "objbase.h"
27 #include "xmllite.h"
28 #include "xmllite_private.h"
29 #include "initguid.h"
31 #include "wine/debug.h"
32 #include "wine/list.h"
33 #include "wine/unicode.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(xmllite);
37 /* not defined in public headers */
38 DEFINE_GUID(IID_IXmlWriterOutput, 0xc1131708, 0x0f59, 0x477f, 0x93, 0x59, 0x7d, 0x33, 0x24, 0x51, 0xbc, 0x1a);
40 #define ARRAY_SIZE(array) (sizeof(array)/sizeof((array)[0]))
42 static const WCHAR closeelementW[] = {'<','/'};
43 static const WCHAR closepiW[] = {'?','>'};
44 static const WCHAR ltW[] = {'<'};
45 static const WCHAR gtW[] = {'>'};
47 struct output_buffer
49 char *data;
50 unsigned int allocated;
51 unsigned int written;
52 UINT codepage;
55 typedef enum
57 XmlWriterState_Initial, /* output is not set yet */
58 XmlWriterState_Ready, /* SetOutput() was called, ready to start */
59 XmlWriterState_PIDocStarted, /* document was started with manually added 'xml' PI */
60 XmlWriterState_DocStarted, /* document was started with WriteStartDocument() */
61 XmlWriterState_ElemStarted, /* writing element */
62 XmlWriterState_Content, /* content is accepted at this point */
63 XmlWriterState_DocClosed /* WriteEndDocument was called */
64 } XmlWriterState;
66 typedef struct
68 IXmlWriterOutput IXmlWriterOutput_iface;
69 LONG ref;
70 IUnknown *output;
71 ISequentialStream *stream;
72 IMalloc *imalloc;
73 xml_encoding encoding;
74 struct output_buffer buffer;
75 } xmlwriteroutput;
77 static const struct IUnknownVtbl xmlwriteroutputvtbl;
79 struct element
81 struct list entry;
82 WCHAR *qname;
83 unsigned int len; /* qname length in chars */
86 typedef struct _xmlwriter
88 IXmlWriter IXmlWriter_iface;
89 LONG ref;
90 IMalloc *imalloc;
91 xmlwriteroutput *output;
92 BOOL indent;
93 BOOL bom;
94 BOOL omitxmldecl;
95 XmlConformanceLevel conformance;
96 XmlWriterState state;
97 BOOL bomwritten;
98 BOOL starttagopen;
99 struct list elements;
100 } xmlwriter;
102 static inline xmlwriter *impl_from_IXmlWriter(IXmlWriter *iface)
104 return CONTAINING_RECORD(iface, xmlwriter, IXmlWriter_iface);
107 static inline xmlwriteroutput *impl_from_IXmlWriterOutput(IXmlWriterOutput *iface)
109 return CONTAINING_RECORD(iface, xmlwriteroutput, IXmlWriterOutput_iface);
112 static const char *debugstr_writer_prop(XmlWriterProperty prop)
114 static const char * const prop_names[] =
116 "MultiLanguage",
117 "Indent",
118 "ByteOrderMark",
119 "OmitXmlDeclaration",
120 "ConformanceLevel"
123 if (prop > _XmlWriterProperty_Last)
124 return wine_dbg_sprintf("unknown property=%d", prop);
126 return prop_names[prop];
129 /* writer output memory allocation functions */
130 static inline void *writeroutput_alloc(xmlwriteroutput *output, size_t len)
132 return m_alloc(output->imalloc, len);
135 static inline void writeroutput_free(xmlwriteroutput *output, void *mem)
137 m_free(output->imalloc, mem);
140 static inline void *writeroutput_realloc(xmlwriteroutput *output, void *mem, size_t len)
142 return m_realloc(output->imalloc, mem, len);
145 /* writer memory allocation functions */
146 static inline void *writer_alloc(xmlwriter *writer, size_t len)
148 return m_alloc(writer->imalloc, len);
151 static inline void writer_free(xmlwriter *writer, void *mem)
153 m_free(writer->imalloc, mem);
156 static struct element *alloc_element(xmlwriter *writer, const WCHAR *prefix, const WCHAR *local)
158 struct element *ret;
159 int len;
161 ret = writer_alloc(writer, sizeof(*ret));
162 if (!ret) return ret;
164 len = prefix ? strlenW(prefix) + 1 /* ':' */ : 0;
165 len += strlenW(local);
167 ret->qname = writer_alloc(writer, (len + 1)*sizeof(WCHAR));
168 ret->len = len;
169 if (prefix) {
170 static const WCHAR colonW[] = {':',0};
171 strcpyW(ret->qname, prefix);
172 strcatW(ret->qname, colonW);
174 else
175 ret->qname[0] = 0;
176 strcatW(ret->qname, local);
178 return ret;
181 static void free_element(xmlwriter *writer, struct element *element)
183 writer_free(writer, element->qname);
184 writer_free(writer, element);
187 static void push_element(xmlwriter *writer, struct element *element)
189 list_add_head(&writer->elements, &element->entry);
192 static struct element *pop_element(xmlwriter *writer)
194 struct element *element = LIST_ENTRY(list_head(&writer->elements), struct element, entry);
196 if (element)
197 list_remove(&element->entry);
199 return element;
202 static HRESULT init_output_buffer(xmlwriteroutput *output)
204 struct output_buffer *buffer = &output->buffer;
205 const int initial_len = 0x2000;
206 HRESULT hr;
207 UINT cp;
209 hr = get_code_page(output->encoding, &cp);
210 if (FAILED(hr)) return hr;
212 buffer->data = writeroutput_alloc(output, initial_len);
213 if (!buffer->data) return E_OUTOFMEMORY;
215 memset(buffer->data, 0, 4);
216 buffer->allocated = initial_len;
217 buffer->written = 0;
218 buffer->codepage = cp;
220 return S_OK;
223 static void free_output_buffer(xmlwriteroutput *output)
225 struct output_buffer *buffer = &output->buffer;
226 writeroutput_free(output, buffer->data);
227 buffer->data = NULL;
228 buffer->allocated = 0;
229 buffer->written = 0;
232 static HRESULT grow_output_buffer(xmlwriteroutput *output, int length)
234 struct output_buffer *buffer = &output->buffer;
235 /* grow if needed, plus 4 bytes to be sure null terminator will fit in */
236 if (buffer->allocated < buffer->written + length + 4) {
237 int grown_size = max(2*buffer->allocated, buffer->allocated + length);
238 char *ptr = writeroutput_realloc(output, buffer->data, grown_size);
239 if (!ptr) return E_OUTOFMEMORY;
240 buffer->data = ptr;
241 buffer->allocated = grown_size;
244 return S_OK;
247 static HRESULT write_output_buffer(xmlwriteroutput *output, const WCHAR *data, int len)
249 struct output_buffer *buffer = &output->buffer;
250 int length;
251 HRESULT hr;
252 char *ptr;
254 if (buffer->codepage != ~0) {
255 length = WideCharToMultiByte(buffer->codepage, 0, data, len, NULL, 0, NULL, NULL);
256 hr = grow_output_buffer(output, length);
257 if (FAILED(hr)) return hr;
258 ptr = buffer->data + buffer->written;
259 length = WideCharToMultiByte(buffer->codepage, 0, data, len, ptr, length, NULL, NULL);
260 buffer->written += len == -1 ? length-1 : length;
262 else {
263 /* WCHAR data just copied */
264 length = len == -1 ? strlenW(data) : len;
265 if (length) {
266 length *= sizeof(WCHAR);
268 hr = grow_output_buffer(output, length);
269 if (FAILED(hr)) return hr;
270 ptr = buffer->data + buffer->written;
272 memcpy(ptr, data, length);
273 buffer->written += length;
274 ptr += length;
275 /* null termination */
276 *(WCHAR*)ptr = 0;
280 return S_OK;
283 static HRESULT write_output_buffer_quoted(xmlwriteroutput *output, const WCHAR *data, int len)
285 static const WCHAR quoteW[] = {'"'};
286 write_output_buffer(output, quoteW, ARRAY_SIZE(quoteW));
287 write_output_buffer(output, data, len);
288 write_output_buffer(output, quoteW, ARRAY_SIZE(quoteW));
289 return S_OK;
292 /* TODO: test if we need to validate char range */
293 static HRESULT write_output_qname(xmlwriteroutput *output, const WCHAR *prefix, const WCHAR *local_name)
295 if (prefix) {
296 static const WCHAR colW[] = {':'};
297 write_output_buffer(output, prefix, -1);
298 write_output_buffer(output, colW, ARRAY_SIZE(colW));
301 write_output_buffer(output, local_name, -1);
303 return S_OK;
306 static void writeroutput_release_stream(xmlwriteroutput *writeroutput)
308 if (writeroutput->stream) {
309 ISequentialStream_Release(writeroutput->stream);
310 writeroutput->stream = NULL;
314 static inline HRESULT writeroutput_query_for_stream(xmlwriteroutput *writeroutput)
316 HRESULT hr;
318 writeroutput_release_stream(writeroutput);
319 hr = IUnknown_QueryInterface(writeroutput->output, &IID_IStream, (void**)&writeroutput->stream);
320 if (hr != S_OK)
321 hr = IUnknown_QueryInterface(writeroutput->output, &IID_ISequentialStream, (void**)&writeroutput->stream);
323 return hr;
326 static HRESULT writeroutput_flush_stream(xmlwriteroutput *output)
328 struct output_buffer *buffer;
329 ULONG written, offset = 0;
330 HRESULT hr;
332 if (!output || !output->stream)
333 return S_OK;
335 buffer = &output->buffer;
337 /* It will loop forever until everything is written or an error occurred. */
338 do {
339 written = 0;
340 hr = ISequentialStream_Write(output->stream, buffer->data + offset, buffer->written, &written);
341 if (FAILED(hr)) {
342 WARN("write to stream failed (0x%08x)\n", hr);
343 buffer->written = 0;
344 return hr;
347 offset += written;
348 buffer->written -= written;
349 } while (buffer->written > 0);
351 return S_OK;
354 static HRESULT write_encoding_bom(xmlwriter *writer)
356 if (!writer->bom || writer->bomwritten) return S_OK;
358 if (writer->output->encoding == XmlEncoding_UTF16) {
359 static const char utf16bom[] = {0xff, 0xfe};
360 struct output_buffer *buffer = &writer->output->buffer;
361 int len = sizeof(utf16bom);
362 HRESULT hr;
364 hr = grow_output_buffer(writer->output, len);
365 if (FAILED(hr)) return hr;
366 memcpy(buffer->data + buffer->written, utf16bom, len);
367 buffer->written += len;
370 writer->bomwritten = TRUE;
371 return S_OK;
374 static HRESULT writer_close_starttag(xmlwriter *writer)
376 HRESULT hr;
378 if (!writer->starttagopen) return S_OK;
379 hr = write_output_buffer(writer->output, gtW, ARRAY_SIZE(gtW));
380 writer->starttagopen = FALSE;
381 writer->state = XmlWriterState_Content;
382 return hr;
385 static HRESULT WINAPI xmlwriter_QueryInterface(IXmlWriter *iface, REFIID riid, void **ppvObject)
387 xmlwriter *This = impl_from_IXmlWriter(iface);
389 TRACE("%p %s %p\n", This, debugstr_guid(riid), ppvObject);
391 if (IsEqualGUID(riid, &IID_IUnknown) ||
392 IsEqualGUID(riid, &IID_IXmlWriter))
394 *ppvObject = iface;
397 IXmlWriter_AddRef(iface);
399 return S_OK;
402 static ULONG WINAPI xmlwriter_AddRef(IXmlWriter *iface)
404 xmlwriter *This = impl_from_IXmlWriter(iface);
405 TRACE("%p\n", This);
406 return InterlockedIncrement(&This->ref);
409 static ULONG WINAPI xmlwriter_Release(IXmlWriter *iface)
411 xmlwriter *This = impl_from_IXmlWriter(iface);
412 LONG ref;
414 TRACE("%p\n", This);
416 ref = InterlockedDecrement(&This->ref);
417 if (ref == 0) {
418 struct element *element, *element2;
419 IMalloc *imalloc = This->imalloc;
421 IXmlWriter_Flush(iface);
422 if (This->output) IUnknown_Release(&This->output->IXmlWriterOutput_iface);
424 /* element stack */
425 LIST_FOR_EACH_ENTRY_SAFE(element, element2, &This->elements, struct element, entry) {
426 list_remove(&element->entry);
427 free_element(This, element);
430 writer_free(This, This);
431 if (imalloc) IMalloc_Release(imalloc);
434 return ref;
437 /*** IXmlWriter methods ***/
438 static HRESULT WINAPI xmlwriter_SetOutput(IXmlWriter *iface, IUnknown *output)
440 xmlwriter *This = impl_from_IXmlWriter(iface);
441 IXmlWriterOutput *writeroutput;
442 HRESULT hr;
444 TRACE("(%p)->(%p)\n", This, output);
446 if (This->output) {
447 writeroutput_release_stream(This->output);
448 IUnknown_Release(&This->output->IXmlWriterOutput_iface);
449 This->output = NULL;
450 This->bomwritten = FALSE;
453 /* just reset current output */
454 if (!output) {
455 This->state = XmlWriterState_Initial;
456 return S_OK;
459 /* now try IXmlWriterOutput, ISequentialStream, IStream */
460 hr = IUnknown_QueryInterface(output, &IID_IXmlWriterOutput, (void**)&writeroutput);
461 if (hr == S_OK) {
462 if (writeroutput->lpVtbl == &xmlwriteroutputvtbl)
463 This->output = impl_from_IXmlWriterOutput(writeroutput);
464 else {
465 ERR("got external IXmlWriterOutput implementation: %p, vtbl=%p\n",
466 writeroutput, writeroutput->lpVtbl);
467 IUnknown_Release(writeroutput);
468 return E_FAIL;
473 if (hr != S_OK || !writeroutput) {
474 /* create IXmlWriterOutput basing on supplied interface */
475 hr = CreateXmlWriterOutputWithEncodingName(output, This->imalloc, NULL, &writeroutput);
476 if (hr != S_OK) return hr;
477 This->output = impl_from_IXmlWriterOutput(writeroutput);
480 This->state = XmlWriterState_Ready;
481 return writeroutput_query_for_stream(This->output);
484 static HRESULT WINAPI xmlwriter_GetProperty(IXmlWriter *iface, UINT property, LONG_PTR *value)
486 xmlwriter *This = impl_from_IXmlWriter(iface);
488 TRACE("(%p)->(%s %p)\n", This, debugstr_writer_prop(property), value);
490 if (!value) return E_INVALIDARG;
492 switch (property)
494 case XmlWriterProperty_Indent:
495 *value = This->indent;
496 break;
497 case XmlWriterProperty_ByteOrderMark:
498 *value = This->bom;
499 break;
500 case XmlWriterProperty_OmitXmlDeclaration:
501 *value = This->omitxmldecl;
502 break;
503 case XmlWriterProperty_ConformanceLevel:
504 *value = This->conformance;
505 break;
506 default:
507 FIXME("Unimplemented property (%u)\n", property);
508 return E_NOTIMPL;
511 return S_OK;
514 static HRESULT WINAPI xmlwriter_SetProperty(IXmlWriter *iface, UINT property, LONG_PTR value)
516 xmlwriter *This = impl_from_IXmlWriter(iface);
518 TRACE("(%p)->(%s %lu)\n", This, debugstr_writer_prop(property), value);
520 switch (property)
522 case XmlWriterProperty_ByteOrderMark:
523 This->bom = !!value;
524 break;
525 case XmlWriterProperty_OmitXmlDeclaration:
526 This->omitxmldecl = !!value;
527 break;
528 default:
529 FIXME("Unimplemented property (%u)\n", property);
530 return E_NOTIMPL;
533 return S_OK;
536 static HRESULT WINAPI xmlwriter_WriteAttributes(IXmlWriter *iface, IXmlReader *pReader,
537 BOOL fWriteDefaultAttributes)
539 xmlwriter *This = impl_from_IXmlWriter(iface);
541 FIXME("%p %p %d\n", This, pReader, fWriteDefaultAttributes);
543 return E_NOTIMPL;
546 static HRESULT WINAPI xmlwriter_WriteAttributeString(IXmlWriter *iface, LPCWSTR pwszPrefix,
547 LPCWSTR pwszLocalName, LPCWSTR pwszNamespaceUri,
548 LPCWSTR pwszValue)
550 xmlwriter *This = impl_from_IXmlWriter(iface);
552 FIXME("%p %s %s %s %s\n", This, wine_dbgstr_w(pwszPrefix), wine_dbgstr_w(pwszLocalName),
553 wine_dbgstr_w(pwszNamespaceUri), wine_dbgstr_w(pwszValue));
555 return E_NOTIMPL;
558 static HRESULT WINAPI xmlwriter_WriteCData(IXmlWriter *iface, LPCWSTR pwszText)
560 xmlwriter *This = impl_from_IXmlWriter(iface);
562 FIXME("%p %s\n", This, wine_dbgstr_w(pwszText));
564 return E_NOTIMPL;
567 static HRESULT WINAPI xmlwriter_WriteCharEntity(IXmlWriter *iface, WCHAR wch)
569 return E_NOTIMPL;
572 static HRESULT WINAPI xmlwriter_WriteChars(IXmlWriter *iface, const WCHAR *pwch, UINT cwch)
574 xmlwriter *This = impl_from_IXmlWriter(iface);
576 FIXME("%p %s %d\n", This, wine_dbgstr_w(pwch), cwch);
578 return E_NOTIMPL;
581 static HRESULT WINAPI xmlwriter_WriteComment(IXmlWriter *iface, LPCWSTR pwszComment)
583 return E_NOTIMPL;
586 static HRESULT WINAPI xmlwriter_WriteDocType(IXmlWriter *iface, LPCWSTR pwszName, LPCWSTR pwszPublicId,
587 LPCWSTR pwszSystemId, LPCWSTR pwszSubset)
589 xmlwriter *This = impl_from_IXmlWriter(iface);
591 FIXME("%p %s %s %s %s\n", This, wine_dbgstr_w(pwszName), wine_dbgstr_w(pwszPublicId),
592 wine_dbgstr_w(pwszSystemId), wine_dbgstr_w(pwszSubset));
594 return E_NOTIMPL;
597 static HRESULT WINAPI xmlwriter_WriteElementString(IXmlWriter *iface, LPCWSTR prefix,
598 LPCWSTR local_name, LPCWSTR uri, LPCWSTR value)
600 xmlwriter *This = impl_from_IXmlWriter(iface);
602 TRACE("(%p)->(%s %s %s %s)\n", This, wine_dbgstr_w(prefix), wine_dbgstr_w(local_name),
603 wine_dbgstr_w(uri), wine_dbgstr_w(value));
605 switch (This->state)
607 case XmlWriterState_Initial:
608 return E_UNEXPECTED;
609 case XmlWriterState_ElemStarted:
610 writer_close_starttag(This);
611 break;
612 case XmlWriterState_DocClosed:
613 return WR_E_INVALIDACTION;
614 default:
618 write_encoding_bom(This);
619 write_output_buffer(This->output, ltW, ARRAY_SIZE(ltW));
620 write_output_qname(This->output, prefix, local_name);
621 write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
623 if (value)
624 write_output_buffer(This->output, value, -1);
626 write_output_buffer(This->output, closeelementW, ARRAY_SIZE(closeelementW));
627 write_output_qname(This->output, prefix, local_name);
628 write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
629 This->state = XmlWriterState_Content;
631 return S_OK;
634 static HRESULT WINAPI xmlwriter_WriteEndDocument(IXmlWriter *iface)
636 xmlwriter *This = impl_from_IXmlWriter(iface);
637 HRESULT hr = S_OK;
639 TRACE("%p\n", This);
641 switch (This->state)
643 case XmlWriterState_Initial:
644 hr = E_UNEXPECTED;
645 break;
646 case XmlWriterState_Ready:
647 case XmlWriterState_DocClosed:
648 hr = WR_E_INVALIDACTION;
649 break;
650 default:
654 if (FAILED(hr)) {
655 This->state = XmlWriterState_DocClosed;
656 return hr;
659 /* empty element stack */
660 while (IXmlWriter_WriteEndElement(iface) == S_OK)
663 This->state = XmlWriterState_DocClosed;
664 return S_OK;
667 static HRESULT WINAPI xmlwriter_WriteEndElement(IXmlWriter *iface)
669 xmlwriter *This = impl_from_IXmlWriter(iface);
670 struct element *element;
672 TRACE("%p\n", This);
674 element = pop_element(This);
675 if (!element)
676 return WR_E_INVALIDACTION;
678 if (This->starttagopen) {
679 static WCHAR closetagW[] = {' ','/','>'};
680 write_output_buffer(This->output, closetagW, ARRAY_SIZE(closetagW));
681 This->starttagopen = FALSE;
683 else {
684 /* write full end tag */
685 write_output_buffer(This->output, closeelementW, ARRAY_SIZE(closeelementW));
686 write_output_buffer(This->output, element->qname, element->len);
687 write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
690 return S_OK;
693 static HRESULT WINAPI xmlwriter_WriteEntityRef(IXmlWriter *iface, LPCWSTR pwszName)
695 xmlwriter *This = impl_from_IXmlWriter(iface);
697 FIXME("%p %s\n", This, wine_dbgstr_w(pwszName));
699 return E_NOTIMPL;
702 static HRESULT WINAPI xmlwriter_WriteFullEndElement(IXmlWriter *iface)
704 xmlwriter *This = impl_from_IXmlWriter(iface);
705 struct element *element;
707 TRACE("%p\n", This);
709 element = pop_element(This);
710 if (!element)
711 return WR_E_INVALIDACTION;
713 /* write full end tag */
714 write_output_buffer(This->output, closeelementW, ARRAY_SIZE(closeelementW));
715 write_output_buffer(This->output, element->qname, element->len);
716 write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
717 This->starttagopen = FALSE;
719 return S_OK;
722 static HRESULT WINAPI xmlwriter_WriteName(IXmlWriter *iface, LPCWSTR pwszName)
724 xmlwriter *This = impl_from_IXmlWriter(iface);
726 FIXME("%p %s\n", This, wine_dbgstr_w(pwszName));
728 return E_NOTIMPL;
731 static HRESULT WINAPI xmlwriter_WriteNmToken(IXmlWriter *iface, LPCWSTR pwszNmToken)
733 xmlwriter *This = impl_from_IXmlWriter(iface);
735 FIXME("%p %s\n", This, wine_dbgstr_w(pwszNmToken));
737 return E_NOTIMPL;
740 static HRESULT WINAPI xmlwriter_WriteNode(IXmlWriter *iface, IXmlReader *pReader,
741 BOOL fWriteDefaultAttributes)
743 xmlwriter *This = impl_from_IXmlWriter(iface);
745 FIXME("%p %p %d\n", This, pReader, fWriteDefaultAttributes);
747 return E_NOTIMPL;
750 static HRESULT WINAPI xmlwriter_WriteNodeShallow(IXmlWriter *iface, IXmlReader *pReader,
751 BOOL fWriteDefaultAttributes)
753 xmlwriter *This = impl_from_IXmlWriter(iface);
755 FIXME("%p %p %d\n", This, pReader, fWriteDefaultAttributes);
757 return E_NOTIMPL;
760 static HRESULT WINAPI xmlwriter_WriteProcessingInstruction(IXmlWriter *iface, LPCWSTR name,
761 LPCWSTR text)
763 xmlwriter *This = impl_from_IXmlWriter(iface);
764 static const WCHAR xmlW[] = {'x','m','l',0};
765 static const WCHAR openpiW[] = {'<','?'};
766 static const WCHAR spaceW[] = {' '};
768 TRACE("(%p)->(%s %s)\n", This, wine_dbgstr_w(name), wine_dbgstr_w(text));
770 switch (This->state)
772 case XmlWriterState_Initial:
773 return E_UNEXPECTED;
774 case XmlWriterState_DocStarted:
775 if (!strcmpW(name, xmlW))
776 return WR_E_INVALIDACTION;
777 break;
778 case XmlWriterState_ElemStarted:
779 case XmlWriterState_DocClosed:
780 return WR_E_INVALIDACTION;
781 default:
785 write_encoding_bom(This);
786 write_output_buffer(This->output, openpiW, ARRAY_SIZE(openpiW));
787 write_output_buffer(This->output, name, -1);
788 write_output_buffer(This->output, spaceW, ARRAY_SIZE(spaceW));
789 write_output_buffer(This->output, text, -1);
790 write_output_buffer(This->output, closepiW, ARRAY_SIZE(closepiW));
792 if (!strcmpW(name, xmlW))
793 This->state = XmlWriterState_PIDocStarted;
795 return S_OK;
798 static HRESULT WINAPI xmlwriter_WriteQualifiedName(IXmlWriter *iface, LPCWSTR pwszLocalName,
799 LPCWSTR pwszNamespaceUri)
801 xmlwriter *This = impl_from_IXmlWriter(iface);
803 FIXME("%p %s %s\n", This, wine_dbgstr_w(pwszLocalName), wine_dbgstr_w(pwszNamespaceUri));
805 return E_NOTIMPL;
808 static HRESULT WINAPI xmlwriter_WriteRaw(IXmlWriter *iface, LPCWSTR pwszData)
810 xmlwriter *This = impl_from_IXmlWriter(iface);
812 FIXME("%p %s\n", This, wine_dbgstr_w(pwszData));
814 return E_NOTIMPL;
817 static HRESULT WINAPI xmlwriter_WriteRawChars(IXmlWriter *iface, const WCHAR *pwch, UINT cwch)
819 xmlwriter *This = impl_from_IXmlWriter(iface);
821 FIXME("%p %s %d\n", This, wine_dbgstr_w(pwch), cwch);
823 return E_NOTIMPL;
826 static HRESULT WINAPI xmlwriter_WriteStartDocument(IXmlWriter *iface, XmlStandalone standalone)
828 static const WCHAR versionW[] = {'<','?','x','m','l',' ','v','e','r','s','i','o','n','=','"','1','.','0','"'};
829 static const WCHAR encodingW[] = {' ','e','n','c','o','d','i','n','g','='};
830 xmlwriter *This = impl_from_IXmlWriter(iface);
832 TRACE("(%p)->(%d)\n", This, standalone);
834 switch (This->state)
836 case XmlWriterState_Initial:
837 return E_UNEXPECTED;
838 case XmlWriterState_PIDocStarted:
839 This->state = XmlWriterState_DocStarted;
840 return S_OK;
841 case XmlWriterState_DocStarted:
842 case XmlWriterState_ElemStarted:
843 case XmlWriterState_DocClosed:
844 return WR_E_INVALIDACTION;
845 default:
849 write_encoding_bom(This);
850 This->state = XmlWriterState_DocStarted;
851 if (This->omitxmldecl) return S_OK;
853 /* version */
854 write_output_buffer(This->output, versionW, ARRAY_SIZE(versionW));
856 /* encoding */
857 write_output_buffer(This->output, encodingW, ARRAY_SIZE(encodingW));
858 write_output_buffer_quoted(This->output, get_encoding_name(This->output->encoding), -1);
860 /* standalone */
861 if (standalone == XmlStandalone_Omit)
862 write_output_buffer(This->output, closepiW, ARRAY_SIZE(closepiW));
863 else {
864 static const WCHAR standaloneW[] = {' ','s','t','a','n','d','a','l','o','n','e','=','\"'};
865 static const WCHAR yesW[] = {'y','e','s','\"','?','>'};
866 static const WCHAR noW[] = {'n','o','\"','?','>'};
868 write_output_buffer(This->output, standaloneW, ARRAY_SIZE(standaloneW));
869 if (standalone == XmlStandalone_Yes)
870 write_output_buffer(This->output, yesW, ARRAY_SIZE(yesW));
871 else
872 write_output_buffer(This->output, noW, ARRAY_SIZE(noW));
875 return S_OK;
878 static HRESULT WINAPI xmlwriter_WriteStartElement(IXmlWriter *iface, LPCWSTR prefix, LPCWSTR local_name, LPCWSTR uri)
880 xmlwriter *This = impl_from_IXmlWriter(iface);
881 struct element *element;
883 TRACE("(%p)->(%s %s %s)\n", This, wine_dbgstr_w(prefix), wine_dbgstr_w(local_name), wine_dbgstr_w(uri));
885 switch (This->state)
887 case XmlWriterState_Initial:
888 return E_UNEXPECTED;
889 case XmlWriterState_DocClosed:
890 return WR_E_INVALIDACTION;
891 default:
895 if (!local_name)
896 return E_INVALIDARG;
898 /* close pending element */
899 if (This->starttagopen)
900 write_output_buffer(This->output, gtW, ARRAY_SIZE(gtW));
902 element = alloc_element(This, prefix, local_name);
903 if (!element)
904 return E_OUTOFMEMORY;
906 write_encoding_bom(This);
907 This->state = XmlWriterState_ElemStarted;
908 This->starttagopen = TRUE;
910 push_element(This, element);
912 write_output_buffer(This->output, ltW, ARRAY_SIZE(ltW));
913 write_output_qname(This->output, prefix, local_name);
915 return S_OK;
918 static HRESULT WINAPI xmlwriter_WriteString(IXmlWriter *iface, LPCWSTR pwszText)
920 xmlwriter *This = impl_from_IXmlWriter(iface);
922 FIXME("%p %s\n", This, wine_dbgstr_w(pwszText));
924 return E_NOTIMPL;
927 static HRESULT WINAPI xmlwriter_WriteSurrogateCharEntity(IXmlWriter *iface, WCHAR wchLow, WCHAR wchHigh)
929 xmlwriter *This = impl_from_IXmlWriter(iface);
931 FIXME("%p %d %d\n", This, wchLow, wchHigh);
933 return E_NOTIMPL;
936 static HRESULT WINAPI xmlwriter_WriteWhitespace(IXmlWriter *iface, LPCWSTR pwszWhitespace)
938 xmlwriter *This = impl_from_IXmlWriter(iface);
940 FIXME("%p %s\n", This, wine_dbgstr_w(pwszWhitespace));
942 return E_NOTIMPL;
945 static HRESULT WINAPI xmlwriter_Flush(IXmlWriter *iface)
947 xmlwriter *This = impl_from_IXmlWriter(iface);
949 TRACE("%p\n", This);
951 return writeroutput_flush_stream(This->output);
954 static const struct IXmlWriterVtbl xmlwriter_vtbl =
956 xmlwriter_QueryInterface,
957 xmlwriter_AddRef,
958 xmlwriter_Release,
959 xmlwriter_SetOutput,
960 xmlwriter_GetProperty,
961 xmlwriter_SetProperty,
962 xmlwriter_WriteAttributes,
963 xmlwriter_WriteAttributeString,
964 xmlwriter_WriteCData,
965 xmlwriter_WriteCharEntity,
966 xmlwriter_WriteChars,
967 xmlwriter_WriteComment,
968 xmlwriter_WriteDocType,
969 xmlwriter_WriteElementString,
970 xmlwriter_WriteEndDocument,
971 xmlwriter_WriteEndElement,
972 xmlwriter_WriteEntityRef,
973 xmlwriter_WriteFullEndElement,
974 xmlwriter_WriteName,
975 xmlwriter_WriteNmToken,
976 xmlwriter_WriteNode,
977 xmlwriter_WriteNodeShallow,
978 xmlwriter_WriteProcessingInstruction,
979 xmlwriter_WriteQualifiedName,
980 xmlwriter_WriteRaw,
981 xmlwriter_WriteRawChars,
982 xmlwriter_WriteStartDocument,
983 xmlwriter_WriteStartElement,
984 xmlwriter_WriteString,
985 xmlwriter_WriteSurrogateCharEntity,
986 xmlwriter_WriteWhitespace,
987 xmlwriter_Flush
990 /** IXmlWriterOutput **/
991 static HRESULT WINAPI xmlwriteroutput_QueryInterface(IXmlWriterOutput *iface, REFIID riid, void** ppvObject)
993 xmlwriteroutput *This = impl_from_IXmlWriterOutput(iface);
995 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppvObject);
997 if (IsEqualGUID(riid, &IID_IXmlWriterOutput) ||
998 IsEqualGUID(riid, &IID_IUnknown))
1000 *ppvObject = iface;
1002 else
1004 WARN("interface %s not implemented\n", debugstr_guid(riid));
1005 *ppvObject = NULL;
1006 return E_NOINTERFACE;
1009 IUnknown_AddRef(iface);
1011 return S_OK;
1014 static ULONG WINAPI xmlwriteroutput_AddRef(IXmlWriterOutput *iface)
1016 xmlwriteroutput *This = impl_from_IXmlWriterOutput(iface);
1017 ULONG ref = InterlockedIncrement(&This->ref);
1018 TRACE("(%p)->(%d)\n", This, ref);
1019 return ref;
1022 static ULONG WINAPI xmlwriteroutput_Release(IXmlWriterOutput *iface)
1024 xmlwriteroutput *This = impl_from_IXmlWriterOutput(iface);
1025 LONG ref = InterlockedDecrement(&This->ref);
1027 TRACE("(%p)->(%d)\n", This, ref);
1029 if (ref == 0)
1031 IMalloc *imalloc = This->imalloc;
1032 if (This->output) IUnknown_Release(This->output);
1033 if (This->stream) ISequentialStream_Release(This->stream);
1034 free_output_buffer(This);
1035 writeroutput_free(This, This);
1036 if (imalloc) IMalloc_Release(imalloc);
1039 return ref;
1042 static const struct IUnknownVtbl xmlwriteroutputvtbl =
1044 xmlwriteroutput_QueryInterface,
1045 xmlwriteroutput_AddRef,
1046 xmlwriteroutput_Release
1049 HRESULT WINAPI CreateXmlWriter(REFIID riid, void **obj, IMalloc *imalloc)
1051 xmlwriter *writer;
1053 TRACE("(%s, %p, %p)\n", wine_dbgstr_guid(riid), obj, imalloc);
1055 if (!IsEqualGUID(riid, &IID_IXmlWriter))
1057 ERR("Unexpected IID requested -> (%s)\n", wine_dbgstr_guid(riid));
1058 return E_FAIL;
1061 if (imalloc)
1062 writer = IMalloc_Alloc(imalloc, sizeof(*writer));
1063 else
1064 writer = heap_alloc(sizeof(*writer));
1065 if(!writer) return E_OUTOFMEMORY;
1067 writer->IXmlWriter_iface.lpVtbl = &xmlwriter_vtbl;
1068 writer->ref = 1;
1069 writer->imalloc = imalloc;
1070 if (imalloc) IMalloc_AddRef(imalloc);
1071 writer->output = NULL;
1072 writer->indent = FALSE;
1073 writer->bom = TRUE;
1074 writer->omitxmldecl = FALSE;
1075 writer->conformance = XmlConformanceLevel_Document;
1076 writer->state = XmlWriterState_Initial;
1077 writer->bomwritten = FALSE;
1078 writer->starttagopen = FALSE;
1079 list_init(&writer->elements);
1081 *obj = &writer->IXmlWriter_iface;
1083 TRACE("returning iface %p\n", *obj);
1085 return S_OK;
1088 static HRESULT create_writer(IUnknown *stream, IMalloc *imalloc, xml_encoding encoding,
1089 IXmlWriterOutput **output)
1091 xmlwriteroutput *writeroutput;
1092 HRESULT hr;
1094 *output = NULL;
1096 if (imalloc)
1097 writeroutput = IMalloc_Alloc(imalloc, sizeof(*writeroutput));
1098 else
1099 writeroutput = heap_alloc(sizeof(*writeroutput));
1100 if(!writeroutput) return E_OUTOFMEMORY;
1102 writeroutput->IXmlWriterOutput_iface.lpVtbl = &xmlwriteroutputvtbl;
1103 writeroutput->ref = 1;
1104 writeroutput->imalloc = imalloc;
1105 if (imalloc) IMalloc_AddRef(imalloc);
1106 writeroutput->encoding = encoding;
1107 writeroutput->stream = NULL;
1108 hr = init_output_buffer(writeroutput);
1109 if (FAILED(hr)) {
1110 IUnknown_Release(&writeroutput->IXmlWriterOutput_iface);
1111 return hr;
1114 IUnknown_QueryInterface(stream, &IID_IUnknown, (void**)&writeroutput->output);
1116 *output = &writeroutput->IXmlWriterOutput_iface;
1118 TRACE("returning iface %p\n", *output);
1120 return S_OK;
1123 HRESULT WINAPI CreateXmlWriterOutputWithEncodingName(IUnknown *stream,
1124 IMalloc *imalloc,
1125 LPCWSTR encoding,
1126 IXmlWriterOutput **output)
1128 static const WCHAR utf8W[] = {'U','T','F','-','8',0};
1129 xml_encoding xml_enc;
1131 TRACE("%p %p %s %p\n", stream, imalloc, debugstr_w(encoding), output);
1133 if (!stream || !output) return E_INVALIDARG;
1135 xml_enc = parse_encoding_name(encoding ? encoding : utf8W, -1);
1136 return create_writer(stream, imalloc, xml_enc, output);
1139 HRESULT WINAPI CreateXmlWriterOutputWithEncodingCodePage(IUnknown *stream,
1140 IMalloc *imalloc,
1141 UINT codepage,
1142 IXmlWriterOutput **output)
1144 xml_encoding xml_enc;
1146 TRACE("%p %p %u %p\n", stream, imalloc, codepage, output);
1148 if (!stream || !output) return E_INVALIDARG;
1150 xml_enc = get_encoding_from_codepage(codepage);
1151 return create_writer(stream, imalloc, xml_enc, output);