00001 /**************************************************************** 00002 * Vidalia is distributed under the following license: 00003 * 00004 * Copyright (C) 2006, Matt Edman, Justin Hipple 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 ****************************************************************/ 00021 00022 /** 00023 * \file file.cpp 00024 * \version $Id: file.cpp 1238 2006-09-25 17:50:57Z edmanm $ 00025 * \brief Functions and definitions for common file I/O operations 00026 */ 00027 00028 #include <QDir> 00029 #include <QFile> 00030 #include "file.h" 00031 00032 00033 /** Create an empty file named <b>filename</b>. if <b>createdir</b> is true, 00034 * then the full path to <b>filename</b> will be created. Returns true on 00035 * success, or false on error and <b>errmsg</b> will be set. */ 00036 bool 00037 touch_file(QString filename, bool createdir, QString *errmsg) 00038 { 00039 /* If the file's path doesn't exist and we're supposed to create it, do that 00040 * now. */ 00041 if (createdir && !create_path(QFileInfo(filename).absolutePath())) { 00042 return false; 00043 } 00044 00045 /* Touch the file */ 00046 QFile file(filename); 00047 if (!QFileInfo(filename).exists()) { 00048 if (!file.open(QIODevice::WriteOnly)) { 00049 return err(errmsg, file.errorString()); 00050 } 00051 } 00052 return true; 00053 } 00054 00055 /** Creates all directories in <b>path</b>, if they do not exist. */ 00056 bool 00057 create_path(QString path) 00058 { 00059 QDir dir(path); 00060 if (!dir.exists()) { 00061 path = dir.absolutePath(); 00062 if (!dir.mkpath(path)) { 00063 return false; 00064 } 00065 } 00066 return true; 00067 } 00068