~alan-griffiths/miral/debug

« back to all changes in this revision

Viewing changes to miral-qt/src/modules/Unity/Application/proc_info.cpp

  • Committer: Gerry Boland
  • Date: 2016-06-01 22:06:51 UTC
  • mto: This revision was merged to the branch mainline in revision 178.
  • Revision ID: gerry.boland@canonical.com-20160601220651-ge508tffql4e7u7c
Import QtMir code into miral-qt subdirectory. Disabled by default, use -DMIRAL_ENABLE_QT=1 to build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2014-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
#include "proc_info.h"
 
18
 
 
19
// Qt
 
20
#include <QFile>
 
21
#include <QRegularExpression>
 
22
 
 
23
namespace qtmir
 
24
{
 
25
 
 
26
ProcInfo::~ProcInfo() {
 
27
}
 
28
 
 
29
std::unique_ptr<ProcInfo::CommandLine> ProcInfo::commandLine(pid_t pid) {
 
30
    QFile cmdline(QString("/proc/%1/cmdline").arg(pid));
 
31
    if (!cmdline.open(QIODevice::ReadOnly | QIODevice::Text)) {
 
32
        return nullptr;
 
33
    }
 
34
 
 
35
    return std::unique_ptr<CommandLine>(new CommandLine{ cmdline.readLine().replace('\0', ' ') });
 
36
}
 
37
QStringList ProcInfo::CommandLine::asStringList() const {
 
38
    return QString(m_command.data()).split(' ');
 
39
}
 
40
 
 
41
bool ProcInfo::CommandLine::startsWith(char const* prefix) const {
 
42
    return m_command.startsWith(prefix);
 
43
}
 
44
 
 
45
bool ProcInfo::CommandLine::contains(char const* prefix) const {
 
46
    return m_command.contains(prefix);
 
47
}
 
48
 
 
49
QString ProcInfo::CommandLine::getParameter(const char* name) const {
 
50
    QString pattern = QRegularExpression::escape(name) + "(\\S+)";
 
51
    QRegularExpression regExp(pattern);
 
52
    QRegularExpressionMatch regExpMatch = regExp.match(m_command);
 
53
 
 
54
    if (!regExpMatch.hasMatch()) {
 
55
        return QString();
 
56
    }
 
57
 
 
58
    return QString(regExpMatch.captured(1));
 
59
}
 
60
 
 
61
} // namespace qtmir