Code::Blocks  SVN r11506
projecttemplateloader.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Code::Blocks IDE and licensed under the GNU Lesser General Public License, version 3
3  * http://www.gnu.org/licenses/lgpl-3.0.html
4  *
5  * $Revision: 7608 $
6  * $Id: projecttemplateloader.cpp 7608 2011-11-25 20:50:28Z mortenmacfly $
7  * $HeadURL: https://svn.code.sf.net/p/codeblocks/code/trunk/src/sdk/projecttemplateloader.cpp $
8  */
9 
10 #include "sdk_precomp.h"
11 
12 #ifndef CB_PRECOMP
13  #include "manager.h"
14  #include "logmanager.h"
15  #include "globals.h"
16 #endif
17 
18 #include "projecttemplateloader.h"
19 #include <wx/arrimpl.cpp>
20 
21 WX_DEFINE_OBJARRAY(FileSetFileArray);
22 WX_DEFINE_OBJARRAY(FileSetArray);
23 WX_DEFINE_OBJARRAY(TemplateOptionArray);
24 
26 {
27  //ctor
28 }
29 
31 {
32  //dtor
33 }
34 
36 {
38  if (!pMsg)
39  return false;
40 
41 // pMsg->DebugLog(_T("Reading template file %s"), filename.c_str());
42 
43  TiXmlDocument doc(filename.mb_str());
44  if (!doc.LoadFile())
45  return false;
46 
47  TiXmlElement* root;
48 
49  root = doc.FirstChildElement("CodeBlocks_template_file");
50  if (!root)
51  {
52  // old tag
53  root = doc.FirstChildElement("Code::Blocks_template_file");
54  if (!root)
55  {
56  pMsg->DebugLog(_T("Not a valid Code::Blocks template file..."));
57  return false;
58  }
59  }
60 
61  DoTemplate(root);
62 
63  return true;
64 }
65 
66 void ProjectTemplateLoader::DoTemplate(TiXmlElement* parentNode)
67 {
68  TiXmlElement* node = parentNode->FirstChildElement("Template");
69  while (node)
70  {
71  if (node->Attribute("name"))
72  m_Name = cbC2U(node->Attribute("name"));
73  if (node->Attribute("title"))
74  m_Title = cbC2U(node->Attribute("title"));
75  if (node->Attribute("category"))
76  m_Category = cbC2U(node->Attribute("category"));
77  if (node->Attribute("bitmap"))
78  m_Bitmap = cbC2U(node->Attribute("bitmap"));
79 
80  DoTemplateNotice(node);
81  DoFileSet(node);
82  DoOption(node);
83 
84  node = node->NextSiblingElement("Template");
85  }
86 }
87 
88 void ProjectTemplateLoader::DoTemplateNotice(TiXmlElement* parentNode)
89 {
90  TiXmlElement* node = parentNode->FirstChildElement("Notice");
91  if (!node)
92  return;
93  m_Notice = cbC2U(node->Attribute("value"));
94  while (m_Notice.Replace(_T(" "), _T(" ")))
95  ;
96  m_Notice.Replace(_T("\t"), _T(""));
97  m_NoticeMsgType = cbC2U(node->Attribute("value")) == _T("0") ? wxICON_INFORMATION : wxICON_WARNING;
98 }
99 
100 void ProjectTemplateLoader::DoFileSet(TiXmlElement* parentNode)
101 {
102  TiXmlElement* node = parentNode->FirstChildElement("FileSet");
103  while (node)
104  {
105  FileSet fs;
106  if (node->Attribute("name"))
107  fs.name = cbC2U(node->Attribute("name"));
108  if (node->Attribute("title"))
109  fs.title = cbC2U(node->Attribute("title"));
110 
111  if (!fs.name.IsEmpty() && !fs.title.IsEmpty())
112  {
113  DoFileSetFile(node, fs);
114  m_FileSets.Add(fs);
115  }
116 
117  node = node->NextSiblingElement("FileSet");
118  }
119 }
120 
121 void ProjectTemplateLoader::DoFileSetFile(TiXmlElement* parentNode, FileSet& fs)
122 {
123  TiXmlElement* node = parentNode->FirstChildElement("File");
124  while (node)
125  {
126  FileSetFile fsf;
127  if (node->Attribute("source"))
128  fsf.source = cbC2U(node->Attribute("source"));
129  if (node->Attribute("destination"))
130  fsf.destination = cbC2U(node->Attribute("destination"));
131  if (node->Attribute("targets"))
132  fsf.targets = cbC2U(node->Attribute("targets"));
133 
134  if (!fsf.source.IsEmpty() && !fsf.destination.IsEmpty())
135  fs.files.Add(fsf);
136 
137  node = node->NextSiblingElement("File");
138  }
139 }
140 
141 void ProjectTemplateLoader::DoOption(TiXmlElement* parentNode)
142 {
143  TiXmlElement* node = parentNode->FirstChildElement("Option");
144  while (node)
145  {
146  TemplateOption to;
147  to.useDefaultCompiler = true;
148 
149  if (node->Attribute("name"))
150  to.name = cbC2U(node->Attribute("name"));
151 
152  if (!to.name.IsEmpty())
153  {
154  TiXmlElement* tmpnode = node->FirstChildElement("Notice");
155  if (tmpnode)
156  {
157  to.notice << _T("\n") << cbC2U(tmpnode->Attribute("value"));
158  while (to.notice.Replace(_T(" "), _T(" ")))
159  ;
160  to.notice.Replace(_T("\t"), _T(""));
161  to.noticeMsgType = strncmp(tmpnode->Attribute("value"), "0", 1) == 0 ? wxICON_INFORMATION : wxICON_WARNING;
162  }
163 
164  DoOptionProject(node, to);
165  DoOptionCompiler(node, to);
166  DoOptionLinker(node, to);
167  m_TemplateOptions.Add(to);
168  }
169 
170  node = node->NextSiblingElement("Option");
171  }
172 }
173 
174 void ProjectTemplateLoader::DoOptionProject(TiXmlElement* parentNode, TemplateOption& to)
175 {
176  TiXmlElement* node = parentNode->FirstChildElement("Project");
177  if (node)
178  {
179  if (node->Attribute("file"))
180  to.file = cbC2U(node->Attribute("file"));
181  if (node->Attribute("useDefaultCompiler"))
182  to.useDefaultCompiler = strncmp(node->Attribute("useDefaultCompiler"), "0", 1) != 0;
183  }
184 }
185 
186 void ProjectTemplateLoader::DoOptionCompiler(TiXmlElement* parentNode, TemplateOption& to)
187 {
188  TiXmlElement* node = parentNode->FirstChildElement("Compiler");
189  while (node)
190  {
191  if (node->Attribute("flag"))
192  to.extraCFlags.Add(cbC2U(node->Attribute("flag")));
193 
194  node = node->NextSiblingElement("Compiler");
195  }
196 }
197 
198 void ProjectTemplateLoader::DoOptionLinker(TiXmlElement* parentNode, TemplateOption& to)
199 {
200  TiXmlElement* node = parentNode->FirstChildElement("Linker");
201  while (node)
202  {
203  if (node->Attribute("flag"))
204  to.extraLDFlags.Add(cbC2U(node->Attribute("flag")));
205 
206  node = node->NextSiblingElement("Linker");
207  }
208 }
void DoOption(TiXmlElement *parentNode)
#define wxICON_WARNING
FileSetFileArray files
static Manager * Get()
Use Manager::Get() to get a pointer to its instance Manager::Get() is guaranteed to never return an i...
Definition: manager.cpp:182
void DoOptionProject(TiXmlElement *parentNode, TemplateOption &to)
#define _T(string)
void DoFileSet(TiXmlElement *parentNode)
const wxCharBuffer mb_str(const wxMBConv &conv=wxConvLibc) const
#define wxICON_INFORMATION
TemplateOptionArray m_TemplateOptions
void DoFileSetFile(TiXmlElement *parentNode, FileSet &fs)
DLLIMPORT wxString cbC2U(const char *str)
Return str as a proper unicode-compatible string.
Definition: globals.cpp:733
void DoTemplate(TiXmlElement *parentNode)
size_t Replace(const wxString &strOld, const wxString &strNew, bool replaceAll=true)
void DoTemplateNotice(TiXmlElement *parentNode)
LogManager * GetLogManager() const
Definition: manager.cpp:439
wxArrayString extraLDFlags
bool IsEmpty() const
void DoOptionLinker(TiXmlElement *parentNode, TemplateOption &to)
void DebugLog(const wxString &msg, Logger::level lv=Logger::info)
Definition: logmanager.h:146
size_t Add(const wxString &str, size_t copies=1)
void DoOptionCompiler(TiXmlElement *parentNode, TemplateOption &to)
wxArrayString extraCFlags
bool Open(const wxString &filename)
WX_DEFINE_OBJARRAY(FileSetFileArray)