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  MHPTagged *pTag = (MHPTagged *)this;
65  return pTag->m_Args.Size();
66  }
67  if (m_nNodeType == PNSeq)
68  {
69  MHParseSequence *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  MHPTagged *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  MHParseSequence *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  MHParseSequence *pSeq = (MHParseSequence *)this;
149  return pSeq->Size();
150 }
151 
153 {
154  if (m_nNodeType != PNSeq)
155  {
156  Failure("Expected sequence");
157  }
158 
159  MHParseSequence *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 }
void Failure(const char *p)
Definition: ParseNode.cpp:43
#define MHERROR(__text)
Definition: Logging.h:42
BASE GetAt(int i) const
Definition: BaseClasses.h:48
enum NodeType m_nNodeType
Definition: ParseNode.h:46
const unsigned char * Bytes() const
Definition: BaseClasses.h:122
void PrintTabs(FILE *fd, int n)
Definition: ParseNode.cpp:34
MHParseNode * GetSeqN(int n)
Definition: ParseNode.cpp:152
void Copy(const MHOctetString &str)
int GetIntValue()
Definition: ParseNode.cpp:170
void GetStringValue(MHOctetString &str)
Definition: ParseNode.cpp:203
MHParseNode * GetNamedArg(int nTag)
Definition: ParseNode.cpp:110
MHParseSequence m_Args
Definition: ParseNode.h:92
int FILE
Definition: mythburn.py:110
int GetEnumValue()
Definition: ParseNode.cpp:181
int GetSeqCount()
Definition: ParseNode.cpp:141
int GetArgCount()
Definition: ParseNode.cpp:60
bool GetBoolValue()
Definition: ParseNode.cpp:192
void PrintMe(FILE *)
Definition: ParseNode.cpp:213
MHParseNode * GetArgN(int n)
Definition: ParseNode.cpp:78
int GetTagNo()
Definition: ParseNode.cpp:49
void Append(BASE b)
Definition: BaseClasses.h:62
void AddArg(MHParseNode *pArg)
Definition: ParseNode.cpp:28
int Size() const
Definition: BaseClasses.h:46