Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PreprocessorWrapper.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 
20 #include "PreprocessorWrapper.h"
21 
22 #include "graphics/ShaderDefines.h"
23 #include "ps/CLogger.h"
24 
25 void CPreprocessorWrapper::AddDefine(const char* name, const char* value)
26 {
27  m_Preprocessor.Define(name, value);
28 }
29 
31 {
32  std::map<CStrIntern, CStrIntern> map = defines.GetMap();
33  for (std::map<CStrIntern, CStrIntern>::const_iterator it = map.begin(); it != map.end(); ++it)
34  m_Preprocessor.Define(it->first.c_str(), it->first.length(), it->second.c_str(), it->second.length());
35 }
36 
38 {
39  // Construct a dummy program so we can trigger the preprocessor's expression
40  // code without modifying its public API.
41  // Be careful that the API buggily returns a statically allocated pointer
42  // (which we will try to free()) if the input just causes it to append a single
43  // sequence of newlines to the output; the "\n" after the "#endif" is enough
44  // to avoid this case.
45  CStr input = "#if ";
46  input += expr;
47  input += "\n1\n#endif\n";
48 
49  size_t len = 0;
50  char* output = m_Preprocessor.Parse(input.c_str(), input.size(), len);
51 
52  if (!output)
53  {
54  LOGERROR(L"Failed to parse conditional expression '%hs'", expr.c_str());
55  return false;
56  }
57 
58  bool ret = (memchr(output, '1', len) != NULL);
59 
60  // Free output if it's not inside the source string
61  if (!(output >= input.c_str() && output < input.c_str() + input.size()))
62  free(output);
63 
64  return ret;
65 
66 }
67 
69 {
70  size_t len = 0;
71  char* output = m_Preprocessor.Parse(input.c_str(), input.size(), len);
72 
73  if (!output)
74  {
75  LOGERROR(L"Shader preprocessing failed");
76  return "";
77  }
78 
79  CStr ret(output, len);
80 
81  // Free output if it's not inside the source string
82  if (!(output >= input.c_str() && output < input.c_str() + input.size()))
83  free(output);
84 
85  return ret;
86 }
void Define(const char *iMacroName, size_t iMacroNameLen, const char *iMacroValue, size_t iMacroValueLen)
Define a macro without parameters.
#define LOGERROR
Definition: CLogger.h:35
CPreprocessor m_Preprocessor
tuple output
Definition: tests.py:116
Token Parse(const Token &iSource)
Parse the input string and return a token containing the whole output.
bool TestConditional(const CStr &expr)
std::map< CStrIntern, value_t > GetMap() const
Return a copy of the current name/value mapping.
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
CStr Preprocess(const CStr &input)
void AddDefines(const CShaderDefines &defines)
void AddDefine(const char *name, const char *value)
tuple input
Definition: tests.py:115