xref: /aoo41x/main/xml2cmp/source/support/cmdline.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "cmdline.hxx"
29 
30 #include <ctype.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <iostream>
34 
35 
36 char C_sUseText[] = "Use: xml2cmp.exe \n"
37 					"        [-func funcFile] \n"
38 					"        [-html htmlFile] \n"
39 					"        [-types typeFile] \n"
40 					"        [-idlpath idlPath] \n"
41 					"        Xml_FileName\n"
42 					" or: xml2cmp.exe \n"
43 					"        -ix \n"
44 					"        sourceDirectory \n"
45 					"        outputDirectory \n"
46 					"        [-idlpath idlPath] \n"
47 					"        tagname [tagname ...]";
48 
49 
50 char C_sCmdFunc[] 		= "-func";
51 char C_sCmdHtml[]       = "-html";
52 char C_sCmdType[]       = "-types";
53 char C_sCmdIndex[]      = "-ix";
54 char C_sCmdIdlPath[]    = "-idlpath";
55 
56 
57 
58 bool GetParameter( Simstr & o_rMemory, int & io_nCountArg, int argc, char * argv[] );
59 
60 
61 CommandLine::CommandLine( int			argc,
62 						  char *		argv[] )
63 	:	bIsOk(true)
64 {
65 	bool bDisplayUse = false;
66 
67 	/* Check command line: */
68 	if ( argc < 2 )
69 		bDisplayUse = true;
70 	else if ( argc == 2 && ! isalnum(argv[1][0]) )
71 		bDisplayUse = true;
72 	else if ( strcmp( argv[1], C_sCmdIndex ) == 0 && argc < 5 )
73 		bDisplayUse = true;
74 
75 	if (bDisplayUse)
76 	{
77 		std::cout << C_sUseText << std::endl;
78 		bIsOk = false;
79 		exit(0);
80 	}
81 
82 	if ( strcmp( argv[1], C_sCmdIndex ) == 0 )
83 	{
84 		ParseIndexCommand(argc,argv);
85 	}
86 	else
87 	{
88 		ParseSingleFileCommand(argc,argv);
89 	}
90 
91 	if ( sXmlSourceFile.l() == 0
92 		 && sXmlSourceDirectory.l() == 0 )
93 	{
94 		bIsOk = false;
95 	}
96 	else if ( sXmlSourceFile.l() > 0
97 			  && sXmlSourceDirectory.l() > 0 )
98 	{
99 		bIsOk = false;
100 	}
101 	else if ( sIndexFile.l() > 0
102 			  && ( sXmlSourceDirectory.l() == 0
103 				   || aTagsInIndex.size() == 0
104 				 )
105 			)
106 	{
107 		bIsOk = false;
108 	}
109 
110 }
111 
112 CommandLine::~CommandLine()
113 {
114 }
115 
116 
117 const char *
118 CommandLine::ErrorText() const
119 {
120 	static char cOut[] = "Error:  Command line was incorrect. Probably there was a flag without\n"
121 						 "        the corresponding parameter. Or there was no XML-sourcefile specified.\n";
122 	return cOut;
123 }
124 
125 bool
126 GetParameter( Simstr &      o_pMemory,
127 			  int & 	    io_pCountArg,
128 			  int           argc,
129 			  char *        argv[] )
130 {
131 	io_pCountArg++;
132 	if( io_pCountArg < argc )
133 	{
134 		o_pMemory = argv[io_pCountArg];
135 		return true;
136 	}
137 	return false;
138 }
139 
140 
141 void
142 CommandLine::ParseIndexCommand( int					argc,
143 								char *				argv[] )
144 {
145 	int nCountArg = 1;
146 	bIsOk = GetParameter(
147 				sXmlSourceDirectory,
148 				nCountArg,
149 				argc,
150 				argv  );
151 	if (bIsOk)
152 		bIsOk = GetParameter(
153 					sOutputDirectory,
154 					nCountArg,
155 					argc,
156 					argv  );
157 	if (bIsOk && strcmp( argv[nCountArg+1], C_sCmdIdlPath ) == 0 )
158 		bIsOk = GetParameter(
159 					sIdlRootPath,
160 					++nCountArg,
161 					argc,
162 					argv  );
163 
164 	sIndexFile = sOutputDirectory;
165 #if defined(WNT) || defined(OS2)
166 	sIndexFile+= "\\xmlindex.html";
167 #elif defined(UNX)
168 	sIndexFile+= "/xmlindex.html";
169 #endif
170 
171 	for ( ++nCountArg; nCountArg < argc; ++nCountArg )
172 	{
173 		Simstr sElementName(argv[nCountArg]);
174 		aTagsInIndex.push_back( sElementName );
175 	}
176 }
177 
178 
179 void
180 CommandLine::ParseSingleFileCommand( int				argc,
181 									 char *				argv[] )
182 {
183 	for ( int nCountArg = 1; nCountArg < argc && bIsOk; ++nCountArg )
184 	{
185 		if ( strcmp( argv[nCountArg], C_sCmdFunc ) == 0 )
186 		{
187 			bIsOk = GetParameter(
188 								sFuncFile,
189 								nCountArg,
190 								argc,
191 								argv  );
192 		}
193 		else if ( strcmp( argv[nCountArg], C_sCmdHtml ) == 0 )
194 		{
195 			bIsOk = GetParameter(
196 								sHtmlFile,
197 								nCountArg,
198 								argc,
199 								argv  );
200 		}
201 		else if ( strcmp( argv[nCountArg], C_sCmdType ) == 0 )
202 		{
203 			bIsOk = GetParameter(
204 								sTypeInfoFile,
205 								nCountArg,
206 								argc,
207 								argv  );
208 		}
209 		else if ( strcmp( argv[nCountArg], C_sCmdIdlPath ) == 0 )
210 		{
211 			bIsOk = GetParameter(
212 								sIdlRootPath,
213 								nCountArg,
214 								argc,
215 								argv  );
216 		}
217 		else
218 		{
219 			sXmlSourceFile = argv[nCountArg];
220 		}
221 	}  	/* end for */
222 }
223