OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / shared / cpaster / splitter.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "splitter.h"
34
35 #include <QtCore/QRegExp>
36
37 FileDataList splitDiffToFiles(const QByteArray &data)
38 {
39     FileDataList ret;
40     QString strData = data;
41     QString splitExpression;
42
43     if (data.contains("==== ") && data.contains(" ====\n")) {
44         // Perforce diff
45         splitExpression = "==== ([^\\n\\r]+) - ([^\\n\\r]+) ====";
46
47     } else if (data.contains("--- ") && data.contains("\n+++ ")) {
48         // Unified contextual diff
49         splitExpression = "\\-\\-\\- ([^\\n\\r]*)"
50                           "\\n\\+\\+\\+ ([^\\n\\r]*)";
51
52     } else if (data.contains("*** ") && data.contains("\n--- ")) {
53         // Copied contextual diff
54         splitExpression = "\\*\\*\\* ([^\\n\\r]*) [0-9\\-]* [0-9:\\.]*[^\\n\\r]*"
55                           "\\n\\-\\-\\- ([^\\n\\r]*) [0-9\\-]* [0-9:\\.]*[^\\n\\r]*";
56
57     } else {
58         return FileDataList();
59     }
60
61     int splitIndex = 0, previousSplit = -1;
62     QRegExp splitExpr(splitExpression);
63     QString filename, content;
64     // The algorithm works like this:
65     // On the first match we only get the filename of the first patch part
66     // On the second match (if any) we get the diff content, and the name of the next file patch
67
68     while (-1 != (splitIndex = splitExpr.indexIn(strData,splitIndex))) {
69         if (!filename.isEmpty()) {
70             QString content = strData.mid(previousSplit, splitIndex - previousSplit);
71             ret.append(FileData(filename, content.toLatin1()));
72         }
73
74         // If the first index in not at the beginning of the file, then we know there's content
75         // we're about to skip, which is common in commit diffs, so we get that content and give it
76         // a 'fake' filename.
77         if (previousSplit == -1 && splitIndex > 0 && filename.isEmpty()) {
78             QString content = strData.left(splitIndex);
79             ret.append(FileData("<Header information>", content.toLatin1()));
80         }
81
82         filename = splitExpr.cap(1);
83         previousSplit = splitIndex;
84         ++splitIndex;
85     }
86     // Append the last patch content
87     if (!filename.isEmpty()) {
88         QString content = strData.mid(previousSplit);
89         ret.append(FileData(filename, content.toLatin1()));
90     }
91
92     return ret;
93 }