Sync with trunk head (part 1 of 2)
[reactos.git] / dll / win32 / 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 #include "windef.h"
29 #include "winbase.h"
30 #include "winuser.h"
31 #include "ole2.h"
32 #include "msxml.h"
33 #include "msxml2.h"
34
35 #include "wine/unicode.h"
36 #include "wine/debug.h"
37 #include "wine/library.h"
38
39 #include "msxml_private.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msxml);
42
43 #ifdef HAVE_LIBXML2
44
45 /* Support for loading xml files from a Wine Windows drive */
46 static int wineXmlMatchCallback (char const * filename)
47 {
48 int nRet = 0;
49
50 TRACE("%s\n", filename);
51
52 /*
53 * We will deal with loading XML files from the file system
54 * We only care about files that linux cannot find.
55 * e.g. C:,D: etc
56 */
57 if(isalpha(filename[0]) && filename[1] == ':')
58 nRet = 1;
59
60 return nRet;
61 }
62
63 static void *wineXmlOpenCallback (char const * filename)
64 {
65 BSTR sFilename = bstr_from_xmlChar( (const xmlChar*)filename);
66 HANDLE hFile;
67
68 TRACE("%s\n", debugstr_w(sFilename));
69
70 hFile = CreateFileW(sFilename, GENERIC_READ,FILE_SHARE_READ, NULL,
71 OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL, NULL);
72 if(hFile == INVALID_HANDLE_VALUE) hFile = 0;
73 SysFreeString(sFilename);
74 return hFile;
75 }
76
77 static int wineXmlReadCallback(void * context, char * buffer, int len)
78 {
79 DWORD dwBytesRead;
80
81 TRACE("%p %s %d\n", context, buffer, len);
82
83 if ((context == NULL) || (buffer == NULL))
84 return(-1);
85
86 if(!ReadFile( context, buffer,len, &dwBytesRead, NULL))
87 {
88 ERR("Failed to read file\n");
89 return -1;
90 }
91
92 TRACE("Read %d\n", dwBytesRead);
93
94 return dwBytesRead;
95 }
96
97 static int wineXmlFileCloseCallback (void * context)
98 {
99 return CloseHandle(context) ? 0 : -1;
100 }
101
102 #endif
103
104
105 HRESULT WINAPI DllCanUnloadNow(void)
106 {
107 return S_FALSE;
108 }
109
110
111 void* libxslt_handle = NULL;
112 #ifdef SONAME_LIBXSLT
113 # define DECL_FUNCPTR(f) typeof(f) * p##f = NULL
114 DECL_FUNCPTR(xsltApplyStylesheet);
115 DECL_FUNCPTR(xsltCleanupGlobals);
116 DECL_FUNCPTR(xsltFreeStylesheet);
117 DECL_FUNCPTR(xsltParseStylesheetDoc);
118 # undef MAKE_FUNCPTR
119 #endif
120
121 static void init_libxslt(void)
122 {
123 #ifdef SONAME_LIBXSLT
124 void (*pxsltInit)(void); /* Missing in libxslt <= 1.1.14 */
125
126 libxslt_handle = wine_dlopen(SONAME_LIBXSLT, RTLD_NOW, NULL, 0);
127 if (!libxslt_handle)
128 return;
129
130 #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; }
131 LOAD_FUNCPTR(xsltInit, 0);
132 LOAD_FUNCPTR(xsltApplyStylesheet, 1);
133 LOAD_FUNCPTR(xsltCleanupGlobals, 1);
134 LOAD_FUNCPTR(xsltFreeStylesheet, 1);
135 LOAD_FUNCPTR(xsltParseStylesheetDoc, 1);
136 #undef LOAD_FUNCPTR
137
138 if (pxsltInit)
139 pxsltInit();
140 return;
141
142 sym_not_found:
143 wine_dlclose(libxslt_handle, NULL, 0);
144 libxslt_handle = NULL;
145 #endif
146 }
147
148
149 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
150 {
151 switch(fdwReason)
152 {
153 case DLL_PROCESS_ATTACH:
154 #ifdef HAVE_LIBXML2
155 xmlInitParser();
156
157 /* Set the default indent character to a single tab,
158 for this thread and as default for new threads */
159 xmlTreeIndentString = "\t";
160 xmlThrDefTreeIndentString("\t");
161
162 /* Register callbacks for loading XML files */
163 if(xmlRegisterInputCallbacks(wineXmlMatchCallback, wineXmlOpenCallback,
164 wineXmlReadCallback, wineXmlFileCloseCallback) == -1)
165 WARN("Failed to register callbacks\n");
166
167 #endif
168 init_libxslt();
169 DisableThreadLibraryCalls(hInstDLL);
170 break;
171 case DLL_PROCESS_DETACH:
172 #ifdef SONAME_LIBXSLT
173 if (libxslt_handle)
174 {
175 pxsltCleanupGlobals();
176 wine_dlclose(libxslt_handle, NULL, 0);
177 libxslt_handle = NULL;
178 }
179 #endif
180 #ifdef HAVE_LIBXML2
181 /* Restore default Callbacks */
182 xmlCleanupInputCallbacks();
183 xmlRegisterDefaultInputCallbacks();
184
185 xmlCleanupParser();
186 #endif
187 release_typelib();
188 break;
189 }
190 return TRUE;
191 }