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 
29*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
30*cdf0e10cSrcweir #include "precompiled_shell.hxx"
31*cdf0e10cSrcweir #include <stdexcept>
32*cdf0e10cSrcweir #include <osl/diagnose.h>
33*cdf0e10cSrcweir #include "cmdline.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir //---------------------------------
36*cdf0e10cSrcweir /** Simple command line abstraction
37*cdf0e10cSrcweir */
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir //################################
40*cdf0e10cSrcweir // Creation
41*cdf0e10cSrcweir //################################
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir CommandLine::CommandLine(size_t argc, char* argv[], const std::string& ArgPrefix) :
45*cdf0e10cSrcweir 	m_argc(argc),
46*cdf0e10cSrcweir 	m_argv(argv),
47*cdf0e10cSrcweir 	m_argprefix(ArgPrefix)
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir }
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir //################################
53*cdf0e10cSrcweir // Query
54*cdf0e10cSrcweir //################################
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir /** Return the argument count
58*cdf0e10cSrcweir */
59*cdf0e10cSrcweir size_t CommandLine::get_arg_count() const
60*cdf0e10cSrcweir {
61*cdf0e10cSrcweir 	return m_argc;
62*cdf0e10cSrcweir }
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir /** Return an argument by index
65*cdf0e10cSrcweir 	This method doesn't skip argument
66*cdf0e10cSrcweir 	names if any, so if the second
67*cdf0e10cSrcweir 	argument is an argument name the
68*cdf0e10cSrcweir 	function nevertheless returns it.
69*cdf0e10cSrcweir 
70*cdf0e10cSrcweir 	@precond	0 <= Index < GetArgumentCount
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 	@throws std::out_of_range exception
73*cdf0e10cSrcweir 	if the given index is to high
74*cdf0e10cSrcweir */
75*cdf0e10cSrcweir std::string CommandLine::get_arg(size_t Index) const
76*cdf0e10cSrcweir {
77*cdf0e10cSrcweir 	OSL_PRECOND(Index < m_argc, "Index out of range");
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 	if (Index > (m_argc - 1))
80*cdf0e10cSrcweir 		throw std::out_of_range("Invalid index");
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 	return m_argv[Index];
83*cdf0e10cSrcweir }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir /** Returns all argument name found in the
87*cdf0e10cSrcweir 	command line. An argument will be identified
88*cdf0e10cSrcweir 	by a specified prefix. The standard prefix
89*cdf0e10cSrcweir 	is '-'.
90*cdf0e10cSrcweir 	If the are no argument names the returned
91*cdf0e10cSrcweir 	container is empty.
92*cdf0e10cSrcweir */
93*cdf0e10cSrcweir StringListPtr_t CommandLine::get_arg_names() const
94*cdf0e10cSrcweir {
95*cdf0e10cSrcweir 	StringListPtr_t arg_cont(new StringList_t());
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 	for (size_t i = 0; i < m_argc; i++)
98*cdf0e10cSrcweir 	{
99*cdf0e10cSrcweir 		std::string argn = m_argv[i];
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 		if (is_arg_name(argn))
102*cdf0e10cSrcweir 			arg_cont->push_back(argn);
103*cdf0e10cSrcweir 	}
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 	return arg_cont;
106*cdf0e10cSrcweir }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir /** Returns an argument by name. If there are
109*cdf0e10cSrcweir 	duplicate argument names in the command line,
110*cdf0e10cSrcweir 	the first one wins.
111*cdf0e10cSrcweir 	Argument name an the argument value must be separated
112*cdf0e10cSrcweir 	by spaces. If the argument value starts with an
113*cdf0e10cSrcweir 	argument prefix use quotes else the return value is
114*cdf0e10cSrcweir 	an empty string because the value will be interpreted
115*cdf0e10cSrcweir 	as the next argument name.
116*cdf0e10cSrcweir 	If an argument value contains spaces use quotes.
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 	@precond	GetArgumentNames() -> has element ArgumentName
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 	@throws std::invalid_argument exception
121*cdf0e10cSrcweir 	if the specified argument could not be
122*cdf0e10cSrcweir 	found
123*cdf0e10cSrcweir */
124*cdf0e10cSrcweir std::string CommandLine::get_arg(const std::string& ArgumentName) const
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir 	std::string arg_value;
127*cdf0e10cSrcweir 	size_t i;
128*cdf0e10cSrcweir 	for ( i = 0; i < m_argc; i++)
129*cdf0e10cSrcweir 	{
130*cdf0e10cSrcweir 		std::string arg = m_argv[i];
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 		if (ArgumentName == arg && ((i+1) < m_argc) && !is_arg_name(m_argv[i+1]))
133*cdf0e10cSrcweir 		{
134*cdf0e10cSrcweir 			arg_value = m_argv[i+1];
135*cdf0e10cSrcweir 			break;
136*cdf0e10cSrcweir 		}
137*cdf0e10cSrcweir 	}
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 	if (i == m_argc)
140*cdf0e10cSrcweir 		throw std::invalid_argument("Invalid argument name");
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir 	return arg_value;
143*cdf0e10cSrcweir }
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir //################################
147*cdf0e10cSrcweir // Command
148*cdf0e10cSrcweir //################################
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir /** Set the prefix used to identify arguments in
152*cdf0e10cSrcweir 	the command line.
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 	@precond	prefix is not empty
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 	@throws std::invalid_argument exception if
157*cdf0e10cSrcweir 	the prefix is empty
158*cdf0e10cSrcweir */
159*cdf0e10cSrcweir void CommandLine::set_arg_prefix(const std::string& Prefix)
160*cdf0e10cSrcweir {
161*cdf0e10cSrcweir 	OSL_PRECOND(Prefix.length(), "Empty argument prefix!");
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 	if (0 == Prefix.length())
164*cdf0e10cSrcweir 		throw std::invalid_argument("Empty argument prefix not allowed");
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 	m_argprefix = Prefix;
167*cdf0e10cSrcweir }
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir /** Returns whether a given argument is an argument name
171*cdf0e10cSrcweir */
172*cdf0e10cSrcweir bool CommandLine::is_arg_name(const std::string& Argument) const
173*cdf0e10cSrcweir {
174*cdf0e10cSrcweir 	return (0 == Argument.compare(0, m_argprefix.length(), m_argprefix));
175*cdf0e10cSrcweir }
176