File: xml2_api.cpp

package info (click to toggle)
intel-compute-runtime 20.44.18297-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 34,780 kB
  • sloc: cpp: 379,729; lisp: 4,931; python: 299; sh: 196; makefile: 8
file content (176 lines) | stat: -rw-r--r-- 6,425 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Copyright (C) 2020 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "level_zero/tools/source/sysman/linux/xml_parser/xml2_api.h"

namespace L0 {

const std::string Xml2Api::xml2LibraryFile = "libxml2.so.2";
const std::string Xml2Api::xmlInitParserRoutine = "xmlInitParser";
const std::string Xml2Api::xmlCleanupParserRoutine = "xmlCleanupParser";
const std::string Xml2Api::xmlReadFileRoutine = "xmlReadFile";
const std::string Xml2Api::xmlReadMemoryRoutine = "xmlReadMemory";
const std::string Xml2Api::xmlFreeDocRoutine = "xmlFreeDoc";
const std::string Xml2Api::xmlXPathNewContextRoutine = "xmlXPathNewContext";
const std::string Xml2Api::xmlXPathFreeContextRoutine = "xmlXPathFreeContext";
const std::string Xml2Api::xmlXPathEvalRoutine = "xmlXPathEval";
const std::string Xml2Api::xmlXPathNodeEvalRoutine = "xmlXPathNodeEval";
const std::string Xml2Api::xmlXPathCastNodeToStringRoutine = "xmlXPathCastNodeToString";
const std::string Xml2Api::xmlXPathFreeObjectRoutine = "xmlXPathFreeObject";
const std::string Xml2Api::xmlGetNodePathRoutine = "xmlGetNodePath";
const std::string Xml2Api::xmlGetPropRoutine = "xmlGetProp";
const std::string Xml2Api::xmlMemGetRoutine = "xmlMemGet";

template <class T>
bool Xml2Api::getProcAddr(const std::string name, T &proc) {
    void *addr;
    addr = libraryHandle->getProcAddress(name);
    proc = reinterpret_cast<T>(addr);
    return nullptr != proc;
}

bool Xml2Api::loadEntryPoints() {
    bool ok = true;
    ok = ok && getProcAddr(xmlInitParserRoutine, xmlInitParserEntry);
    ok = ok && getProcAddr(xmlCleanupParserRoutine, xmlCleanupParserEntry);
    ok = ok && getProcAddr(xmlReadFileRoutine, xmlReadFileEntry);
    ok = ok && getProcAddr(xmlReadMemoryRoutine, xmlReadMemoryEntry);
    ok = ok && getProcAddr(xmlFreeDocRoutine, xmlFreeDocEntry);
    ok = ok && getProcAddr(xmlXPathNewContextRoutine, xmlXPathNewContextEntry);
    ok = ok && getProcAddr(xmlXPathFreeContextRoutine, xmlXPathFreeContextEntry);
    ok = ok && getProcAddr(xmlXPathEvalRoutine, xmlXPathEvalEntry);
    ok = ok && getProcAddr(xmlXPathNodeEvalRoutine, xmlXPathNodeEvalEntry);
    ok = ok && getProcAddr(xmlXPathCastNodeToStringRoutine, xmlXPathCastNodeToStringEntry);
    ok = ok && getProcAddr(xmlXPathFreeObjectRoutine, xmlXPathFreeObjectEntry);
    ok = ok && getProcAddr(xmlGetNodePathRoutine, xmlGetNodePathEntry);
    ok = ok && getProcAddr(xmlGetPropRoutine, xmlGetPropEntry);

    // Can't just get the symbol for xmlFree. Use xmlMemGet to find the xmlFree routine the library is using
    pXmlMemGet xmlMemGetEntry;
    ok = ok && getProcAddr(xmlMemGetRoutine, xmlMemGetEntry);
    ok = ok && !(*xmlMemGetEntry)(&xmlFreeEntry, nullptr, nullptr, nullptr);
    return ok;
}

void Xml2Api::xmlInitParser() {
    UNRECOVERABLE_IF(nullptr == xmlInitParserEntry);
    (*xmlInitParserEntry)();
    return;
}

void Xml2Api::xmlCleanupParser() {
    UNRECOVERABLE_IF(nullptr == xmlCleanupParserEntry);
    (*xmlCleanupParserEntry)();
    return;
}

xmlDocPtr Xml2Api::xmlReadFile(const std::string filename, int options) {
    UNRECOVERABLE_IF(nullptr == xmlReadFileEntry);
    return (*xmlReadFileEntry)(filename.c_str(), NULL, options);
}

xmlDocPtr Xml2Api::xmlReadMemory(const std::string buffer, int options) {
    UNRECOVERABLE_IF(nullptr == xmlReadMemoryEntry);
    const char *cbuf = buffer.c_str();
    int size = static_cast<int>(buffer.length());
    return (*xmlReadMemoryEntry)(cbuf, size, NULL, NULL, options);
}

void Xml2Api::xmlFreeDoc(xmlDocPtr doc) {
    UNRECOVERABLE_IF(nullptr == xmlFreeDocEntry);
    (*xmlFreeDocEntry)(doc);
    return;
}

xmlXPathContextPtr Xml2Api::xmlXPathNewContext(xmlDocPtr doc) {
    UNRECOVERABLE_IF(nullptr == xmlXPathNewContextEntry);
    return (*xmlXPathNewContextEntry)(doc);
}

void Xml2Api::xmlXPathFreeContext(xmlXPathContextPtr cntx) {
    UNRECOVERABLE_IF(nullptr == xmlXPathFreeContextEntry);
    (*xmlXPathFreeContextEntry)(cntx);
    return;
}

xmlXPathObjectPtr Xml2Api::xmlXPathEval(const std::string expr, xmlXPathContextPtr cntx) {
    UNRECOVERABLE_IF(nullptr == xmlXPathEvalEntry);
    return (*xmlXPathEvalEntry)(reinterpret_cast<const xmlChar *>(expr.c_str()), cntx);
}

xmlXPathObjectPtr Xml2Api::xmlXPathNodeEval(xmlNodePtr node, const std::string expr, xmlXPathContextPtr cntx) {
    UNRECOVERABLE_IF(nullptr == xmlXPathNodeEvalEntry);
    return (*xmlXPathNodeEvalEntry)(node, reinterpret_cast<const xmlChar *>(expr.c_str()), cntx);
}

std::string Xml2Api::xmlXPathCastNodeToString(xmlNodePtr node) {
    UNRECOVERABLE_IF(nullptr == xmlXPathCastNodeToStringEntry);
    xmlChar *pXmlChar = (*xmlXPathCastNodeToStringEntry)(node);
    if (nullptr == pXmlChar) {
        return std::string();
    } else {
        std::string str(reinterpret_cast<char *>(pXmlChar));
        this->xmlFree(pXmlChar);
        return str;
    }
}

void Xml2Api::xmlXPathFreeObject(xmlXPathObjectPtr obj) {
    UNRECOVERABLE_IF(nullptr == xmlXPathFreeObjectEntry);
    (*xmlXPathFreeObjectEntry)(obj);
    return;
}

std::string Xml2Api::xmlGetNodePath(const xmlNodePtr node) {
    UNRECOVERABLE_IF(nullptr == xmlGetNodePathEntry);
    xmlChar *pXmlChar = (*xmlGetNodePathEntry)(node);
    if (nullptr == pXmlChar) {
        return std::string();
    } else {
        std::string str(reinterpret_cast<const char *>(pXmlChar));
        this->xmlFree(pXmlChar);
        return str;
    }
}

std::string Xml2Api::xmlGetProp(const xmlNodePtr node, std::string prop) {
    UNRECOVERABLE_IF(nullptr == xmlGetPropEntry);
    xmlChar *pXmlChar = (*xmlGetPropEntry)(node, reinterpret_cast<const xmlChar *>(prop.c_str()));
    if (nullptr == pXmlChar) {
        return std::string();
    } else {
        std::string str(reinterpret_cast<char *>(pXmlChar));
        this->xmlFree(pXmlChar);
        return str;
    }
}

void Xml2Api::xmlFree(void *obj) {
    UNRECOVERABLE_IF(nullptr == xmlFreeEntry);
    (*xmlFreeEntry)(obj);
    return;
}

Xml2Api *Xml2Api::create() {
    Xml2Api *pXml2Api = new Xml2Api;
    UNRECOVERABLE_IF(nullptr == pXml2Api);

    pXml2Api->libraryHandle = NEO::OsLibrary::load(xml2LibraryFile);
    if (pXml2Api->libraryHandle == nullptr || !pXml2Api->loadEntryPoints()) {
        delete pXml2Api;
        return nullptr;
    }
    return pXml2Api;
}

Xml2Api::~Xml2Api() {
    if (nullptr != libraryHandle) {
        delete libraryHandle;
        libraryHandle = nullptr;
    }
}
} // namespace L0