xref: /AOO42X/main/unodevtools/source/unodevtools/options.cxx (revision b1c5455db1639c48e26c568e4fa7ee78ca5d60ee)
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 <stdio.h>
25 
26 #include "rtl/ustring.hxx"
27 #include "rtl/ustrbuf.hxx"
28 #include "rtl/process.h"
29 
30 #include "com/sun/star/uno/RuntimeException.hpp"
31 
32 using namespace ::rtl;
33 using namespace ::com::sun::star::uno;
34 
35 namespace unodevtools {
36 
37 //-------------------------------------------------------------------------------
38 #if OSL_DEBUG_LEVEL > 1
out(const sal_Char * pText)39 static void out( const sal_Char * pText )
40 {
41     fprintf( stderr, pText );
42 }
43 #endif
44 
45 //-------------------------------------------------------------------------------
readOption(OUString * pValue,const sal_Char * pOpt,sal_Int32 * pnIndex,const OUString & aArg)46 sal_Bool readOption( OUString * pValue, const sal_Char * pOpt,
47                      sal_Int32 * pnIndex, const OUString & aArg)
48     throw (RuntimeException)
49 {
50     const OUString dash = OUString(RTL_CONSTASCII_USTRINGPARAM("-"));
51     if(aArg.indexOf(dash) != 0)
52         return sal_False;
53 
54     OUString aOpt = OUString::createFromAscii( pOpt );
55 
56     if (aArg.getLength() < aOpt.getLength())
57         return sal_False;
58 
59     if (aOpt.equalsIgnoreAsciiCase( aArg.copy(1) )) {
60         // take next argument
61         ++(*pnIndex);
62 
63         rtl_getAppCommandArg(*pnIndex, &pValue->pData);
64         if (*pnIndex >= (sal_Int32)rtl_getAppCommandArgCount() ||
65             pValue->copy(1).equals(dash))
66         {
67             OUStringBuffer buf( 32 );
68             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("incomplete option \"-") );
69             buf.appendAscii( pOpt );
70             buf.appendAscii( RTL_CONSTASCII_STRINGPARAM("\" given!") );
71             throw RuntimeException( buf.makeStringAndClear(), Reference< XInterface >() );
72         } else {
73 #if OSL_DEBUG_LEVEL > 1
74             out( "\n> identified option -" );
75             out( pOpt );
76             out( " = " );
77             OString tmp = OUStringToOString(*pValue, RTL_TEXTENCODING_ASCII_US);
78             out( tmp.getStr() );
79 #endif
80             ++(*pnIndex);
81             return sal_True;
82         }
83     } else if (aArg.indexOf(aOpt) == 1) {
84         *pValue = aArg.copy(1 + aOpt.getLength());
85 #if OSL_DEBUG_LEVEL > 1
86         out( "\n> identified option -" );
87         out( pOpt );
88         out( " = " );
89         OString tmp = OUStringToOString(*pValue, RTL_TEXTENCODING_ASCII_US);
90         out( tmp.getStr() );
91 #endif
92         ++(*pnIndex);
93 
94         return sal_True;
95     }
96     return sal_False;
97 }
98 
99 //-------------------------------------------------------------------------------
readOption(sal_Bool * pbOpt,const sal_Char * pOpt,sal_Int32 * pnIndex,const OUString & aArg)100 sal_Bool readOption( sal_Bool * pbOpt, const sal_Char * pOpt,
101                      sal_Int32 * pnIndex, const OUString & aArg)
102 {
103     const OUString dashdash(RTL_CONSTASCII_USTRINGPARAM("--"));
104     const OUString dash(RTL_CONSTASCII_USTRINGPARAM("-"));
105     OUString aOpt = OUString::createFromAscii(pOpt);
106 
107     if((aArg.indexOf(dash) == 0 && aOpt.equalsIgnoreAsciiCase(aArg.copy(1))) ||
108        (aArg.indexOf(dashdash) == 0 && aOpt.equalsIgnoreAsciiCase(aArg.copy(2))) )
109     {
110         ++(*pnIndex);
111         *pbOpt = sal_True;
112 #if OSL_DEBUG_LEVEL > 1
113         out( "\n> identified option --" );
114         out( pOpt );
115 #endif
116         return sal_True;
117     }
118     return sal_False;
119 }
120 
121 } // end of namespace unodevtools
122