-
Notifications
You must be signed in to change notification settings - Fork 4
/
util.cpp
executable file
·57 lines (50 loc) · 1.37 KB
/
util.cpp
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
#include "util.h"
#include <QDir>
#include <QDebug>
Util::Util()
{
}
int Util::createFile( const QString filePath, const QString fileName)
{
QDir tempDir;
QString currentDir = tempDir.currentPath();
if(!tempDir.exists(filePath))
{
tempDir.mkpath(filePath);
}
QFile *tempFile = new QFile;
tempDir.setCurrent(filePath);
if(tempFile->exists(fileName))
{
qDebug()<<fileName<<QObject::tr("already Exist");
tempDir.setCurrent(currentDir);
return -1;
}
tempFile->setFileName(fileName);
if(!tempFile->open(QIODevice::WriteOnly|QIODevice::Text))
{
tempDir.setCurrent(currentDir);
return -1;
}
tempFile->close();
tempDir.setCurrent(currentDir);
return 0;
}
QJsonObject * Util::parseJsonFile(const QString fileName)
{
//QDir filepath(filePath);
QFile file(fileName);
file.open(QIODevice::ReadOnly | QIODevice::Text);
QString value = file.readAll();
file.close();
QJsonParseError parseJsonErr;
QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(),&parseJsonErr);
if(!(parseJsonErr.error == QJsonParseError::NoError))
{
qDebug()<<"解析json文件错误!";
return nullptr;
}
QJsonObject * result = new QJsonObject();
*result = document.object();
return result;
}