xref: /aoo41x/main/xml2cmp/source/xcd/main.cxx (revision ab595ff6)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  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,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <iostream>
25 #include <fstream>
26 #include <stdio.h>
27 
28 
29 #include <string.h>
30 #include "../support/cmdline.hxx"
31 #include "cr_metho.hxx"
32 #include "cr_html.hxx"
33 #include "cr_index.hxx"
34 #include "xmlelem.hxx"
35 #include "xmltree.hxx"
36 #include "parse.hxx"
37 #include "../support/syshelp.hxx"
38 #include "../support/heap.hxx"
39 
40 
41 
42 int      				Do_IndexCommandLine(
43 							const CommandLine &		i_rCommandLine );
44 int      				Do_SingleFileCommandLine(
45 							const CommandLine &		i_rCommandLine );
46 void					Create_TypeInfo(
47 							const char *		  	o_sOutputFile,
48 							ModuleDescription &	    i_rData );
49 
50 
51 int
52 #ifdef WNT
53 _cdecl
54 #endif
main(int argc,char * argv[])55 main( int 		argc,
56 	  char *    argv[] )
57 {
58 	// Variables
59 	CommandLine				aCommandLine(argc, argv);
60 	int ret = 0;
61 
62 	if (! aCommandLine.IsOk())
63 	{
64         std::cerr <<  aCommandLine.ErrorText() << std::endl ;
65 		return 1;
66 	}
67 
68 	if ( aCommandLine.IsIndexCommand() )
69 		ret = Do_IndexCommandLine(aCommandLine);
70 	else
71 		ret = Do_SingleFileCommandLine(aCommandLine);
72 
73 	return ret;
74 }
75 
76 
77 int
Do_SingleFileCommandLine(const CommandLine & i_rCommandLine)78 Do_SingleFileCommandLine(const CommandLine & i_rCommandLine)
79 {
80 	ModuleDescription	aDescr;
81 	X2CParser			aParser(aDescr);
82 
83 	// Load file and create Function-file
84 	bool bLoadResult = aParser.LoadFile(i_rCommandLine.XmlSrcFile());
85 	if (! bLoadResult)
86 	{
87 		std::cerr << "Error: File " << i_rCommandLine.XmlSrcFile() << " could not be loaded." << std::endl;
88 		return 1;
89 	}
90 
91 	if ( strlen(i_rCommandLine.FuncFile()) > 0 )
92 	{
93 		Create_AccessMethod( i_rCommandLine.FuncFile(),
94 							 aParser.PureText() );
95 
96     	std::cout << "File "
97 	    	 << i_rCommandLine.FuncFile()
98 		     << " with component_getDescriptionFunc() is created now."
99              << std::endl;
100 	}
101 
102 	// Parse
103 	aParser.Parse();
104 
105 	// Produce output
106 	if ( strlen(i_rCommandLine.HtmlFile()) > 0 )
107 	{
108 		HtmlCreator aHtmlCreator( i_rCommandLine.HtmlFile(),
109 								  aDescr,
110 								  i_rCommandLine.IdlRootPath() );
111 		aHtmlCreator.Run();
112 	}
113 
114 	if (strlen(i_rCommandLine.TypeInfoFile()) > 0)
115 	{
116 		Create_TypeInfo( i_rCommandLine.TypeInfoFile(),
117 						 aDescr );
118 	}
119 
120 	return 0;
121 };
122 
123 int
Do_IndexCommandLine(const CommandLine & i_rCommandLine)124 Do_IndexCommandLine(const CommandLine & i_rCommandLine)
125 {
126 	// Parsen files:
127 	List<Simstr>	aFiles;
128 	Index	        aIndex( i_rCommandLine.OutputDirectory(),
129 							i_rCommandLine.IdlRootPath(),
130 							i_rCommandLine.IndexedTags() );
131 
132 	std::cout << "Gather xml-files ..." << std::endl;
133 	GatherFileNames( aFiles, i_rCommandLine.XmlSrcDirectory() );
134 
135 	std::cout << "Create output ..." << std::endl;
136 	aIndex.GatherData(aFiles);
137 	aIndex.WriteOutput( i_rCommandLine.IndexOutputFile() );
138 
139 	std::cout << "... done." << std::endl;
140 
141 	return 0;
142 };
143 
144 
145 
146 //********************      Creating of typeinfo       ********************//
147 
148 
149 void					Put2StdOut_TypeInfo(
150 							ModuleDescription &	    i_rData );
151 void					Put2File_TypeInfo(
152                             const char *            i_sOutputFile,
153 							ModuleDescription &	    i_rData );
154 void					StreamOut_TypeInfo(
155                             std::ostream &               o_rOut,
156 							ModuleDescription &	    i_rData,
157                             const char *            i_sSeparator );
158 
159 
160 
161 
162 void
Create_TypeInfo(const char * o_sOutputFile,ModuleDescription & i_rData)163 Create_TypeInfo( const char *	   		o_sOutputFile,
164 				 ModuleDescription &	i_rData )
165 {
166     if ( strcmp(o_sOutputFile, "stdout") == 0 )
167         Put2StdOut_TypeInfo(i_rData);
168     else
169         Put2File_TypeInfo(o_sOutputFile,i_rData);
170 
171 #if 0
172 	std::ofstream aOut(o_sOutputFile, std::ios::out
173 #if defined(WNT) || defined(OS2)
174 											   | std::ios::binary
175 #endif
176 	);
177 	if ( !aOut )
178 	{
179 		std::cerr << "Error: " << o_sOutputFile << " could not be created." << std::endl;
180 		return;
181 	}
182 
183 	Heap 	aTypesHeap(12);
184 	Simstr	sLibPrefix = i_rData.ModuleName();
185 
186 	// Gather types:
187 	List< const MultipleTextElement * > aTypes;
188 	i_rData.Get_Types(aTypes);
189 
190 	for ( unsigned t = 0; t < aTypes.size(); ++t )
191 	{
192 		unsigned i_max = aTypes[t]->Size();
193 		for ( unsigned  i = 0; i < i_max; ++i )
194 		{
195 			aTypesHeap.InsertValue( aTypes[t]->Data(i), "" );
196 		}  // end for
197 	}
198 
199 	// Write types:
200 	WriteStr( aOut, sLibPrefix );
201 	WriteStr( aOut, "_XML2CMPTYPES= ");
202 
203 	HeapItem * pLastHeapTop = 0;
204 	for ( HeapItem * pHeapTop = aTypesHeap.ReleaseTop(); pHeapTop != 0; pHeapTop = aTypesHeap.ReleaseTop() )
205 	{
206 		if (pLastHeapTop != 0)
207 		{
208 			if ( 0 == strcmp(pHeapTop->Key(), pLastHeapTop->Key()) )
209 				continue;
210 			delete pLastHeapTop;
211 			// pLastHeapTop = 0;
212 		}
213 		pLastHeapTop = pHeapTop;
214 
215 		WriteStr( aOut, "\t\\\n\t\t" );
216 
217 		const char * sEnd = strchr( pHeapTop->Key(), ' ' );
218 		if (sEnd != 0)
219 		{
220 			const char * sQuali = strrchr( pHeapTop->Key(), ' ' )+1;
221 			WriteStr( aOut, sQuali );
222 			WriteStr( aOut, "." );
223 			aOut.write( pHeapTop->Key(), sEnd - pHeapTop->Key() );
224 		}
225 		else
226 			WriteStr( aOut, pHeapTop->Key() );
227 	}	// end for
228 
229 	if (pLastHeapTop != 0)
230 	{
231 		delete pLastHeapTop;
232 		pLastHeapTop = 0;
233 	}
234 
235 	aOut.close();
236 #endif // 0
237 }
238 
239 void
Put2StdOut_TypeInfo(ModuleDescription & i_rData)240 Put2StdOut_TypeInfo( ModuleDescription &	i_rData )
241 {
242     StreamOut_TypeInfo(std::cout, i_rData, " ");
243 }
244 
245 void
Put2File_TypeInfo(const char * i_sOutputFile,ModuleDescription & i_rData)246 Put2File_TypeInfo( const char *            i_sOutputFile,
247 				   ModuleDescription &	   i_rData )
248 {
249 	std::ofstream aOut(i_sOutputFile, std::ios::out
250 #if defined(WNT) || defined(OS2)
251 											   | std::ios::binary
252 #endif
253 	);
254 	if ( !aOut )
255 	{
256 		std::cerr << "Error: " << i_sOutputFile << " could not be created." << std::endl;
257 		return;
258 	}
259 
260 	Simstr	sLibPrefix = i_rData.ModuleName();
261 	WriteStr( aOut, sLibPrefix );
262 	WriteStr( aOut, "_XML2CMPTYPES= ");
263 
264     StreamOut_TypeInfo(aOut, i_rData, "\t\\\n\t\t");
265 
266     aOut.close();
267 }
268 
269 void
StreamOut_TypeInfo(std::ostream & o_rOut,ModuleDescription & i_rData,const char * i_sSeparator)270 StreamOut_TypeInfo( std::ostream &               o_rOut,
271 				    ModuleDescription &	    i_rData,
272                     const char *            i_sSeparator )
273 {
274 	Heap 	aTypesHeap(12);
275 
276 	// Gather types:
277 	List< const MultipleTextElement * > aTypes;
278 	i_rData.Get_Types(aTypes);
279 
280 	for ( unsigned t = 0; t < aTypes.size(); ++t )
281 	{
282 		unsigned i_max = aTypes[t]->Size();
283 		for ( unsigned  i = 0; i < i_max; ++i )
284 		{
285 			aTypesHeap.InsertValue( aTypes[t]->Data(i), "" );
286 		}  // end for
287 	}
288 
289 	// Write types:
290 	HeapItem * pLastHeapTop = 0;
291 	for ( HeapItem * pHeapTop = aTypesHeap.ReleaseTop(); pHeapTop != 0; pHeapTop = aTypesHeap.ReleaseTop() )
292 	{
293 		if (pLastHeapTop != 0)
294 		{
295 			if ( 0 == strcmp(pHeapTop->Key(), pLastHeapTop->Key()) )
296 				continue;
297 			delete pLastHeapTop;
298 			// pLastHeapTop = 0;
299 		}
300 		pLastHeapTop = pHeapTop;
301 
302 		WriteStr( o_rOut, i_sSeparator );
303 
304 		const char * sEnd = strchr( pHeapTop->Key(), ' ' );
305 		if (sEnd != 0)
306 		{
307 			const char * sQuali = strrchr( pHeapTop->Key(), ' ' ) + 1;
308 			WriteStr( o_rOut, sQuali );
309 			WriteStr( o_rOut, "." );
310 			o_rOut.write( pHeapTop->Key(), sEnd - pHeapTop->Key() );
311 		}
312 		else
313 			WriteStr( o_rOut, pHeapTop->Key() );
314 	}	// end for
315 
316 	if (pLastHeapTop != 0)
317 	{
318 		delete pLastHeapTop;
319 		pLastHeapTop = 0;
320 	}
321 }
322 
323