Skip to content

Commit 26d46fc

Browse files
committed
Add option to select between SI and IEC units
1 parent eeb6820 commit 26d46fc

7 files changed

Lines changed: 115 additions & 92 deletions

File tree

include/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ class Config
325325
int iProgressInterval;
326326
int iMsgLevel;
327327
unsigned int iListFormat;
328+
unsigned int iUnitFormat;
328329

329330
Json::Value transformationsJSON;
330331
};

include/globalconstants.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ namespace GlobalConstants
1515
const int GAMEDETAILS_CACHE_VERSION = 7;
1616
const int ZLIB_WINDOW_SIZE = 15;
1717

18+
// Unit formatting
19+
const unsigned int UNIT_FORMAT_IEC = 1;
20+
const unsigned int UNIT_FORMAT_SI = 2;
21+
const int UNIT_DIVISOR_K_IEC = 1024;
22+
const int UNIT_DIVISOR_M_IEC = 1048576;
23+
const int UNIT_DIVISOR_K_SI = 1000;
24+
const int UNIT_DIVISOR_M_SI = 1000000;
25+
const std::string UNIT_STRING_K_IEC = "KiB";
26+
const std::string UNIT_STRING_M_IEC = "MiB";
27+
const std::string UNIT_STRING_K_SI = "kB";
28+
const std::string UNIT_STRING_M_SI = "MB";
29+
1830
struct optionsStruct {const unsigned int id; const std::string code; const std::string str; const std::string regexp;};
1931
const std::string PROTOCOL_PREFIX = "gogdownloader://";
2032

include/util.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ namespace Util
9191
curl_off_t CurlWriteMemoryCallback(char *ptr, curl_off_t size, curl_off_t nmemb, void *userp);
9292
curl_off_t CurlWriteChunkMemoryCallback(void *contents, curl_off_t size, curl_off_t nmemb, void *userp);
9393
curl_off_t CurlReadChunkMemoryCallback(void *contents, curl_off_t size, curl_off_t nmemb, ChunkMemoryStruct *userp);
94-
std::string makeSizeString(const unsigned long long& iSizeInBytes);
94+
std::string makeSizeString(const unsigned long long& iSizeInBytes, const unsigned int& unit_format = GlobalConstants::UNIT_FORMAT_IEC);
95+
std::string makeRateString(double rate, const unsigned int& unit_format = GlobalConstants::UNIT_FORMAT_IEC);
9596

