2
* Copyright (C) 2015 Canonical, Ltd.
4
* This program is free software: you can redistribute it and/or modify it under
5
* the terms of the GNU Lesser General Public License version 3, as published by
6
* the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful, but WITHOUT
9
* ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10
* SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
* Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#ifndef ARGVHELPER_P_H
18
#define ARGVHELPER_P_H
24
// Function to edit argv to strip out unmatched entries of targetArray, so both arrays have identical contents
25
// Note: Mir parses out arguments that it understands, but it also removes argv[0] (bug pad.lv/1511509)
26
// so need to ensure argv[0] is the binary name as usual.
27
void editArgvToMatch(int &argcToEdit, char** argvToEdit, int targetCount, const char* const targetArray[])
29
// Make copy of the argv array of pointers, as we will be editing the original
30
const size_t arraySize = (argcToEdit + 1) * sizeof(char*);
31
char** argvCopy = static_cast<char**>(malloc(arraySize));
32
memcpy(argvCopy, argvToEdit, arraySize);
34
int k=1; // index of argv we want to edit - note we'll leave argv[0] alone
35
for (int i=0; i<targetCount; i++) { // taking each argument Mir did not parse out
36
for (int j=1; j<argcToEdit; j++) { // find pointer to same argument in argvCopy (leave arg[0] out)
37
if (strcmp(targetArray[i], argvCopy[j]) == 0) {
38
argvToEdit[k] = const_cast<char*>(argvCopy[j]); // edit argv to position that argument to match argv2.
44
assert(k == targetCount+1);
45
argvToEdit[k] = nullptr;
47
argcToEdit = targetCount+1; // now argv and targetArray should have list the same strings.
52
#endif // ARGVHELPER_P_H