Avoid infinite loop in AddPixelsWhile when removing Sheet
[LibreOffice.git] / sal / rtl / cmdargs.cxx
blob8eb328258dfec8c11ce31bd9eeaa63c3ddcba2d0
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)
52 return;
54 sal_Int32 i, n = osl_getCommandArgCount();
56 g_ppCommandArgs =
57 static_cast<rtl_uString**>(rtl_allocateZeroMemory (n * sizeof(rtl_uString*)));
58 for (i = 0; i < n; i++)
60 rtl_uString * pArg = nullptr;
61 osl_getCommandArg (i, &pArg);
62 if ((pArg->buffer[0] == '-' || pArg->buffer[0] == '/') &&
63 pArg->buffer[1] == 'e' &&
64 pArg->buffer[2] == 'n' &&
65 pArg->buffer[3] == 'v' &&
66 pArg->buffer[4] == ':' &&
67 rtl_ustr_indexOfChar (&(pArg->buffer[5]), '=') >= 0 )
69 // ignore.
70 rtl_uString_release (pArg);
72 else
74 // assign.
75 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: */