1/*-------------------------------------------------------------------------
2 * drawElements Quality Program Test Executor
3 * ------------------------------------------
4 *
5 * Copyright 2014 The Android Open Source Project
6 *
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
10 *
11 *      http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 *//*!
20 * \file
21 * \brief Extract sample lists from logs.
22 *//*--------------------------------------------------------------------*/
23
24#include "xeTestLogParser.hpp"
25#include "xeTestResultParser.hpp"
26#include "deFilePath.hpp"
27#include "deString.h"
28#include "deStringUtil.hpp"
29
30#include <vector>
31#include <string>
32#include <cstdio>
33#include <cstdlib>
34#include <fstream>
35#include <iostream>
36#include <stdexcept>
37
38using std::vector;
39using std::string;
40using std::set;
41using std::map;
42
43void writeSampleList (const char* casePath, int listNdx, const xe::ri::SampleList& sampleList)
44{
45	const string	filename	= string(casePath) + "." + de::toString(listNdx) + ".csv";
46	std::ofstream	out			(filename.c_str(), std::ios_base::binary);
47
48	if (!out.good())
49		throw std::runtime_error("Failed to open " + filename);
50
51	// Header
52	for (int ndx = 0; ndx < sampleList.sampleInfo.valueInfos.getNumItems(); ndx++)
53	{
54		if (ndx != 0)
55			out << ",";
56		out << static_cast<const xe::ri::ValueInfo&>(sampleList.sampleInfo.valueInfos.getItem(ndx)).name;
57	}
58	out << "\n";
59
60	// Samples
61	for (int sampleNdx = 0; sampleNdx < sampleList.samples.getNumItems(); sampleNdx++)
62	{
63		const xe::ri::Sample&	sample	= static_cast<const xe::ri::Sample&>(sampleList.samples.getItem(sampleNdx));
64
65		for (int valNdx = 0; valNdx < sample.values.getNumItems(); valNdx++)
66		{
67			const xe::ri::SampleValue&	value	= static_cast<const xe::ri::SampleValue&>(sample.values.getItem(valNdx));
68
69			if (valNdx != 0)
70				out << ",";
71
72			out << value.value;
73		}
74		out << "\n";
75	}
76}
77
78void extractSampleLists (const char* casePath, int* listNdx, const xe::ri::List& items)
79{
80	for (int itemNdx = 0; itemNdx < items.getNumItems(); itemNdx++)
81	{
82		const xe::ri::Item& child = items.getItem(itemNdx);
83
84		if (child.getType() == xe::ri::TYPE_SECTION)
85			extractSampleLists(casePath, listNdx, static_cast<const xe::ri::Section&>(child).items);
86		else if (child.getType() == xe::ri::TYPE_SAMPLELIST)
87		{
88			writeSampleList(casePath, *listNdx, static_cast<const xe::ri::SampleList&>(child));
89			*listNdx += 1;
90		}
91	}
92}
93
94void extractSampleLists (const xe::TestCaseResult& result)
95{
96	int listNdx = 0;
97	extractSampleLists(result.casePath.c_str(), &listNdx, result.resultItems);
98}
99
100class SampleListParser : public xe::TestLogHandler
101{
102public:
103	SampleListParser (void)
104	{
105	}
106
107	void setSessionInfo (const xe::SessionInfo&)
108	{
109		// Ignored.
110	}
111
112	xe::TestCaseResultPtr startTestCaseResult (const char* casePath)
113	{
114		return xe::TestCaseResultPtr(new xe::TestCaseResultData(casePath));
115	}
116
117	void testCaseResultUpdated (const xe::TestCaseResultPtr&)
118	{
119		// Ignored.
120	}
121
122	void testCaseResultComplete (const xe::TestCaseResultPtr& caseData)
123	{
124		xe::TestCaseResult result;
125		xe::parseTestCaseResultFromData(&m_testResultParser, &result, *caseData.get());
126		extractSampleLists(result);
127	}
128
129private:
130	xe::TestResultParser	m_testResultParser;
131};
132
133static void processLogFile (const char* filename)
134{
135	std::ifstream		in				(filename, std::ifstream::binary|std::ifstream::in);
136	SampleListParser	resultHandler;
137	xe::TestLogParser	parser			(&resultHandler);
138	deUint8				buf				[1024];
139	int					numRead			= 0;
140
141	if (!in.good())
142		throw std::runtime_error(string("Failed to open '") + filename + "'");
143
144	for (;;)
145	{
146		in.read((char*)&buf[0], DE_LENGTH_OF_ARRAY(buf));
147		numRead = (int)in.gcount();
148
149		if (numRead <= 0)
150			break;
151
152		parser.parse(&buf[0], numRead);
153	}
154
155	in.close();
156}
157
158int main (int argc, const char* const* argv)
159{
160	if (argc != 2)
161	{
162		printf("%s: [filename]\n", de::FilePath(argv[0]).getBaseName().c_str());
163		return -1;
164	}
165
166	try
167	{
168		processLogFile(argv[1]);
169	}
170	catch (const std::exception& e)
171	{
172		printf("FATAL ERROR: %s\n", e.what());
173		return -1;
174	}
175
176	return 0;
177}
178