xref: /aoo41x/main/sal/rtl/source/cmdargs.cxx (revision 87d2adbc)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sal.hxx"
26 
27 #include <osl/mutex.hxx>
28 #include <rtl/process.h>
29 #include <rtl/ustring.hxx>
30 
31 namespace {
32 
33 rtl_uString ** g_ppCommandArgs = 0;
34 sal_uInt32     g_nCommandArgCount = 0;
35 
36 struct ArgHolder
37 {
38 	~ArgHolder();
39 };
40 
~ArgHolder()41 ArgHolder::~ArgHolder()
42 {
43 	while (g_nCommandArgCount > 0)
44 		rtl_uString_release (g_ppCommandArgs[--g_nCommandArgCount]);
45 
46 	rtl_freeMemory (g_ppCommandArgs);
47 	g_ppCommandArgs = 0;
48 }
49 
50 // The destructor of this static ArgHolder is "activated" by the assignments to
51 // g_ppCommandArgs and g_nCommandArgCount in init():
52 ArgHolder argHolder;
53 
init()54 void init()
55 {
56 	osl::MutexGuard guard( osl::Mutex::getGlobalMutex() );
57 	if (!g_ppCommandArgs)
58 	{
59 		sal_Int32 i, n = osl_getCommandArgCount();
60 
61 		g_ppCommandArgs =
62 			(rtl_uString**)rtl_allocateZeroMemory (n * sizeof(rtl_uString*));
63 		for (i = 0; i < n; i++)
64 		{
65 			rtl_uString * pArg = 0;
66 			osl_getCommandArg (i, &pArg);
67 			if (('-' == pArg->buffer[0] || '/' == pArg->buffer[0]) &&
68 				 'e' == pArg->buffer[1] &&
69 				 'n' == pArg->buffer[2] &&
70 				 'v' == pArg->buffer[3] &&
71 				 ':' == pArg->buffer[4] &&
72 				rtl_ustr_indexOfChar (&(pArg->buffer[5]), '=') >= 0 )
73 			{
74 				// ignore.
75 				rtl_uString_release (pArg);
76 			}
77 			else
78 			{
79 				// assign.
80 				g_ppCommandArgs[g_nCommandArgCount++] = pArg;
81 			}
82 		}
83 	}
84 }
85 
86 }
87 
rtl_getAppCommandArg(sal_uInt32 nArg,rtl_uString ** ppCommandArg)88 oslProcessError SAL_CALL rtl_getAppCommandArg (
89 	sal_uInt32 nArg, rtl_uString **ppCommandArg)
90 {
91     init();
92 	oslProcessError result = osl_Process_E_NotFound;
93 	if( nArg < g_nCommandArgCount )
94 	{
95  		rtl_uString_assign( ppCommandArg, g_ppCommandArgs[nArg] );
96 		result = osl_Process_E_None;
97 	}
98 	return (result);
99 }
100 
rtl_getAppCommandArgCount(void)101 sal_uInt32 SAL_CALL rtl_getAppCommandArgCount (void)
102 {
103     init();
104 	return g_nCommandArgCount;
105 }
106