-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisourcefile.cpp
More file actions
100 lines (90 loc) · 2.64 KB
/
Copy pathlisourcefile.cpp
File metadata and controls
100 lines (90 loc) · 2.64 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
/****************************************************************************
**
** Copyright (C) 2015 Mikhail Y. Zvyozdochkin aka DarkHobbit
** Contact: pub@zvyozdochkin.ru
**
** This file is part of the LInvert utility for Qt
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public License
** version 2.1 or version 3 as published by the Free Software Foundation.
** Please review the ** following information to ensure the
** GNU Lesser General Public License requirements will be met:
** https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
****************************************************************************/
#include "lisourcefile.h"
const QString BACKUP_SUFFIX = "~";
bool LIMessage::operator ==(const LIMessage& m2)
{
return (this->oldText==m2.oldText);
}
LISourceFile::LISourceFile(const QString& _fileName, QTextStream& _out):
fileName(_fileName), out(_out)
{
}
bool LISourceFile::isSameFile(const QString& _fileName)
{
return fileName==_fileName;
}
bool LISourceFile::addMessage(const QString& _oldText, const QString& _newText, uint _line)
{
LIMessage item;
item.oldText = _oldText;
item.newText = _newText;
item.line = _line;
if (!messages.contains(item)) {
messages.push_back(item);
return true;
}
else
return false;
}
bool LISourceFile::process()
{
if (!read()) return false;
for (int i=0; i<messages.count(); i++)
if (!processMessage(messages[i]))
return false;
return write();
}
void LISourceFile::rollBack()
{
QFile::remove(fileName);
QFile::rename(fileName+BACKUP_SUFFIX, fileName);
}
bool LISourceFile::read()
{
QString bckName = fileName+BACKUP_SUFFIX;
QFile::remove(bckName); // only if exists
if (!QFile::rename(fileName, bckName)) {
out << tr("linvert error: Can't rename file %1\n").arg(fileName);
return false;
}
QFile file(bckName);
if (!file.open( QIODevice::ReadOnly)) {
out << tr("linvert error: Can't open file %1\n").arg(bckName);
return false;
}
content.clear();
QTextStream stream(&file);
do {
content.push_back(stream.readLine());
} while (!stream.atEnd());
file.close();
return true;
}
bool LISourceFile::write()
{
QFile file(fileName);
if (!file.open( QIODevice::WriteOnly)) {
out << tr("linvert error: Can't open file %1\n").arg(fileName);
return false;
}
QTextStream stream(&file);
for (int i=0; i<content.count(); i++)
stream << content[i] << "\n";
file.close();
return true;
}