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++) 00043 { 00044 fprintf(fd, " "); 00045 } 00046 } 00047 00048 // Report a failure. This can be called when we use the parse tree to set up object tree. 00049 void MHParseNode::Failure(const char *p) 00050 { 00051 MHERROR(p); 00052 } 00053 00054 00055 int MHParseNode::GetTagNo() 00056 { 00057 if (m_nNodeType != PNTagged) 00058 { 00059 Failure("Expected tagged value"); 00060 } 00061 00062 return ((MHPTagged *)this)->m_TagNo; 00063 } 00064 00065 // Return the number of items in the sequence. 00066 int MHParseNode::GetArgCount() 00067 { 00068 if (m_nNodeType == PNTagged) 00069 { 00070 MHPTagged *pTag = (MHPTagged *)this; 00071 return pTag->m_Args.Size(); 00072 } 00073 else if (m_nNodeType == PNSeq) 00074 { 00075 MHParseSequence *pSeq = (MHParseSequence *)this; 00076 return pSeq->Size(); 00077 } 00078 else 00079 { 00080 Failure("Expected tagged value"); 00081 } 00082 00083 return 0; // To keep the compiler happy 00084 } 00085 00086 // Get the Nth entry. 00087 MHParseNode *MHParseNode::GetArgN(int n) 00088 { 00089 if (m_nNodeType == PNTagged) 00090 { 00091 MHPTagged *pTag = (MHPTagged *)this; 00092 00093 if (n < 0 || n >= pTag->m_Args.Size()) 00094 { 00095 Failure("Argument not found"); 00096 } 00097 00098 return pTag->m_Args.GetAt(n); 00099 } 00100 else if (m_nNodeType == PNSeq) 00101 { 00102 MHParseSequence *pSeq = (MHParseSequence *)this; 00103 00104 if (n < 0 || n >= pSeq->Size()) 00105 { 00106 Failure("Argument not found"); 00107 } 00108 00109 return pSeq->GetAt(n); 00110 } 00111 else 00112 { 00113 Failure("Expected tagged value"); 00114 } 00115 00116 return 0; // To keep the compiler happy 00117 } 00118 00119 // Get an argument with a specific tag. Returns NULL if it doesn't exist. 00120 // There is a defined order of tags for both the binary and textual representations. 00121 // Unfortunately they're not the same. 00122 MHParseNode *MHParseNode::GetNamedArg(int nTag) 00123 { 00124 MHParseSequence *pArgs = NULL; 00125 00126 if (m_nNodeType == PNTagged) 00127 { 00128 pArgs = &((MHPTagged *)this)->m_Args; 00129 } 00130 else if (m_nNodeType == PNSeq) 00131 { 00132 pArgs = (MHParseSequence *)this; 00133 } 00134 else 00135 { 00136 Failure("Expected tagged value or sequence"); 00137 } 00138 00139 for (int i = 0; i < pArgs->Size(); i++) 00140 { 00141 MHParseNode *p = pArgs->GetAt(i); 00142 00143 if (p && p->m_nNodeType == PNTagged && ((MHPTagged *)p)->m_TagNo == nTag) 00144 { 00145 return p; 00146 } 00147 } 00148 00149 return NULL; 00150 } 00151 00152 // Sequence. 00153 int MHParseNode::GetSeqCount() 00154 { 00155 if (m_nNodeType != PNSeq) 00156 { 00157 Failure("Expected sequence"); 00158 } 00159 00160 MHParseSequence *pSeq = (MHParseSequence *)this; 00161 return pSeq->Size(); 00162 } 00163 00164 MHParseNode *MHParseNode::GetSeqN(int n) 00165 { 00166 if (m_nNodeType != PNSeq) 00167 { 00168 Failure("Expected sequence"); 00169 } 00170 00171 MHParseSequence *pSeq = (MHParseSequence *)this; 00172 00173 if (n < 0 || n >= pSeq->Size()) 00174 { 00175 Failure("Argument not found"); 00176 } 00177 00178 return pSeq->GetAt(n); 00179 } 00180 00181 // Int 00182 int MHParseNode::GetIntValue() 00183 { 00184 if (m_nNodeType != PNInt) 00185 { 00186 Failure("Expected integer"); 00187 } 00188 00189 return ((MHPInt *)this)->m_Value; 00190 } 00191 00192 // Enum 00193 int MHParseNode::GetEnumValue() 00194 { 00195 if (m_nNodeType != PNEnum) 00196 { 00197 Failure("Expected enumerated type"); 00198 } 00199 00200 return ((MHPEnum *)this)->m_Value; 00201 } 00202 00203 // Bool 00204 bool MHParseNode::GetBoolValue() 00205 { 00206 if (m_nNodeType != PNBool) 00207 { 00208 Failure("Expected boolean"); 00209 } 00210 00211 return ((MHPBool *)this)->m_Value; 00212 } 00213 00214 // String 00215 void MHParseNode::GetStringValue(MHOctetString &str) 00216 { 00217 if (m_nNodeType != PNString) 00218 { 00219 Failure("Expected string"); 00220 } 00221 00222 str.Copy(((MHPString *)this)->m_Value); 00223 } 00224
1.6.3