1 /*-------------------------------------------------------------------------
2 * drawElements Quality Program Test Executor
3 * ------------------------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Test log container format parser.
22 *//*--------------------------------------------------------------------*/
24 #include "xeContainerFormatParser.hpp"
32 CONTAINERFORMATPARSER_INITIAL_BUFFER_SIZE = 1024
35 static int getNextBufferSize (int curSize, int minNewSize)
37 return de::max(curSize*2, 1<<deLog2Ceil32(minNewSize));
40 ContainerFormatParser::ContainerFormatParser (void)
41 : m_element (CONTAINERELEMENT_INCOMPLETE)
43 , m_state (STATE_AT_LINE_START)
44 , m_buf (CONTAINERFORMATPARSER_INITIAL_BUFFER_SIZE)
48 ContainerFormatParser::~ContainerFormatParser (void)
52 void ContainerFormatParser::clear (void)
54 m_element = CONTAINERELEMENT_INCOMPLETE;
56 m_state = STATE_AT_LINE_START;
60 void ContainerFormatParser::error (const std::string& what)
62 throw ContainerParseError(what);
65 void ContainerFormatParser::feed (const deUint8* bytes, size_t numBytes)
67 // Grow buffer if necessary.
68 if (m_buf.getNumFree() < (int)numBytes)
69 m_buf.resize(getNextBufferSize(m_buf.getSize(), m_buf.getNumElements()+(int)numBytes));
72 m_buf.pushFront(bytes, (int)numBytes);
74 // If we haven't parsed complete element, re-try after data feed.
75 if (m_element == CONTAINERELEMENT_INCOMPLETE)
79 const char* ContainerFormatParser::getSessionInfoAttribute (void) const
81 DE_ASSERT(m_element == CONTAINERELEMENT_SESSION_INFO);
82 return m_attribute.c_str();
85 const char* ContainerFormatParser::getSessionInfoValue (void) const
87 DE_ASSERT(m_element == CONTAINERELEMENT_SESSION_INFO);
88 return m_value.c_str();
91 const char* ContainerFormatParser::getTestCasePath (void) const
93 DE_ASSERT(m_element == CONTAINERELEMENT_BEGIN_TEST_CASE_RESULT);
94 return m_value.c_str();
97 const char* ContainerFormatParser::getTerminateReason (void) const
99 DE_ASSERT(m_element == CONTAINERELEMENT_TERMINATE_TEST_CASE_RESULT);
100 return m_value.c_str();
103 int ContainerFormatParser::getDataSize (void) const
105 DE_ASSERT(m_element == CONTAINERELEMENT_TEST_LOG_DATA);
109 void ContainerFormatParser::getData (deUint8* dst, int numBytes, int offset)
111 DE_ASSERT(de::inBounds(offset, 0, m_elementLen) && numBytes > 0 && de::inRange(numBytes+offset, 0, m_elementLen));
113 for (int ndx = 0; ndx < numBytes; ndx++)
114 dst[ndx] = m_buf.peekBack(offset+ndx);
117 int ContainerFormatParser::getChar (int offset) const
119 DE_ASSERT(de::inRange(offset, 0, m_buf.getNumElements()));
121 if (offset < m_buf.getNumElements())
122 return m_buf.peekBack(offset);
124 return END_OF_BUFFER;
127 void ContainerFormatParser::advance (void)
129 if (m_element != CONTAINERELEMENT_INCOMPLETE)
131 m_buf.popBack(m_elementLen);
133 m_element = CONTAINERELEMENT_INCOMPLETE;
141 int curChar = getChar(m_elementLen);
143 if (curChar != (int)END_OF_BUFFER)
146 if (curChar == END_OF_STRING)
148 if (m_elementLen == 1)
149 m_element = CONTAINERELEMENT_END_OF_STRING;
150 else if (m_state == STATE_CONTAINER_LINE)
151 parseContainerLine();
153 m_element = CONTAINERELEMENT_TEST_LOG_DATA;
157 else if (curChar == (int)END_OF_BUFFER)
159 if (m_elementLen > 0 && m_state == STATE_DATA)
160 m_element = CONTAINERELEMENT_TEST_LOG_DATA;
164 else if (curChar == '\r' || curChar == '\n')
167 int nextChar = getChar(m_elementLen);
168 if (curChar == '\n' || (nextChar != (int)END_OF_BUFFER && nextChar != '\n'))
170 if (m_state == STATE_CONTAINER_LINE)
171 parseContainerLine();
173 m_element = CONTAINERELEMENT_TEST_LOG_DATA;
175 m_state = STATE_AT_LINE_START;
178 // else handle following end or \n in next iteration.
180 else if (m_state == STATE_AT_LINE_START)
182 DE_ASSERT(m_elementLen == 1);
183 m_state = (curChar == '#') ? STATE_CONTAINER_LINE : STATE_DATA;
188 void ContainerFormatParser::parseContainerLine (void)
193 ContainerElement element;
196 { "beginTestCaseResult", CONTAINERELEMENT_BEGIN_TEST_CASE_RESULT },
197 { "endTestCaseResult", CONTAINERELEMENT_END_TEST_CASE_RESULT },
198 { "terminateTestCaseResult", CONTAINERELEMENT_TERMINATE_TEST_CASE_RESULT },
199 { "sessionInfo", CONTAINERELEMENT_SESSION_INFO },
200 { "beginSession", CONTAINERELEMENT_BEGIN_SESSION },
201 { "endSession", CONTAINERELEMENT_END_SESSION }
204 DE_ASSERT(m_elementLen >= 1);
205 DE_ASSERT(getChar(0) == '#');
209 for (int elemNdx = 0; elemNdx < DE_LENGTH_OF_ARRAY(s_elements); elemNdx++)
211 bool isMatch = false;
216 int bufChar = (offset+ndx < m_elementLen) ? getChar(offset+ndx) : 0;
217 bool bufEnd = bufChar == 0 || bufChar == ' ' || bufChar == '\r' || bufChar == '\n' || bufChar == (int)END_OF_BUFFER;
218 int elemChar = s_elements[elemNdx].name[ndx];
219 bool elemEnd = elemChar == 0;
221 if (bufEnd || elemEnd)
223 isMatch = bufEnd == elemEnd;
226 else if (bufChar != elemChar)
234 m_element = s_elements[elemNdx].element;
242 case CONTAINERELEMENT_BEGIN_SESSION:
243 case CONTAINERELEMENT_END_SESSION:
244 case CONTAINERELEMENT_END_TEST_CASE_RESULT:
245 break; // No attribute or value.
247 case CONTAINERELEMENT_BEGIN_TEST_CASE_RESULT:
248 case CONTAINERELEMENT_TERMINATE_TEST_CASE_RESULT:
249 if (getChar(offset) != ' ')
250 error("Expected value after instruction");
252 parseContainerValue(m_value, offset);
255 case CONTAINERELEMENT_SESSION_INFO:
256 if (getChar(offset) != ' ')
257 error("Expected attribute name after #sessionInfo");
259 parseContainerValue(m_attribute, offset);
260 if (getChar(offset) != ' ')
261 error("No value for #sessionInfo attribute");
264 if (m_attribute == "timestamp")
268 // \note Candy produces timestamps in very stupid fashion.
271 const int curChar = offset < m_elementLen ? getChar(offset) : 0;
272 const bool isEnd = curChar == 0 || curChar == (int)END_OF_BUFFER || curChar == '\n' || curChar == '\t';
277 m_value.push_back((char)curChar);
283 parseContainerValue(m_value, offset);
287 // \todo [2012-06-09 pyry] Implement better way to handle # at the beginning of log lines.
288 m_element = CONTAINERELEMENT_TEST_LOG_DATA;
293 void ContainerFormatParser::parseContainerValue (std::string& dst, int& offset) const
295 DE_ASSERT(offset < m_elementLen);
297 bool isString = getChar(offset) == '"' || getChar(offset) == '\'';
298 int quotChar = isString ? getChar(offset) : 0;
307 int curChar = offset < m_elementLen ? getChar(offset) : 0;
308 bool isEnd = curChar == 0 || curChar == (int)END_OF_BUFFER ||
309 (isString ? (curChar == quotChar) : (curChar == ' ' || curChar == '\n' || curChar == '\r'));
315 // \todo [2012-06-09 pyry] Escapes.
316 dst.push_back((char)curChar);
322 if (isString && getChar(offset) == quotChar)