~lukas-kde/miral/shellchrome-windowinfo

« back to all changes in this revision

Viewing changes to miral-qt/src/platforms/mirserver/argvHelper.h

  • Committer: Larry Price
  • Date: 2016-09-13 16:19:29 UTC
  • mto: (330.4.1 miral)
  • mto: This revision was merged to the branch mainline in revision 352.
  • Revision ID: larry.price@canonical.com-20160913161929-vs9ka1capmljq1es
Removing miral-qt from release branch, updating copyright file, and adding GPL3 license to root dir

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2015 Canonical, Ltd.
3
 
 *
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.
7
 
 *
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.
12
 
 *
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/>.
15
 
 */
16
 
 
17
 
#ifndef ARGVHELPER_P_H
18
 
#define ARGVHELPER_P_H
19
 
 
20
 
#include <cassert>
21
 
 
22
 
namespace qtmir {
23
 
 
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[])
28
 
{
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);
33
 
 
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.
39
 
                k++;
40
 
                break;
41
 
            }
42
 
        }
43
 
    }
44
 
    assert(k == targetCount+1);
45
 
    argvToEdit[k] = nullptr;
46
 
    free(argvCopy);
47
 
    argcToEdit = targetCount+1; // now argv and targetArray should have list the same strings.
48
 
}
49
 
 
50
 
} // namespace qtmir
51
 
 
52
 
#endif // ARGVHELPER_P_H