00001 #include "pparser.h" 00002 #include <iostream> 00003 00004 00005 PParser::PParser(char *packet, unsigned int length): mp_packet(packet), m_packet_length(length), m_packet_index(0), m_frame_counter(0) 00006 {} 00007 PParser::~PParser() // default destructor 00008 {} 00009 void PParser::setLength(unsigned int length) 00010 { 00011 m_packet_length = length; 00012 m_packet_index = 0; 00013 } 00014 void PParser::setPacket(char* packet) 00015 { 00016 mp_packet = packet; 00017 } 00018 char * PParser::getPacket() 00019 { 00020 return mp_packet; 00021 } 00022 bool PParser::parse() 00023 { 00024 State state = INIT; 00025 // every datagram start with "fr" tag that mean framepointer 00026 if ('f' == mp_packet[0] && 'r' == mp_packet[1] && ' ' == mp_packet[2]) 00027 { 00028 m_packet_index = 3; // set packet iterator to next number 00029 m_frame_counter = parse_fr(); 00030 std::cout << parse_int(); 00031 } 00032 else 00033 { 00034 return false; // bad packet syntax! 00035 } 00036 00037 while ( m_packet_index < m_packet_length) // while not end of the packet 00038 { 00039 char chunk = mp_packet[m_packet_index++]; // get next chunk 00040 switch (state) 00041 { 00042 case INIT: 00043 { 00044 // changing state 00045 if ('t' == chunk) state = S1; 00046 else if ('3' == chunk) state = S3; 00047 else if ('g' == chunk) state = S5; 00048 else if ('6' == chunk) state = S7; 00049 else return false; // instead of going to error state 00050 break; 00051 } 00052 case S1: 00053 { 00054 if ('s' == chunk) state = S2; 00055 else return false; // insted of going to error state 00056 break; 00057 } 00058 case S2: 00059 { 00060 if (' ' == chunk) 00061 { 00062 parse_ts(); // tag is OK - now parsing arguments of tag... 00063 state = INIT; // after parsing tag - INIT state 00064 } 00065 else return false; // instead of going to error state 00066 break; 00067 } 00068 case S3: 00069 { 00070 if ('d' == chunk) state = S4; 00071 else return false; 00072 break; 00073 } 00074 case S4: 00075 { 00076 if (' ' == chunk) 00077 { 00078 parse_3DOF(); // tag OK - parse rest of line 00079 state = INIT; // try to fing another parser 00080 } 00081 else return false; 00082 break; 00083 } 00084 case S5: 00085 { 00086 if ('l' == chunk) state = S6; 00087 else return false; 00088 break; 00089 } 00090 case S6: 00091 { 00092 if (' ' == chunk) 00093 { 00094 parse_gl(); 00095 state = INIT; 00096 } 00097 else return false; 00098 break; 00099 } 00100 case S7: 00101 { 00102 if ('d' == chunk) state = S8; 00103 else return false; 00104 break; 00105 } 00106 case S8: 00107 { 00108 if ('f' == chunk) state = S9; 00109 else if ('c' == chunk) state = S11; 00110 else if (' ' == chunk) 00111 { 00112 parse_6DOF(); 00113 state = INIT; 00114 } 00115 else return false; 00116 break; 00117 } 00118 case S9: 00119 { 00120 if ('2' == chunk) state = S10; 00121 else if (' ' == chunk) 00122 { 00123 parse_flystick(); 00124 state = INIT; 00125 } 00126 else return false; 00127 break; 00128 } 00129 case S10: 00130 { 00131 if (' ' == chunk) 00132 { 00133 parse_flystick2(); 00134 state = INIT; 00135 } 00136 else return false; 00137 break; 00138 } 00139 case S11: 00140 { 00141 if ('a' == chunk) state = S12; 00142 else return false; 00143 break; 00144 } 00145 case S12: 00146 { 00147 if ('l' == chunk) state = S13; 00148 else return false; 00149 break; 00150 } 00151 case S13: 00152 { 00153 if (' ' == chunk) 00154 { 00155 parse_6dcal(); 00156 state = INIT; 00157 } 00158 else return false; 00159 break; 00160 } 00161 default: return false; // Also error state- if ERR or undefined state - parsing unsuccesfull 00162 } 00163 } 00164 return true; 00165 } 00166 unsigned int PParser::parse_fr() 00167 { 00168 // TODO:: Implement 00169 return 0; 00170 } 00171 void PParser::parse_ts() 00172 { 00173 // TODO:: Implement 00174 } 00175 void PParser::parse_3DOF() 00176 { 00177 // TODO:: Implement 00178 } 00179 void PParser::parse_gl() 00180 { 00181 // TODO:: Implement 00182 } 00183 void PParser::parse_6DOF() 00184 { 00185 // TODO:: Implement 00186 } 00187 void PParser::parse_flystick() 00188 { 00189 // TODO:: Implement 00190 } 00191 void PParser::parse_flystick2() 00192 { 00193 // TODO:: Implement 00194 } 00195 void PParser::parse_6dcal() 00196 { 00197 // TODO:: Implement 00198 } 00199 00200 int PParser::parse_int() 00201 { 00202 // TODO:: rebulid this method 00203 int tmp = 0; 00204 char chunk = mp_packet[m_packet_index++]; 00205 while('0' <= chunk && '9' >= chunk) 00206 { 00207 if (m_packet_index >= m_packet_length) return 0; 00208 tmp = tmp * 10 + (int)(chunk - '0'); 00209 chunk = mp_packet[m_packet_index++]; 00210 } 00211 return tmp; 00212 } 00213 00214 00215 int main() 00216 { 00217 char * aa = "fr 45632f"; 00218 PParser parser; 00219 parser.setLength(7); 00220 parser.setPacket(aa); 00221 parser.parse(); 00222 00223 00224 return 0; 00225 }
1.5.5