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