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