Skip to content

Instantly share code, notes, and snippets.

@odzhan
Last active October 18, 2020 03:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save odzhan/3ed55e3a9cbcdc3e2fa84073fc1adf4c to your computer and use it in GitHub Desktop.
Save odzhan/3ed55e3a9cbcdc3e2fa84073fc1adf4c to your computer and use it in GitHub Desktop.
Loading XSL from memory.
/**
BSD 3-Clause License
Copyright (c) 2019, TheWover, Odzhan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <windows.h>
#include <msxml2.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <inttypes.h>
#include <fcntl.h>
#include <io.h>
#pragma comment (lib, "msxml2.lib")
#pragma comment (lib, "ole32.lib")
// read and convert the script into unicode format
// return pointer to the script in memory
PWCHAR read_script(const char *path) {
int in, len, i;
struct stat fs;
PWCHAR mem;
uint8_t c;
// Script is inaccessibe? exit
if(stat(path, &fs) != 0) return NULL;
// Zero file size? exit
if(fs.st_size == 0) return NULL;
// Open script for reading
in = open(path, O_RDONLY);
if(in == 0) return NULL;
// allocate memory for writing
mem = (PWCHAR)calloc(1, (fs.st_size + 1) * 2);
if(mem != NULL) {
// convert input to unicode
for(i=0;;i++) {
len = read(in, &c, 1);
if(len == 0) break;
mem[i] = c;
}
}
close(in);
return mem;
}
void run_xml_script(const char *path) {
IXMLDOMDocument *pDoc;
IXMLDOMNode *pNode;
HRESULT hr;
PWCHAR xml_str;
VARIANT_BOOL loaded;
BSTR res;
xml_str = read_script(path);
if(xml_str == NULL) return;
// 1. Initialize COM
hr = CoInitialize(NULL);
if(hr == S_OK) {
// 2. Instantiate XMLDOMDocument object
hr = CoCreateInstance(
&CLSID_DOMDocument30,
NULL, CLSCTX_INPROC_SERVER,
&IID_IXMLDOMDocument,
(void**)&pDoc);
if(hr == S_OK) {
// 3. load XML file
hr = pDoc->lpVtbl->loadXML(pDoc, xml_str, &loaded);
if(hr == S_OK) {
// 4. create node interface
hr = pDoc->lpVtbl->QueryInterface(
pDoc, &IID_IXMLDOMNode, (void **)&pNode);
if(hr == S_OK) {
// 5. execute script
hr = pDoc->lpVtbl->transformNode(pDoc, pNode, &res);
pNode->lpVtbl->Release(pNode);
}
}
pDoc->lpVtbl->Release(pDoc);
}
CoUninitialize();
}
free(xml_str);
}
int main(int argc, char *argv[])
{
if(argc != 2) {
printf("usage: load_xml <file.xml>\n");
return 0;
}
run_xml_script(argv[1]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment