mirror of
https://github.com/tuxbox-neutrino/neutrino.git
synced 2025-08-27 15:32:59 +02:00
CLuaInstCurl::CurlDownload: Rework progress display
- Set Lua api version to 1.31
This commit is contained in:
@@ -83,9 +83,9 @@ size_t CLuaInstCurl::CurlWriteToString(void *ptr, size_t size, size_t nmemb, voi
|
|||||||
return size*nmemb;
|
return size*nmemb;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CLuaInstCurl::CurlProgressFunc(void *p, double dltotal, double dlnow, double /*ultotal*/, double /*ulnow*/)
|
int CLuaInstCurl::CurlProgressFunc(void *p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t /*ultotal*/, curl_off_t /*ulnow*/)
|
||||||
{
|
{
|
||||||
if (dltotal <= 0.0)
|
if (dltotal == 0)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
struct progressData *_pgd = static_cast<struct progressData*>(p);
|
struct progressData *_pgd = static_cast<struct progressData*>(p);
|
||||||
@@ -98,15 +98,16 @@ int CLuaInstCurl::CurlProgressFunc(void *p, double dltotal, double dlnow, double
|
|||||||
curl_easy_getinfo(_pgd->curl, CURLINFO_SPEED_DOWNLOAD, &dlSpeed);
|
curl_easy_getinfo(_pgd->curl, CURLINFO_SPEED_DOWNLOAD, &dlSpeed);
|
||||||
curl_easy_getinfo(_pgd->curl, CURLINFO_RESPONSE_CODE, &responseCode);
|
curl_easy_getinfo(_pgd->curl, CURLINFO_RESPONSE_CODE, &responseCode);
|
||||||
|
|
||||||
double dlFragment = dlnow / dltotal;
|
uint32_t MUL = 0x7FFF;
|
||||||
|
uint32_t dlFragment = (dlnow * MUL) / dltotal;
|
||||||
if (responseCode != 200) {
|
if (responseCode != 200) {
|
||||||
dlFragment = 0;
|
dlFragment = 0;
|
||||||
dlSpeed = 0;
|
dlSpeed = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int showDots = 50;
|
int showDots = 50;
|
||||||
int dots = round(dlFragment * showDots);
|
int dots = (dlFragment * showDots) / MUL;
|
||||||
printf(" %3.0f%% [", dlFragment * 100);
|
printf(" %d%% [", (dlFragment * 100) / MUL);
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (; i < dots-1; i++)
|
for (; i < dots-1; i++)
|
||||||
printf("=");
|
printf("=");
|
||||||
@@ -117,10 +118,16 @@ int CLuaInstCurl::CurlProgressFunc(void *p, double dltotal, double dlnow, double
|
|||||||
for (; i < showDots; i++)
|
for (; i < showDots; i++)
|
||||||
printf(" ");
|
printf(" ");
|
||||||
printf("] speed: %.03f KB/sec \r", dlSpeed/1024); fflush(stdout);
|
printf("] speed: %.03f KB/sec \r", dlSpeed/1024); fflush(stdout);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if LIBCURL_VERSION_NUM < 0x072000
|
||||||
|
int CLuaInstCurl::CurlProgressFunc_old(void *p, double dltotal, double dlnow, double ultotal, double ulnow)
|
||||||
|
{
|
||||||
|
return CurlProgressFunc(p, (curl_off_t)dltotal, (curl_off_t)dlnow, (curl_off_t)ultotal, (curl_off_t)ulnow);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
int CLuaInstCurl::CurlDownload(lua_State *L)
|
int CLuaInstCurl::CurlDownload(lua_State *L)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
@@ -275,10 +282,15 @@ Example:
|
|||||||
progressData pgd;
|
progressData pgd;
|
||||||
|
|
||||||
if (!silent) {
|
if (!silent) {
|
||||||
curl_easy_setopt(curl_handle, CURLOPT_PROGRESSFUNCTION, CLuaInstCurl::CurlProgressFunc);
|
|
||||||
pgd.curl = curl_handle;
|
pgd.curl = curl_handle;
|
||||||
pgd.last_dlnow = -1;
|
pgd.last_dlnow = -1;
|
||||||
|
#if LIBCURL_VERSION_NUM >= 0x072000
|
||||||
|
curl_easy_setopt(curl_handle, CURLOPT_XFERINFOFUNCTION, CLuaInstCurl::CurlProgressFunc);
|
||||||
|
curl_easy_setopt(curl_handle, CURLOPT_XFERINFODATA, &pgd);
|
||||||
|
#else
|
||||||
|
curl_easy_setopt(curl_handle, CURLOPT_PROGRESSFUNCTION, CLuaInstCurl::CurlProgressFunc_old);
|
||||||
curl_easy_setopt(curl_handle, CURLOPT_PROGRESSDATA, &pgd);
|
curl_easy_setopt(curl_handle, CURLOPT_PROGRESSDATA, &pgd);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
char cerror[CURL_ERROR_SIZE];
|
char cerror[CURL_ERROR_SIZE];
|
||||||
|
@@ -29,7 +29,7 @@ class CLuaCurl
|
|||||||
|
|
||||||
struct progressData {
|
struct progressData {
|
||||||
CURL *curl;
|
CURL *curl;
|
||||||
double last_dlnow;
|
curl_off_t last_dlnow;
|
||||||
};
|
};
|
||||||
|
|
||||||
class CLuaInstCurl
|
class CLuaInstCurl
|
||||||
@@ -52,7 +52,10 @@ class CLuaInstCurl
|
|||||||
static CLuaCurl *CurlCheckData(lua_State *L, int n);
|
static CLuaCurl *CurlCheckData(lua_State *L, int n);
|
||||||
static int CurlNew(lua_State *L);
|
static int CurlNew(lua_State *L);
|
||||||
static size_t CurlWriteToString(void *ptr, size_t size, size_t nmemb, void *data);
|
static size_t CurlWriteToString(void *ptr, size_t size, size_t nmemb, void *data);
|
||||||
static int CurlProgressFunc(void *p, double dltotal, double dlnow, double ultotal, double ulnow);
|
#if LIBCURL_VERSION_NUM < 0x072000
|
||||||
|
static int CurlProgressFunc_old(void *p, double dltotal, double dlnow, double ultotal, double ulnow);
|
||||||
|
#endif
|
||||||
|
static int CurlProgressFunc(void *p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
|
||||||
static int CurlDownload(lua_State *L);
|
static int CurlDownload(lua_State *L);
|
||||||
static int CurlDelete(lua_State *L);
|
static int CurlDelete(lua_State *L);
|
||||||
};
|
};
|
||||||
|
@@ -31,7 +31,7 @@ extern "C" {
|
|||||||
#include "luainstance_helpers.h"
|
#include "luainstance_helpers.h"
|
||||||
|
|
||||||
#define LUA_API_VERSION_MAJOR 1
|
#define LUA_API_VERSION_MAJOR 1
|
||||||
#define LUA_API_VERSION_MINOR 30
|
#define LUA_API_VERSION_MINOR 31
|
||||||
|
|
||||||
/* inspired by Steve Kemp http://www.steve.org.uk/ */
|
/* inspired by Steve Kemp http://www.steve.org.uk/ */
|
||||||
class CLuaInstance
|
class CLuaInstance
|
||||||
|
Reference in New Issue
Block a user