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