From 7f09220773e20a36799c203ea9670b7ee2e1e2b1 Mon Sep 17 00:00:00 2001 From: Alexey Uzhva Date: Mon, 26 Mar 2007 17:45:28 +0000 Subject: new functions: $receivedBytes and $sentBytes git-svn-id: https://svn.kvirc.de/svn/trunk/kvirc@436 17fca916-40b9-46aa-a4ea-0a15b648b75c --- src/kvilib/file/kvi_fileutils.cpp | 998 ++++---- src/kvilib/file/kvi_fileutils.h | 1 + src/kvilib/kvilib_vc05.vcproj | 7 +- src/kvilib/net/kvi_socket.cpp | 7 +- src/kvilib/net/kvi_socket.h | 24 +- src/kvirc/kernel/kvi_app.cpp | 2 + src/kvirc/kvirc_vc05.vcproj | 8 +- src/kvirc/kvs/kvi_kvs_corefunctions.cpp | 2 + src/kvirc/kvs/kvi_kvs_corefunctions.h | 2 + src/kvirc/kvs/kvi_kvs_corefunctions_mr.cpp | 22 + src/kvirc/kvs/kvi_kvs_corefunctions_sz.cpp | 24 + src/modules/about/about_vc05.vcproj | 10 +- src/modules/action/action_vc05.vcproj | 10 +- src/modules/actioneditor/actioneditor_vc05.vcproj | 10 +- src/modules/addon/addon_vc05.vcproj | 10 +- src/modules/aliaseditor/aliaseditor_vc05.vcproj | 10 +- src/modules/avatar/avatar_05.vcproj | 10 +- src/modules/chan/chan_vc05.vcproj | 10 +- src/modules/channelsjoin/channelsjoin_vc05.vcproj | 10 +- src/modules/codetester/codetester_vc05.vcproj | 10 +- src/modules/config/config_vc05.vcproj | 10 +- src/modules/dcc/dcc_vc05.vcproj | 10 +- src/modules/dialog/dialog_vc05.vcproj | 10 +- src/modules/dockwidget/dockwidget_vc05.vcproj | 10 +- src/modules/editor/editor_vc05.vcproj | 10 +- src/modules/eventeditor/eventeditor_vc05.vcproj | 10 +- src/modules/file/file_vc05.vcproj | 12 +- src/modules/file/libkvifile.cpp | 2526 ++++++++++---------- .../filetransferwindow_vc05.vcproj | 10 +- src/modules/help/help_vc05.vcproj | 10 +- src/modules/http/http_vc05.vcproj | 10 +- src/modules/ident/ident_vc05.vcproj | 10 +- src/modules/lag/lag_vc05.vcproj | 10 +- src/modules/lamerizer/lamerizer_vc05.vcproj | 10 +- src/modules/links/links_vc05.vcproj | 10 +- src/modules/list/list_vc05.vcproj | 10 +- src/modules/log/log_vc05.vcproj | 10 +- src/modules/logview/logview_vc05.vcproj | 10 +- src/modules/mask/mask_vc05.vcproj | 10 +- src/modules/math/math_vc05.vcproj | 10 +- src/modules/mediaplayer/mediaplayer_vc05.vcproj | 10 +- src/modules/mediaplayer/winamp_vc05.vcproj | 5 +- src/modules/mircimport/mircimport_vc05.vcproj | 10 +- src/modules/my/my_vc05.vcproj | 10 +- src/modules/my/win32/idleui_05.vcproj | 9 +- src/modules/notifier/notifier_vc05.vcproj | 10 +- src/modules/objects/objects_vc05.vcproj | 10 +- src/modules/options/options_vc05.vcproj | 10 +- src/modules/perl/perl_vc05.vcproj | 10 +- src/modules/perlcore/perlcore_vc05.vcproj | 10 +- src/modules/popupeditor/popupeditor_vc05.vcproj | 10 +- src/modules/raweditor/raweditor_vc05.vcproj | 10 +- src/modules/regchan/regchan_vc05.vcproj | 10 +- src/modules/reguser/reguser_vc05.vcproj | 10 +- src/modules/rijndael/rijndael_vc05.vcproj | 10 +- src/modules/setup/setup_vc05.vcproj | 10 +- src/modules/sharedfile/sharedfile_vc05.vcproj | 10 +- .../sharedfileswindow_vc05.vcproj | 10 +- src/modules/snd/snd_vc05.vcproj | 10 +- src/modules/socketspy/socketspy_vc05.vcproj | 10 +- src/modules/spaste/spaste_vc05.vcproj | 10 +- src/modules/str/str_vc05.vcproj | 10 +- src/modules/system/system_vc05.vcproj | 10 +- src/modules/texticons/texticon_vc05.vcproj | 10 +- src/modules/theme/theme_vc05.vcproj | 10 +- src/modules/tip/tip_vc05.vcproj | 10 +- src/modules/tmphighlight/tmphighlight_vc05.vcproj | 10 +- src/modules/toolbar/toolbar_vc05.vcproj | 10 +- .../toolbareditor/toolbareditor_vc05.vcproj | 10 +- src/modules/url/url_vc05.vcproj | 10 +- src/modules/window/window_vc05.vcproj | 10 +- 71 files changed, 1916 insertions(+), 2293 deletions(-) (limited to 'src') diff --git a/src/kvilib/file/kvi_fileutils.cpp b/src/kvilib/file/kvi_fileutils.cpp index 82e689d0b..a6d89413f 100644 --- a/src/kvilib/file/kvi_fileutils.cpp +++ b/src/kvilib/file/kvi_fileutils.cpp @@ -1,489 +1,509 @@ -//============================================================================= -// -// 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. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, 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) - { -#ifdef COMPILE_USE_QT4 - return QFileInfo(szFileNameWithPath).absoluteFilePath(); -#else - return QFileInfo(szFileNameWithPath).absFilePath(); -#endif - } - - bool readLine(QFile * f,QString &szBuffer,bool bUtf8) - { - QTextStream stream(f); - szBuffer=stream.readLine(); - return !szBuffer.isNull(); - } - - 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; -} - - +//============================================================================= +// +// 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. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, 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) + { +#ifdef COMPILE_USE_QT4 + return QFileInfo(szFileNameWithPath).absoluteFilePath(); +#else + return QFileInfo(szFileNameWithPath).absFilePath(); +#endif + } + + 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; +} + + diff --git a/src/kvilib/file/kvi_fileutils.h b/src/kvilib/file/kvi_fileutils.h index 511f421d1..2e3689cbf 100644 --- a/src/kvilib/file/kvi_fileutils.h +++ b/src/kvilib/file/kvi_fileutils.h @@ -90,6 +90,7 @@ namespace KviFileUtils extern KVILIB_API bool makeDir(const char* path); // reads a text line, returns false if EOF is reached extern KVILIB_API bool readLine(QFile * f,QString &szBuffer,bool bUtf8 = true); + extern KVILIB_API bool readLines(QFile * f,QStringList &buffer,int iStartLine = 0, int iCount = -1, bool bUtf8 = true); extern KVILIB_API bool isReadable(const QString &szFname); extern KVILIB_API bool isAbsolutePath(const QString &szPath); }; diff --git a/src/kvilib/kvilib_vc05.vcproj b/src/kvilib/kvilib_vc05.vcproj index b636bb315..e2f25945f 100644 --- a/src/kvilib/kvilib_vc05.vcproj +++ b/src/kvilib/kvilib_vc05.vcproj @@ -129,17 +129,14 @@ +#include "kvi_inttypes.h" + +//#ifndef _KVI_SOCKET_CPP_ + extern KVILIB_API kvi_u64_t g_uOutgoingTraffic; + extern KVILIB_API kvi_u64_t g_uIncomingTraffic; +//#endif //!_KVI_SOCKET_CPP_ + + + #ifdef COMPILE_ON_WINDOWS #define KVI_INVALID_SOCKET INVALID_SOCKET @@ -94,6 +103,13 @@ inline kvi_socket_t kvi_socket_create(int pf,int type,int proto) // // Check if a socket is valid or not // + +inline void kvi_socket_flushTrafficCounters() +{ + g_uOutgoingTraffic = 0; + g_uIncomingTraffic = 0; +} + inline bool kvi_socket_isValid(kvi_socket_t sock) { return (sock != ((kvi_socket_t)(KVI_INVALID_SOCKET))); @@ -233,6 +249,7 @@ inline int kvi_socket_select(int nhpo,fd_set *r,fd_set *w,fd_set *e,struct timev inline int kvi_socket_send(kvi_socket_t sock,const void * buf,int size) { + g_uOutgoingTraffic+=size; #ifdef COMPILE_ON_WINDOWS return ::send(sock,(const char *)buf,size,0); #else @@ -252,11 +269,14 @@ inline int kvi_socket_send(kvi_socket_t sock,const void * buf,int size) inline int kvi_socket_recv(kvi_socket_t sock,void * buf,int maxlen) { + int iReceived; #ifdef COMPILE_ON_WINDOWS - return ::recv(sock,(char *)buf,maxlen,0); + iReceived = ::recv(sock,(char *)buf,maxlen,0); #else - return ::recv(sock,buf,maxlen,MSG_NOSIGNAL); + iReceived = ::recv(sock,buf,maxlen,MSG_NOSIGNAL); #endif + g_uIncomingTraffic+=iReceived; + return iReceived; }; //================================================================================================ diff --git a/src/kvirc/kernel/kvi_app.cpp b/src/kvirc/kernel/kvi_app.cpp index 5c701aa92..0bae5c106 100644 --- a/src/kvirc/kernel/kvi_app.cpp +++ b/src/kvirc/kernel/kvi_app.cpp @@ -26,6 +26,7 @@ #define _KVI_APP_CPP_ +#include "kvi_socket.h" #include "kvi_app.h" #include "kvi_debug.h" #include "kvi_frame.h" @@ -179,6 +180,7 @@ KviApp::KviApp(int &argc,char ** argv) m_iHeartbeatTimerId = -1; // next step is setup() m_bSetupDone = false; + kvi_socket_flushTrafficCounters(); } void KviApp::setup() diff --git a/src/kvirc/kvirc_vc05.vcproj b/src/kvirc/kvirc_vc05.vcproj index 9dfc5a3ee..7635b6cdd 100644 --- a/src/kvirc/kvirc_vc05.vcproj +++ b/src/kvirc/kvirc_vc05.vcproj @@ -127,14 +127,14 @@ /> $receivedBytes() + @description: + Returns total received bytes + @seealso: + [fnc]$sentBytes[/fnc] + */ + + KVSCF(receivedBytes) + { + KVSCF_pRetBuffer->setInteger(g_uIncomingTraffic); + return true; + } /* @doc: rsort diff --git a/src/kvirc/kvs/kvi_kvs_corefunctions_sz.cpp b/src/kvirc/kvs/kvi_kvs_corefunctions_sz.cpp index 94a67eacd..781b019a9 100644 --- a/src/kvirc/kvs/kvi_kvs_corefunctions_sz.cpp +++ b/src/kvirc/kvs/kvi_kvs_corefunctions_sz.cpp @@ -42,6 +42,7 @@ #include "kvi_modulemanager.h" #include "kvi_mirccntrl.h" #include "kvi_sourcesdate.h" +#include "kvi_socket.h" #include // rand & srand @@ -144,6 +145,29 @@ namespace KviKvsCoreFunctions return true; } + + /* + @doc: sentBytes + @type: + function + @title: + $sentBytes + @short: + Returns total sent bytes + @syntax: + $sentBytes() + @description: + Returns total sent bytes + @seealso: + [fnc]$receivedBytes[/fnc] + */ + + KVSCF(sentBytes) + { + KVSCF_pRetBuffer->setInteger(g_uOutgoingTraffic); + return true; + } + ///////////////////////////////////////////////////////////////////////////////////////// /* diff --git a/src/modules/about/about_vc05.vcproj b/src/modules/about/about_vc05.vcproj index ac9483793..9ada3be8c 100644 --- a/src/modules/about/about_vc05.vcproj +++ b/src/modules/about/about_vc05.vcproj @@ -129,22 +129,15 @@ -#include "kvi_file.h" -#include - -/* - @doc: file.copy - @type: - command - @title: - file.copy - @keyterms: - copying files - @short: - Makes a copy of a file - @syntax: - file.copy [-o] - @switches: - !sw: -o | --overwrite - Overwrites the file even if it already exists. - @description: - Makes a copy of the file as .[br] - If the [-o] switch is used , the file is overwritten , if already exists.[br] - With no [-o] switch , this command does not overwrite files.[br] - The destination path must be already existing: if you want to ensure this, use [fnc]$file.mkdir[/fnc] first.[br] - The paths ( and ) are adjusted according to the system that KVIrc - is running on so you don't have to bother about portability: it *should* be automatically - guaranteed. Just use UNIX style paths for them.[br] - @seealso: - [cmd]file.rename[/cmd], [fnc]$file.exists[/fnc]() -*/ - -static bool file_kvs_cmd_copy(KviKvsModuleCommandCall * c) -{ - QString szSrc,szDst; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("source",KVS_PT_NONEMPTYSTRING,0,szSrc) - KVSM_PARAMETER("destination",KVS_PT_NONEMPTYSTRING,0,szDst) - KVSM_PARAMETERS_END(c) - KviFileUtils::adjustFilePath(szSrc); - KviFileUtils::adjustFilePath(szDst); - if(KviFileUtils::fileExists(szDst) && !c->switches()->find('o',"overwrite")) - { - c->warning(__tr2qs("Destinaion file exists: no copy made")); - return true; - } - if(!KviFileUtils::copyFile(szSrc,szDst)) - { - c->warning(__tr2qs("Failed to copy from %Q to %Q"),&szSrc,&szDst); - c->warning(__tr2qs("Either the source doesn't exist or the destination can not be created")); - } - return true; -} - - -/* - @doc: file.addimagepath - @type: - command - @title: - file.addimagepath - @short: - Adds an image search path - @syntax: - file.addimagepath - @description: - Adds to the image search path list.[br] - KVIrc will look for images files (also) in that path (when a relative image file name is used).[br] - Yes, this function is a bit misplaced... - @seealso: - [cmd]file.delimagepath[/cmd] -*/ - -static bool file_kvs_cmd_addimagepath(KviKvsModuleCommandCall * c) -{ - QString szDst; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("path",KVS_PT_NONEMPTYSTRING,0,szDst) - KVSM_PARAMETERS_END(c) - KviFileUtils::adjustFilePath(szDst); - - QStringList::Iterator it = KVI_OPTION_STRINGLIST(KviOption_stringlistImageSearchPaths).find(szDst); - if(it == KVI_OPTION_STRINGLIST(KviOption_stringlistImageSearchPaths).end()) - KVI_OPTION_STRINGLIST(KviOption_stringlistImageSearchPaths).append(szDst); - return true; -} - - - -/* - @doc: file.delimagepath - @type: - command - @title: - file.delimagepath - @short: - Removes an image search path - @syntax: - file.delimagepath - @description: - Removes from the image search path list.[br] - Yes, this function is a bit misplaced... - @seealso: - [cmd]file.addimagepath[/cmd] -*/ - -static bool file_kvs_cmd_delimagepath(KviKvsModuleCommandCall * c) -{ - QString szDst; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("path",KVS_PT_NONEMPTYSTRING,0,szDst) - KVSM_PARAMETERS_END(c) - KviFileUtils::adjustFilePath(szDst); - - QStringList::Iterator it = KVI_OPTION_STRINGLIST(KviOption_stringlistImageSearchPaths).find(szDst); - if(it == KVI_OPTION_STRINGLIST(KviOption_stringlistImageSearchPaths).end()) - KVI_OPTION_STRINGLIST(KviOption_stringlistImageSearchPaths).remove(szDst); - return true; -} - - -/* - @doc: file.write - @type: - command - @title: - file.write - @keyterms: - writing data to files - @short: - Writes an ascii data string to a file - @syntax: - file.write [-a] [-l] - @switches: - !sw: -a | --append - If the file already exists, append the data instead of overwriting the original contents. - !sw: -l | --local-8-bit - Causes the file to be written in the local 8 bit character set instead of the - default utf8. - @description: - Writes (which is an ASCII string) to the file .[br] - It does NOT append a traling LF character: if you want it you must explicitly specify it in the parameter.[br] - -a causes the command to append the to the file instead of overwriting the entire file.[br] - The path is adjusted according to the system that KVIrc - is running on so you don't have to bother about portability: it *should* be automatically - guaranteed. Just use UNIX style paths for them.[br] - The file is saved in utf8 unless the -l switch is specified (in that case the local 8 bit encoding is used). - Please note that uf8 is the only character set that preserves ALL the possible characters. - @seealso: - [cmd]file.rename[/cmd], [fnc]$file.exists[/fnc]() -*/ - -static bool file_kvs_cmd_write(KviKvsModuleCommandCall * c) -{ - QString szFileName,szData; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("filename",KVS_PT_NONEMPTYSTRING,0,szFileName) - KVSM_PARAMETER("data",KVS_PT_NONEMPTYSTRING,0,szData) - KVSM_PARAMETERS_END(c) - KviFileUtils::adjustFilePath(szFileName); - - bool bRet; - if(c->switches()->find('l',"local-8-bit")) - bRet = KviFileUtils::writeFileLocal8Bit(szFileName,szData,c->switches()->find('a',"append")); - else - bRet = KviFileUtils::writeFile(szFileName,szData,c->switches()->find('a',"append")); - - if(!bRet) - c->warning(__tr2qs("Failed to write to file %Q: the destination couldn't be opened"),&szFileName); - - return true; -} - - -/* - @doc: file.rename - @type: - command - @title: - file.rename - @keyterms: - copying files - @short: - Makes a copy of a file - @syntax: - file.rename - @description: - Renames a file from to .[br] - This command can also rename directories.[br] - If the file already exists , this command fails.[br] - The paths ( and ) are adjusted according to the system that KVIrc - is running on so you don't have to bother about portability: it *should* be automatically - guaranteed.Just use UNIX style paths for them.[br] - @seealso: - [cmd]file.copy[/cmd], [fnc]$file.exists[/fnc]() -*/ - -static bool file_kvs_cmd_rename(KviKvsModuleCommandCall * c) -{ - QString szOld,szNew; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("oldname",KVS_PT_NONEMPTYSTRING,0,szOld) - KVSM_PARAMETER("newname",KVS_PT_NONEMPTYSTRING,0,szNew) - KVSM_PARAMETERS_END(c) - - KviFileUtils::adjustFilePath(szOld); - KviFileUtils::adjustFilePath(szNew); - - if(KviFileUtils::fileExists(szNew)) - c->warning(__tr2qs("Destination file exists: file not renamed")); - - if(!KviFileUtils::renameFile(szOld,szNew)) - c->warning(__tr2qs("Failed to rename %Q to %Q"),&szOld,&szNew); - return true; -} - - -/* - @doc: file.mkdir - @type: - command - @title: - file.mkdir - @keyterms: - creating directories - @short: - Creates a directory - @syntax: - file.mkdir - @description: - Creates the .[br] - The path is adjusted according to the system that KVIrc - is running on so you don't have to bother about portability: it *should* be automatically - guaranteed. Just use an UNIX style path.[br] - @seealso: - [fnc]$file.exists[/fnc]() -*/ - -static bool file_kvs_cmd_mkdir(KviKvsModuleCommandCall * c) -{ - QString szDir; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("directory",KVS_PT_NONEMPTYSTRING,0,szDir) - KVSM_PARAMETERS_END(c) - KviFileUtils::adjustFilePath(szDir); - if(!KviFileUtils::makeDir(szDir)) - c->warning(__tr2qs("Failed to make the directory %Q"),&szDir); - return true; -} - - - -/* - @doc: file.remove - @type: - command - @title: - file.remove - @keyterms: - removing files - @short: - Removes a file - @syntax: - file.remove [-q] - @switches: - !sw: -q | --quiet - Suppresses any warning message - @description: - Removes the file .[br] - -q suppresses any warning message (about non existing file , for example).[br] - The path is adjusted according to the system that KVIrc - is running on so you don't have to bother about portability: it *should* be automatically - guaranteed. Just use an UNIX style path.[br] - @seealso: - [fnc]$file.exists[/fnc]() -*/ - -static bool file_kvs_cmd_remove(KviKvsModuleCommandCall * c) -{ - QString szName; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("name",KVS_PT_NONEMPTYSTRING,0,szName) - KVSM_PARAMETERS_END(c) - KviFileUtils::adjustFilePath(szName); - if(!KviFileUtils::removeFile(szName)) - { - if(!c->switches()->find('q',"quiet")) - c->warning(__tr2qs("Failed to remove the file %Q"),&szName); - } - return true; -} - - -/* - @doc: file.rmdir - @type: - command - @title: - file.rmdir - @keyterms: - removing directories - @short: - Removes a directory - @syntax: - file.rmdir [-q] - @switches: - !sw: -q | --quiet - Suppresses any warning message - @description: - Removes the directory .[br] - The directory must be empty for this command to success.[br] - -q suppresses any warning message (about non existing directory , for example).[br] - The path is adjusted according to the system that KVIrc - is running on so you don't have to bother about portability: it *should* be automatically - guaranteed. Just use an UNIX style path.[br] - @seealso: - [fnc]$file.exists[/fnc]() - [cmd]file.remove[/cmd] -*/ - -static bool file_kvs_cmd_rmdir(KviKvsModuleCommandCall * c) -{ - QString szName; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("name",KVS_PT_NONEMPTYSTRING,0,szName) - KVSM_PARAMETERS_END(c) - KviFileUtils::adjustFilePath(szName); - if(!KviFileUtils::removeDir(szName)) - { - if(!c->switches()->find('q',"quiet")) - c->warning(__tr2qs("Failed to remove the directory %Q"),&szName); - } - return true; -} - - -/* - @doc: file.exists - @type: - function - @title: - $file.exists - @short: - Check if a file exists - @syntax: - $file.exists() - @description: - Returns true if the file exists (this is also valid for directories!).[br] - The should be an unix-style file path and is adjusted according to the system that KVIrc is running on.[br] -*/ - -static bool file_kvs_fnc_exists(KviKvsModuleFunctionCall * c) -{ - QString szName; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("filename",KVS_PT_STRING,0,szName) - KVSM_PARAMETERS_END(c) - KviFileUtils::adjustFilePath(szName); - QFileInfo f(szName); - c->returnValue()->setBoolean(f.exists()); - return true; -} - - -/* - @doc: file.type - @type: - function - @title: - $file.type - @short: - Checks the type of a path - @syntax: - $file.type() - @description: - Returns "f" if the points to a real file , "d" if - is the name of a directory or "l" if it is a symbolic link.[br] - The should be an unix-style file path and is adjusted according to the system that KVIrc is running on.[br] -*/ - -static bool file_kvs_fnc_type(KviKvsModuleFunctionCall * c) -{ - QString szName; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("filename",KVS_PT_NONEMPTYSTRING,0,szName) - KVSM_PARAMETERS_END(c) - KviFileUtils::adjustFilePath(szName); - QFileInfo f(szName); - if(f.isFile())c->returnValue()->setString("f"); - else if(f.isDir())c->returnValue()->setString("d"); - else if(f.isSymLink())c->returnValue()->setString("l"); - return true; -} - - -/* - @doc: file.size - @type: - function - @title: - $file.size - @short: - Returns the size of a file - @syntax: - $file.size() - @description: - Returns the size of the file pointed by .[br] - If the file does not exist , this function returns 0.[br] - The should be an unix-style file path and is adjusted according to the system that KVIrc is running on.[br] -*/ - -static bool file_kvs_fnc_size(KviKvsModuleFunctionCall * c) -{ - QString szName; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("filename",KVS_PT_NONEMPTYSTRING,0,szName) - KVSM_PARAMETERS_END(c) - KviFileUtils::adjustFilePath(szName); - QFileInfo f(szName); - c->returnValue()->setInteger(f.size()); - return true; -} -/* - @doc: file.allSizese - @type: - function - @title: - $file.allSizes - @short: - Returns all sizes of a specified directory. - @syntax: - $file.allSize() - @description: - Returns the size of every files of the specified directory as an array.[br] -*/ - -static bool file_kvs_fnc_allSizes(KviKvsModuleFunctionCall * c) -{ - QString szDir; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("directory",KVS_PT_NONEMPTYSTRING,0,szDir) - KVSM_PARAMETERS_END(c) - KviFileUtils::adjustFilePath(szDir); - - QDir d(szDir); - if(!d.exists()) - { - c->warning(__tr2qs("The specified directory does not exist '%Q'"),&szDir); - return true; - } - - QStringList sl; - sl = d.entryList(QDir::Files); - - KviKvsArray * a = new KviKvsArray(); - QString szFile; - if(!sl.isEmpty()) - { - int idx = 0; - for(QStringList::Iterator it = sl.begin();it != sl.end();++it) - { - szFile=szDir+(*it); - QFileInfo f(szFile); - a->set(idx,new KviKvsVariant((kvs_int_t)f.size())); - idx++; - } - } - c->returnValue()->setArray(a); - - - - - return true; -} - -/* - @doc: file.fixpath - @type: - function - @title: - $file.fixpath - @short: - Converts file paths - @syntax: - $file.fixpath() - @description: - Returns the adjusted to match the current operating - system file path conventions.[br] This means that on UNIX , a path like "C:\folder\file.mp3" - will be returned as "/folder/file.mp3" and vice-versa.[br] - There is a minor problem with unix paths converted to the windows system: unix - has no "drive" concept thus the unix paths do not contain a drive. KVIrc will - always map the paths to the "default" C: drive.[br] - This is a good reason to avoid using absolute hard-coded paths :).[br] - Please note that you DON'T NEED to call this function on paths that you - pass to the other file.* functions: they are adjusted automatically.[br] - @seealso: - [fnc]$file.ps[/fnc] -*/ - - -static bool file_kvs_fnc_fixpath(KviKvsModuleFunctionCall * c) -{ - QString szName; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("filename",KVS_PT_STRING,0,szName) - KVSM_PARAMETERS_END(c) - KviFileUtils::adjustFilePath(szName); - c->returnValue()->setString(szName); - return true; -} - - -/* - @doc: file.ps - @type: - function - @title: - $file.ps - @short: - Returns the file path separator - @syntax: - $file.ps() - @description: - Returns the file path separator for the current operating system.[br] - On windows , '\' is returned , on UNIX , '/'.[br] - @seealso: - [fnc]$file.fixpath[/fnc] -*/ - -static bool file_kvs_fnc_ps(KviKvsModuleFunctionCall * c) -{ - c->returnValue()->setString(QString(QChar(KVI_PATH_SEPARATOR_CHAR))); - return true; -} - - -/* - @doc: file.ls - @type: - function - @title: - $file.ls - @short: - Returns a directory listing - @syntax: - $file.ls(,[[,]]) - @description: - Returns the listing of the specified directory as an array.[br] - The should be an unix-style file path and is adjusted according to the system that KVIrc is running on.[br] - may be a combination of the following characters:[br] - d: list directories[br] - f: list files[br] - l: list symbolic links[br] - r: list readable files[br] - w: list writeable files[br] - x: list executable files[br] - h: list hidden files[br] - s: list system files[br] - n: sort files by name[br] - t: sort files by file time[br] - b: sort files by file size[br] - z: put the directories first, then the files[br] - k: invert sort order[br] - i: case insensitive sort[br] - If is empty then a default of 'dfrwxhs'.[br] - If none of r,w,x is set then KVIrc sets all of them by default.[br] - If is passed then it is interpreted as a wildcard string - that must match the entries to be returned.[br] - @example: - [example] - %dir[]=$file.ls(/,"d") - [cmd]foreach[/cmd](%f,%dir[])echo %f - %dir[]=$file.ls(/usr/include,"f","t*.h"); - [cmd]foreach[/cmd](%f,%dir[])echo %f - %dir[]=$file.ls($file.homedir,"dfr"); - [cmd]foreach[/cmd](%f,%dir[])echo %f - [/example] -*/ - -static bool file_kvs_fnc_ls(KviKvsModuleFunctionCall * c) -{ - QString szDir,szFlags,szFilter; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("directory",KVS_PT_NONEMPTYSTRING,0,szDir) - KVSM_PARAMETER("flags",KVS_PT_STRING,KVS_PF_OPTIONAL,szFlags) - KVSM_PARAMETER("filter",KVS_PT_STRING,KVS_PF_OPTIONAL,szFilter) - KVSM_PARAMETERS_END(c) - - KviFileUtils::adjustFilePath(szDir); - - QDir d(szDir); - if(!d.exists()) - { - c->warning(__tr2qs("The specified directory does not exist '%Q'"),&szDir); - return true; - } - -#ifdef COMPILE_USE_QT4 - QFlags iFlags = 0; -#else - int iFlags = 0; -#endif - if(szFlags.isEmpty())iFlags = QDir::Dirs | QDir::Files | QDir::NoSymLinks | QDir::Readable | QDir::Writable | QDir::Executable | QDir::Hidden | QDir::System; - else { - if(szFlags.find('d',false) != -1)iFlags |= QDir::Dirs; - if(szFlags.find('f',false) != -1)iFlags |= QDir::Files; - if(szFlags.find('l',false) == -1)iFlags |= QDir::NoSymLinks; - if(szFlags.find('r',false) != -1)iFlags |= QDir::Readable; - if(szFlags.find('w',false) != -1)iFlags |= QDir::Writable; - if(szFlags.find('x',false) != -1)iFlags |= QDir::Executable; - if(szFlags.find('h',false) != -1)iFlags |= QDir::Hidden; - if(szFlags.find('s',false) != -1)iFlags |= QDir::System; - } -#ifdef COMPILE_USE_QT4 - QFlags iSort = 0; -#else - int iSort = 0; -#endif - if(szFlags.isEmpty())iSort = QDir::Unsorted; - else { - if(szFlags.find('n',false) != -1)iSort |= QDir::Name; - if(szFlags.find('t',false) != -1)iSort |= QDir::Time; - if(szFlags.find('b',false) != -1)iSort |= QDir::Size; - if(szFlags.find('z',false) != -1)iSort |= QDir::DirsFirst; - if(szFlags.find('k',false) != -1)iSort |= QDir::Reversed; - if(szFlags.find('i',false) != -1)iSort |= QDir::IgnoreCase; - } - - QStringList sl; - if(!szFilter.isEmpty())sl = d.entryList(szFilter,iFlags,iSort); - else sl = d.entryList(iFlags,iSort); - - KviKvsArray * a = new KviKvsArray(); - if(!sl.isEmpty()) - { - int idx = 0; - for(QStringList::Iterator it = sl.begin();it != sl.end();++it) - { - a->set(idx,new KviKvsVariant(*it)); - idx++; - } - } - c->returnValue()->setArray(a); - - return true; -} - -/* - @doc: file.read - @type: - function - @title: - $file.read - @short: - Reads a text file - @syntax: - $file.read([,[,]]) - @description: - Reads at most bytes of the file pointed by .[br] - is an upper limit but may be not reached if the real file is smaller.[br] - The data read is returned as a string , so if the file contains binary data, - expect strange results.[br] If is not specified, then KVIrc tries to read - the whole file up to the 1 MB limit (so if you want to read a file that is - bigger thatn 1 MB then you MUST specify the ).[br] - WARNING: always check the file size before attemting to read a whole file... - reading a CDROM iso image may sit down your system :) (and will prolly crash while - allocating memory , before attempting to read anything)[br] - An empty string is returned if a serious error occures.[br] - The is adjusted according to the system that KVIrc is running on.[br] - Flags are actually limited to the single letter 'l'. By default the file - is decoded from the ut8 characters set. If 'l' is present the the file - is decoded by using the local 8 bit character set instead. - @examples: - [example] - echo $file.read(/proc/cpuinfo) - [/example] - @seealso: - [fnc]$file.readbinary[/fnc] -*/ - -static bool file_kvs_fnc_read(KviKvsModuleFunctionCall * c) -{ - QString szNameZ; - kvs_uint_t uSize; - QString szFlags; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("filename",KVS_PT_NONEMPTYSTRING,0,szNameZ) - KVSM_PARAMETER("size",KVS_PT_UINT,KVS_PF_OPTIONAL,uSize) - KVSM_PARAMETER("flags",KVS_PT_STRING,KVS_PF_OPTIONAL,szFlags) - KVSM_PARAMETERS_END(c) - KviFileUtils::adjustFilePath(szNameZ); - - QFile f(szNameZ); - if(!f.open(IO_ReadOnly)) - { - c->warning(__tr2qs("Can't open the file \"%Q\" for reading"),&szNameZ); - return true; - } - - if(c->params()->count() < 2)uSize = 1024 * 1024; // 1 meg file default - - char * buf = (char *)kvi_malloc(sizeof(char) * (uSize + 1)); - unsigned int uReaded = 0; - unsigned int uRetries = 0; - - while((uReaded < uSize) && (!f.atEnd())) - { - int readedNow = f.readBlock(buf + uReaded,uSize - uReaded); - if(readedNow < 0) - { - kvi_free(buf); - c->warning(__tr2qs("Read error for file %Q"),&szNameZ); - return true; - } else readedNow += uReaded; - uRetries ++; - if(uRetries > 1000) - { - // ops - kvi_free(buf); - c->warning(__tr2qs("Read error for file %Q (have been unable to read the requested size in 1000 retries)"),&szNameZ); - return true; - } - uReaded += readedNow; - } - - buf[uReaded] = '\0'; - - if(szFlags.find('l',false) == -1) - c->returnValue()->setString(QString::fromUtf8(buf)); - else - c->returnValue()->setString(QString::fromLocal8Bit(buf)); - - kvi_free(buf); - - return true; -} - - -/* - @doc: file.readLines - @type: - function - @title: - $file.readLines - @short: - Reads lines of a text file - @syntax: - $file.readLines([,,[[,]]]) - @description: - Reads lines from the specified file and returns them as an array of strings. - The lines are assumed to be separated by linefeed characters (which are NOT returned). - Eventual terminating carriage return characters at the end of the line are stripped. - If is specified, then all the lines with indexes lower that are - discarded. If is specified then a maximum of lines is returned. - If is not specified then all the lines until the end are read. - The is adjusted according to the system that KVIrc is running on.[br] - Flags are actually limited to the single letter 'l'. By default the file - is decoded from the ut8 characters set. If 'l' is present the the file - is decoded by using the local 8 bit character set instead. - WARNING: Always check the size of the file you're going to read: it is not - a good idea attempting to read a 700 MB binary file with this function since - it will probably sit down your system and exhaust your virtual memory. - @examples: - [example] - echo $file.readLines(/proc/cpuinfo) - [/example] - @seealso: - [fnc]$file.read[/fnc](), [cmd]file.writeLines[/fnc], [fnc]$lf[/fnc] -*/ - - - -static bool file_kvs_fnc_readLines(KviKvsModuleFunctionCall * c) -{ - QString szName; - QString szFlags; - kvs_int_t iStartLine; - kvs_int_t iCount; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("filename",KVS_PT_NONEMPTYSTRING,0,szName) - KVSM_PARAMETER("startline",KVS_PT_INT,KVS_PF_OPTIONAL,iStartLine) - KVSM_PARAMETER("count",KVS_PT_INT,KVS_PF_OPTIONAL,iCount) - KVSM_PARAMETER("flags",KVS_PT_STRING,KVS_PF_OPTIONAL,szFlags) - KVSM_PARAMETERS_END(c) - KviFileUtils::adjustFilePath(szName); - - QFile f(szName); - if(!f.open(IO_ReadOnly)) - { - c->warning(__tr2qs("Can't open the file \"%Q\" for reading"),&szName); - return true; - } - - if(c->params()->count() < 2)iStartLine = 0; - if(c->params()->count() < 3)iCount = -1; - - bool bLocal8Bit = szFlags.find('l',0,false) != -1; - - KviKvsArray * a = new KviKvsArray(); - - int i = 0; - int idx = 0; - while(idx < iStartLine) - { - QString szDummy; - KviFileUtils::readLine(&f,szDummy); - idx++; - } - - QString szBuf; - - if(iCount > 0) - { - int iEndLine = iStartLine + iCount; - while(idx < iEndLine) - { - if(KviFileUtils::readLine(&f,szBuf,!bLocal8Bit)) - a->set(i,new KviKvsVariant(szBuf)); - else - break; - i++; - idx++; - } - } else { - while(KviFileUtils::readLine(&f,szBuf,!bLocal8Bit)) - { - a->set(i,new KviKvsVariant(szBuf)); - i++; - } - } - - c->returnValue()->setArray(a); - - return true; -} - - -/* - @doc: file.writelines - @type: - command - @title: - file.writeLines - @short: - Writes an array of lines to a file - @syntax: - file.writeLines [-l] [-a] [-c] - @switches: - !sw: -l | --local-8-bit - Causes the lines to be saved in local 8 bit character set instead - of the default unicode encoding. - !sw: -a | --append - If the file already exists, then the lines are appended to the end - instead of overwriting the file. - !sw: -c | --crlf - The lines are separated by a carriage-return+line-feed character - combination, compatible with windows text mode files. - This is the only way to make the file readable in windows notepad, for example. - Please note that this is broken design: do NOT use it :) - !sw: -n | --no-separator - Do not separate the lines at all (either the separators are already - inside the lines array or no line separation is desired at all). - -n takes precedence over -c. - !sw: -q | --quiet - Don't complain if the file can't be opened: just fail silently - @description: - Writes the array of to the specified file. - The lines are separated by a single linefeed character (see also [fnc]$lf[/fnc]) - unless the -c or -n switches are used. If the file already exists then it is - overwritten with the new data unless the -a switch is used. - The lines array is encoded in the ut8 character set unless the -l switch is used. - @seealso: - [fnc]$file.readLines[/fnc] -*/ - - - -static bool file_kvs_cmd_writeLines(KviKvsModuleCommandCall * c) -{ - QString szFile,szFlags; - KviKvsArrayCast a; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("filename",KVS_PT_NONEMPTYSTRING,0,szFile) - KVSM_PARAMETER("lines",KVS_PT_ARRAYCAST,0,a) - KVSM_PARAMETERS_END(c) - - KviFileUtils::adjustFilePath(szFile); - - KviFile f(szFile); - int iFlags = IO_WriteOnly; - - if(!f.openForWriting(c->switches()->find('a',"append"))) - { - if(!c->switches()->find('q',"quiet")) - c->warning(__tr2qs("Can't open the file \"%Q\" for writing"),&szFile); - return true; - } - - bool bLocal8Bit = c->switches()->find('l',"local-8-bit"); - bool bNoSeparator = c->switches()->find('n',"no-separator"); - bool bAddCR = c->switches()->find('c',"crlf"); - - unsigned int u = 0; - while(u < a.array()->size()) - { - KviKvsVariant * v = a.array()->at(u); - KviQCString dat; - if(v) - { - QString szDat; - v->asString(szDat); - KviQCString dat = bLocal8Bit ? szDat.local8Bit() : szDat.utf8(); - } - if(!bNoSeparator) - { - if(bAddCR)dat += "\r\n"; - else dat += '\n'; - } - f.writeBlock(dat.data(),dat.length()); - u++; - } - - f.close(); - - return true; -} - - -/* - @doc: file.localdir - @type: - function - @title: - $file.localdir - @short: - Get the KVIrc local directory - @syntax: - $file.localdir([relative_path:string]) - @description: - Returns the path to the KVIrc local data directory.[br] - The KVIrc local data directory is always writeable and contains - the various subdirectories that KVIrc uses internally: audio , avatars , - config , help , incoming , log , modules , msgcolors and pics.[br] - If is passed , then it is appended at the end of the directory - to form a complete filepath.[br] - The path is adjusted to contain single separators suitable for the platform - that KVIrc is atually running on (thus you not need to care about path - separators in the , KVIrc will adjust them).[br] - @examples: - [example] - echo KVIrc looks for pictures in $file.localdir(pics) - echo panic.png would be translated to $file.localdir(pics/panic.png) - [/example] -*/ - -static bool file_kvs_fnc_localdir(KviKvsModuleFunctionCall * c) -{ - QString szName; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("relative_path",KVS_PT_STRING,KVS_PF_OPTIONAL,szName) - KVSM_PARAMETERS_END(c) - if(szName.isEmpty())szName.append(KVI_PATH_SEPARATOR_CHAR); - QString szPath; - g_pApp->getLocalKvircDirectory(szPath,KviApp::None,szName); - KviFileUtils::adjustFilePath(szPath); - c->returnValue()->setString(szPath); - return true; -} - - - -/* - @doc: file.homedir - @type: - function - @title: - $file.homedir - @short: - Get the user's HOME directory - @syntax: - $file.homedir([relative_path:string]) - @description: - Returns the user's HOME directory path.[br] - If is passed , then it is appended at the end of the directory - to form a complete filepath.[br] - The path is adjusted to contain single separators suitable for the platform - that KVIrc is atually running on (thus you not need to care about path - separators in the , KVIrc will adjust them).[br] -*/ - -static bool file_kvs_fnc_homedir(KviKvsModuleFunctionCall * c) -{ - QString szName; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("relative_path",KVS_PT_STRING,KVS_PF_OPTIONAL,szName) - KVSM_PARAMETERS_END(c) - if(szName.isEmpty())szName.append(KVI_PATH_SEPARATOR_CHAR); - QString szPath = QDir::homeDirPath(); - KviQString::ensureLastCharIs(szPath,KVI_PATH_SEPARATOR_CHAR); - szPath.append(szName); - KviFileUtils::adjustFilePath(szPath); - c->returnValue()->setString(szPath); - return true; -} - - - -/* - @doc: file.rootdir - @type: - function - @title: - $file.rootdir - @short: - Get the system root directory - @syntax: - $file.rootdir() - $file.rootdir - @description: - Returns the system's root directory (/ on UNIX and C:/ on Windows).[br] - If is passed , then it is appended at the end of the directory - to form a complete filepath.[br] - The path is adjusted to contain single separators suitable for the platform - that KVIrc is atually running on (thus you not need to care about path - separators in the , KVIrc will adjust them).[br] -*/ - -static bool file_kvs_fnc_rootdir(KviKvsModuleFunctionCall * c) -{ - QString szName; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("relative_path",KVS_PT_STRING,KVS_PF_OPTIONAL,szName) - KVSM_PARAMETERS_END(c) - if(szName.isEmpty())szName.append(KVI_PATH_SEPARATOR_CHAR); - QString szPath = QDir::rootDirPath(); - KviQString::ensureLastCharIs(szPath,KVI_PATH_SEPARATOR_CHAR); - szPath.append(szName); - KviFileUtils::adjustFilePath(szPath); - c->returnValue()->setString(szPath); - return true; -} - - -/* - @doc: file.cwd - @type: - function - @title: - $file.currentdir - @short: - Get the current directory - @syntax: - $file.cwd([relative_path:string]) - @description: - Returns the current working directory.[br] - If is passed , then it is appended at the end of the directory - to form a complete filepath.[br] - The path is adjusted to contain single separators suitable for the platform - that KVIrc is atually running on (thus you not need to care about path - separators in the , KVIrc will adjust them).[br] -*/ - -static bool file_kvs_fnc_cwd(KviKvsModuleFunctionCall * c) -{ - QString szName; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("relative_path",KVS_PT_STRING,KVS_PF_OPTIONAL,szName) - KVSM_PARAMETERS_END(c) - if(szName.isEmpty())szName.append(KVI_PATH_SEPARATOR_CHAR); - QString szPath = QDir::currentDirPath(); - KviQString::ensureLastCharIs(szPath,KVI_PATH_SEPARATOR_CHAR); - szPath.append(szName); - KviFileUtils::adjustFilePath(szPath); - c->returnValue()->setString(szPath); - return true; -} - - -/* - @doc: file.globaldir - @type: - function - @title: - $file.globaldir - @short: - Get the KVIrc global directory - @syntax: - $file.globaldir() - $file.globaldir - @description: - Returns the path to the KVIrc global data directory.[br] - The KVIrc local data directory is always readable but usually not writeable and contains - the various subdirectories that KVIrc uses internally: audio , avatars , - config , help , incoming , log , modules , msgcolors and pics.[br] - If is passed , then it is appended at the end of the directory - to form a complete filepath.[br] - The path is adjusted to contain single separators suitable for the platform - that KVIrc is atually running on (thus you not need to care about path - separators in the , KVIrc will adjust them).[br] - @examples: - [example] - echo KVIrc looks for pictures in $file.globaldir(pics) - echo panic.png would be translated to $file.globaldir(pics/panic.png) - [/example] -*/ - -static bool file_kvs_fnc_globaldir(KviKvsModuleFunctionCall * c) -{ - QString szName; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("relative_path",KVS_PT_STRING,KVS_PF_OPTIONAL,szName) - KVSM_PARAMETERS_END(c) - if(szName.isEmpty())szName.append(KVI_PATH_SEPARATOR_CHAR); - QString szPath; - g_pApp->getGlobalKvircDirectory(szPath,KviApp::None,szName); - KviFileUtils::adjustFilePath(szPath); - c->returnValue()->setString(szPath); - return true; -} - -/* - @doc: file.extractpath - @type: - function - @title: - $file.extractpath - @short: - Extract the path from a filename - @syntax: - $file.extractpath() - @description: - Returns the path part of the translated to match the current - platform filesystem conventions.[br] - The path will NOT contain a trailing path separator.[br] - For example, if is /usr/arch/mp3/Carisma_SuonoDelSilenzio.mp3 then - this function will return /usr/arch/mp3 on UNIX and C:\usr\arch\mp3 on Windows. - @seealso: - [fnc]$file.extractfilename[/fnc] -*/ - -static bool file_kvs_fnc_extractpath(KviKvsModuleFunctionCall * c) -{ - QString szName; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("filepath",KVS_PT_NONEMPTYSTRING,0,szName) - KVSM_PARAMETERS_END(c) - c->returnValue()->setString(QFileInfo(szName).dirPath(TRUE)); - return true; -} - - -/* - @doc: file.extractfilename - @type: - function - @title: - $file.extractfilename - @short: - Extract the filename from a file path - @syntax: - $file.extractpath() - @description: - Returns the filename part of the filepath translated to match the current - platform filesystem conventions.[br] - For example, if is /usr/arch/mp3/Carisma_SuonoDelSilenzio.mp3 then - this function will return Carisma_SuonoDelSilenzio.mp3 - @seealso: - [fnc]$file.extractpath[/fnc] -*/ - -static bool file_kvs_fnc_extractfilename(KviKvsModuleFunctionCall * c) -{ - QString szName; - KVSM_PARAMETERS_BEGIN(c) - KVSM_PARAMETER("filepath",KVS_PT_NONEMPTYSTRING,0,szName) - KVSM_PARAMETERS_END(c) - KviFileUtils::extractFileName(szName); - KviQString::cutToLast(szName,KVI_PATH_SEPARATOR_CHAR); - c->returnValue()->setString(szName); - return true; -} - - - -static bool file_module_init(KviModule * m) -{ - - KVSM_REGISTER_SIMPLE_COMMAND(m,"copy",file_kvs_cmd_copy); - KVSM_REGISTER_SIMPLE_COMMAND(m,"rename",file_kvs_cmd_rename); - KVSM_REGISTER_SIMPLE_COMMAND(m,"mkdir",file_kvs_cmd_mkdir); - KVSM_REGISTER_SIMPLE_COMMAND(m,"write",file_kvs_cmd_write); - KVSM_REGISTER_SIMPLE_COMMAND(m,"remove",file_kvs_cmd_remove); - KVSM_REGISTER_SIMPLE_COMMAND(m,"rmdir",file_kvs_cmd_rmdir); - KVSM_REGISTER_SIMPLE_COMMAND(m,"addimagepath",file_kvs_cmd_addimagepath); - KVSM_REGISTER_SIMPLE_COMMAND(m,"delimagepath",file_kvs_cmd_delimagepath); - KVSM_REGISTER_SIMPLE_COMMAND(m,"writeLines",file_kvs_cmd_writeLines); - - KVSM_REGISTER_FUNCTION(m,"exists",file_kvs_fnc_exists); - KVSM_REGISTER_FUNCTION(m,"type",file_kvs_fnc_type); - KVSM_REGISTER_FUNCTION(m,"size",file_kvs_fnc_size); - - KVSM_REGISTER_FUNCTION(m,"allsizes",file_kvs_fnc_allSizes); - - KVSM_REGISTER_FUNCTION(m,"fixpath",file_kvs_fnc_fixpath); - KVSM_REGISTER_FUNCTION(m,"ps",file_kvs_fnc_ps); - KVSM_REGISTER_FUNCTION(m,"read",file_kvs_fnc_read); - KVSM_REGISTER_FUNCTION(m,"localdir",file_kvs_fnc_localdir); - KVSM_REGISTER_FUNCTION(m,"homedir",file_kvs_fnc_homedir); - KVSM_REGISTER_FUNCTION(m,"rootdir",file_kvs_fnc_rootdir); - KVSM_REGISTER_FUNCTION(m,"cwd",file_kvs_fnc_cwd); - KVSM_REGISTER_FUNCTION(m,"globaldir",file_kvs_fnc_globaldir); - KVSM_REGISTER_FUNCTION(m,"extractpath",file_kvs_fnc_extractpath); - KVSM_REGISTER_FUNCTION(m,"extractfilename",file_kvs_fnc_extractfilename); - KVSM_REGISTER_FUNCTION(m,"ls",file_kvs_fnc_ls); - KVSM_REGISTER_FUNCTION(m,"readLines",file_kvs_fnc_readLines); - - - return true; -} - -static bool file_module_cleanup(KviModule *m) -{ - return true; -} - -KVIRC_MODULE( - "File", // module name - "1.0.0", // module version - "Copyright (C) 2001 Szymon Stefanek (pragma at kvirc dot net)", // author & (C) - "Interface to the file system", - file_module_init, - 0, - 0, - file_module_cleanup -) +//============================================================================= +// +// File : libkvifile.cpp +// Creation date : Fri Nov 9 03:27:59 2001 GMT by Szymon Stefanek +// +// This file is part of the KVirc irc client distribution +// Copyright (C) 2001-2005 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. ,59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// +//============================================================================= + +#include "kvi_module.h" +#include "kvi_fileutils.h" +#include "kvi_locale.h" +#include "kvi_malloc.h" +#include "kvi_app.h" +#include "kvi_options.h" +#include "kvi_qcstring.h" + +#include "kvi_kvs_arraycast.h" + +#include +#include "kvi_file.h" +#include + +/* + @doc: file.copy + @type: + command + @title: + file.copy + @keyterms: + copying files + @short: + Makes a copy of a file + @syntax: + file.copy [-o] + @switches: + !sw: -o | --overwrite + Overwrites the file even if it already exists. + @description: + Makes a copy of the file as .[br] + If the [-o] switch is used , the file is overwritten , if already exists.[br] + With no [-o] switch , this command does not overwrite files.[br] + The destination path must be already existing: if you want to ensure this, use [fnc]$file.mkdir[/fnc] first.[br] + The paths ( and ) are adjusted according to the system that KVIrc + is running on so you don't have to bother about portability: it *should* be automatically + guaranteed. Just use UNIX style paths for them.[br] + @seealso: + [cmd]file.rename[/cmd], [fnc]$file.exists[/fnc]() +*/ + +static bool file_kvs_cmd_copy(KviKvsModuleCommandCall * c) +{ + QString szSrc,szDst; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("source",KVS_PT_NONEMPTYSTRING,0,szSrc) + KVSM_PARAMETER("destination",KVS_PT_NONEMPTYSTRING,0,szDst) + KVSM_PARAMETERS_END(c) + KviFileUtils::adjustFilePath(szSrc); + KviFileUtils::adjustFilePath(szDst); + if(KviFileUtils::fileExists(szDst) && !c->switches()->find('o',"overwrite")) + { + c->warning(__tr2qs("Destinaion file exists: no copy made")); + return true; + } + if(!KviFileUtils::copyFile(szSrc,szDst)) + { + c->warning(__tr2qs("Failed to copy from %Q to %Q"),&szSrc,&szDst); + c->warning(__tr2qs("Either the source doesn't exist or the destination can not be created")); + } + return true; +} + + +/* + @doc: file.addimagepath + @type: + command + @title: + file.addimagepath + @short: + Adds an image search path + @syntax: + file.addimagepath + @description: + Adds to the image search path list.[br] + KVIrc will look for images files (also) in that path (when a relative image file name is used).[br] + Yes, this function is a bit misplaced... + @seealso: + [cmd]file.delimagepath[/cmd] +*/ + +static bool file_kvs_cmd_addimagepath(KviKvsModuleCommandCall * c) +{ + QString szDst; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("path",KVS_PT_NONEMPTYSTRING,0,szDst) + KVSM_PARAMETERS_END(c) + KviFileUtils::adjustFilePath(szDst); + + QStringList::Iterator it = KVI_OPTION_STRINGLIST(KviOption_stringlistImageSearchPaths).find(szDst); + if(it == KVI_OPTION_STRINGLIST(KviOption_stringlistImageSearchPaths).end()) + KVI_OPTION_STRINGLIST(KviOption_stringlistImageSearchPaths).append(szDst); + return true; +} + + + +/* + @doc: file.delimagepath + @type: + command + @title: + file.delimagepath + @short: + Removes an image search path + @syntax: + file.delimagepath + @description: + Removes from the image search path list.[br] + Yes, this function is a bit misplaced... + @seealso: + [cmd]file.addimagepath[/cmd] +*/ + +static bool file_kvs_cmd_delimagepath(KviKvsModuleCommandCall * c) +{ + QString szDst; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("path",KVS_PT_NONEMPTYSTRING,0,szDst) + KVSM_PARAMETERS_END(c) + KviFileUtils::adjustFilePath(szDst); + + QStringList::Iterator it = KVI_OPTION_STRINGLIST(KviOption_stringlistImageSearchPaths).find(szDst); + if(it == KVI_OPTION_STRINGLIST(KviOption_stringlistImageSearchPaths).end()) + KVI_OPTION_STRINGLIST(KviOption_stringlistImageSearchPaths).remove(szDst); + return true; +} + + +/* + @doc: file.write + @type: + command + @title: + file.write + @keyterms: + writing data to files + @short: + Writes an ascii data string to a file + @syntax: + file.write [-a] [-l] + @switches: + !sw: -a | --append + If the file already exists, append the data instead of overwriting the original contents. + !sw: -l | --local-8-bit + Causes the file to be written in the local 8 bit character set instead of the + default utf8. + @description: + Writes (which is an ASCII string) to the file .[br] + It does NOT append a traling LF character: if you want it you must explicitly specify it in the parameter.[br] + -a causes the command to append the to the file instead of overwriting the entire file.[br] + The path is adjusted according to the system that KVIrc + is running on so you don't have to bother about portability: it *should* be automatically + guaranteed. Just use UNIX style paths for them.[br] + The file is saved in utf8 unless the -l switch is specified (in that case the local 8 bit encoding is used). + Please note that uf8 is the only character set that preserves ALL the possible characters. + @seealso: + [cmd]file.rename[/cmd], [fnc]$file.exists[/fnc]() +*/ + +static bool file_kvs_cmd_write(KviKvsModuleCommandCall * c) +{ + QString szFileName,szData; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("filename",KVS_PT_NONEMPTYSTRING,0,szFileName) + KVSM_PARAMETER("data",KVS_PT_NONEMPTYSTRING,0,szData) + KVSM_PARAMETERS_END(c) + KviFileUtils::adjustFilePath(szFileName); + + bool bRet; + if(c->switches()->find('l',"local-8-bit")) + bRet = KviFileUtils::writeFileLocal8Bit(szFileName,szData,c->switches()->find('a',"append")); + else + bRet = KviFileUtils::writeFile(szFileName,szData,c->switches()->find('a',"append")); + + if(!bRet) + c->warning(__tr2qs("Failed to write to file %Q: the destination couldn't be opened"),&szFileName); + + return true; +} + + +/* + @doc: file.rename + @type: + command + @title: + file.rename + @keyterms: + copying files + @short: + Makes a copy of a file + @syntax: + file.rename + @description: + Renames a file from to .[br] + This command can also rename directories.[br] + If the file already exists , this command fails.[br] + The paths ( and ) are adjusted according to the system that KVIrc + is running on so you don't have to bother about portability: it *should* be automatically + guaranteed.Just use UNIX style paths for them.[br] + @seealso: + [cmd]file.copy[/cmd], [fnc]$file.exists[/fnc]() +*/ + +static bool file_kvs_cmd_rename(KviKvsModuleCommandCall * c) +{ + QString szOld,szNew; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("oldname",KVS_PT_NONEMPTYSTRING,0,szOld) + KVSM_PARAMETER("newname",KVS_PT_NONEMPTYSTRING,0,szNew) + KVSM_PARAMETERS_END(c) + + KviFileUtils::adjustFilePath(szOld); + KviFileUtils::adjustFilePath(szNew); + + if(KviFileUtils::fileExists(szNew)) + c->warning(__tr2qs("Destination file exists: file not renamed")); + + if(!KviFileUtils::renameFile(szOld,szNew)) + c->warning(__tr2qs("Failed to rename %Q to %Q"),&szOld,&szNew); + return true; +} + + +/* + @doc: file.mkdir + @type: + command + @title: + file.mkdir + @keyterms: + creating directories + @short: + Creates a directory + @syntax: + file.mkdir + @description: + Creates the .[br] + The path is adjusted according to the system that KVIrc + is running on so you don't have to bother about portability: it *should* be automatically + guaranteed. Just use an UNIX style path.[br] + @seealso: + [fnc]$file.exists[/fnc]() +*/ + +static bool file_kvs_cmd_mkdir(KviKvsModuleCommandCall * c) +{ + QString szDir; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("directory",KVS_PT_NONEMPTYSTRING,0,szDir) + KVSM_PARAMETERS_END(c) + KviFileUtils::adjustFilePath(szDir); + if(!KviFileUtils::makeDir(szDir)) + c->warning(__tr2qs("Failed to make the directory %Q"),&szDir); + return true; +} + + + +/* + @doc: file.remove + @type: + command + @title: + file.remove + @keyterms: + removing files + @short: + Removes a file + @syntax: + file.remove [-q] + @switches: + !sw: -q | --quiet + Suppresses any warning message + @description: + Removes the file .[br] + -q suppresses any warning message (about non existing file , for example).[br] + The path is adjusted according to the system that KVIrc + is running on so you don't have to bother about portability: it *should* be automatically + guaranteed. Just use an UNIX style path.[br] + @seealso: + [fnc]$file.exists[/fnc]() +*/ + +static bool file_kvs_cmd_remove(KviKvsModuleCommandCall * c) +{ + QString szName; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("name",KVS_PT_NONEMPTYSTRING,0,szName) + KVSM_PARAMETERS_END(c) + KviFileUtils::adjustFilePath(szName); + if(!KviFileUtils::removeFile(szName)) + { + if(!c->switches()->find('q',"quiet")) + c->warning(__tr2qs("Failed to remove the file %Q"),&szName); + } + return true; +} + + +/* + @doc: file.rmdir + @type: + command + @title: + file.rmdir + @keyterms: + removing directories + @short: + Removes a directory + @syntax: + file.rmdir [-q] + @switches: + !sw: -q | --quiet + Suppresses any warning message + @description: + Removes the directory .[br] + The directory must be empty for this command to success.[br] + -q suppresses any warning message (about non existing directory , for example).[br] + The path is adjusted according to the system that KVIrc + is running on so you don't have to bother about portability: it *should* be automatically + guaranteed. Just use an UNIX style path.[br] + @seealso: + [fnc]$file.exists[/fnc]() + [cmd]file.remove[/cmd] +*/ + +static bool file_kvs_cmd_rmdir(KviKvsModuleCommandCall * c) +{ + QString szName; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("name",KVS_PT_NONEMPTYSTRING,0,szName) + KVSM_PARAMETERS_END(c) + KviFileUtils::adjustFilePath(szName); + if(!KviFileUtils::removeDir(szName)) + { + if(!c->switches()->find('q',"quiet")) + c->warning(__tr2qs("Failed to remove the directory %Q"),&szName); + } + return true; +} + + +/* + @doc: file.exists + @type: + function + @title: + $file.exists + @short: + Check if a file exists + @syntax: + $file.exists() + @description: + Returns true if the file exists (this is also valid for directories!).[br] + The should be an unix-style file path and is adjusted according to the system that KVIrc is running on.[br] +*/ + +static bool file_kvs_fnc_exists(KviKvsModuleFunctionCall * c) +{ + QString szName; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("filename",KVS_PT_STRING,0,szName) + KVSM_PARAMETERS_END(c) + KviFileUtils::adjustFilePath(szName); + QFileInfo f(szName); + c->returnValue()->setBoolean(f.exists()); + return true; +} + + +/* + @doc: file.type + @type: + function + @title: + $file.type + @short: + Checks the type of a path + @syntax: + $file.type() + @description: + Returns "f" if the points to a real file , "d" if + is the name of a directory or "l" if it is a symbolic link.[br] + The should be an unix-style file path and is adjusted according to the system that KVIrc is running on.[br] +*/ + +static bool file_kvs_fnc_type(KviKvsModuleFunctionCall * c) +{ + QString szName; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("filename",KVS_PT_NONEMPTYSTRING,0,szName) + KVSM_PARAMETERS_END(c) + KviFileUtils::adjustFilePath(szName); + QFileInfo f(szName); + if(f.isFile())c->returnValue()->setString("f"); + else if(f.isDir())c->returnValue()->setString("d"); + else if(f.isSymLink())c->returnValue()->setString("l"); + return true; +} + + +/* + @doc: file.size + @type: + function + @title: + $file.size + @short: + Returns the size of a file + @syntax: + $file.size() + @description: + Returns the size of the file pointed by .[br] + If the file does not exist , this function returns 0.[br] + The should be an unix-style file path and is adjusted according to the system that KVIrc is running on.[br] +*/ + +static bool file_kvs_fnc_size(KviKvsModuleFunctionCall * c) +{ + QString szName; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("filename",KVS_PT_NONEMPTYSTRING,0,szName) + KVSM_PARAMETERS_END(c) + KviFileUtils::adjustFilePath(szName); + QFileInfo f(szName); + c->returnValue()->setInteger(f.size()); + return true; +} +/* + @doc: file.allSizese + @type: + function + @title: + $file.allSizes + @short: + Returns all sizes of a specified directory. + @syntax: + $file.allSize() + @description: + Returns the size of every files of the specified directory as an array.[br] +*/ + +static bool file_kvs_fnc_allSizes(KviKvsModuleFunctionCall * c) +{ + QString szDir; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("directory",KVS_PT_NONEMPTYSTRING,0,szDir) + KVSM_PARAMETERS_END(c) + KviFileUtils::adjustFilePath(szDir); + + QDir d(szDir); + if(!d.exists()) + { + c->warning(__tr2qs("The specified directory does not exist '%Q'"),&szDir); + return true; + } + + QStringList sl; + sl = d.entryList(QDir::Files); + + KviKvsArray * a = new KviKvsArray(); + QString szFile; + if(!sl.isEmpty()) + { + int idx = 0; + for(QStringList::Iterator it = sl.begin();it != sl.end();++it) + { + szFile=szDir+(*it); + QFileInfo f(szFile); + a->set(idx,new KviKvsVariant((kvs_int_t)f.size())); + idx++; + } + } + c->returnValue()->setArray(a); + + + + + return true; +} + +/* + @doc: file.fixpath + @type: + function + @title: + $file.fixpath + @short: + Converts file paths + @syntax: + $file.fixpath() + @description: + Returns the adjusted to match the current operating + system file path conventions.[br] This means that on UNIX , a path like "C:\folder\file.mp3" + will be returned as "/folder/file.mp3" and vice-versa.[br] + There is a minor problem with unix paths converted to the windows system: unix + has no "drive" concept thus the unix paths do not contain a drive. KVIrc will + always map the paths to the "default" C: drive.[br] + This is a good reason to avoid using absolute hard-coded paths :).[br] + Please note that you DON'T NEED to call this function on paths that you + pass to the other file.* functions: they are adjusted automatically.[br] + @seealso: + [fnc]$file.ps[/fnc] +*/ + + +static bool file_kvs_fnc_fixpath(KviKvsModuleFunctionCall * c) +{ + QString szName; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("filename",KVS_PT_STRING,0,szName) + KVSM_PARAMETERS_END(c) + KviFileUtils::adjustFilePath(szName); + c->returnValue()->setString(szName); + return true; +} + + +/* + @doc: file.ps + @type: + function + @title: + $file.ps + @short: + Returns the file path separator + @syntax: + $file.ps() + @description: + Returns the file path separator for the current operating system.[br] + On windows , '\' is returned , on UNIX , '/'.[br] + @seealso: + [fnc]$file.fixpath[/fnc] +*/ + +static bool file_kvs_fnc_ps(KviKvsModuleFunctionCall * c) +{ + c->returnValue()->setString(QString(QChar(KVI_PATH_SEPARATOR_CHAR))); + return true; +} + + +/* + @doc: file.ls + @type: + function + @title: + $file.ls + @short: + Returns a directory listing + @syntax: + $file.ls(,[[,]]) + @description: + Returns the listing of the specified directory as an array.[br] + The should be an unix-style file path and is adjusted according to the system that KVIrc is running on.[br] + may be a combination of the following characters:[br] + d: list directories[br] + f: list files[br] + l: list symbolic links[br] + r: list readable files[br] + w: list writeable files[br] + x: list executable files[br] + h: list hidden files[br] + s: list system files[br] + n: sort files by name[br] + t: sort files by file time[br] + b: sort files by file size[br] + z: put the directories first, then the files[br] + k: invert sort order[br] + i: case insensitive sort[br] + If is empty then a default of 'dfrwxhs'.[br] + If none of r,w,x is set then KVIrc sets all of them by default.[br] + If is passed then it is interpreted as a wildcard string + that must match the entries to be returned.[br] + @example: + [example] + %dir[]=$file.ls(/,"d") + [cmd]foreach[/cmd](%f,%dir[])echo %f + %dir[]=$file.ls(/usr/include,"f","t*.h"); + [cmd]foreach[/cmd](%f,%dir[])echo %f + %dir[]=$file.ls($file.homedir,"dfr"); + [cmd]foreach[/cmd](%f,%dir[])echo %f + [/example] +*/ + +static bool file_kvs_fnc_ls(KviKvsModuleFunctionCall * c) +{ + QString szDir,szFlags,szFilter; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("directory",KVS_PT_NONEMPTYSTRING,0,szDir) + KVSM_PARAMETER("flags",KVS_PT_STRING,KVS_PF_OPTIONAL,szFlags) + KVSM_PARAMETER("filter",KVS_PT_STRING,KVS_PF_OPTIONAL,szFilter) + KVSM_PARAMETERS_END(c) + + KviFileUtils::adjustFilePath(szDir); + + QDir d(szDir); + if(!d.exists()) + { + c->warning(__tr2qs("The specified directory does not exist '%Q'"),&szDir); + return true; + } + +#ifdef COMPILE_USE_QT4 + QFlags iFlags = 0; +#else + int iFlags = 0; +#endif + if(szFlags.isEmpty())iFlags = QDir::Dirs | QDir::Files | QDir::NoSymLinks | QDir::Readable | QDir::Writable | QDir::Executable | QDir::Hidden | QDir::System; + else { + if(szFlags.find('d',false) != -1)iFlags |= QDir::Dirs; + if(szFlags.find('f',false) != -1)iFlags |= QDir::Files; + if(szFlags.find('l',false) == -1)iFlags |= QDir::NoSymLinks; + if(szFlags.find('r',false) != -1)iFlags |= QDir::Readable; + if(szFlags.find('w',false) != -1)iFlags |= QDir::Writable; + if(szFlags.find('x',false) != -1)iFlags |= QDir::Executable; + if(szFlags.find('h',false) != -1)iFlags |= QDir::Hidden; + if(szFlags.find('s',false) != -1)iFlags |= QDir::System; + } +#ifdef COMPILE_USE_QT4 + QFlags iSort = 0; +#else + int iSort = 0; +#endif + if(szFlags.isEmpty())iSort = QDir::Unsorted; + else { + if(szFlags.find('n',false) != -1)iSort |= QDir::Name; + if(szFlags.find('t',false) != -1)iSort |= QDir::Time; + if(szFlags.find('b',false) != -1)iSort |= QDir::Size; + if(szFlags.find('z',false) != -1)iSort |= QDir::DirsFirst; + if(szFlags.find('k',false) != -1)iSort |= QDir::Reversed; + if(szFlags.find('i',false) != -1)iSort |= QDir::IgnoreCase; + } + + QStringList sl; + if(!szFilter.isEmpty())sl = d.entryList(szFilter,iFlags,iSort); + else sl = d.entryList(iFlags,iSort); + + KviKvsArray * a = new KviKvsArray(); + if(!sl.isEmpty()) + { + int idx = 0; + for(QStringList::Iterator it = sl.begin();it != sl.end();++it) + { + a->set(idx,new KviKvsVariant(*it)); + idx++; + } + } + c->returnValue()->setArray(a); + + return true; +} + +/* + @doc: file.read + @type: + function + @title: + $file.read + @short: + Reads a text file + @syntax: + $file.read([,[,]]) + @description: + Reads at most bytes of the file pointed by .[br] + is an upper limit but may be not reached if the real file is smaller.[br] + The data read is returned as a string , so if the file contains binary data, + expect strange results.[br] If is not specified, then KVIrc tries to read + the whole file up to the 1 MB limit (so if you want to read a file that is + bigger thatn 1 MB then you MUST specify the ).[br] + WARNING: always check the file size before attemting to read a whole file... + reading a CDROM iso image may sit down your system :) (and will prolly crash while + allocating memory , before attempting to read anything)[br] + An empty string is returned if a serious error occures.[br] + The is adjusted according to the system that KVIrc is running on.[br] + Flags are actually limited to the single letter 'l'. By default the file + is decoded from the ut8 characters set. If 'l' is present the the file + is decoded by using the local 8 bit character set instead. + @examples: + [example] + echo $file.read(/proc/cpuinfo) + [/example] + @seealso: + [fnc]$file.readbinary[/fnc] +*/ + +static bool file_kvs_fnc_read(KviKvsModuleFunctionCall * c) +{ + QString szNameZ; + kvs_uint_t uSize; + QString szFlags; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("filename",KVS_PT_NONEMPTYSTRING,0,szNameZ) + KVSM_PARAMETER("size",KVS_PT_UINT,KVS_PF_OPTIONAL,uSize) + KVSM_PARAMETER("flags",KVS_PT_STRING,KVS_PF_OPTIONAL,szFlags) + KVSM_PARAMETERS_END(c) + KviFileUtils::adjustFilePath(szNameZ); + + QFile f(szNameZ); + if(!f.open(IO_ReadOnly)) + { + c->warning(__tr2qs("Can't open the file \"%Q\" for reading"),&szNameZ); + return true; + } + + if(c->params()->count() < 2)uSize = 1024 * 1024; // 1 meg file default + + char * buf = (char *)kvi_malloc(sizeof(char) * (uSize + 1)); + unsigned int uReaded = 0; + unsigned int uRetries = 0; + + while((uReaded < uSize) && (!f.atEnd())) + { + int readedNow = f.readBlock(buf + uReaded,uSize - uReaded); + if(readedNow < 0) + { + kvi_free(buf); + c->warning(__tr2qs("Read error for file %Q"),&szNameZ); + return true; + } else readedNow += uReaded; + uRetries ++; + if(uRetries > 1000) + { + // ops + kvi_free(buf); + c->warning(__tr2qs("Read error for file %Q (have been unable to read the requested size in 1000 retries)"),&szNameZ); + return true; + } + uReaded += readedNow; + } + + buf[uReaded] = '\0'; + + if(szFlags.find('l',false) == -1) + c->returnValue()->setString(QString::fromUtf8(buf)); + else + c->returnValue()->setString(QString::fromLocal8Bit(buf)); + + kvi_free(buf); + + return true; +} + + +/* + @doc: file.readLines + @type: + function + @title: + $file.readLines + @short: + Reads lines of a text file + @syntax: + $file.readLines([,,[[,]]]) + @description: + Reads lines from the specified file and returns them as an array of strings. + The lines are assumed to be separated by linefeed characters (which are NOT returned). + Eventual terminating carriage return characters at the end of the line are stripped. + If is specified, then all the lines with indexes lower that are + discarded. If is specified then a maximum of lines is returned. + If is not specified then all the lines until the end are read. + The is adjusted according to the system that KVIrc is running on.[br] + Flags are actually limited to the single letter 'l'. By default the file + is decoded from the ut8 characters set. If 'l' is present the the file + is decoded by using the local 8 bit character set instead. + WARNING: Always check the size of the file you're going to read: it is not + a good idea attempting to read a 700 MB binary file with this function since + it will probably sit down your system and exhaust your virtual memory. + @examples: + [example] + echo $file.readLines(/proc/cpuinfo) + [/example] + @seealso: + [fnc]$file.read[/fnc](), [cmd]file.writeLines[/fnc], [fnc]$lf[/fnc] +*/ + + + +static bool file_kvs_fnc_readLines(KviKvsModuleFunctionCall * c) +{ + QString szName; + QString szFlags; + kvs_int_t iStartLine; + kvs_int_t iCount; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("filename",KVS_PT_NONEMPTYSTRING,0,szName) + KVSM_PARAMETER("startline",KVS_PT_INT,KVS_PF_OPTIONAL,iStartLine) + KVSM_PARAMETER("count",KVS_PT_INT,KVS_PF_OPTIONAL,iCount) + KVSM_PARAMETER("flags",KVS_PT_STRING,KVS_PF_OPTIONAL,szFlags) + KVSM_PARAMETERS_END(c) + KviFileUtils::adjustFilePath(szName); + + QFile f(szName); + if(!f.open(IO_ReadOnly)) + { + c->warning(__tr2qs("Can't open the file \"%Q\" for reading"),&szName); + return true; + } + + if(c->params()->count() < 2)iStartLine = 0; + if(c->params()->count() < 3)iCount = -1; + + bool bLocal8Bit = szFlags.find('l',0,false) != -1; + + KviKvsArray * a = new KviKvsArray(); + + int iIndex=0; + QTextStream stream( &f ); + + stream.setEncoding(bLocal8Bit ? QTextStream::Locale : QTextStream::UnicodeUTF8); + for(int i=0;i0) + { + for(; (iCount>0 && !stream.atEnd()) ; iCount-- ) + a->set(iIndex,new KviKvsVariant(stream.readLine())); + iIndex++; + } else { + while(!stream.atEnd()) { + a->set(iIndex,new KviKvsVariant(stream.readLine())); + iIndex++; + } + } + + f.close(); + + c->returnValue()->setArray(a); + + return true; +} + + +/* + @doc: file.writelines + @type: + command + @title: + file.writeLines + @short: + Writes an array of lines to a file + @syntax: + file.writeLines [-l] [-a] [-c] + @switches: + !sw: -l | --local-8-bit + Causes the lines to be saved in local 8 bit character set instead + of the default unicode encoding. + !sw: -a | --append + If the file already exists, then the lines are appended to the end + instead of overwriting the file. + !sw: -c | --crlf + The lines are separated by a carriage-return+line-feed character + combination, compatible with windows text mode files. + This is the only way to make the file readable in windows notepad, for example. + Please note that this is broken design: do NOT use it :) + !sw: -n | --no-separator + Do not separate the lines at all (either the separators are already + inside the lines array or no line separation is desired at all). + -n takes precedence over -c. + !sw: -q | --quiet + Don't complain if the file can't be opened: just fail silently + @description: + Writes the array of to the specified file. + The lines are separated by a single linefeed character (see also [fnc]$lf[/fnc]) + unless the -c or -n switches are used. If the file already exists then it is + overwritten with the new data unless the -a switch is used. + The lines array is encoded in the ut8 character set unless the -l switch is used. + @seealso: + [fnc]$file.readLines[/fnc] +*/ + + + +static bool file_kvs_cmd_writeLines(KviKvsModuleCommandCall * c) +{ + QString szFile,szFlags; + KviKvsArrayCast a; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("filename",KVS_PT_NONEMPTYSTRING,0,szFile) + KVSM_PARAMETER("lines",KVS_PT_ARRAYCAST,0,a) + KVSM_PARAMETERS_END(c) + + KviFileUtils::adjustFilePath(szFile); + + KviFile f(szFile); + int iFlags = IO_WriteOnly; + + if(!f.openForWriting(c->switches()->find('a',"append"))) + { + if(!c->switches()->find('q',"quiet")) + c->warning(__tr2qs("Can't open the file \"%Q\" for writing"),&szFile); + return true; + } + + bool bLocal8Bit = c->switches()->find('l',"local-8-bit"); + bool bNoSeparator = c->switches()->find('n',"no-separator"); + bool bAddCR = c->switches()->find('c',"crlf"); + + unsigned int u = 0; + while(u < a.array()->size()) + { + KviKvsVariant * v = a.array()->at(u); + KviQCString dat; + if(v) + { + QString szDat; + v->asString(szDat); + KviQCString dat = bLocal8Bit ? szDat.local8Bit() : szDat.utf8(); + } + if(!bNoSeparator) + { + if(bAddCR)dat += "\r\n"; + else dat += '\n'; + } + f.writeBlock(dat.data(),dat.length()); + u++; + } + + f.close(); + + return true; +} + + +/* + @doc: file.localdir + @type: + function + @title: + $file.localdir + @short: + Get the KVIrc local directory + @syntax: + $file.localdir([relative_path:string]) + @description: + Returns the path to the KVIrc local data directory.[br] + The KVIrc local data directory is always writeable and contains + the various subdirectories that KVIrc uses internally: audio , avatars , + config , help , incoming , log , modules , msgcolors and pics.[br] + If is passed , then it is appended at the end of the directory + to form a complete filepath.[br] + The path is adjusted to contain single separators suitable for the platform + that KVIrc is atually running on (thus you not need to care about path + separators in the , KVIrc will adjust them).[br] + @examples: + [example] + echo KVIrc looks for pictures in $file.localdir(pics) + echo panic.png would be translated to $file.localdir(pics/panic.png) + [/example] +*/ + +static bool file_kvs_fnc_localdir(KviKvsModuleFunctionCall * c) +{ + QString szName; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("relative_path",KVS_PT_STRING,KVS_PF_OPTIONAL,szName) + KVSM_PARAMETERS_END(c) + if(szName.isEmpty())szName.append(KVI_PATH_SEPARATOR_CHAR); + QString szPath; + g_pApp->getLocalKvircDirectory(szPath,KviApp::None,szName); + KviFileUtils::adjustFilePath(szPath); + c->returnValue()->setString(szPath); + return true; +} + + + +/* + @doc: file.homedir + @type: + function + @title: + $file.homedir + @short: + Get the user's HOME directory + @syntax: + $file.homedir([relative_path:string]) + @description: + Returns the user's HOME directory path.[br] + If is passed , then it is appended at the end of the directory + to form a complete filepath.[br] + The path is adjusted to contain single separators suitable for the platform + that KVIrc is atually running on (thus you not need to care about path + separators in the , KVIrc will adjust them).[br] +*/ + +static bool file_kvs_fnc_homedir(KviKvsModuleFunctionCall * c) +{ + QString szName; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("relative_path",KVS_PT_STRING,KVS_PF_OPTIONAL,szName) + KVSM_PARAMETERS_END(c) + if(szName.isEmpty())szName.append(KVI_PATH_SEPARATOR_CHAR); + QString szPath = QDir::homeDirPath(); + KviQString::ensureLastCharIs(szPath,KVI_PATH_SEPARATOR_CHAR); + szPath.append(szName); + KviFileUtils::adjustFilePath(szPath); + c->returnValue()->setString(szPath); + return true; +} + + + +/* + @doc: file.rootdir + @type: + function + @title: + $file.rootdir + @short: + Get the system root directory + @syntax: + $file.rootdir() + $file.rootdir + @description: + Returns the system's root directory (/ on UNIX and C:/ on Windows).[br] + If is passed , then it is appended at the end of the directory + to form a complete filepath.[br] + The path is adjusted to contain single separators suitable for the platform + that KVIrc is atually running on (thus you not need to care about path + separators in the , KVIrc will adjust them).[br] +*/ + +static bool file_kvs_fnc_rootdir(KviKvsModuleFunctionCall * c) +{ + QString szName; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("relative_path",KVS_PT_STRING,KVS_PF_OPTIONAL,szName) + KVSM_PARAMETERS_END(c) + if(szName.isEmpty())szName.append(KVI_PATH_SEPARATOR_CHAR); + QString szPath = QDir::rootDirPath(); + KviQString::ensureLastCharIs(szPath,KVI_PATH_SEPARATOR_CHAR); + szPath.append(szName); + KviFileUtils::adjustFilePath(szPath); + c->returnValue()->setString(szPath); + return true; +} + + +/* + @doc: file.cwd + @type: + function + @title: + $file.currentdir + @short: + Get the current directory + @syntax: + $file.cwd([relative_path:string]) + @description: + Returns the current working directory.[br] + If is passed , then it is appended at the end of the directory + to form a complete filepath.[br] + The path is adjusted to contain single separators suitable for the platform + that KVIrc is atually running on (thus you not need to care about path + separators in the , KVIrc will adjust them).[br] +*/ + +static bool file_kvs_fnc_cwd(KviKvsModuleFunctionCall * c) +{ + QString szName; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("relative_path",KVS_PT_STRING,KVS_PF_OPTIONAL,szName) + KVSM_PARAMETERS_END(c) + if(szName.isEmpty())szName.append(KVI_PATH_SEPARATOR_CHAR); + QString szPath = QDir::currentDirPath(); + KviQString::ensureLastCharIs(szPath,KVI_PATH_SEPARATOR_CHAR); + szPath.append(szName); + KviFileUtils::adjustFilePath(szPath); + c->returnValue()->setString(szPath); + return true; +} + + +/* + @doc: file.globaldir + @type: + function + @title: + $file.globaldir + @short: + Get the KVIrc global directory + @syntax: + $file.globaldir() + $file.globaldir + @description: + Returns the path to the KVIrc global data directory.[br] + The KVIrc local data directory is always readable but usually not writeable and contains + the various subdirectories that KVIrc uses internally: audio , avatars , + config , help , incoming , log , modules , msgcolors and pics.[br] + If is passed , then it is appended at the end of the directory + to form a complete filepath.[br] + The path is adjusted to contain single separators suitable for the platform + that KVIrc is atually running on (thus you not need to care about path + separators in the , KVIrc will adjust them).[br] + @examples: + [example] + echo KVIrc looks for pictures in $file.globaldir(pics) + echo panic.png would be translated to $file.globaldir(pics/panic.png) + [/example] +*/ + +static bool file_kvs_fnc_globaldir(KviKvsModuleFunctionCall * c) +{ + QString szName; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("relative_path",KVS_PT_STRING,KVS_PF_OPTIONAL,szName) + KVSM_PARAMETERS_END(c) + if(szName.isEmpty())szName.append(KVI_PATH_SEPARATOR_CHAR); + QString szPath; + g_pApp->getGlobalKvircDirectory(szPath,KviApp::None,szName); + KviFileUtils::adjustFilePath(szPath); + c->returnValue()->setString(szPath); + return true; +} + +/* + @doc: file.extractpath + @type: + function + @title: + $file.extractpath + @short: + Extract the path from a filename + @syntax: + $file.extractpath() + @description: + Returns the path part of the translated to match the current + platform filesystem conventions.[br] + The path will NOT contain a trailing path separator.[br] + For example, if is /usr/arch/mp3/Carisma_SuonoDelSilenzio.mp3 then + this function will return /usr/arch/mp3 on UNIX and C:\usr\arch\mp3 on Windows. + @seealso: + [fnc]$file.extractfilename[/fnc] +*/ + +static bool file_kvs_fnc_extractpath(KviKvsModuleFunctionCall * c) +{ + QString szName; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("filepath",KVS_PT_NONEMPTYSTRING,0,szName) + KVSM_PARAMETERS_END(c) + c->returnValue()->setString(QFileInfo(szName).dirPath(TRUE)); + return true; +} + + +/* + @doc: file.extractfilename + @type: + function + @title: + $file.extractfilename + @short: + Extract the filename from a file path + @syntax: + $file.extractpath() + @description: + Returns the filename part of the filepath translated to match the current + platform filesystem conventions.[br] + For example, if is /usr/arch/mp3/Carisma_SuonoDelSilenzio.mp3 then + this function will return Carisma_SuonoDelSilenzio.mp3 + @seealso: + [fnc]$file.extractpath[/fnc] +*/ + +static bool file_kvs_fnc_extractfilename(KviKvsModuleFunctionCall * c) +{ + QString szName; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("filepath",KVS_PT_NONEMPTYSTRING,0,szName) + KVSM_PARAMETERS_END(c) + KviFileUtils::extractFileName(szName); + KviQString::cutToLast(szName,KVI_PATH_SEPARATOR_CHAR); + c->returnValue()->setString(szName); + return true; +} + + + +static bool file_module_init(KviModule * m) +{ + + KVSM_REGISTER_SIMPLE_COMMAND(m,"copy",file_kvs_cmd_copy); + KVSM_REGISTER_SIMPLE_COMMAND(m,"rename",file_kvs_cmd_rename); + KVSM_REGISTER_SIMPLE_COMMAND(m,"mkdir",file_kvs_cmd_mkdir); + KVSM_REGISTER_SIMPLE_COMMAND(m,"write",file_kvs_cmd_write); + KVSM_REGISTER_SIMPLE_COMMAND(m,"remove",file_kvs_cmd_remove); + KVSM_REGISTER_SIMPLE_COMMAND(m,"rmdir",file_kvs_cmd_rmdir); + KVSM_REGISTER_SIMPLE_COMMAND(m,"addimagepath",file_kvs_cmd_addimagepath); + KVSM_REGISTER_SIMPLE_COMMAND(m,"delimagepath",file_kvs_cmd_delimagepath); + KVSM_REGISTER_SIMPLE_COMMAND(m,"writeLines",file_kvs_cmd_writeLines); + + KVSM_REGISTER_FUNCTION(m,"exists",file_kvs_fnc_exists); + KVSM_REGISTER_FUNCTION(m,"type",file_kvs_fnc_type); + KVSM_REGISTER_FUNCTION(m,"size",file_kvs_fnc_size); + + KVSM_REGISTER_FUNCTION(m,"allsizes",file_kvs_fnc_allSizes); + + KVSM_REGISTER_FUNCTION(m,"fixpath",file_kvs_fnc_fixpath); + KVSM_REGISTER_FUNCTION(m,"ps",file_kvs_fnc_ps); + KVSM_REGISTER_FUNCTION(m,"read",file_kvs_fnc_read); + KVSM_REGISTER_FUNCTION(m,"localdir",file_kvs_fnc_localdir); + KVSM_REGISTER_FUNCTION(m,"homedir",file_kvs_fnc_homedir); + KVSM_REGISTER_FUNCTION(m,"rootdir",file_kvs_fnc_rootdir); + KVSM_REGISTER_FUNCTION(m,"cwd",file_kvs_fnc_cwd); + KVSM_REGISTER_FUNCTION(m,"globaldir",file_kvs_fnc_globaldir); + KVSM_REGISTER_FUNCTION(m,"extractpath",file_kvs_fnc_extractpath); + KVSM_REGISTER_FUNCTION(m,"extractfilename",file_kvs_fnc_extractfilename); + KVSM_REGISTER_FUNCTION(m,"ls",file_kvs_fnc_ls); + KVSM_REGISTER_FUNCTION(m,"readLines",file_kvs_fnc_readLines); + + + return true; +} + +static bool file_module_cleanup(KviModule *m) +{ + return true; +} + +KVIRC_MODULE( + "File", // module name + "1.0.0", // module version + "Copyright (C) 2001 Szymon Stefanek (pragma at kvirc dot net)", // author & (C) + "Interface to the file system", + file_module_init, + 0, + 0, + file_module_cleanup +) diff --git a/src/modules/filetransferwindow/filetransferwindow_vc05.vcproj b/src/modules/filetransferwindow/filetransferwindow_vc05.vcproj index 9064a20c5..07b49df6e 100644 --- a/src/modules/filetransferwindow/filetransferwindow_vc05.vcproj +++ b/src/modules/filetransferwindow/filetransferwindow_vc05.vcproj @@ -129,22 +129,15 @@