-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathstd.cpp
More file actions
102 lines (86 loc) · 2.44 KB
/
std.cpp
File metadata and controls
102 lines (86 loc) · 2.44 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
97
98
99
100
101
102
/*
Ted, a simple text editor/IDE.
Copyright 2012, Blitz Research Ltd.
See LICENSE.TXT for licensing terms.
*/
#include "std.h"
QString stripDir( const QString &path ){
int i=path.lastIndexOf( '/' );
if( i==-1 ) return path;
return path.mid( i+1 );
}
QString extractDir( const QString &path ){
int i=path.lastIndexOf( '/' );
if( i==-1 ) return "";
#ifdef Q_OS_WIN32
if( i && path[i-1]==':' ) return "";
#endif
return path.left( i );
}
QString extractExt( const QString &path ){
int i=path.lastIndexOf( '.' )+1;
return i && path.indexOf( '/',i )==-1 ? path.mid( i ) : "";
}
// Converts \ to /, removes trailing /s and prefixes drive if necessary.
//
QString fixPath( QString path ){
if( path.isEmpty() )
return path;
if( isUrl( path ) )
return path;
path = path.replace( '\\','/' );
path = QDir::cleanPath( path );
#ifdef Q_OS_WIN32
if( path.startsWith( "//" ) )
return path;
if( path.startsWith( '/' ) )
path = QDir::rootPath()+path.mid( 1 );
if( path.endsWith( '/' ) && !path.endsWith( ":/" ) )
path = path.left( path.length()-1 );
#else
if( path.endsWith( '/' ) && path!="/" )
path = path.left( path.length()-1 );
#endif
return path;
}
bool removeDir( const QString &path ){
bool result=true;
QDir dir( path );
if( dir.exists( path ) ){
Q_FOREACH( QFileInfo info,dir.entryInfoList( QDir::NoDotAndDotDot|QDir::System|QDir::Hidden|QDir::AllDirs|QDir::Files,QDir::DirsFirst ) ){
if( info.isDir() ){
result=removeDir( info.absoluteFilePath() );
}else{
result=QFile::remove( info.absoluteFilePath() );
}
if( !result ) return result;
}
result=dir.rmdir( path );
}
return result;
}
bool isUrl( const QString &path ){
return path.startsWith( "file:" ) || path.startsWith( "http:" ) || path.startsWith( "https:" ) || path.startsWith( "qrc:" );
}
bool isMonkeyFile(const QString &path)
{
QString ext = extractExt(path);
return monkeyFilesTypes().contains(ext);
}
bool isImageFile(const QString &path)
{
static QStringList list;
if (list.isEmpty()) {
list<<"jpg"<<"jpeg"<<"png"<<"ico"<<"bmp"<<"gif";
}
QString ext = extractExt(path);
return list.contains(ext);
}
QStringList monkeyFilesTypes()
{
static QStringList list;
if (list.isEmpty()) {
list << "monkey" << "cxs";
}
return list;
}