forked from KDE/kdiff3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
183 lines (160 loc) · 5.24 KB
/
Utils.cpp
File metadata and controls
183 lines (160 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* KDiff3 - Text Diff And Merge Tool
*
* SPDX-FileCopyrightText: 2002-2011 Joachim Eibl, joachim.eibl at gmx.de
* SPDX-FileCopyrightText: 2018-2020 Michael Reeves reeves.87@gmail.com
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "Utils.h"
#include "fileaccess.h"
#include <KLocalizedString>
#include <QString>
#include <QStringList>
#include <QHash>
#include <QRegularExpression>
/* Split the command line into arguments.
* Normally split at white space separators except when quoting with " or '.
* Backslash is treated as meta.
* Detect parsing errors like unclosed quotes.
* The first item in the list will be the command itself.
* Returns the error reason as string or an empty string on success.
* Eg. >"1" "2"< => >1<, >2<
* Eg. >'\'\\'< => >'\< backslash is a meta character
* Eg. > "\\" < => >\<
* Eg. >"c:\sed" 's/a/\' /g'< => >c:\sed<, >s/a/' /g<
*/
QString Utils::getArguments(QString cmd, QString& program, QStringList& args)
{
program = QString();
args.clear();
for(int i = 0; i < cmd.length(); ++i)
{
while(i < cmd.length() && cmd[i].isSpace())
{
++i;
}
if(cmd[i] == '"' || cmd[i] == '\'') // argument beginning with a quote
{
QChar quoteChar = cmd[i];
++i;
int argStart = i;
bool bSkip = false;
while(i < cmd.length() && (cmd[i] != quoteChar || bSkip))
{
if(bSkip)
{
bSkip = false;
//Don't emulate bash here we are not talking to it.
//For us all quotes are the same.
if(cmd[i] == '\\' || cmd[i] == '\'' || cmd[i] == '"')
{
cmd.remove(i - 1, 1); // remove the backslash '\'
continue;
}
}
else if(cmd[i] == '\\')
bSkip = true;
++i;
}
if(i < cmd.length())
{
args << cmd.mid(argStart, i - argStart);
if(i + 1 < cmd.length() && !cmd[i + 1].isSpace())
return i18n("Expecting space after closing quote.");
}
else
return i18n("Unmatched quote.");
continue;
}
else
{
int argStart = i;
while(i < cmd.length() && (!cmd[i].isSpace() /*|| bSkip*/))
{
if(cmd[i] == '"' || cmd[i] == '\'')
return i18n("Unexpected quote character within argument.");
++i;
}
args << cmd.mid(argStart, i - argStart);
}
}
if(args.isEmpty())
return i18n("No program specified.");
else
{
program = args[0];
args.pop_front();
}
return QString();
}
bool Utils::wildcardMultiMatch(const QString& wildcard, const QString& testString, bool bCaseSensitive)
{
static QHash<QString, QRegularExpression> s_patternMap;
const QStringList regExpList = wildcard.split(QChar(';'));
for(const QString& regExp : regExpList)
{
QHash<QString, QRegularExpression>::iterator patIt = s_patternMap.find(regExp);
if(patIt == s_patternMap.end())
{
QRegularExpression pattern(QRegularExpression::wildcardToRegularExpression(regExp), bCaseSensitive ? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption);
patIt = s_patternMap.insert(regExp, pattern);
}
if(patIt.value().match(testString).hasMatch())
return true;
}
return false;
}
//TODO: Only used by calcTokenPos.
bool Utils::isCTokenChar(QChar c)
{
return (c == '_') ||
(c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
(c >= '0' && c <= '9');
}
//TODO: Needed? Only user of isCTokenChar.
/// Calculate where a token starts and ends, given the x-position on screen.
void Utils::calcTokenPos(const QString& s, int posOnScreen, int& pos1, int& pos2)
{
int pos = std::max(0, posOnScreen);
if(pos >= (int)s.length())
{
pos1 = s.length();
pos2 = s.length();
return;
}
pos1 = pos;
pos2 = pos + 1;
if(isCTokenChar(s[pos1]))
{
while(pos1 >= 0 && isCTokenChar(s[pos1]))
--pos1;
++pos1;
while(pos2 < (int)s.length() && isCTokenChar(s[pos2]))
++pos2;
}
}
QString Utils::calcHistoryLead(const QString& s)
{
static const QRegularExpression nonWhitespace("\\S"), whitespace("\\s");
// Return the start of the line until the first white char after the first non white char.
int i = s.indexOf(nonWhitespace);
if(i == -1)
return QString("");
i = s.indexOf(whitespace, i);
if(Q_UNLIKELY(i == -1))
return s;// Very unlikely
return s.left(i);
}
/*
QUrl::toLocalFile does some special handling for locally visable windows network drives.
If QUrl::isLocal however it returns false we get an empty string back.
*/
QString Utils::urlToString(const QUrl &url)
{
if(!FileAccess::isLocal(url))
return url.toString();
QString result = url.toLocalFile();
if(result.isEmpty())
return url.path();
return result;
}