1 /***************************************************************************
2             GenerateMethod.cpp  -  generate class with rpc methods
3                              -------------------
4     begin                : Sun June 03 2007
5     email                : ulxmlrpcpp@ewald-arnold.de
6 
7     $Id: GenerateMethod.cpp 1020 2007-07-23 09:09:26Z ewald-arnold $
8 
9  ***************************************************************************/
10 
11 /**************************************************************************
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License as
15  * published by the Free Software Foundation; either version 2 of the License,
16  * or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  *
27  ***************************************************************************/
28 
29 #include "GenerateMethod.h"
30 #include "UlxrIdlClass.h"
31 
32 #include <iostream>
33 #include <fstream>
34 
35 #include <sys/stat.h>
36 
37 #include <ulxmlrpcpp/ulxr_http_protocol.h>
38 
39 #ifdef _MSC_VER
40 #  include <ulxmlrpcpp/ulxr_config-msvc.h>
41 
42 #elif defined(__BORLANDC__)
43 # include <ulxmlrpcpp/ulxr_config-bcb5.h>
44 
45 #elif defined(__CYGWIN__)
46 # ifdef HAVE_CONFIG_H
47 #  include <config.h>
48 # else
49 #  include <ulxmlrpcpp/ulxr_config.h>
50 # endif
51 #else
52 
53 # ifdef HAVE_CONFIG_H
54 #  include <config.h>
55 # else
56 #  include <ulxmlrpcpp/ulxr_config.h>
57 # endif
58 #endif
59 
60 
61 bool GenerateMethod::date_output = true;
62 
63 
setDateOutput(bool output)64 void GenerateMethod::setDateOutput(bool output)
65 {
66   date_output = output;
67 }
68 
69 
~GenerateMethod()70 GenerateMethod::~GenerateMethod()
71 {
72 }
73 
dateString()74 std::string GenerateMethod::dateString()
75 {
76   if (date_output)
77     return std::string("// Generated: ") + ulxr::getLatin1(ulxr::HttpProtocol::getDateStr()) + "\n";
78   else
79     return "";
80 
81 }
82 
83 
generateHeaderHead(std::ostream & h_file,const std::string & name)84 void GenerateMethod::generateHeaderHead(std::ostream & h_file,
85                                         const std::string &name)
86 {
87   h_file << "#ifndef " << name + "_H\n"
88          << "#define " << name + "_H\n"
89          << "\n"
90            << "///////////////////////////////////////////////////////////////////\n"
91            << "// Automatically generated by xml2ulxr v" << ULXR_VERSION << ".\n"
92            << dateString()
93            << "//\n"
94            << "// xml2ulxr is part of Ultra Lightweight XML RPC for C++.\n"
95            << "// See also http://ulxmlrpcpp.sourceforge.net\n"
96            << "//\n"
97            << "// Don't edit manually unless you know what you are doing\n"
98            << "///////////////////////////////////////////////////////////////////\n"
99          << "\n\n";
100 
101   h_file << "//#define ULXR_UNICODE_ONLY_HELPERS\n"
102             "#include <ulxmlrpcpp/ulxmlrpcpp.h> // always first\n\n";
103 }
104 
105 
generateHeaderTail(std::ostream & h_file,const std::string & name)106 void GenerateMethod::generateHeaderTail(std::ostream & h_file,
107                                         const std::string &name)
108 {
109   h_file << "};\n\n";
110   h_file << "#endif // " << name + "_H\n\n";
111 }
112 
113 
generateSourceHead(std::ostream & cpp_file,const std::string & h_name)114 void GenerateMethod::generateSourceHead(std::ostream & cpp_file,
115                                         const std::string &h_name)
116 {
117   cpp_file << "///////////////////////////////////////////////////////////////////\n"
118            << "// Automatically generated by xml2ulxr v" << ULXR_VERSION << ".\n"
119            << dateString()
120            << "//\n"
121            << "// xml2ulxr is part of Ultra Lightweight XML RPC for C++.\n"
122            << "// See also http://ulxmlrpcpp.sourceforge.net\n"
123            << "//\n"
124            << "// Don't edit manually unless you know what you are doing\n"
125            << "///////////////////////////////////////////////////////////////////\n"
126            << "\n";
127 
128   cpp_file << "//#define ULXR_UNICODE_ONLY_HELPERS\n"
129               "#include <ulxmlrpcpp/ulxmlrpcpp.h> // always first\n\n";
130 
131   cpp_file << "#include \"" << h_name << "\"\n\n";
132 }
133 
134 
generateUserSourceHead(std::ostream & cpp_file,const std::string & h_name)135 void GenerateMethod::generateUserSourceHead(std::ostream & cpp_file,
136                                             const std::string &h_name)
137 {
138   cpp_file << "///////////////////////////////////////////////////////////////////\n"
139            << "// Automatically generated by xml2ulxr v" << ULXR_VERSION << ".\n"
140            << dateString()
141            << "//\n"
142            << "// xml2ulxr is part of Ultra Lightweight XML RPC for C++.\n"
143            << "// See also http://ulxmlrpcpp.sourceforge.net\n"
144            << "//\n"
145            << "// Intended for manual and persistent changes \n"
146            << "/////////////////////////////////////////////////\n"
147            << "\n";
148 
149   cpp_file << "//#define ULXR_UNICODE_ONLY_HELPERS\n"
150               "#include <ulxmlrpcpp/ulxmlrpcpp.h> // always first\n\n";
151   cpp_file << "#include <ulxmlrpcpp/ulxr_value.h>\n";
152   cpp_file << "#include \"" << h_name << "\"\n\n";
153 }
154 
155 
generate_NameDefines(const std::string & destdir,const std::string & name,const std::vector<Method> & theMethods,const std::string & suffix)156 void GenerateMethod::generate_NameDefines(const std::string &destdir,
157                                           const std::string &name,
158                                           const std::vector<Method> &theMethods,
159                                           const std::string &suffix)
160 {
161   std::string h_name = destdir + name + "_ulxr_names.h";
162 
163   struct stat statbuf;
164   if (stat(h_name.c_str(), &statbuf) >= 0)
165   {
166     std::cout << "User file already exists: " << h_name << std::endl;
167     h_name += ".new";
168     std::cout << "New template will be created: " << h_name << std::endl;
169   }
170 
171   std::ofstream h_file(h_name.c_str());
172   std::cout << "Header file will be created: " << h_name << std::endl;
173 
174   h_file << "///////////////////////////////////////////////////////////////////\n"
175          << "// Automatically generated by xml2ulxr v" << ULXR_VERSION << ".\n"
176          << dateString()
177          << "//\n"
178          << "// xml2ulxr is part of Ultra Lightweight XML RPC for C++.\n"
179          << "// See also http://ulxmlrpcpp.sourceforge.net\n"
180          << "//\n"
181          << "// Intended for manual and persistent changes \n"
182          << "/////////////////////////////////////////////////\n"
183          << "\n";
184 
185   h_file << "#ifndef " << name + "_names_H\n"
186          << "#define " << name + "_names_H\n\n\n"
187          << "// method names\n\n";
188 
189   for (unsigned i = 0; i < theMethods.size(); ++i)
190   {
191     Method method = theMethods[i];
192     method.extractNamespace();
193 
194     h_file << "// mapped to: " << method.getCppString(0, true, "");
195     if (method.getName() != method.getOverloadName())
196        h_file << " (there are overloaded methods)";
197     h_file << "\n";
198 
199     h_file << "#define ULXR_CALLTO_" << suffix << method.getOverloadName(true, "_", "")
200            << " \\\n  ULXR_PCHAR(\"" << name << "_" << method.getOverloadName(false) << "\")\n\n";
201   }
202 
203   h_file << "\n#endif // " << name + "_H\n";
204 }
205 
206 
207 
208