diff options
| author | 2011-04-30 18:54:09 +0000 | |
|---|---|---|
| committer | 2011-04-30 18:54:09 +0000 | |
| commit | cd0fe293f3524576ddc98142c4ba5cfb7e8c6fb0 (patch) | |
| tree | a8a969863040182a75fe138fd7f565e3d210e49d /src/modules/file | |
| parent | Code optimization in classeditor export (diff) | |
| download | KVIrc-cd0fe293f3524576ddc98142c4ba5cfb7e8c6fb0.tar.gz KVIrc-cd0fe293f3524576ddc98142c4ba5cfb7e8c6fb0.tar.bz2 KVIrc-cd0fe293f3524576ddc98142c4ba5cfb7e8c6fb0.zip | |
Added file.diskspace function (working on linux, macos and win)
git-svn-id: https://svn.kvirc.de/svn/trunk/kvirc@5810 17fca916-40b9-46aa-a4ea-0a15b648b75c
Diffstat (limited to 'src/modules/file')
| -rw-r--r-- | src/modules/file/libkvifile.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/modules/file/libkvifile.cpp b/src/modules/file/libkvifile.cpp index 44ce6f5f4..9a84961d1 100644 --- a/src/modules/file/libkvifile.cpp +++ b/src/modules/file/libkvifile.cpp @@ -37,6 +37,13 @@ #include <QTextCodec> #include <QByteArray> +#if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW) +#include <windows.h> +#else +#include <sys/statvfs.h> +#endif + + #if defined(COMPILE_SSL_SUPPORT) && !defined(COMPILE_CRYPTOPP_SUPPORT) // The current implementation #include <openssl/evp.h> @@ -1379,6 +1386,55 @@ static bool file_kvs_fnc_extractfilename(KviKvsModuleFunctionCall * c) } /* + @doc: file.diskspace + @type: + function + @title: + $file.diskspace + @short: + Return the available diskspace. + @syntax: + <hash> $file.diskspace([dirpath:string]) + @description: + Returns as hash, with 'freespace' and 'totalspace' as key, the free diskspace in bytes.[br] + Without optional dirpath parameter will be returned the current device's disk space (i.e. the disk on which KVIrc has been started).[br] +*/ +static bool file_kvs_fnc_diskSpace(KviKvsModuleFunctionCall * c) +{ + QString szPath; + KVSM_PARAMETERS_BEGIN(c) + KVSM_PARAMETER("dir_path",KVS_PT_STRING,KVS_PF_OPTIONAL,szPath) + KVSM_PARAMETERS_END(c) + if (szPath.isEmpty()) szPath="."; + const char *path = szPath.toUtf8().data(); + int fKB = 1024; + int fTotal; + int fFree; + // this for win + #if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW) + ULARGE_INTEGER free,total; + GetDiskFreeSpaceExA( ((LPCTSTR)path , NULL , &total , &free ); + + fFree = static_cast<double>( static_cast<__int64>(free.QuadPart) ) / fKB; + fTotal = static_cast<double>( static_cast<__int64>(total.QuadPart) ) / fKB; + #else + // this one for linux and macos + struct statvfs stFileSystem; + statvfs(path,&stFileSystem); + fFree = stFileSystem.f_bavail * ( stFileSystem.f_bsize / fKB ); + fTotal = stFileSystem.f_blocks * ( stFileSystem.f_bsize / fKB ); + #endif + KviKvsHash * pHash = new KviKvsHash(); + pHash->set("freespace",new KviKvsVariant((kvs_int_t) fFree)); + pHash->set("totalspace",new KviKvsVariant((kvs_int_t) fTotal)); + c->returnValue()->setHash(pHash); + return true; +} + + + + +/* @doc: file.digest @type: function @@ -1539,6 +1595,8 @@ static bool file_module_init(KviModule * m) KVSM_REGISTER_SIMPLE_COMMAND(m,"writeBytes",file_kvs_cmd_writeBytes); KVSM_REGISTER_SIMPLE_COMMAND(m,"writeLines",file_kvs_cmd_writeLines); + KVSM_REGISTER_FUNCTION(m,"diskspace",file_kvs_fnc_diskSpace); + KVSM_REGISTER_FUNCTION(m,"allsizes",file_kvs_fnc_allSizes); KVSM_REGISTER_FUNCTION(m,"cwd",file_kvs_fnc_cwd); KVSM_REGISTER_FUNCTION(m,"digest",file_kvs_fnc_digest); |