9697
template<typename ... Args> std::string formattedString(const std::string& format, Args ... args)
9798
{

main.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ int main(int argc, char *argv[])
211211
std::string sGalaxyArch;
212212
std::string sGalaxyCDN;
213213
std::string sListFormat;
214+
std::string sUnitFormat;
214215
Globals::globalConfig.bReport = false;
215216
// Commandline options (no config file)
216217
options_cli_no_cfg.add_options()
@@ -304,6 +305,7 @@ int main(int argc, char *argv[])
304305
("no-fast-status-check", bpo::value<bool>(&bNoFastStatusCheck)->zero_tokens()->default_value(false), "Don't use fast status check.\nMakes --status much slower but able to catch corrupted files by calculating local file hash for all files.")
305306
("trust-api-for-extras", bpo::value<bool>(&Globals::globalConfig.bTrustAPIForExtras)->zero_tokens()->default_value(false), "Trust API responses for extras to be correct.")
306307
("interface", bpo::value<std::string>(&Globals::globalConfig.curlConf.sInterface)->default_value(""), "Perform operations using a specified network interface")
308+
("unit-format", bpo::value<std::string>(&sUnitFormat)->default_value("IEC"), "Select unit format to use: IEC or SI")
307309
;
308310

309311
options_cli_no_cfg_hidden.add_options()
@@ -586,6 +588,15 @@ int main(int argc, char *argv[])
586588
Globals::globalConfig.dlConf.iInclude = include_value & ~exclude_value;
587589

588590
Globals::globalConfig.iListFormat = Util::getOptionValue(sListFormat, GlobalConstants::LIST_FORMAT, false);
591+
592+
if (sUnitFormat == "SI" || sUnitFormat == "si")
593+
{
594+
Globals::globalConfig.iUnitFormat = GlobalConstants::UNIT_FORMAT_SI;
595+
}
596+
else
597+
{
598+
Globals::globalConfig.iUnitFormat = GlobalConstants::UNIT_FORMAT_IEC;
599+
}
589600
}
590601
catch (std::exception& e)
591602
{

man/lgogdownloader.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,9 @@ Trust API responses for extras to be correct.
457457
.TP
458458
\fB\-\-interface\fR arg
459459
Perform operations using a specified network interface
460+
.TP
461+
\fB\-\-unit\-format\fR arg
462+
Select unit format to use: IEC or SI
460463
.SS "Experimental:"
461464
.TP
462465
\fB\-\-galaxy\-install\fR arg

src/downloader.cpp

Lines changed: 40 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ void Downloader::download()
784784
if (!dlQueue.empty())
785785
{
786786
unsigned long long totalSizeBytes = iTotalRemainingBytes.load();
787-
std::cout << "Total size: " << Util::makeSizeString(totalSizeBytes) << std::endl;
787+
std::cout << "Total size: " << Util::makeSizeString(totalSizeBytes, Globals::globalConfig.iUnitFormat) << std::endl;
788788

789789
if (Globals::globalConfig.dlConf.bFreeSpaceCheck)
790790
{
@@ -801,7 +801,7 @@ void Downloader::download()
801801
if (space.available < totalSizeBytes)
802802
{
803803
std::cerr << "Not enough free space in " << boost::filesystem::canonical(path) << " ("
804-
<< Util::makeSizeString(space.available) << ")"<< std::endl;
804+
<< Util::makeSizeString(space.available, Globals::globalConfig.iUnitFormat) << ")"<< std::endl;
805805
exit(1);
806806
}
807807
}
@@ -1561,18 +1561,23 @@ int Downloader::progressCallback(void *clientp, curl_off_t dltotal, curl_off_t d
15611561
std::cout << Util::formattedString("\033[0K\r%3.0f%% ", fraction * 100);
15621562

15631563
// Download rate unit conversion
1564-
std::string rate_unit;
1565-
if (rate > 1048576) // 1 MB
1564+
std::string unit;
1565+
int divisor_M;
1566+
1567+
if (Globals::globalConfig.iUnitFormat == GlobalConstants::UNIT_FORMAT_IEC)
15661568
{
1567-
rate /= 1048576;
1568-
rate_unit = "MB/s";
1569+
divisor_M = GlobalConstants::UNIT_DIVISOR_M_IEC;
1570+
unit = GlobalConstants::UNIT_STRING_M_IEC;
15691571
}
15701572
else
15711573
{
1572-
rate /= 1024;
1573-
rate_unit = "kB/s";
1574+
divisor_M = GlobalConstants::UNIT_DIVISOR_M_SI;
1575+
unit = GlobalConstants::UNIT_STRING_M_SI;
15741576
}
1575-
std::string status_text = Util::formattedString(" %0.2f/%0.2fMB @ %0.2f%s ETA: %s\r", static_cast<double>(dlnow)/1024/1024, static_cast<double>(dltotal)/1024/1024, rate, rate_unit.c_str(), etastring.c_str());
1577+
1578+
std::string rate_string = Util::makeRateString(rate, Globals::globalConfig.iUnitFormat);
1579+
1580+
std::string status_text = Util::formattedString(" %0.2f/%0.2f%s @ %s ETA: %s\r", static_cast<double>(dlnow)/divisor_M, static_cast<double>(dltotal)/divisor_M, unit.c_str(), rate_string.c_str(), etastring.c_str());
15761581
int status_text_length = status_text.length() + 6;
15771582

15781583
if ((status_text_length + bar_length) > iTermWidth)
@@ -2898,22 +2903,10 @@ void Downloader::processCloudSaveDownloadQueue(Config conf, const unsigned int&
28982903
}
28992904

29002905
// Average download speed
2901-
std::ostringstream dlrate_avg;
2902-
std::string rate_unit;
29032906
progressInfo progress_info = vDownloadInfo[tid].getProgressInfo();
2904-
if (progress_info.rate_avg > 1048576) // 1 MB
2905-
{
2906-
progress_info.rate_avg /= 1048576;
2907-
rate_unit = "MB/s";
2908-
}
2909-
else
2910-
{
2911-
progress_info.rate_avg /= 1024;
2912-
rate_unit = "kB/s";
2913-
}
2914-
dlrate_avg << std::setprecision(2) << std::fixed << progress_info.rate_avg << rate_unit;
2907+
std::string rate_string = Util::makeRateString(progress_info.rate_avg, Globals::globalConfig.iUnitFormat);
29152908

2916-
msgQueue.push(Message("Download complete: " + csf.path + " (@ " + dlrate_avg.str() + ")", MSGTYPE_SUCCESS, msg_prefix, MSGLEVEL_DEFAULT));
2909+
msgQueue.push(Message("Download complete: " + csf.path + " (@ " + rate_string + ")", MSGTYPE_SUCCESS, msg_prefix, MSGLEVEL_DEFAULT));
29172910
}
29182911
else
29192912
{
@@ -3408,22 +3401,10 @@ void Downloader::processDownloadQueue(Config conf, const unsigned int& tid)
34083401
}
34093402

34103403
// Average download speed
3411-
std::ostringstream dlrate_avg;
3412-
std::string rate_unit;
34133404
progressInfo progress_info = vDownloadInfo[tid].getProgressInfo();
3414-
if (progress_info.rate_avg > 1048576) // 1 MB
3415-
{
3416-
progress_info.rate_avg /= 1048576;
3417-
rate_unit = "MB/s";
3418-
}
3419-
else
3420-
{
3421-
progress_info.rate_avg /= 1024;
3422-
rate_unit = "kB/s";
3423-
}
3424-
dlrate_avg << std::setprecision(2) << std::fixed << progress_info.rate_avg << rate_unit;
3405+
std::string rate_string = Util::makeRateString(progress_info.rate_avg, Globals::globalConfig.iUnitFormat);
34253406

3426-
msgQueue.push(Message("Download complete: " + filepath.filename().string() + " (@ " + dlrate_avg.str() + ")", MSGTYPE_SUCCESS, msg_prefix, MSGLEVEL_DEFAULT));
3407+
msgQueue.push(Message("Download complete: " + filepath.filename().string() + " (@ " + rate_string + ")", MSGTYPE_SUCCESS, msg_prefix, MSGLEVEL_DEFAULT));
34273408
}
34283409
else
34293410
{
@@ -3527,6 +3508,14 @@ int Downloader::progressCallbackForThread(void *clientp, curl_off_t dltotal, cur
35273508

35283509
template <typename T> void Downloader::printProgress(const ThreadSafeQueue<T>& download_queue)
35293510
{
3511+
int divisor_M = GlobalConstants::UNIT_DIVISOR_M_IEC;
3512+
std::string unit_M = GlobalConstants::UNIT_STRING_M_IEC;
3513+
if (Globals::globalConfig.iUnitFormat == GlobalConstants::UNIT_FORMAT_SI)
3514+
{
3515+
divisor_M = GlobalConstants::UNIT_DIVISOR_M_SI;
3516+
unit_M = GlobalConstants::UNIT_STRING_M_SI;
3517+
}
3518+
35303519
// Print progress information until all threads have finished their tasks
35313520
ProgressBar bar(Globals::globalConfig.bUnicode, Globals::globalConfig.bColor);
35323521
unsigned int dl_status = DLSTATUS_NOTSTARTED;
@@ -3586,19 +3575,10 @@ template <typename T> void Downloader::printProgress(const ThreadSafeQueue<T>& d
35863575
eta_total_seconds += eta;
35873576
std::string etastring = Util::makeEtaString(eta);
35883577

3589-
std::string rate_unit;
3590-
if (progress_info.rate > 1048576) // 1 MB
3591-
{
3592-
progress_info.rate /= 1048576;
3593-
rate_unit = "MB/s";
3594-
}
3595-
else
3596-
{
3597-
progress_info.rate /= 1024;
3598-
rate_unit = "kB/s";
3599-
}
3578+
std::string unit = unit_M;
3579+
std::string rate_string = Util::makeRateString(progress_info.rate_avg, Globals::globalConfig.iUnitFormat);
36003580

3601-
std::string progress_status_text = Util::formattedString(" %0.2f/%0.2fMB @ %0.2f%s ETA: %s", static_cast<double>(progress_info.dlnow)/1024/1024, static_cast<double>(progress_info.dltotal)/1024/1024, progress_info.rate, rate_unit.c_str(), etastring.c_str());
3581+
std::string progress_status_text = Util::formattedString(" %0.2f/%0.2f%s @ %s ETA: %s", static_cast<double>(progress_info.dlnow)/divisor_M, static_cast<double>(progress_info.dltotal)/divisor_M, unit.c_str(), rate_string.c_str(), etastring.c_str());
36023582
int status_text_length = progress_status_text.length() + 1;
36033583

36043584
if ((status_text_length + progress_percentage_text_length + bar_length) > iTermWidth)
@@ -3627,41 +3607,16 @@ template <typename T> void Downloader::printProgress(const ThreadSafeQueue<T>& d
36273607
bptime::time_duration eta(bptime::seconds((long)(total_remaining / total_rate)));
36283608
eta += eta_total_seconds;
36293609
std::string eta_str = Util::makeEtaString(eta);
3610+
std::string total_remaining_string = Util::makeSizeString(total_remaining, Globals::globalConfig.iUnitFormat);
36303611

3631-
double total_remaining_double = static_cast<double>(total_remaining)/1048576;
3632-
std::string total_remaining_unit = "MB";
3633-
std::vector<std::string> units = { "GB", "TB", "PB" };
3634-
3635-
if (total_remaining_double > 1024)
3636-
{
3637-
for (const auto& unit : units)
3638-
{
3639-
total_remaining_double /= 1024;
3640-
total_remaining_unit = unit;
3641-
3642-
if (total_remaining_double < 1024)
3643-
break;
3644-
}
3645-
}
3646-
3647-
total_eta_str = Util::formattedString(" (%0.2f%s) ETA: %s", total_remaining_double, total_remaining_unit.c_str(), eta_str.c_str());
3612+
total_eta_str = Util::formattedString(" (%s) ETA: %s", total_remaining_string.c_str(), eta_str.c_str());
36483613
}
36493614

36503615
std::ostringstream ss;
36513616
if (Globals::globalConfig.iThreads > 1)
36523617
{
3653-
std::string rate_unit;
3654-
if (total_rate > 1048576) // 1 MB
3655-
{
3656-
total_rate /= 1048576;
3657-
rate_unit = "MB/s";
3658-
}
3659-
else
3660-
{
3661-
total_rate /= 1024;
3662-
rate_unit = "kB/s";
3663-
}
3664-
ss << "Total: " << std::setprecision(2) << std::fixed << total_rate << rate_unit << " | ";
3618+
std::string total_rate_string = Util::makeRateString(total_rate, Globals::globalConfig.iUnitFormat);
3619+
ss << "Total: " << total_rate_string << " | ";
36653620
}
36663621
ss << "Remaining: " << download_queue.size();
36673622

@@ -4256,7 +4211,7 @@ void Downloader::galaxyInstallGameById(const std::string& product_id, const std:
42564211

42574212
std::cout << game_title << std::endl;
42584213
std::cout << "Files: " << items.size() << std::endl;
4259-
std::cout << "Total size installed: " << Util::makeSizeString(totalSize) << std::endl;
4214+
std::cout << "Total size installed: " << Util::makeSizeString(totalSize, Globals::globalConfig.iUnitFormat) << std::endl;
42604215

42614216
if (Globals::globalConfig.dlConf.bFreeSpaceCheck)
42624217
{
@@ -4273,7 +4228,7 @@ void Downloader::galaxyInstallGameById(const std::string& product_id, const std:
42734228
if (space.available < totalSize)
42744229
{
42754230
std::cerr << "Not enough free space in " << boost::filesystem::canonical(path) << " ("
4276-
<< Util::makeSizeString(space.available) << ")"<< std::endl;
4231+
<< Util::makeSizeString(space.available, Globals::globalConfig.iUnitFormat) << ")"<< std::endl;
42774232
exit(1);
42784233
}
42794234
}
@@ -5815,7 +5770,7 @@ void Downloader::galaxyInstallGame_MojoSetupHack(const std::string& product_id)
58155770

58165771
std::cout << game.title << std::endl;
58175772
std::cout << "Files: " << dlQueueGalaxy_MojoSetupHack.size() << std::endl;
5818-
std::cout << "Total size installed: " << Util::makeSizeString(totalSize) << std::endl;
5773+
std::cout << "Total size installed: " << Util::makeSizeString(totalSize, Globals::globalConfig.iUnitFormat) << std::endl;
58195774

58205775
if (Globals::globalConfig.dlConf.bFreeSpaceCheck)
58215776
{
@@ -5832,7 +5787,7 @@ void Downloader::galaxyInstallGame_MojoSetupHack(const std::string& product_id)
58325787
if (space.available < totalSize)
58335788
{
58345789
std::cerr << "Not enough free space in " << boost::filesystem::canonical(path) << " ("
5835-
<< Util::makeSizeString(space.available) << ")"<< std::endl;
5790+
<< Util::makeSizeString(space.available, Globals::globalConfig.iUnitFormat) << ")"<< std::endl;
58365791
exit(1);
58375792
}
58385793
}
@@ -6226,7 +6181,7 @@ void Downloader::processGalaxyDownloadQueue_MojoSetupHack(Config conf, const uns
62266181
// Download file
62276182
CURLcode result = CURLE_RECV_ERROR;
62286183

6229-
off_t max_size_memory = 5 << 20; // 5MB
6184+
off_t max_size_memory = 5 << 20; // 5MiB
62306185
if (zfe.comp_size < max_size_memory) // Handle small files in memory
62316186
{
62326187
std::ofstream ofs(path.string(), std::ofstream::out | std::ofstream::binary);
@@ -6549,8 +6504,8 @@ int Downloader::mojoSetupGetFileVector(const gameFile& gf, std::vector<zipFileEn
65496504
return 1;
65506505
}
65516506

6552-
off_t head_size = 100 << 10; // 100 kB
6553-
off_t tail_size = 200 << 10; // 200 kB
6507+
off_t head_size = 100 << 10; // 100 KiB
6508+
off_t tail_size = 200 << 10; // 200 KiB
65546509
std::string head_range = "0-" + std::to_string(head_size);
65556510
std::string tail_range = std::to_string(file_size - tail_size) + "-" + std::to_string(file_size);
65566511

0 commit comments

Comments
 (0)