-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
34 lines (29 loc) · 969 Bytes
/
util.cpp
File metadata and controls
34 lines (29 loc) · 969 Bytes
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
#include "util.h"
#include <QCryptographicHash>
#include <QFileInfo>
QString nowTs(){
return QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
}
QString fileSizeHuman(qint64 b){
static const QStringList u = {"B","KB","MB","GB","TB"};
int i=0; double v=b;
while(v>1024.0 && i<u.size()-1){ v/=1024.0; ++i; }
int prec = (i==0?0:(v<10?2:(v<100?1:0)));
return QString("%1 %2").arg(QString::number(v, 'f', prec)).arg(u[i]);
}
QString sha256File(const QString &path){
QFile f(path);
if(!f.open(QIODevice::ReadOnly)) return QString();
QCryptographicHash h(QCryptographicHash::Sha256);
while(!f.atEnd()) h.addData(f.read(1<<20));
return h.result().toHex();
}
bool isLikelyImage(const QString& path){
QImageReader r(path);
return !r.format().isEmpty();
}
QString sanitizeCsv(QString s){
s.replace("\"","\"\"");
if (s.contains(',') || s.contains('"') || s.contains('\n')) s="\""+s+"\"";
return s;
}