fileprocessor.cpp
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
#include <QDebug>
#include "fileprocessor.h"
FileProcessor::FileProcessor()
{
}
bool FileProcessor::folderExist(const QString &path){
QDir qdir(path);
qDebug() << "check path" << path;
return qdir.exists();
}
bool FileProcessor::fileExist(const QString &path_file){
QFile qfile(path_file);
return qfile.exists();
}
bool FileProcessor::detectUSB(QString &usbPath){
QString checkUsbName[3] = {
"sda",
"sdb",
"sdc"
};
for(int i =2;i >=0 ; i--){
if(folderExist(QString("/sys/block/").append(checkUsbName[i]))){
if(folderExist(QString("/mnt/%111").arg(checkUsbName[i]))){
usbPath = QString("/mnt/%111").arg(checkUsbName[i]);
return true;
}
break;
}
}
qDebug() << "usb detect fail";
return false;
}
void FileProcessor::getAllDirList(QDir d, QStringList &list){
QStringList qsl = d.entryList(QDir::NoDotAndDotDot | QDir::Dirs);
foreach(QString strdir, qsl){
strdir.prepend(d.absolutePath().append("/"));
list.append(strdir);
QDir dir(strdir);
getAllDirList(dir,list);
}
}
quint64 FileProcessor::getDirSize(const QString &str){
qint64 sizex = 0;
QFileInfo str_info(str);
if(str_info.isDir()){
QDir dir(str);
QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot);
for (int i = 0; i < list.size(); ++i)
{
QFileInfo fileInfo = list.at(i);
if(fileInfo.isDir())
{
sizex += getDirSize(fileInfo.absoluteFilePath());
}
else{
//qDebug() << fileInfo.absoluteFilePath() << fileInfo.size();
sizex += fileInfo.size();
}
}
}
return sizex;
}
bool FileProcessor::readFromInfoFile(QMap<QString, QString> &info_map, const QString &filename)
{
QFile file(filename);
QString label,value;
qDebug() << "orgin size " << info_map.size();
if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
while(!file.atEnd()){
QString line = (QString::fromUtf8(file.readLine())).remove(QChar('\n'));
info_map[line.section(',',0,0)] = line.section(',',1,1);
}
qDebug() << file.fileName() << "read OK!";
qDebug() << "final size " << info_map.size();
QList<QString> list = info_map.keys();
for(int i=0; i< info_map.size() ;i++){
qDebug() << list[i] << info_map[list[i]];
}
return true;
}
else {
qDebug() << file.fileName() + " file not found";
}
return false;
}