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.
34 void 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.
43 void 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;
224  case MHParseNode::PNBool:
225  fprintf(f, "bool %s", pn->GetBoolValue() ? "true" : "false");
226  break;
227  case MHParseNode::PNInt:
228  fprintf(f, "int %d", pn->GetIntValue());
229  break;
230  case MHParseNode::PNEnum:
231  fprintf(f, "enum %d", pn->GetEnumValue());
232  break;
233  case MHParseNode::PNString: {
234  MHOctetString str;
235  pn->GetStringValue(str);
236  fprintf(f, "string '%s'", str.Bytes());
237  break;
238  }
239  case MHParseNode::PNNull:
240  fprintf(f, "null");
241  break;
242  case MHParseNode::PNSeq:
243  fprintf(f, "seq { "); pn->PrintMe(f); fprintf(f, "}");
244  break;
245  }
246  }
247  fprintf(f, "\n");
248 }
MHParseNode::m_nNodeType
enum NodeType m_nNodeType
Definition: ParseNode.h:46
MHParseNode::PrintMe
void PrintMe(FILE *f)
Definition: ParseNode.cpp:213
ASN1Codes.h
MHParseNode::GetIntValue
int GetIntValue()
Definition: ParseNode.cpp:170
MHParseNode::GetSeqN
MHParseNode * GetSeqN(int n)
Definition: ParseNode.cpp:152
MHParseNode::PNString
@ PNString
Definition: ParseNode.h:41
MHPTagged
Definition: ParseNode.h:84
MHParseNode::PNEnum
@ PNEnum
Definition: ParseNode.h:41
MHOctetString::Bytes
const unsigned char * Bytes() const
Definition: BaseClasses.h:125
MHOctetString
Definition: BaseClasses.h:107
mythburn.FILE
int FILE
Definition: mythburn.py:139
MHParseNode::PNNull
@ PNNull
Definition: ParseNode.h:41
hardwareprofile.config.p
p
Definition: config.py:33
MHParseNode::GetSeqCount
int GetSeqCount()
Definition: ParseNode.cpp:141
ParseNode.h
PrintTabs
void PrintTabs(FILE *fd, int n)
Definition: ParseNode.cpp:34
MHParseNode::GetArgN
MHParseNode * GetArgN(int n)
Definition: ParseNode.cpp:78
MHParseNode::GetTagNo
int GetTagNo()
Definition: ParseNode.cpp:49
MHParseSequence
Definition: ParseNode.h:75
Engine.h
MHPEnum
Definition: ParseNode.h:106
MHParseNode::GetArgCount
int GetArgCount()
Definition: ParseNode.cpp:60
MHParseNode::GetNamedArg
MHParseNode * GetNamedArg(int nTag)
Definition: ParseNode.cpp:110
MHPString
Definition: ParseNode.h:125
MHParseNode::GetBoolValue
bool GetBoolValue()
Definition: ParseNode.cpp:192
MHPBool
Definition: ParseNode.h:115
MHParseNode::Failure
static void Failure(const char *p)
Definition: ParseNode.cpp:43
MHParseNode::PNBool
@ PNBool
Definition: ParseNode.h:41
MHSequence::Size
int Size() const
Definition: BaseClasses.h:47
MHPTagged::AddArg
void AddArg(MHParseNode *pArg)
Definition: ParseNode.cpp:28
MHParseNode
Definition: ParseNode.h:38
MHSequence::GetAt
BASE GetAt(int i) const
Definition: BaseClasses.h:49
MHParseNode::GetEnumValue
int GetEnumValue()
Definition: ParseNode.cpp:181
MHParseNode::GetStringValue
void GetStringValue(MHOctetString &str)
Definition: ParseNode.cpp:203
MHSequence::Append
void Append(BASE b)
Definition: BaseClasses.h:64
MHParseNode::PNTagged
@ PNTagged
Definition: ParseNode.h:41
MHERROR
#define MHERROR(__text)
Definition: Logging.h:42
Logging.h
MHParseNode::PNInt
@ PNInt
Definition: ParseNode.h:41
MHPInt
Definition: ParseNode.h:96
MHParseNode::PNSeq
@ PNSeq
Definition: ParseNode.h:41
MHPTagged::m_Args
MHParseSequence m_Args
Definition: ParseNode.h:92
MHOctetString::Copy
void Copy(const MHOctetString &str)
Definition: BaseClasses.cpp:119