• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-------------------------------------------------------------------------
2  * drawElements Quality Program Tester Core
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 String template class.
22  *//*--------------------------------------------------------------------*/
23 
24 #include "tcuStringTemplate.hpp"
25 #include "tcuDefs.hpp"
26 
27 #include <sstream>
28 
29 using std::string;
30 using std::map;
31 using std::ostringstream;
32 
33 namespace tcu
34 {
35 
StringTemplate(void)36 StringTemplate::StringTemplate (void)
37 {
38 }
39 
StringTemplate(const std::string & str)40 StringTemplate::StringTemplate (const std::string& str)
41 {
42 	setString(str);
43 }
44 
~StringTemplate(void)45 StringTemplate::~StringTemplate (void)
46 {
47 }
48 
setString(const std::string & str)49 void StringTemplate::setString (const std::string& str)
50 {
51 	m_template = str;
52 }
53 
specialize(const map<string,string> & params) const54 string StringTemplate::specialize (const map<string, string>& params) const
55 {
56 	ostringstream res;
57 
58 	size_t curNdx = 0;
59 	for (;;)
60 	{
61 		size_t paramNdx = m_template.find("${", curNdx);
62 		if (paramNdx != string::npos)
63 		{
64 			// Append in-between stuff.
65 			res << m_template.substr(curNdx, paramNdx - curNdx);
66 
67 			// Find end-of-param.
68 			size_t paramEndNdx = m_template.find("}", paramNdx);
69 			if (paramEndNdx == string::npos)
70 				throw tcu::InternalError("No '}' found in template parameter", "", __FILE__, __LINE__);
71 
72 			// Parse parameter contents.
73 			string	paramStr		= m_template.substr(paramNdx+2, paramEndNdx-2-paramNdx);
74 			bool	paramSingleLine	= false;
75 			string	paramName;
76 			size_t colonNdx = paramStr.find(":");
77 			if (colonNdx != string::npos)
78 			{
79 				paramName = paramStr.substr(0, colonNdx);
80 				string flagsStr = paramStr.substr(colonNdx+1);
81 				TCU_CHECK(flagsStr == "single-line");
82 				paramSingleLine = true;
83 			}
84 			else
85 				paramName = paramStr;
86 
87 			// Fill in parameter value.
88 			if (params.find(paramName) != params.end())
89 			{
90 				const string& val = (*params.find(paramName)).second;
91 				if (paramSingleLine)
92 				{
93 					string tmp = val;
94 					for (size_t ndx = tmp.find("\n"); ndx != string::npos; ndx = tmp.find("\n"))
95 						tmp = tmp.replace(ndx, 1, " ");
96 					res << tmp;
97 				}
98 				else
99 					res << val;
100 			}
101 			else
102 				throw tcu::InternalError((string("Value for parameter '") + paramName + "' not found in map").c_str(), "", __FILE__, __LINE__);
103 
104 			// Skip over template.
105 			curNdx = paramEndNdx + 1;
106 		}
107 		else
108 		{
109 			if (curNdx < m_template.length())
110 				res << &m_template[curNdx];
111 
112 			break;
113 		}
114 	}
115 
116 	return res.str();
117 }
118 
119 } // tcu
120