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