From a6d58bb6052ac8cb01805a48c4ad2f129126116f Mon Sep 17 00:00:00 2001 From: tpearson Date: Wed, 24 Feb 2010 02:13:59 +0000 Subject: Added KDE3 version of kvirc git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/kvirc@1095341 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- src/kvilib/file/kvi_fileutils.cpp | 505 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 505 insertions(+) create mode 100644 src/kvilib/file/kvi_fileutils.cpp (limited to 'src/kvilib/file/kvi_fileutils.cpp') diff --git a/src/kvilib/file/kvi_fileutils.cpp b/src/kvilib/file/kvi_fileutils.cpp new file mode 100644 index 0000000..648d912 --- /dev/null +++ b/src/kvilib/file/kvi_fileutils.cpp @@ -0,0 +1,505 @@ +//============================================================================= +// +// File : kvi_fileutils.cpp +// Creation date : Fri Dec 25 1998 18:26:48 by Szymon Stefanek +// +// This file is part of the KVirc irc client distribution +// Copyright (C) 1998-2007 Szymon Stefanek (pragma at kvirc dot net) +// +// This program is FREE software. You can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your opinion) any later version. +// +// This program is distributed in the HOPE that it will be USEFUL, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +// See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, write to the Free Software Foundation, +// Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +// +//============================================================================= + +#define __KVILIB__ + + +#define _KVI_FILEUTLIS_CPP_ +#include "kvi_fileutils.h" +#include "kvi_qstring.h" +#include "kvi_file.h" +#include "kvi_malloc.h" + +#include +#include +#include +#include +#include + + +namespace KviFileUtils +{ + /* + WORKING CODE BUT UNUSED FOR NOW + bool readLine(QFile * f,QString &szBuffer,bool bClearBuffer) + { + // FIXME: Should this assume UTF8 encoding ? + char tmp_buf[256]; + int cur_len = 0; + //char *cur_ptr = tmp_buf; + if(bClearBuffer)szBuffer = ""; + int ch = f->getch(); + + while((ch != -1)&&(ch != '\n')&&(ch != 0)) + { + tmp_buf[cur_len] = ch; + cur_len++; + if(cur_len > 255) + { + if(tmp_buf[255] == '\r')cur_len--; //Ignore CR... + int lastlen = szBuffer.length(); + szBuffer.setLength(lastlen + cur_len); + QChar *p1 = szBuffer.unicode() + lastlen; + char * p2 = tmp_buf; + for(int i=0;igetch(); + } + if(ch == 0) + { + debug("Warning : %s is not an ascii file",f->name().latin1()); + } + if(cur_len > 0) + { + if(tmp_buf[cur_len - 1] == '\r')cur_len--; //Ignore CR... + int lastlen = szBuffer.length(); + szBuffer.setLength(lastlen + cur_len); + QChar *p1 = szBuffer.unicode() + lastlen; + char * p2 = tmp_buf; + for(int i=0;i 2) && (szPath.at(0) != QChar('/'))) + { + if((szPath.at(1) == QChar(':')) && (szPath.at(2) == QChar('/'))) + { + szPath.remove(0,2); + } + } +#ifdef COMPILE_USE_QT4 + szPath=QDir::cleanPath(szPath); +#else + szPath=QDir::cleanDirPath(szPath); +#endif +#endif + + } + + bool directoryExists(const QString &szPath) + { + QFileInfo f(szPath); + return (f.exists() && f.isDir()); + } + + bool directoryExists(const char* path) + { + QString szPath=QString::fromUtf8(path); + QFileInfo f(szPath); + return (f.exists() && f.isDir()); + } + + bool fileExists(const QString &szPath) + { + QFileInfo f(szPath); + return (f.exists() && f.isFile()); + } + + bool fileExists(const char* path) + { + QString szPath=QString::fromUtf8(path); + return fileExists(szPath); + } + + bool removeFile(const QString &szPath) + { + QDir d; + return d.remove(szPath); + } + + bool removeFile(const char* path) + { + QString szPath=QString::fromUtf8(path); + return removeFile(szPath); + } + + bool removeDir(const QString &szPath) + { + QDir d; + return d.rmdir(szPath); + } + + bool removeDir(const char* path) + { + QString szPath=QString::fromUtf8(path); + return removeDir(szPath); + } + + bool deleteDir(const QString &szPath) + { + QDir d(szPath); + QStringList sl = d.entryList(QDir::Dirs); + QStringList::Iterator it; + for(it=sl.begin();it != sl.end();it++) + { + QString szSubdir = *it; + if(!(KviQString::equalCS(szSubdir,"..") || KviQString::equalCS(szSubdir,"."))) + { + QString szSubPath = szPath; + KviQString::ensureLastCharIs(szSubPath,QChar(KVI_PATH_SEPARATOR_CHAR)); + szSubPath += szSubdir; + if(!KviFileUtils::deleteDir(szSubPath)) + return false; + } + } + + sl = d.entryList(QDir::Files); + for(it=sl.begin();it != sl.end();it++) + { + QString szFilePath = szPath; + KviQString::ensureLastCharIs(szFilePath,QChar(KVI_PATH_SEPARATOR_CHAR)); + szFilePath += *it; + if(!KviFileUtils::removeFile(szFilePath)) + return false; + } + + return KviFileUtils::removeDir(szPath); + } + + bool writeFile(const QString &szPath,const QString &szData,bool bAppend) + { + KviFile f(szPath); + if(!f.openForWriting(bAppend))return false; + KviQCString szTmp = KviQString::toUtf8(szData); + if(!szTmp.data())return true; + if(f.writeBlock(szTmp.data(),szTmp.length()) != ((int)(szTmp.length())))return false; + return true; + } + + bool writeFile(const char* path,const QString &szData,bool bAppend) + { + QString szPath=QString::fromUtf8(path); + return writeFile(szPath,szData,bAppend); + } + + bool writeFileLocal8Bit(const QString &szPath,const QString &szData,bool bAppend) + { + KviFile f(szPath); + if(!f.openForWriting(bAppend))return false; + KviQCString szTmp = QTextCodec::codecForLocale()->fromUnicode(szData); + if(!szTmp.data())return true; + if(f.writeBlock(szTmp.data(),szTmp.length()) != ((int)(szTmp.length())))return false; + return true; + } + + bool writeFileLocal8Bit(const char* path,const QString &szData,bool bAppend) + { + QString szPath=QString::fromUtf8(path); + return writeFileLocal8Bit(szPath,szData,bAppend); + } + + bool readFile(const QString &szPath,QString &szBuffer,unsigned int uMaxSize) + { + KviFile f(szPath); + if(!f.openForReading())return false; + if(f.size() < 1) + { + szBuffer = ""; + f.close(); + return true; + } + if(f.size() > uMaxSize)return false; + char * buf = new char[f.size() + 1]; + if(f.readBlock(buf,f.size()) != ((long int)f.size())) + { + delete buf; + buf = 0; + return false; + } + buf[f.size()] = '\0'; + szBuffer = QString::fromUtf8(buf); + delete[] buf; + return true; + } + + bool readFile(const char* path,QString &szBuffer,unsigned int uMaxSize) + { + QString szPath=QString::fromUtf8(path); + return readFile(szPath,szBuffer,uMaxSize); + } + + + QString extractFileName(const QString &szFileNameWithPath) + { + return QFileInfo(szFileNameWithPath).fileName(); + } + + QString extractFilePath(const QString &szFileNameWithPath) + { + return QFileInfo(szFileNameWithPath).dirPath(true); + } + + bool readLine(QFile * f,QString &szBuffer,bool bUtf8) + { + QTextStream stream(f); + stream.setEncoding(bUtf8 ? QTextStream::UnicodeUTF8 : QTextStream::Locale); + szBuffer=stream.readLine(); + return !szBuffer.isNull(); + } + + bool readLines(QFile * f,QStringList &buffer,int iStartLine, int iCount, bool bUtf8) + { + QTextStream stream( f ); + stream.setEncoding(bUtf8 ? QTextStream::UnicodeUTF8 : QTextStream::Locale); + for(int i=0;i0) + { + for(; (iCount>0 && !stream.atEnd()) ; iCount-- ) + buffer.append(stream.readLine()); + } else { + while(!stream.atEnd()) { + buffer.append(stream.readLine()); + } + } + return buffer.count()!= 0; + } + + bool isReadable(const QString &szFname) + { + QFileInfo f(szFname); + return (f.exists() && f.isFile() && f.isReadable()); + } + + bool isReadable(const char* path) + { + QString szPath=QString::fromUtf8(path); + return isReadable(szPath); + } + + bool isAbsolutePath(const QString &szPath) + { + QFileInfo f(szPath); + return !f.isRelative(); + } +}; + +static char hexchars[16] = { '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' }; + + +void kvi_encodeFileName(KviStr & path) +{ + QString szPath(path.ptr()); + kvi_encodeFileName(szPath); + path=szPath; +} + +void kvi_encodeFileName(QString & path) +{ + QString src(path); + path=""; + for(int i=0;i> 4]; + path+=hexchars[cur.row() & 15]; + } + path+='%'; + path+=hexchars[cur.cell() >> 4]; + path+=hexchars[cur.cell() & 15]; + } else if (cur=='%') + { + path+="%%"; + } else { + path+=cur; + } + } +} + +//================ kvi_isAbsolutePath ===============// + +bool kvi_isAbsolutePath(const char *path) +{ + if(*path == '/')return true; + if(isalpha(*path)) + { + if((*(path + 1)) == ':')return true; + } + return false; +} + +//=================== kvi_readLine =====================// + +bool kvi_readLine(QFile *f,KviStr &str) +{ + QTextStream stream(f); + QString szBuff=stream.readLine(); + str=szBuff; + return szBuff.isNull() ? 1 : 0; +} + + -- cgit v1.2.3