aboutsummaryrefslogtreecommitdiff
path: root/src/bextutil/bextutil.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/bextutil/bextutil.cpp')
-rw-r--r--src/bextutil/bextutil.cpp179
1 files changed, 179 insertions, 0 deletions
diff --git a/src/bextutil/bextutil.cpp b/src/bextutil/bextutil.cpp
new file mode 100644
index 00000000..baf35591
--- /dev/null
+++ b/src/bextutil/bextutil.cpp
@@ -0,0 +1,179 @@
1// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.
2
3#include "precomp.h"
4
5static IBundleExtensionEngine* vpEngine = NULL;
6
7// prototypes
8
9DAPI_(void) BextInitialize(
10 __in IBundleExtensionEngine* pEngine
11 )
12{
13 pEngine->AddRef();
14
15 ReleaseObject(vpEngine);
16 vpEngine = pEngine;
17}
18
19DAPI_(HRESULT) BextInitializeFromCreateArgs(
20 __in const BUNDLE_EXTENSION_CREATE_ARGS* pArgs,
21 __out_opt IBundleExtensionEngine** ppEngine
22 )
23{
24 HRESULT hr = S_OK;
25 IBundleExtensionEngine* pEngine = NULL;
26
27 hr = BextBundleExtensionEngineCreate(pArgs->pfnBundleExtensionEngineProc, pArgs->pvBundleExtensionEngineProcContext, &pEngine);
28 ExitOnFailure(hr, "Failed to create BextBundleExtensionEngine.");
29
30 BextInitialize(pEngine);
31
32 if (ppEngine)
33 {
34 *ppEngine = pEngine;
35 }
36 pEngine = NULL;
37
38LExit:
39 ReleaseObject(pEngine);
40
41 return hr;
42}
43
44
45DAPI_(void) BextUninitialize()
46{
47 ReleaseNullObject(vpEngine);
48}
49
50DAPI_(HRESULT) BextGetBundleExtensionDataNode(
51 __in IXMLDOMDocument* pixdManifest,
52 __in LPCWSTR wzExtensionId,
53 __out IXMLDOMNode** ppixnBundleExtension
54 )
55{
56 HRESULT hr = S_OK;
57 IXMLDOMElement* pixeBundleExtensionData = NULL;
58 IXMLDOMNodeList* pixnNodes = NULL;
59 IXMLDOMNode* pixnNode = NULL;
60 DWORD cNodes = 0;
61 LPWSTR sczId = NULL;
62
63 // Get BundleExtensionData element.
64 hr = pixdManifest->get_documentElement(&pixeBundleExtensionData);
65 ExitOnFailure(hr, "Failed to get BundleExtensionData element.");
66
67 // Select BundleExtension nodes.
68 hr = XmlSelectNodes(pixeBundleExtensionData, L"BundleExtension", &pixnNodes);
69 ExitOnFailure(hr, "Failed to select BundleExtension nodes.");
70
71 // Get BundleExtension node count.
72 hr = pixnNodes->get_length((long*)&cNodes);
73 ExitOnFailure(hr, "Failed to get BundleExtension node count.");
74
75 if (!cNodes)
76 {
77 ExitFunction();
78 }
79
80 // Find requested extension.
81 for (DWORD i = 0; i < cNodes; ++i)
82 {
83 hr = XmlNextElement(pixnNodes, &pixnNode, NULL);
84 ExitOnFailure(hr, "Failed to get next node.");
85
86 // @Id
87 hr = XmlGetAttributeEx(pixnNode, L"Id", &sczId);
88 ExitOnFailure(hr, "Failed to get @Id.");
89
90 if (CSTR_EQUAL == ::CompareStringW(LOCALE_INVARIANT, 0, sczId, -1, wzExtensionId, -1))
91 {
92 *ppixnBundleExtension = pixnNode;
93 pixnNode = NULL;
94
95 ExitFunction1(hr = S_OK);
96 }
97
98 // Prepare next iteration.
99 ReleaseNullObject(pixnNode);
100 }
101
102 hr = E_NOTFOUND;
103
104LExit:
105 ReleaseStr(sczId);
106 ReleaseObject(pixnNode);
107 ReleaseObject(pixnNodes);
108 ReleaseObject(pixeBundleExtensionData);
109
110 return hr;
111}
112
113
114DAPIV_(HRESULT) BextLog(
115 __in BUNDLE_EXTENSION_LOG_LEVEL level,
116 __in_z __format_string LPCSTR szFormat,
117 ...
118 )
119{
120 HRESULT hr = S_OK;
121 va_list args;
122 LPSTR sczFormattedAnsi = NULL;
123 LPWSTR sczMessage = NULL;
124
125 if (!vpEngine)
126 {
127 hr = E_POINTER;
128 ExitOnRootFailure(hr, "BextInitialize() must be called first.");
129 }
130
131 va_start(args, szFormat);
132 hr = StrAnsiAllocFormattedArgs(&sczFormattedAnsi, szFormat, args);
133 va_end(args);
134 ExitOnFailure(hr, "Failed to format log string.");
135
136 hr = StrAllocStringAnsi(&sczMessage, sczFormattedAnsi, 0, CP_UTF8);
137 ExitOnFailure(hr, "Failed to convert log string to Unicode.");
138
139 hr = vpEngine->Log(level, sczMessage);
140
141LExit:
142 ReleaseStr(sczMessage);
143 ReleaseStr(sczFormattedAnsi);
144 return hr;
145}
146
147
148DAPIV_(HRESULT) BextLogError(
149 __in HRESULT hrError,
150 __in_z __format_string LPCSTR szFormat,
151 ...
152 )
153{
154 HRESULT hr = S_OK;
155 va_list args;
156 LPSTR sczFormattedAnsi = NULL;
157 LPWSTR sczMessage = NULL;
158
159 if (!vpEngine)
160 {
161 hr = E_POINTER;
162 ExitOnRootFailure(hr, "BextInitialize() must be called first.");
163 }
164
165 va_start(args, szFormat);
166 hr = StrAnsiAllocFormattedArgs(&sczFormattedAnsi, szFormat, args);
167 va_end(args);
168 ExitOnFailure(hr, "Failed to format error log string.");
169
170 hr = StrAllocFormatted(&sczMessage, L"Error 0x%08x: %S", hrError, sczFormattedAnsi);
171 ExitOnFailure(hr, "Failed to prepend error number to error log string.");
172
173 hr = vpEngine->Log(BUNDLE_EXTENSION_LOG_LEVEL_ERROR, sczMessage);
174
175LExit:
176 ReleaseStr(sczMessage);
177 ReleaseStr(sczFormattedAnsi);
178 return hr;
179}