00001 /* ParseNode.cpp 00002 00003 Copyright (C) David C. J. Matthews 2004 dm at prolingua.co.uk 00004 00005 This program is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU General Public License 00007 as published by the Free Software Foundation; either version 2 00008 of the License, or (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software 00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00018 Or, point your browser to http://www.gnu.org/copyleft/gpl.html 00019 00020 */ 00021 00022 #include "ParseNode.h" 00023 #include "ASN1Codes.h" 00024 #include "Engine.h" 00025 #include "Logging.h" 00026 00027 MHPTagged::MHPTagged(int nTag): MHParseNode(PNTagged) 00028 { 00029 m_TagNo = nTag; 00030 } 00031 00032 00033 // Add an argument to the argument sequence. 00034 void MHPTagged::AddArg(MHParseNode *pArg) 00035 { 00036 m_Args.Append(pArg); 00037 } 00038 00039 // General utility function for display. 00040 void PrintTabs(FILE *fd, int n) 00041 { 00042 for (int i = 0; i < n; i++) fprintf(fd, " "); 00043 } 00044 00045 // Report a failure. This can be called when we use the parse tree to set up object tree. 00046 void MHParseNode::Failure(const char *p) 00047 { 00048 MHERROR(p); 00049 } 00050 00051 00052 int MHParseNode::GetTagNo() 00053 { 00054 if (m_nNodeType != PNTagged) Failure("Expected tagged value"); 00055 return ((MHPTagged*)this)->m_TagNo; 00056 } 00057 00058 // Return the number of items in the sequence. 00059 int MHParseNode::GetArgCount() 00060 { 00061 if (m_nNodeType == PNTagged) { 00062 MHPTagged *pTag = (MHPTagged*)this; 00063 return pTag->m_Args.Size(); 00064 } 00065 else if (m_nNodeType == PNSeq) { 00066 MHParseSequence *pSeq = (MHParseSequence*)this; 00067 return pSeq->Size(); 00068 } 00069 else Failure("Expected tagged value"); 00070 return 0; // To keep the compiler happy 00071 } 00072 00073 // Get the Nth entry. 00074 MHParseNode *MHParseNode::GetArgN(int n) 00075 { 00076 if (m_nNodeType == PNTagged) { 00077 MHPTagged *pTag = (MHPTagged*)this; 00078 if (n < 0 || n >= pTag->m_Args.Size()) Failure("Argument not found"); 00079 return pTag->m_Args.GetAt(n); 00080 } 00081 else if (m_nNodeType == PNSeq) { 00082 MHParseSequence *pSeq = (MHParseSequence*)this; 00083 if (n < 0 || n >= pSeq->Size()) Failure("Argument not found"); 00084 return pSeq->GetAt(n); 00085 } 00086 else Failure("Expected tagged value"); 00087 return 0; // To keep the compiler happy 00088 } 00089 00090 // Get an argument with a specific tag. Returns NULL if it doesn't exist. 00091 // There is a defined order of tags for both the binary and textual representations. 00092 // Unfortunately they're not the same. 00093 MHParseNode *MHParseNode::GetNamedArg(int nTag) 00094 { 00095 MHParseSequence *pArgs = NULL; 00096 if (m_nNodeType == PNTagged) pArgs = &((MHPTagged*)this)->m_Args; 00097 else if (m_nNodeType == PNSeq) pArgs = (MHParseSequence*)this; 00098 else Failure("Expected tagged value or sequence"); 00099 for (int i = 0; i < pArgs->Size(); i++) { 00100 MHParseNode *p = pArgs->GetAt(i); 00101 if (p && p->m_nNodeType == PNTagged && ((MHPTagged*)p)->m_TagNo == nTag) return p; 00102 } 00103 return NULL; 00104 } 00105 00106 // Sequence. 00107 int MHParseNode::GetSeqCount() 00108 { 00109 if (m_nNodeType != PNSeq) Failure("Expected sequence"); 00110 MHParseSequence *pSeq = (MHParseSequence*)this; 00111 return pSeq->Size(); 00112 } 00113 00114 MHParseNode *MHParseNode::GetSeqN(int n) 00115 { 00116 if (m_nNodeType != PNSeq) Failure("Expected sequence"); 00117 MHParseSequence *pSeq = (MHParseSequence*)this; 00118 if (n < 0 || n >= pSeq->Size()) Failure("Argument not found"); 00119 return pSeq->GetAt(n); 00120 } 00121 00122 // Int 00123 int MHParseNode::GetIntValue() 00124 { 00125 if (m_nNodeType != PNInt) Failure("Expected integer"); 00126 return ((MHPInt*)this)->m_Value; 00127 } 00128 00129 // Enum 00130 int MHParseNode::GetEnumValue() 00131 { 00132 if (m_nNodeType != PNEnum) Failure("Expected enumerated type"); 00133 return ((MHPEnum*)this)->m_Value; 00134 } 00135 00136 // Bool 00137 bool MHParseNode::GetBoolValue() 00138 { 00139 if (m_nNodeType != PNBool) Failure("Expected boolean"); 00140 return ((MHPBool*)this)->m_Value; 00141 } 00142 00143 // String 00144 void MHParseNode::GetStringValue(MHOctetString &str) 00145 { 00146 if (m_nNodeType != PNString) Failure("Expected string"); 00147 str.Copy(((MHPString*)this)->m_Value); 00148 } 00149
1.5.5