Newer
Older
/**
* A library for Qt app
*
* LICENSE: The GNU Lesser General Public License, version 3.0
*
* @author Akira Ohgaki <akiraohgaki@gmail.com>
* @copyright Akira Ohgaki
* @license https://opensource.org/licenses/LGPL-3.0 The GNU Lesser General Public License, version 3.0
* @link https://github.com/akiraohgaki/qtlibs
*/
#include "file.h"
#include <QIODevice>
#include <QTextStream>
#include <QFile>
namespace qtlibs {
File::File(const QString &path, QObject *parent)
: QObject(parent), path_(path)
File::File(const File &other, QObject *parent)
: QObject(parent)
{
setPath(other.path());
}
File &File::operator =(const File &other)
{
setPath(other.path());
return *this;
}
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
QString File::path() const
{
return path_;
}
void File::setPath(const QString &path)
{
path_ = path;
}
bool File::exists()
{
QFile file(path());
return file.exists();
}
QByteArray File::readData()
{
QByteArray data;
QFile file(path());
if (file.exists() && file.open(QIODevice::ReadOnly)) {
data = file.readAll();
file.close();
}
return data;
}
bool File::writeData(const QByteArray &data)
{
QFile file(path());
if (file.open(QIODevice::WriteOnly)) {
file.write(data);
file.close();
return true;
}
return false;
}
QString File::readText()
{
QString data;
QFile file(path());
if (file.exists() && file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&file);
in.setCodec("UTF-8");
data = in.readAll();
file.close();
}
return data;
}
bool File::writeText(const QString &data)
{
QFile file(path());
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&file);
out.setCodec("UTF-8");
out << data;
file.close();
return true;
}
return false;
}
bool File::copy(const QString &newPath)
{
QFile file(path());
return file.copy(newPath);
}
bool File::move(const QString &newPath)
{
QFile file(path());
return file.rename(newPath);
}
bool File::remove()
{
QFile file(path());
return file.remove();
}
} // namespace qtlibs