Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / sal / rtl / cmdargs.cxx
blob2d39aebd57c8d31bb3d13a53d0bf17b29e84ef0a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <osl/mutex.hxx>
21 #include <rtl/alloc.h>
22 #include <rtl/process.h>
23 #include <rtl/ustring.hxx>
25 namespace {
27 rtl_uString ** g_ppCommandArgs = nullptr;
28 sal_uInt32 g_nCommandArgCount = 0;
30 struct ArgHolder
32 ~ArgHolder();
35 ArgHolder::~ArgHolder()
37 while (g_nCommandArgCount > 0)
38 rtl_uString_release (g_ppCommandArgs[--g_nCommandArgCount]);
40 free (g_ppCommandArgs);
41 g_ppCommandArgs = nullptr;
44 // The destructor of this static ArgHolder is "activated" by the assignments to
45 // g_ppCommandArgs and g_nCommandArgCount in init():
46 ArgHolder argHolder;
48 void init()
50 osl::MutexGuard guard( osl::Mutex::getGlobalMutex() );
51 if (!g_ppCommandArgs)
53 sal_Int32 i, n = osl_getCommandArgCount();
55 g_ppCommandArgs =
56 static_cast<rtl_uString**>(rtl_allocateZeroMemory (n * sizeof(rtl_uString*)));
57 for (i = 0; i < n; i++)
59 rtl_uString * pArg = nullptr;
60 osl_getCommandArg (i, &pArg);
61 if ((pArg->buffer[0] == '-' || pArg->buffer[0] == '/') &&
62 pArg->buffer[1] == 'e' &&
63 pArg->buffer[2] == 'n' &&
64 pArg->buffer[3] == 'v' &&
65 pArg->buffer[4] == ':' &&
66 rtl_ustr_indexOfChar (&(pArg->buffer[5]), '=') >= 0 )
68 // ignore.
69 rtl_uString_release (pArg);
71 else
73 // assign.
74 g_ppCommandArgs[g_nCommandArgCount++] = pArg;
82 oslProcessError SAL_CALL rtl_getAppCommandArg (
83 sal_uInt32 nArg, rtl_uString **ppCommandArg)
85 init();
86 oslProcessError result = osl_Process_E_NotFound;
87 if( nArg < g_nCommandArgCount )
89 rtl_uString_assign( ppCommandArg, g_ppCommandArgs[nArg] );
90 result = osl_Process_E_None;
92 return result;
95 sal_uInt32 SAL_CALL rtl_getAppCommandArgCount()
97 init();
98 return g_nCommandArgCount;
101 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */