jscript: Store concatenated strings as a rope string to avoid useless copying.
[wine] / dlls / msxml3 / main.c
1 /*
2  *    MSXML Class Factory
3  *
4  * Copyright 2002 Lionel Ulmer
5  * Copyright 2005 Mike McCormack
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #define COBJMACROS
26
27 #include <stdarg.h>
28 #ifdef HAVE_LIBXML2
29 # include <libxml/parser.h>
30 # include <libxml/xmlerror.h>
31 # ifdef SONAME_LIBXSLT
32 #  ifdef HAVE_LIBXSLT_PATTERN_H
33 #   include <libxslt/pattern.h>
34 #  endif
35 #  ifdef HAVE_LIBXSLT_TRANSFORM_H
36 #   include <libxslt/transform.h>
37 #  endif
38 #  include <libxslt/xsltutils.h>
39 #  include <libxslt/xsltInternals.h>
40 # endif
41 #endif
42
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winuser.h"
46 #include "ole2.h"
47 #include "rpcproxy.h"
48 #include "msxml.h"
49 #include "msxml6.h"
50
51 #include "wine/unicode.h"
52 #include "wine/debug.h"
53 #include "wine/library.h"
54
55 #include "msxml_private.h"
56
57 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
58
59 HINSTANCE MSXML_hInstance = NULL;
60
61 #ifdef HAVE_LIBXML2
62
63 void wineXmlCallbackLog(char const* caller, xmlErrorLevel lvl, char const* msg, va_list ap)
64 {
65     static const int max_size = 200;
66     enum __wine_debug_class dbcl;
67     char buff[max_size];
68     int len;
69
70     switch (lvl)
71     {
72         case XML_ERR_NONE:
73             dbcl = __WINE_DBCL_TRACE;
74             break;
75         case XML_ERR_WARNING:
76             dbcl = __WINE_DBCL_WARN;
77             break;
78         default:
79             dbcl = __WINE_DBCL_ERR;
80             break;
81     }
82
83     len = vsnprintf(buff, max_size, msg, ap);
84     if (len == -1 || len >= max_size) buff[max_size-1] = 0;
85
86     wine_dbg_log(dbcl, &__wine_dbch_msxml, caller, "%s", buff);
87 }
88
89 void wineXmlCallbackError(char const* caller, xmlErrorPtr err)
90 {
91     enum __wine_debug_class dbcl;
92
93     switch (err->level)
94     {
95     case XML_ERR_NONE:    dbcl = __WINE_DBCL_TRACE; break;
96     case XML_ERR_WARNING: dbcl = __WINE_DBCL_WARN; break;
97     default:              dbcl = __WINE_DBCL_ERR; break;
98     }
99
100     wine_dbg_log(dbcl, &__wine_dbch_msxml, caller, "error code %d", err->code);
101     if (err->message)
102         wine_dbg_log(dbcl, &__wine_dbch_msxml, caller, ": %s", err->message);
103     else
104         wine_dbg_log(dbcl, &__wine_dbch_msxml, caller, "\n");
105 }
106
107 /* Support for loading xml files from a Wine Windows drive */
108 static int wineXmlMatchCallback (char const * filename)
109 {
110     int nRet = 0;
111
112     TRACE("%s\n", filename);
113
114     /*
115      * We will deal with loading XML files from the file system
116      *   We only care about files that linux cannot find.
117      *    e.g. C:,D: etc
118      */
119     if(isalpha(filename[0]) && filename[1] == ':')
120         nRet = 1;
121
122     return nRet;
123 }
124
125 static void *wineXmlOpenCallback (char const * filename)
126 {
127     BSTR sFilename = bstr_from_xmlChar( (const xmlChar*)filename);
128     HANDLE hFile;
129
130     TRACE("%s\n", debugstr_w(sFilename));
131
132     hFile = CreateFileW(sFilename, GENERIC_READ,FILE_SHARE_READ, NULL,
133                        OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
134     if(hFile == INVALID_HANDLE_VALUE) hFile = 0;
135     SysFreeString(sFilename);
136     return hFile;
137 }
138
139 static int wineXmlReadCallback(void * context, char * buffer, int len)
140 {
141     DWORD dwBytesRead;
142
143     TRACE("%p %s %d\n", context, buffer, len);
144
145     if ((context == NULL) || (buffer == NULL))
146         return(-1);
147
148     if(!ReadFile( context, buffer,len, &dwBytesRead, NULL))
149     {
150         ERR("Failed to read file\n");
151         return -1;
152     }
153
154     TRACE("Read %d\n", dwBytesRead);
155
156     return dwBytesRead;
157 }
158
159 static int wineXmlFileCloseCallback (void * context)
160 {
161     return CloseHandle(context) ? 0 : -1;
162 }
163
164 #endif
165
166
167 HRESULT WINAPI DllCanUnloadNow(void)
168 {
169     return S_FALSE;
170 }
171
172
173 void* libxslt_handle = NULL;
174 #ifdef SONAME_LIBXSLT
175 # define DECL_FUNCPTR(f) typeof(f) * p##f = NULL
176 DECL_FUNCPTR(xsltApplyStylesheet);
177 DECL_FUNCPTR(xsltCleanupGlobals);
178 DECL_FUNCPTR(xsltFreeStylesheet);
179 DECL_FUNCPTR(xsltParseStylesheetDoc);
180 # undef DECL_FUNCPTR
181 #endif
182
183 static void init_libxslt(void)
184 {
185 #ifdef SONAME_LIBXSLT
186     void (*pxsltInit)(void); /* Missing in libxslt <= 1.1.14 */
187
188     libxslt_handle = wine_dlopen(SONAME_LIBXSLT, RTLD_NOW, NULL, 0);
189     if (!libxslt_handle)
190         return;
191
192 #define LOAD_FUNCPTR(f, needed) \
193     if ((p##f = wine_dlsym(libxslt_handle, #f, NULL, 0)) == NULL) \
194         if (needed) { WARN("Can't find symbol %s\n", #f); goto sym_not_found; }
195     LOAD_FUNCPTR(xsltInit, 0);
196     LOAD_FUNCPTR(xsltApplyStylesheet, 1);
197     LOAD_FUNCPTR(xsltCleanupGlobals, 1);
198     LOAD_FUNCPTR(xsltFreeStylesheet, 1);
199     LOAD_FUNCPTR(xsltParseStylesheetDoc, 1);
200 #undef LOAD_FUNCPTR
201
202     if (pxsltInit)
203         pxsltInit();
204     return;
205
206  sym_not_found:
207     wine_dlclose(libxslt_handle, NULL, 0);
208     libxslt_handle = NULL;
209 #endif
210 }
211
212 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
213 {
214     MSXML_hInstance = hInstDLL;
215
216     switch(fdwReason)
217     {
218     case DLL_PROCESS_ATTACH:
219 #ifdef HAVE_LIBXML2
220         xmlInitParser();
221
222         /* Set the default indent character to a single tab,
223            for this thread and as default for new threads */
224         xmlTreeIndentString = "\t";
225         xmlThrDefTreeIndentString("\t");
226
227          /* Register callbacks for loading XML files */
228         if(xmlRegisterInputCallbacks(wineXmlMatchCallback, wineXmlOpenCallback,
229                             wineXmlReadCallback, wineXmlFileCloseCallback) == -1)
230             WARN("Failed to register callbacks\n");
231
232         schemasInit();
233 #endif
234         init_libxslt();
235         DisableThreadLibraryCalls(hInstDLL);
236         break;
237     case DLL_PROCESS_DETACH:
238 #ifdef SONAME_LIBXSLT
239         if (libxslt_handle)
240         {
241             pxsltCleanupGlobals();
242             wine_dlclose(libxslt_handle, NULL, 0);
243             libxslt_handle = NULL;
244         }
245 #endif
246 #ifdef HAVE_LIBXML2
247         /* Restore default Callbacks */
248         xmlCleanupInputCallbacks();
249         xmlRegisterDefaultInputCallbacks();
250
251         xmlCleanupParser();
252         schemasCleanup();
253 #endif
254         release_typelib();
255         break;
256     }
257     return TRUE;
258 }
259
260 const char *debugstr_variant(const VARIANT *v)
261 {
262     if(!v)
263         return "(null)";
264
265     switch(V_VT(v)) {
266     case VT_EMPTY:
267         return "{VT_EMPTY}";
268     case VT_NULL:
269         return "{VT_NULL}";
270     case VT_I1:
271         return wine_dbg_sprintf("{VT_I1: %d}", V_I1(v));
272     case VT_I2:
273         return wine_dbg_sprintf("{VT_I2: %d}", V_I2(v));
274     case VT_I4:
275         return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v));
276     case VT_INT:
277         return wine_dbg_sprintf("{VT_INT: %d}", V_INT(v));
278     case VT_R8:
279         return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v));
280     case VT_BSTR:
281         return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v)));
282     case VT_DISPATCH:
283         return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v));
284     case VT_BOOL:
285         return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v));
286     case VT_UNKNOWN:
287         return wine_dbg_sprintf("{VT_UNKNOWN: %p}", V_UNKNOWN(v));
288     case VT_UINT:
289         return wine_dbg_sprintf("{VT_UINT: %u}", V_UINT(v));
290     case VT_BSTR|VT_BYREF:
291         return wine_dbg_sprintf("{VT_BSTR|VT_BYREF: ptr %p, data %s}",
292             V_BSTRREF(v), debugstr_w(V_BSTRREF(v) ? *V_BSTRREF(v) : NULL));
293     case VT_ERROR:
294         return wine_dbg_sprintf("{VT_ERROR: 0x%08x}", V_ERROR(v));
295     case VT_VARIANT|VT_BYREF:
296         return wine_dbg_sprintf("{VT_VARIANT|VT_BYREF: %s}", debugstr_variant(V_VARIANTREF(v)));
297     case VT_UI1|VT_ARRAY:
298         return "{VT_UI1|VT_ARRAY}";
299     default:
300         return wine_dbg_sprintf("{vt %d}", V_VT(v));
301     }
302 }
303
304 /***********************************************************************
305  *              DllRegisterServer (MSXML3.@)
306  */
307 HRESULT WINAPI DllRegisterServer(void)
308 {
309     return __wine_register_resources( MSXML_hInstance );
310 }
311
312 /***********************************************************************
313  *              DllUnregisterServer (MSXML3.@)
314  */
315 HRESULT WINAPI DllUnregisterServer(void)
316 {
317     return __wine_unregister_resources( MSXML_hInstance );
318 }