1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the qt3to4 porting application of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "filewriter.h"
|
---|
43 | #include <QFile>
|
---|
44 | #include <QFileInfo>
|
---|
45 | #include <QDir>
|
---|
46 | #include <ctype.h>
|
---|
47 |
|
---|
48 | QT_BEGIN_NAMESPACE
|
---|
49 |
|
---|
50 | FileWriter *FileWriter::theInstance = 0;
|
---|
51 |
|
---|
52 | FileWriter *FileWriter::instance()
|
---|
53 | {
|
---|
54 | if(!theInstance)
|
---|
55 | theInstance = new FileWriter();
|
---|
56 |
|
---|
57 | return theInstance;
|
---|
58 | }
|
---|
59 |
|
---|
60 | void FileWriter::deleteInstance()
|
---|
61 | {
|
---|
62 | if(theInstance) {
|
---|
63 | delete theInstance;
|
---|
64 | theInstance=0;
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | FileWriter::FileWriter(OverWriteFiles overWrite, QString overwriteMsg)
|
---|
69 | :overWriteFiles(overWrite)
|
---|
70 | ,overwriteMessage(overwriteMsg)
|
---|
71 | {
|
---|
72 | if(overwriteMessage.isEmpty())
|
---|
73 | overwriteMessage = QLatin1String("Convert file ");
|
---|
74 | }
|
---|
75 |
|
---|
76 | FileWriter::WriteResult FileWriter::writeFileVerbously(QString filePath, QByteArray contents)
|
---|
77 | {
|
---|
78 | const WriteResult result = writeFile(filePath, contents);
|
---|
79 | if (result == WriteSucceeded) {
|
---|
80 | QString cleanPath = QDir::cleanPath(filePath);
|
---|
81 | printf("Wrote to file: %s \n", QDir::toNativeSeparators(cleanPath).toLocal8Bit().constData());
|
---|
82 | }
|
---|
83 | return result;
|
---|
84 | }
|
---|
85 |
|
---|
86 | FileWriter::WriteResult FileWriter::writeFile(QString filePath, QByteArray contents)
|
---|
87 | {
|
---|
88 | if(filePath.isEmpty())
|
---|
89 | return WriteFailed;
|
---|
90 | QString path = QFileInfo(filePath).path();
|
---|
91 | if (!QDir().mkpath(path)){
|
---|
92 | printf("Error creating path %s \n", QDir::toNativeSeparators(path).toLocal8Bit().constData());
|
---|
93 | }
|
---|
94 |
|
---|
95 | QString cleanPath = QDir::cleanPath(filePath);
|
---|
96 | QFile f(cleanPath);
|
---|
97 | if (f.exists()) {
|
---|
98 | if (overWriteFiles == DontOverWrite) {
|
---|
99 | printf("Error writing file %s: It already exists \n",
|
---|
100 | QDir::toNativeSeparators(cleanPath).toLatin1().constData());
|
---|
101 | return WriteFailed;
|
---|
102 | } else if(overWriteFiles == AskOnOverWrite) {
|
---|
103 | printf("%s%s? (Y)es, (N)o, (A)ll ", overwriteMessage.toLatin1().constData(),
|
---|
104 | QDir::toNativeSeparators(cleanPath).toLatin1().constData());
|
---|
105 |
|
---|
106 | char answer = 0;
|
---|
107 | while (answer != 'y' && answer != 'n' && answer != 'a') {
|
---|
108 | #if defined(Q_OS_WIN) && defined(_MSC_VER) && _MSC_VER >= 1400
|
---|
109 | scanf_s("%c", &answer);
|
---|
110 | #else
|
---|
111 | scanf("%c", &answer);
|
---|
112 | #endif
|
---|
113 | answer = tolower(answer);
|
---|
114 | }
|
---|
115 |
|
---|
116 | if(answer == 'n')
|
---|
117 | return WriteSkipped;
|
---|
118 | else if(answer == 'a')
|
---|
119 | overWriteFiles=AlwaysOverWrite;
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | f.open(QFile::WriteOnly);
|
---|
124 | if (f.isOpen() && f.write(contents) == contents.size())
|
---|
125 | return WriteSucceeded;
|
---|
126 |
|
---|
127 | printf("Could not write to to file: %s. Is it write protected?\n",
|
---|
128 | QDir::toNativeSeparators(filePath).toLatin1().constData());
|
---|
129 |
|
---|
130 | return WriteFailed;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /*
|
---|
134 | Sets the write mode for the file writer. writeMode is one of
|
---|
135 | DontOverWrite, AlwaysOverWrite, AskOnOverWrite.
|
---|
136 | */
|
---|
137 | void FileWriter::setOverwriteFiles(OverWriteFiles writeMode)
|
---|
138 | {
|
---|
139 | overWriteFiles = writeMode;
|
---|
140 | }
|
---|
141 |
|
---|
142 | QByteArray detectLineEndings(const QByteArray &array)
|
---|
143 | {
|
---|
144 | if (array.contains("\r\n")) {
|
---|
145 | return "\r\n";
|
---|
146 | } else {
|
---|
147 | return "\n";
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | QT_END_NAMESPACE
|
---|