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 "cmdline.hxx"
25
26 #include <ctype.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <iostream>
30
31
32 char C_sUseText[] = "Use: xml2cmp.exe \n"
33 " [-func funcFile] \n"
34 " [-html htmlFile] \n"
35 " [-types typeFile] \n"
36 " [-idlpath idlPath] \n"
37 " Xml_FileName\n"
38 " or: xml2cmp.exe \n"
39 " -ix \n"
40 " sourceDirectory \n"
41 " outputDirectory \n"
42 " [-idlpath idlPath] \n"
43 " tagname [tagname ...]";
44
45
46 char C_sCmdFunc[] = "-func";
47 char C_sCmdHtml[] = "-html";
48 char C_sCmdType[] = "-types";
49 char C_sCmdIndex[] = "-ix";
50 char C_sCmdIdlPath[] = "-idlpath";
51
52
53
54 bool GetParameter( Simstr & o_rMemory, int & io_nCountArg, int argc, char * argv[] );
55
56
CommandLine(int argc,char * argv[])57 CommandLine::CommandLine( int argc,
58 char * argv[] )
59 : bIsOk(true)
60 {
61 bool bDisplayUse = false;
62
63 /* Check command line: */
64 if ( argc < 2 )
65 bDisplayUse = true;
66 else if ( argc == 2 && ! isalnum(argv[1][0]) )
67 bDisplayUse = true;
68 else if ( strcmp( argv[1], C_sCmdIndex ) == 0 && argc < 5 )
69 bDisplayUse = true;
70
71 if (bDisplayUse)
72 {
73 std::cout << C_sUseText << std::endl;
74 bIsOk = false;
75 exit(0);
76 }
77
78 if ( strcmp( argv[1], C_sCmdIndex ) == 0 )
79 {
80 ParseIndexCommand(argc,argv);
81 }
82 else
83 {
84 ParseSingleFileCommand(argc,argv);
85 }
86
87 if ( sXmlSourceFile.l() == 0
88 && sXmlSourceDirectory.l() == 0 )
89 {
90 bIsOk = false;
91 }
92 else if ( sXmlSourceFile.l() > 0
93 && sXmlSourceDirectory.l() > 0 )
94 {
95 bIsOk = false;
96 }
97 else if ( sIndexFile.l() > 0
98 && ( sXmlSourceDirectory.l() == 0
99 || aTagsInIndex.size() == 0
100 )
101 )
102 {
103 bIsOk = false;
104 }
105
106 }
107
~CommandLine()108 CommandLine::~CommandLine()
109 {
110 }
111
112
113 const char *
ErrorText() const114 CommandLine::ErrorText() const
115 {
116 static char cOut[] = "Error: Command line was incorrect. Probably there was a flag without\n"
117 " the corresponding parameter. Or there was no XML-sourcefile specified.\n";
118 return cOut;
119 }
120
121 bool
GetParameter(Simstr & o_pMemory,int & io_pCountArg,int argc,char * argv[])122 GetParameter( Simstr & o_pMemory,
123 int & io_pCountArg,
124 int argc,
125 char * argv[] )
126 {
127 io_pCountArg++;
128 if( io_pCountArg < argc )
129 {
130 o_pMemory = argv[io_pCountArg];
131 return true;
132 }
133 return false;
134 }
135
136
137 void
ParseIndexCommand(int argc,char * argv[])138 CommandLine::ParseIndexCommand( int argc,
139 char * argv[] )
140 {
141 int nCountArg = 1;
142 bIsOk = GetParameter(
143 sXmlSourceDirectory,
144 nCountArg,
145 argc,
146 argv );
147 if (bIsOk)
148 bIsOk = GetParameter(
149 sOutputDirectory,
150 nCountArg,
151 argc,
152 argv );
153 if (bIsOk && strcmp( argv[nCountArg+1], C_sCmdIdlPath ) == 0 )
154 bIsOk = GetParameter(
155 sIdlRootPath,
156 ++nCountArg,
157 argc,
158 argv );
159
160 sIndexFile = sOutputDirectory;
161 #if defined(WNT) || defined(OS2)
162 sIndexFile+= "\\xmlindex.html";
163 #elif defined(UNX)
164 sIndexFile+= "/xmlindex.html";
165 #endif
166
167 for ( ++nCountArg; nCountArg < argc; ++nCountArg )
168 {
169 Simstr sElementName(argv[nCountArg]);
170 aTagsInIndex.push_back( sElementName );
171 }
172 }
173
174
175 void
ParseSingleFileCommand(int argc,char * argv[])176 CommandLine::ParseSingleFileCommand( int argc,
177 char * argv[] )
178 {
179 for ( int nCountArg = 1; nCountArg < argc && bIsOk; ++nCountArg )
180 {
181 if ( strcmp( argv[nCountArg], C_sCmdFunc ) == 0 )
182 {
183 bIsOk = GetParameter(
184 sFuncFile,
185 nCountArg,
186 argc,
187 argv );
188 }
189 else if ( strcmp( argv[nCountArg], C_sCmdHtml ) == 0 )
190 {
191 bIsOk = GetParameter(
192 sHtmlFile,
193 nCountArg,
194 argc,
195 argv );
196 }
197 else if ( strcmp( argv[nCountArg], C_sCmdType ) == 0 )
198 {
199 bIsOk = GetParameter(
200 sTypeInfoFile,
201 nCountArg,
202 argc,
203 argv );
204 }
205 else if ( strcmp( argv[nCountArg], C_sCmdIdlPath ) == 0 )
206 {
207 bIsOk = GetParameter(
208 sIdlRootPath,
209 nCountArg,
210 argc,
211 argv );
212 }
213 else
214 {
215 sXmlSourceFile = argv[nCountArg];
216 }
217 } /* end for */
218 }
219