xref: /trunk/main/xml2cmp/source/support/cmdline.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
1*ab595ff6SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ab595ff6SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ab595ff6SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ab595ff6SAndrew Rist  * distributed with this work for additional information
6*ab595ff6SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ab595ff6SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ab595ff6SAndrew Rist  * "License"); you may not use this file except in compliance
9*ab595ff6SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*ab595ff6SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*ab595ff6SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ab595ff6SAndrew Rist  * software distributed under the License is distributed on an
15*ab595ff6SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ab595ff6SAndrew Rist  * KIND, either express or implied.  See the License for the
17*ab595ff6SAndrew Rist  * specific language governing permissions and limitations
18*ab595ff6SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*ab595ff6SAndrew Rist  *************************************************************/
21*ab595ff6SAndrew Rist 
22*ab595ff6SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "cmdline.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include <ctype.h>
27cdf0e10cSrcweir #include <string.h>
28cdf0e10cSrcweir #include <stdlib.h>
29cdf0e10cSrcweir #include <iostream>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir char C_sUseText[] = "Use: xml2cmp.exe \n"
33cdf0e10cSrcweir                     "        [-func funcFile] \n"
34cdf0e10cSrcweir                     "        [-html htmlFile] \n"
35cdf0e10cSrcweir                     "        [-types typeFile] \n"
36cdf0e10cSrcweir                     "        [-idlpath idlPath] \n"
37cdf0e10cSrcweir                     "        Xml_FileName\n"
38cdf0e10cSrcweir                     " or: xml2cmp.exe \n"
39cdf0e10cSrcweir                     "        -ix \n"
40cdf0e10cSrcweir                     "        sourceDirectory \n"
41cdf0e10cSrcweir                     "        outputDirectory \n"
42cdf0e10cSrcweir                     "        [-idlpath idlPath] \n"
43cdf0e10cSrcweir                     "        tagname [tagname ...]";
44cdf0e10cSrcweir 
45cdf0e10cSrcweir 
46cdf0e10cSrcweir char C_sCmdFunc[]       = "-func";
47cdf0e10cSrcweir char C_sCmdHtml[]       = "-html";
48cdf0e10cSrcweir char C_sCmdType[]       = "-types";
49cdf0e10cSrcweir char C_sCmdIndex[]      = "-ix";
50cdf0e10cSrcweir char C_sCmdIdlPath[]    = "-idlpath";
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 
54cdf0e10cSrcweir bool GetParameter( Simstr & o_rMemory, int & io_nCountArg, int argc, char * argv[] );
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 
CommandLine(int argc,char * argv[])57cdf0e10cSrcweir CommandLine::CommandLine( int           argc,
58cdf0e10cSrcweir                           char *        argv[] )
59cdf0e10cSrcweir     :   bIsOk(true)
60cdf0e10cSrcweir {
61cdf0e10cSrcweir     bool bDisplayUse = false;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir     /* Check command line: */
64cdf0e10cSrcweir     if ( argc < 2 )
65cdf0e10cSrcweir         bDisplayUse = true;
66cdf0e10cSrcweir     else if ( argc == 2 && ! isalnum(argv[1][0]) )
67cdf0e10cSrcweir         bDisplayUse = true;
68cdf0e10cSrcweir     else if ( strcmp( argv[1], C_sCmdIndex ) == 0 && argc < 5 )
69cdf0e10cSrcweir         bDisplayUse = true;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     if (bDisplayUse)
72cdf0e10cSrcweir     {
73cdf0e10cSrcweir         std::cout << C_sUseText << std::endl;
74cdf0e10cSrcweir         bIsOk = false;
75cdf0e10cSrcweir         exit(0);
76cdf0e10cSrcweir     }
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     if ( strcmp( argv[1], C_sCmdIndex ) == 0 )
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         ParseIndexCommand(argc,argv);
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir     else
83cdf0e10cSrcweir     {
84cdf0e10cSrcweir         ParseSingleFileCommand(argc,argv);
85cdf0e10cSrcweir     }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     if ( sXmlSourceFile.l() == 0
88cdf0e10cSrcweir          && sXmlSourceDirectory.l() == 0 )
89cdf0e10cSrcweir     {
90cdf0e10cSrcweir         bIsOk = false;
91cdf0e10cSrcweir     }
92cdf0e10cSrcweir     else if ( sXmlSourceFile.l() > 0
93cdf0e10cSrcweir               && sXmlSourceDirectory.l() > 0 )
94cdf0e10cSrcweir     {
95cdf0e10cSrcweir         bIsOk = false;
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir     else if ( sIndexFile.l() > 0
98cdf0e10cSrcweir               && ( sXmlSourceDirectory.l() == 0
99cdf0e10cSrcweir                    || aTagsInIndex.size() == 0
100cdf0e10cSrcweir                  )
101cdf0e10cSrcweir             )
102cdf0e10cSrcweir     {
103cdf0e10cSrcweir         bIsOk = false;
104cdf0e10cSrcweir     }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir }
107cdf0e10cSrcweir 
~CommandLine()108cdf0e10cSrcweir CommandLine::~CommandLine()
109cdf0e10cSrcweir {
110cdf0e10cSrcweir }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir 
113cdf0e10cSrcweir const char *
ErrorText() const114cdf0e10cSrcweir CommandLine::ErrorText() const
115cdf0e10cSrcweir {
116cdf0e10cSrcweir     static char cOut[] = "Error:  Command line was incorrect. Probably there was a flag without\n"
117cdf0e10cSrcweir                          "        the corresponding parameter. Or there was no XML-sourcefile specified.\n";
118cdf0e10cSrcweir     return cOut;
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
121cdf0e10cSrcweir bool
GetParameter(Simstr & o_pMemory,int & io_pCountArg,int argc,char * argv[])122cdf0e10cSrcweir GetParameter( Simstr &      o_pMemory,
123cdf0e10cSrcweir               int &         io_pCountArg,
124cdf0e10cSrcweir               int           argc,
125cdf0e10cSrcweir               char *        argv[] )
126cdf0e10cSrcweir {
127cdf0e10cSrcweir     io_pCountArg++;
128cdf0e10cSrcweir     if( io_pCountArg < argc )
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir         o_pMemory = argv[io_pCountArg];
131cdf0e10cSrcweir         return true;
132cdf0e10cSrcweir     }
133cdf0e10cSrcweir     return false;
134cdf0e10cSrcweir }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 
137cdf0e10cSrcweir void
ParseIndexCommand(int argc,char * argv[])138cdf0e10cSrcweir CommandLine::ParseIndexCommand( int                 argc,
139cdf0e10cSrcweir                                 char *              argv[] )
140cdf0e10cSrcweir {
141cdf0e10cSrcweir     int nCountArg = 1;
142cdf0e10cSrcweir     bIsOk = GetParameter(
143cdf0e10cSrcweir                 sXmlSourceDirectory,
144cdf0e10cSrcweir                 nCountArg,
145cdf0e10cSrcweir                 argc,
146cdf0e10cSrcweir                 argv  );
147cdf0e10cSrcweir     if (bIsOk)
148cdf0e10cSrcweir         bIsOk = GetParameter(
149cdf0e10cSrcweir                     sOutputDirectory,
150cdf0e10cSrcweir                     nCountArg,
151cdf0e10cSrcweir                     argc,
152cdf0e10cSrcweir                     argv  );
153cdf0e10cSrcweir     if (bIsOk && strcmp( argv[nCountArg+1], C_sCmdIdlPath ) == 0 )
154cdf0e10cSrcweir         bIsOk = GetParameter(
155cdf0e10cSrcweir                     sIdlRootPath,
156cdf0e10cSrcweir                     ++nCountArg,
157cdf0e10cSrcweir                     argc,
158cdf0e10cSrcweir                     argv  );
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     sIndexFile = sOutputDirectory;
161cdf0e10cSrcweir #if defined(WNT) || defined(OS2)
162cdf0e10cSrcweir     sIndexFile+= "\\xmlindex.html";
163cdf0e10cSrcweir #elif defined(UNX)
164cdf0e10cSrcweir     sIndexFile+= "/xmlindex.html";
165cdf0e10cSrcweir #endif
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     for ( ++nCountArg; nCountArg < argc; ++nCountArg )
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir         Simstr sElementName(argv[nCountArg]);
170cdf0e10cSrcweir         aTagsInIndex.push_back( sElementName );
171cdf0e10cSrcweir     }
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir 
175cdf0e10cSrcweir void
ParseSingleFileCommand(int argc,char * argv[])176cdf0e10cSrcweir CommandLine::ParseSingleFileCommand( int                argc,
177cdf0e10cSrcweir                                      char *             argv[] )
178cdf0e10cSrcweir {
179cdf0e10cSrcweir     for ( int nCountArg = 1; nCountArg < argc && bIsOk; ++nCountArg )
180cdf0e10cSrcweir     {
181cdf0e10cSrcweir         if ( strcmp( argv[nCountArg], C_sCmdFunc ) == 0 )
182cdf0e10cSrcweir         {
183cdf0e10cSrcweir             bIsOk = GetParameter(
184cdf0e10cSrcweir                                 sFuncFile,
185cdf0e10cSrcweir                                 nCountArg,
186cdf0e10cSrcweir                                 argc,
187cdf0e10cSrcweir                                 argv  );
188cdf0e10cSrcweir         }
189cdf0e10cSrcweir         else if ( strcmp( argv[nCountArg], C_sCmdHtml ) == 0 )
190cdf0e10cSrcweir         {
191cdf0e10cSrcweir             bIsOk = GetParameter(
192cdf0e10cSrcweir                                 sHtmlFile,
193cdf0e10cSrcweir                                 nCountArg,
194cdf0e10cSrcweir                                 argc,
195cdf0e10cSrcweir                                 argv  );
196cdf0e10cSrcweir         }
197cdf0e10cSrcweir         else if ( strcmp( argv[nCountArg], C_sCmdType ) == 0 )
198cdf0e10cSrcweir         {
199cdf0e10cSrcweir             bIsOk = GetParameter(
200cdf0e10cSrcweir                                 sTypeInfoFile,
201cdf0e10cSrcweir                                 nCountArg,
202cdf0e10cSrcweir                                 argc,
203cdf0e10cSrcweir                                 argv  );
204cdf0e10cSrcweir         }
205cdf0e10cSrcweir         else if ( strcmp( argv[nCountArg], C_sCmdIdlPath ) == 0 )
206cdf0e10cSrcweir         {
207cdf0e10cSrcweir             bIsOk = GetParameter(
208cdf0e10cSrcweir                                 sIdlRootPath,
209cdf0e10cSrcweir                                 nCountArg,
210cdf0e10cSrcweir                                 argc,
211cdf0e10cSrcweir                                 argv  );
212cdf0e10cSrcweir         }
213cdf0e10cSrcweir         else
214cdf0e10cSrcweir         {
215cdf0e10cSrcweir             sXmlSourceFile = argv[nCountArg];
216cdf0e10cSrcweir         }
217cdf0e10cSrcweir     }   /* end for */
218cdf0e10cSrcweir }
219