EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
XMLPacketGen.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur
24 */
25 
26 #include "eve-xmlpktgen.h"
27 
28 #include "XMLPacketGen.h"
29 
30 /************************************************************************/
31 /* XMLPacketGen */
32 /************************************************************************/
33 const char* const XMLPacketGen::smGenFileComment =
34 "/* EVEmu: EVE Online Server Emulator\n"
35 " \n"
36 " **************************************************************\n"
37 " This file is automatically generated, DO NOT EDIT IT DIRECTLY.\n"
38 " **************************************************************\n"
39 " \n"
40 " (If you need to customize an object, you must copy that object\n"
41 " into another source file, and give up the ability to generate it)\n"
42 " \n"
43 " \n"
44 " This program is free software; you can redistribute it and/or modify\n"
45 " it under the terms of the GNU General Public License as published by\n"
46 " the Free Software Foundation; version 2 of the License.\n"
47 " \n"
48 " This program is distributed in the hope that it will be useful,\n"
49 " but WITHOUT ANY WARRANTY except by those people which sell it, which\n"
50 " are required to give you total support for your newly bought product;\n"
51 " without even the implied warranty of MERCHANTABILITY or FITNESS FOR\n"
52 " A PARTICULAR PURPOSE. See the GNU General Public License for more details.\n"
53 " \n"
54 " You should have received a copy of the GNU General Public License\n"
55 " along with this program; if not, write to the Free Software\n"
56 " Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n"
57 " \n\n"
58 " Updated by Allan, 2016 - 2020"
59 " \n"
60 "*/";
61 
62 XMLPacketGen::XMLPacketGen( const char* header, const char* source )
63 : mHeaderFile( NULL ),
64  mHeaderFileName( header ),
65  mSourceFile( NULL ),
66  mSourceFileName( source )
67 {
71 }
72 
74 {
75  // Close the files
76  SetHeaderFile( "" );
77  SetSourceFile( "" );
78 }
79 
80 bool XMLPacketGen::ParseElements( const TiXmlElement* field )
81 {
82  if( !OpenFiles() )
83  {
84  sLog.Error( "XMLPacketGen", "Unable to open output files: %s.", strerror( errno ) );
85  return false;
86  }
87 
88  const std::string def = FNameToDef( mHeaderFileName.c_str() );
89 
90  //headers:
91  fprintf( mHeaderFile,
92  "%s\n"
93  "\n"
94  "#ifndef %s\n"
95  "#define %s\n"
96  "\n"
97  "#include \"python/PyVisitor.h\"\n"
98  "#include \"python/PyRep.h\"\n"
99  "\n",
101  def.c_str(),
102  def.c_str()
103  );
104  fprintf( mSourceFile,
105  "%s\n"
106  "\n"
107  "#include \"eve-common.h\"\n"
108  "\n"
109  "#include \"%s\"\n"
110  "\n",
112  mHeaderFileName.c_str()
113  );
114 
115  //content
116  bool res = ParseElementChildren( field );
117 
118  //footers:
119  fprintf( mHeaderFile,
120  "#endif /* !%s */\n"
121  "\n",
122  def.c_str()
123  );
124 
125  return res;
126 }
127 
128 bool XMLPacketGen::ParseInclude( const TiXmlElement* field )
129 {
130  const char* file = field->Attribute( "file" );
131  if( file == NULL )
132  {
133  _log( COMMON__ERROR, "field at line %d is missing the file attribute, skipping.", field->Row() );
134  return false;
135  }
136 
137  fprintf( mHeaderFile,
138  "#include \"%s\"\n"
139  "\n",
140  file
141  );
142  return true;
143 }
144 
145 bool XMLPacketGen::ParseElementDef( const TiXmlElement* field )
146 {
147  bool res = ( mClone.ParseElement( field )
148  && mConstruct.ParseElement( field )
149  && mDecode.ParseElement( field )
150  && mDestruct.ParseElement( field )
151  && mDump.ParseElement( field )
152  && mEncode.ParseElement( field )
153  && mHeader.ParseElement( field ) );
154 
155  return res;
156 }
157 
158 void XMLPacketGen::SetHeaderFile( const char* header )
159 {
160  if( mHeaderFileName != header )
161  {
162  if( NULL != mHeaderFile )
163  {
164  fclose( mHeaderFile );
165  mHeaderFile = NULL;
166 
167  // propagate the change to the generators
168  mHeader.SetOutputFile( NULL );
169  }
170 
171  mHeaderFileName = header;
172  }
173 }
174 
175 void XMLPacketGen::SetSourceFile( const char* source )
176 {
177  if( mSourceFileName != source )
178  {
179  if( NULL != mSourceFile )
180  {
181  fclose( mSourceFile );
182  mSourceFile = NULL;
183 
184  // propagate the change to the generators
185  mClone.SetOutputFile( NULL );
186  mConstruct.SetOutputFile( NULL );
187  mDecode.SetOutputFile( NULL );
188  mDestruct.SetOutputFile( NULL );
189  mDump.SetOutputFile( NULL );
190  mEncode.SetOutputFile( NULL );
191  }
192 
193  mSourceFileName = source;
194  }
195 }
196 
198 {
199  bool res = true;
200 
201  if( !mHeaderFileName.empty() && ( NULL == mHeaderFile ) )
202  {
203  mHeaderFile = fopen( mHeaderFileName.c_str(), "w" );
204 
205  if( NULL == mHeaderFile )
206  res = false;
207  else
208  {
209  // propagate the change to the generators
211  }
212  }
213 
214  if( !mSourceFileName.empty() && ( NULL == mSourceFile ) )
215  {
216  mSourceFile = fopen( mSourceFileName.c_str(), "w" );
217 
218  if( NULL == mSourceFile )
219  res = false;
220  else
221  {
222  // propagate the change to the generators
229  }
230  }
231 
232  return res;
233 }
234 
235 std::string XMLPacketGen::FNameToDef( const char* buf )
236 {
237  std::string res;
238  res += "__";
239 
240  for(; '\0' != *buf; ++buf )
241  {
242  if( !IsPrintable( *buf ) || *buf == '/' || *buf == '\\' || *buf == ':' || *buf == '.' || *buf == '-' )
243  res += '_';
244  else
245  res += (char)toupper( *buf );
246  }
247 
248  res += "__";
249  return res;
250 }
251 
252 
253 
254 
255 
256 
257 
258 
bool ParseElement(const TiXmlElement *element) const
Parses element using registered parsers.
Definition: XMLParser.cpp:61
#define _log(type, fmt,...)
Definition: logsys.h:124
static const char *const smGenFileComment
Definition: XMLPacketGen.h:104
bool ParseElementDef(const TiXmlElement *field)
bool ParseInclude(const TiXmlElement *field)
FILE * mSourceFile
Definition: XMLPacketGen.h:91
ClassCloneGenerator mClone
Definition: XMLPacketGen.h:94
std::string mHeaderFileName
Definition: XMLPacketGen.h:90
void SetOutputFile(FILE *outputFile)
Sets output file.
Definition: Generator.h:53
ClassDumpGenerator mDump
Definition: XMLPacketGen.h:98
bool IsPrintable(const PyString *str)
Checks whether string is printable.
Definition: EVEUtils.cpp:31
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
ClassConstructGenerator mConstruct
Definition: XMLPacketGen.h:95
static std::string FNameToDef(const char *buf)
ClassDecodeGenerator mDecode
Definition: XMLPacketGen.h:96
void SetHeaderFile(const char *header)
Sets header file.
XMLPacketGen(const char *header="", const char *source="")
Primary constructor.
ClassDestructGenerator mDestruct
Definition: XMLPacketGen.h:97
ClassHeaderGenerator mHeader
Definition: XMLPacketGen.h:100
bool OpenFiles()
Opens output files.
void SetSourceFile(const char *source)
Sets source file.
ClassEncodeGenerator mEncode
Definition: XMLPacketGen.h:99
std::string mSourceFileName
Definition: XMLPacketGen.h:92
bool ParseElementChildren(const TiXmlElement *element, size_t max=0) const
Parses element's children using registered parsers.
Definition: XMLParser.cpp:72
~XMLPacketGen()
Destructory; closes output files.
void AddMemberParser(const char *name, T &instance, bool(T::*method)(const TiXmlElement *))
Adds a member parser.
Definition: XMLParserEx.h:55
FILE * mHeaderFile
Definition: XMLPacketGen.h:89
bool ParseElements(const TiXmlElement *field)