1 mike 1.1 /*
2 **==============================================================================
3 **
4 ** Open Management Infrastructure (OMI)
5 **
6 ** Copyright (c) Microsoft Corporation
7 **
8 ** Licensed under the Apache License, Version 2.0 (the "License"); you may not
9 ** use this file except in compliance with the License. You may obtain a copy
10 ** of the License at
11 **
12 ** http://www.apache.org/licenses/LICENSE-2.0
13 **
14 ** THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 ** KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 ** WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 ** MERCHANTABLITY OR NON-INFRINGEMENT.
18 **
19 ** See the Apache 2 License for the specific language governing permissions
20 ** and limitations under the License.
21 **
22 mike 1.1 **==============================================================================
23 */
24
25 #include "Parser.h"
26
27 extern "C" void errorCallback(
28 const char* msg,
29 const wchar_t* wmsg,
30 void* data)
31 {
32 MI_UNUSED(data);
33
34 #if defined(_MSC_VER)
35 if (wmsg)
36 {
37 fwprintf(stderr, L"%s\n", wmsg);
38 }
39 else
40 #endif
41 {
42 fprintf(stderr, "%s\n", msg);
43 mike 1.1 }
44
45 exit(1);
46 }
47
48 extern "C" void warningCallback(
49 const char* msg,
50 const wchar_t* wmsg,
51 void* data)
52 {
53 Parser* parser = (Parser*)data;
54
55 if (parser->m_warnings)
56 {
57 #if defined(_MSC_VER)
58 if (wmsg)
59 {
60 fwprintf(stderr, L"%s\n", wmsg);
61 }
62 else
63 #endif
64 mike 1.1 {
65 fprintf(stderr, "%s\n", msg);
66 }
67 }
68 }
69
70 extern "C" void pragmaCallback(
71 const char* /*pragma*/,
72 const char* /*value*/,
73 void* /*data*/)
74 {
75 }
76
77 extern "C" void classDeclCallback(const MI_ClassDecl* decl, void* data)
78 {
79 Parser* parser = (Parser*)data;
80 parser->m_classDecls.insert(Parser::ClassDeclPair(decl->name, decl));
81 }
82
83 extern "C" void qualifierDeclCallback(const MI_QualifierDecl* decl, void* data)
84 {
85 mike 1.1 Parser* parser = (Parser*)data;
86 parser->m_qualifierDecls.insert(
87 Parser::QualifierDeclPair(decl->name, decl));
88 }
89
90 Parser::Parser(const std::vector<std::string>& paths, bool warnings) :
91 m_warnings(warnings)
92 {
93 char** tmp = new char*[paths.size()];
94
95 for (size_t i = 0; i < paths.size(); i++)
96 tmp[i] = (char*)paths[i].c_str();
97
98 m_parser = MOF_Parser_New(tmp, paths.size());
99 delete [] tmp;
100
101 MOF_Parser_SetErrorCallback(m_parser, (MOF_ErrorCallback)errorCallback,
102 this);
103 MOF_Parser_SetWarningCallback(m_parser,
104 (MOF_WarningCallback)warningCallback, this);
105 MOF_Parser_SetPragmaCallback(m_parser, (MOF_PragmaCallback)pragmaCallback,
106 mike 1.1 this);
107 MOF_Parser_SetClassDeclCallback(m_parser,
108 (MOF_ClassDeclCallback)classDeclCallback, this);
109 MOF_Parser_SetQualifierDeclCallback(m_parser,
110 (MOF_QualifierDeclCallback)qualifierDeclCallback, this);
111 }
112
113 int Parser::parse(const char* path)
114 {
115 return MOF_Parser_Parse(m_parser, path);
116 }
117
118 const MI_ClassDecl* Parser::findClassDecl(const std::string& name) const
119 {
120 ClassDeclMap::const_iterator pos = m_classDecls.find(name);
121
122 if (pos == m_classDecls.end())
123 return NULL;
124
125 return (*pos).second;
126 }
127 mike 1.1
128 void Parser::getClassNames(std::vector<std::string>& names) const
129 {
130 names.clear();
131
132 ClassDeclMap::const_iterator p = m_classDecls.begin();
133 ClassDeclMap::const_iterator end = m_classDecls.end();
134
135 for (; p != end; p++)
136 {
137 names.push_back((*p).first);
138 }
139 }
140
141 const MI_QualifierDecl* Parser::findQualifierDecl(const std::string& name) const
142 {
143 QualifierDeclMap::const_iterator pos = m_qualifierDecls.find(name);
144
145 if (pos == m_qualifierDecls.end())
146 return NULL;
147
148 mike 1.1 return (*pos).second;
149 }
150
151 void Parser::getQualifierDeclNames(std::vector<std::string>& names) const
152 {
153 QualifierDeclMap::const_iterator first = m_qualifierDecls.begin();
154 QualifierDeclMap::const_iterator last = m_qualifierDecls.end();
155
156 while (first != last)
157 {
158 names.push_back((*first).first);
159 first++;
160 }
161 }
162
163 Parser::~Parser()
164 {
165 MOF_Parser_Delete(m_parser);
166 }
|