MythTV master
ParseNode.cpp
Go to the documentation of this file.
1/* ParseNode.cpp
2
3 Copyright (C) David C. J. Matthews 2004 dm at prolingua.co.uk
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18 Or, point your browser to http://www.gnu.org/copyleft/gpl.html
19
20*/
21
22#include "ParseNode.h"
23#include "ASN1Codes.h"
24#include "Engine.h"
25#include "Logging.h"
26
27// Add an argument to the argument sequence.
29{
30 m_Args.Append(pArg);
31}
32
33// General utility function for display.
34void PrintTabs(FILE *fd, int n)
35{
36 for (int i = 0; i < n; i++)
37 {
38 fprintf(fd, " ");
39 }
40}
41
42// Report a failure. This can be called when we use the parse tree to set up object tree.
43void MHParseNode::Failure(const char *p)
44{
45 MHERROR(p);
46}
47
48
50{
51 if (m_nNodeType != PNTagged)
52 {
53 Failure("Expected tagged value");
54 }
55
56 return ((MHPTagged *)this)->m_TagNo;
57}
58
59// Return the number of items in the sequence.
61{
62 if (m_nNodeType == PNTagged)
63 {
64 auto *pTag = (MHPTagged *)this;
65 return pTag->m_Args.Size();
66 }
67 if (m_nNodeType == PNSeq)
68 {
69 auto *pSeq = (MHParseSequence *)this;
70 return pSeq->Size();
71 }
72
73 Failure("Expected tagged value");
74 return 0; // To keep the compiler happy
75}
76
77// Get the Nth entry.
79{
80 if (m_nNodeType == PNTagged)
81 {
82 auto *pTag = (MHPTagged *)this;
83
84 if (n < 0 || n >= pTag->m_Args.Size())
85 {
86 Failure("Argument not found");
87 }
88
89 return pTag->m_Args.GetAt(n);
90 }
91 if (m_nNodeType == PNSeq)
92 {
93 auto *pSeq = (MHParseSequence *)this;
94
95 if (n < 0 || n >= pSeq->Size())
96 {
97 Failure("Argument not found");
98 }
99
100 return pSeq->GetAt(n);
101 }
102
103 Failure("Expected tagged value");
104 return nullptr; // To keep the compiler happy
105}
106
107// Get an argument with a specific tag. Returns nullptr if it doesn't exist.
108// There is a defined order of tags for both the binary and textual representations.
109// Unfortunately they're not the same.
111{
112 MHParseSequence *pArgs = nullptr;
113
114 if (m_nNodeType == PNTagged)
115 {
116 pArgs = &((MHPTagged *)this)->m_Args;
117 }
118 else if (m_nNodeType == PNSeq)
119 {
120 pArgs = (MHParseSequence *)this;
121 }
122 else
123 {
124 Failure("Expected tagged value or sequence");
125 }
126
127 for (int i = 0; i < pArgs->Size(); i++)
128 {
129 MHParseNode *p = pArgs->GetAt(i);
130
131 if (p && p->m_nNodeType == PNTagged && ((MHPTagged *)p)->m_TagNo == nTag)
132 {
133 return p;
134 }
135 }
136
137 return nullptr;
138}
139
140// Sequence.
142{
143 if (m_nNodeType != PNSeq)
144 {
145 Failure("Expected sequence");
146 }
147
148 auto *pSeq = (MHParseSequence *)this;
149 return pSeq->Size();
150}
151
153{
154 if (m_nNodeType != PNSeq)
155 {
156 Failure("Expected sequence");
157 }
158
159 auto *pSeq = (MHParseSequence *)this;
160
161 if (n < 0 || n >= pSeq->Size())
162 {
163 Failure("Argument not found");
164 }
165
166 return pSeq->GetAt(n);
167}
168
169// Int
171{
172 if (m_nNodeType != PNInt)
173 {
174 Failure("Expected integer");
175 }
176
177 return ((MHPInt *)this)->m_Value;
178}
179
180// Enum
182{
183 if (m_nNodeType != PNEnum)
184 {
185 Failure("Expected enumerated type");
186 }
187
188 return ((MHPEnum *)this)->m_Value;
189}
190
191// Bool
193{
194 if (m_nNodeType != PNBool)
195 {
196 Failure("Expected boolean");
197 }
198
199 return ((MHPBool *)this)->m_Value;
200}
201
202// String
204{
205 if (m_nNodeType != PNString)
206 {
207 Failure("Expected string");
208 }
209
210 str.Copy(((MHPString *)this)->m_Value);
211}
212
214{
215 for (int i = 0; i < GetArgCount(); ++i)
216 {
217 if (i) fprintf(f, ", ");
218 MHParseNode* pn = GetArgN(i);
219 switch (pn->m_nNodeType)
220 {
222 fprintf(f, "tagged { "); pn->PrintMe(f); fprintf(f, "}");
223 break;
225 fprintf(f, "bool %s", pn->GetBoolValue() ? "true" : "false");
226 break;
228 fprintf(f, "int %d", pn->GetIntValue());
229 break;
231 fprintf(f, "enum %d", pn->GetEnumValue());
232 break;
234 MHOctetString str;
235 pn->GetStringValue(str);
236 fprintf(f, "string '%s'", str.Bytes());
237 break;
238 }
240 fprintf(f, "null");
241 break;
243 fprintf(f, "seq { "); pn->PrintMe(f); fprintf(f, "}");
244 break;
245 }
246 }
247 fprintf(f, "\n");
248}
#define MHERROR(__text)
Definition: Logging.h:42
void PrintTabs(FILE *fd, int n)
Definition: ParseNode.cpp:34
void Copy(const MHOctetString &str)
const unsigned char * Bytes() const
Definition: BaseClasses.h:125
MHParseSequence m_Args
Definition: ParseNode.h:92
void AddArg(MHParseNode *pArg)
Definition: ParseNode.cpp:28
MHParseNode * GetSeqN(int n)
Definition: ParseNode.cpp:152
MHParseNode * GetArgN(int n)
Definition: ParseNode.cpp:78
static void Failure(const char *p)
Definition: ParseNode.cpp:43
int GetEnumValue()
Definition: ParseNode.cpp:181
void GetStringValue(MHOctetString &str)
Definition: ParseNode.cpp:203
int GetArgCount()
Definition: ParseNode.cpp:60
int GetTagNo()
Definition: ParseNode.cpp:49
void PrintMe(FILE *f)
Definition: ParseNode.cpp:213
bool GetBoolValue()
Definition: ParseNode.cpp:192
int GetSeqCount()
Definition: ParseNode.cpp:141
MHParseNode * GetNamedArg(int nTag)
Definition: ParseNode.cpp:110
enum NodeType m_nNodeType
Definition: ParseNode.h:46
int GetIntValue()
Definition: ParseNode.cpp:170
int Size() const
Definition: BaseClasses.h:47
void Append(BASE b)
Definition: BaseClasses.h:64
BASE GetAt(int i) const
Definition: BaseClasses.h:49
int FILE
Definition: mythburn.py:138