file.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <QDir>
00018 #include <QFile>
00019 #include "stringutil.h"
00020 #include "file.h"
00021
00022 #if defined(Q_OS_WIN32)
00023 #include "win32.h"
00024 #endif
00025
00026
00027
00028
00029
00030 bool
00031 touch_file(QString filename, bool createdir, QString *errmsg)
00032 {
00033
00034
00035 filename = expand_filename(filename);
00036
00037
00038
00039 if (createdir && !create_path(QFileInfo(filename).absolutePath())) {
00040 return false;
00041 }
00042
00043
00044 QFile file(filename);
00045 if (!QFileInfo(filename).exists()) {
00046 if (!file.open(QIODevice::WriteOnly)) {
00047 return err(errmsg, file.errorString());
00048 }
00049 }
00050 return true;
00051 }
00052
00053
00054 bool
00055 create_path(QString path)
00056 {
00057 QDir dir(path);
00058 if (!dir.exists()) {
00059 path = dir.absolutePath();
00060 if (!dir.mkpath(path)) {
00061 return false;
00062 }
00063 }
00064 return true;
00065 }
00066
00067
00068
00069
00070 bool
00071 copy_dir(QString source, QString dest)
00072 {
00073
00074 QDir src(source);
00075 QDir dst(dest);
00076
00077
00078 QFileInfoList contents = src.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
00079
00080
00081 foreach (QFileInfo fileInfo, contents) {
00082
00083 QString fileName = fileInfo.fileName();
00084 QString srcFilePath = src.absoluteFilePath(fileName);
00085 QString dstFilePath = dst.absoluteFilePath(fileName);
00086
00087 if (fileInfo.isDir()) {
00088
00089 if (!dst.mkdir(fileName))
00090 return false;
00091 if (!copy_dir(srcFilePath, dstFilePath))
00092 return false;
00093 } else if (fileInfo.isFile()) {
00094
00095 if (!QFile::copy(srcFilePath, dstFilePath))
00096 return false;
00097 }
00098
00099
00100 }
00101 return true;
00102 }
00103
00104
00105
00106
00107 QString
00108 expand_filename(QString filename)
00109 {
00110 #if defined(Q_OS_WIN32)
00111 if (filename.startsWith("%APPDATA%\\") ||
00112 filename.startsWith("%APPDATA%/"))
00113 return filename.replace(0, 9, win32_app_data_folder());
00114
00115 if (filename.startsWith("%PROGRAMFILES%\\") ||
00116 filename.startsWith("%PROGRAMFILES%/"))
00117 return filename.replace(0, 14, win32_program_files_folder());
00118 #else
00119 if (filename.startsWith("~/"))
00120 return filename.replace(0, 1, QDir::homePath());
00121 #endif
00122 return filename;
00123 }
00124