wined3d: Get rid of wined3d_buffer_get_desc().
[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     char* buf = NULL;
66     int len = 32, needed;
67     enum __wine_debug_class dbcl = __WINE_DBCL_ERR;
68     switch (lvl)
69     {
70         case XML_ERR_NONE:
71             dbcl = __WINE_DBCL_TRACE;
72             break;
73         case XML_ERR_WARNING:
74             dbcl = __WINE_DBCL_WARN;
75             break;
76         default:
77             break;
78     }
79
80     do
81     {
82         heap_free(buf);
83         buf = heap_alloc(len);
84         needed = vsnprintf(buf, len, msg, ap);
85         if (needed == -1)
86             len *= 2;
87         else if (needed >= len)
88             len = needed + 1;
89         else
90             needed = 0;
91     }
92     while (needed);
93
94     wine_dbg_log(dbcl, &__wine_dbch_msxml, caller, "%s", buf);
95     heap_free(buf);
96 }
97
98 void wineXmlCallbackError(char const* caller, xmlErrorPtr err)
99 {
100     enum __wine_debug_class dbcl;
101
102     switch (err->level)
103     {
104     case XML_ERR_NONE:    dbcl = __WINE_DBCL_TRACE; break;
105     case XML_ERR_WARNING: dbcl = __WINE_DBCL_WARN; break;
106     default:              dbcl = __WINE_DBCL_ERR; break;
107     }
108     wine_dbg_log(dbcl, &__wine_dbch_msxml, caller, "%s", err->message);
109 }
110
111 /* Support for loading xml files from a Wine Windows drive */
112 static int wineXmlMatchCallback (char const * filename)
113 {
114     int nRet = 0;
115
116     TRACE("%s\n", filename);
117
118     /*
119      * We will deal with loading XML files from the file system
120      *   We only care about files that linux cannot find.
121      *    e.g. C:,D: etc
122      */
123     if(isalpha(filename[0]) && filename[1] == ':')
124         nRet = 1;
125
126     return nRet;
127 }
128
129 static void *wineXmlOpenCallback (char const * filename)
130 {
131     BSTR sFilename = bstr_from_xmlChar( (const xmlChar*)filename);
132     HANDLE hFile;
133
134     TRACE("%s\n", debugstr_w(sFilename));
135
136     hFile = CreateFileW(sFilename, GENERIC_READ,FILE_SHARE_READ, NULL,
137                        OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
138     if(hFile == INVALID_HANDLE_VALUE) hFile = 0;
139     SysFreeString(sFilename);
140     return hFile;
141 }
142
143 static int wineXmlReadCallback(void * context, char * buffer, int len)
144 {
145     DWORD dwBytesRead;
146
147     TRACE("%p %s %d\n", context, buffer, len);
148
149     if ((context == NULL) || (buffer == NULL))
150         return(-1);
151
152     if(!ReadFile( context, buffer,len, &dwBytesRead, NULL))
153     {
154         ERR("Failed to read file\n");
155         return -1;
156     }
157
158     TRACE("Read %d\n", dwBytesRead);
159
160     return dwBytesRead;
161 }
162
163 static int wineXmlFileCloseCallback (void * context)
164 {
165     return CloseHandle(context) ? 0 : -1;
166 }
167
168 #endif
169
170
171 HRESULT WINAPI DllCanUnloadNow(void)
172 {
173     return S_FALSE;
174 }
175
176
177 void* libxslt_handle = NULL;
178 #ifdef SONAME_LIBXSLT
179 # define DECL_FUNCPTR(f) typeof(f) * p##f = NULL
180 DECL_FUNCPTR(xsltApplyStylesheet);
181 DECL_FUNCPTR(xsltCleanupGlobals);
182 DECL_FUNCPTR(xsltFreeStylesheet);
183 DECL_FUNCPTR(xsltParseStylesheetDoc);
184 # undef DECL_FUNCPTR
185 #endif
186
187 static void init_libxslt(void)
188 {
189 #ifdef SONAME_LIBXSLT
190     void (*pxsltInit)(void); /* Missing in libxslt <= 1.1.14 */
191
192     libxslt_handle = wine_dlopen(SONAME_LIBXSLT, RTLD_NOW, NULL, 0);
193     if (!libxslt_handle)
194         return;
195
196 #define LOAD_FUNCPTR(f, needed) if ((p##f = wine_dlsym(libxslt_handle, #f, NULL, 0)) == NULL && needed) { WARN("Can't find symbol %s\n", #f); goto sym_not_found; }
197     LOAD_FUNCPTR(xsltInit, 0);
198     LOAD_FUNCPTR(xsltApplyStylesheet, 1);
199     LOAD_FUNCPTR(xsltCleanupGlobals, 1);
200     LOAD_FUNCPTR(xsltFreeStylesheet, 1);
201     LOAD_FUNCPTR(xsltParseStylesheetDoc, 1);
202 #undef LOAD_FUNCPTR
203
204     if (pxsltInit)
205         pxsltInit();
206     return;
207
208  sym_not_found:
209     wine_dlclose(libxslt_handle, NULL, 0);
210     libxslt_handle = NULL;
211 #endif
212 }
213
214
215 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
216 {
217     MSXML_hInstance = hInstDLL;
218
219     switch(fdwReason)
220     {
221     case DLL_PROCESS_ATTACH:
222 #ifdef HAVE_LIBXML2
223         xmlInitParser();
224
225         /* Set the default indent character to a single tab,
226            for this thread and as default for new threads */
227         xmlTreeIndentString = "\t";
228         xmlThrDefTreeIndentString("\t");
229
230          /* Register callbacks for loading XML files */
231         if(xmlRegisterInputCallbacks(wineXmlMatchCallback, wineXmlOpenCallback,
232                             wineXmlReadCallback, wineXmlFileCloseCallback) == -1)
233             WARN("Failed to register callbacks\n");
234
235         schemasInit();
236 #endif
237         init_libxslt();
238         DisableThreadLibraryCalls(hInstDLL);
239         break;
240     case DLL_PROCESS_DETACH:
241 #ifdef SONAME_LIBXSLT
242         if (libxslt_handle)
243         {
244             pxsltCleanupGlobals();
245             wine_dlclose(libxslt_handle, NULL, 0);
246             libxslt_handle = NULL;
247         }
248 #endif
249 #ifdef HAVE_LIBXML2
250         /* Restore default Callbacks */
251         xmlCleanupInputCallbacks();
252         xmlRegisterDefaultInputCallbacks();
253
254         xmlCleanupParser();
255         schemasCleanup();
256 #endif
257         release_typelib();
258         break;
259     }
260     return TRUE;
261 }
262
263 const char *debugstr_variant(const VARIANT *v)
264 {
265     if(!v)
266         return "(null)";
267
268     switch(V_VT(v)) {
269     case VT_EMPTY:
270         return "{VT_EMPTY}";
271     case VT_NULL:
272         return "{VT_NULL}";
273     case VT_I4:
274         return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v));
275     case VT_R8:
276         return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v));
277     case VT_BSTR:
278         return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v)));
279     case VT_DISPATCH:
280         return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v));
281     case VT_BOOL:
282         return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v));
283     case VT_UNKNOWN:
284         return wine_dbg_sprintf("{VT_UNKNOWN: %p}", V_UNKNOWN(v));
285     case VT_UINT:
286         return wine_dbg_sprintf("{VT_UINT: %u}", V_UINT(v));
287     default:
288         return wine_dbg_sprintf("{vt %d}", V_VT(v));
289     }
290 }
291
292 /***********************************************************************
293  *              DllRegisterServer (MSXML3.@)
294  */
295 HRESULT WINAPI DllRegisterServer(void)
296 {
297     return __wine_register_resources( MSXML_hInstance, NULL );
298 }
299
300 /***********************************************************************
301  *              DllUnregisterServer (MSXML3.@)
302  */
303 HRESULT WINAPI DllUnregisterServer(void)
304 {
305     return __wine_unregister_resources( MSXML_hInstance, NULL );
306 }