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