Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/lround overflow #91

Merged
merged 5 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 39 additions & 14 deletions inst/include/RProgress.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cerrno>

#include <R.h>
#include <Rinternals.h>
Expand All @@ -39,14 +40,14 @@ class RProgress {

public:

RProgress(std::string format = "[:bar] :percent",
double total = 100,
int width = Rf_GetOptionWidth() - 2,
const char* cursor_char = "=",
const char* complete_char = "=",
const char* incomplete_char = "-",
bool clear = true,
double show_after = 0.2) :
RProgress(std::string format,
double total,
int width,
std::string cursor_char,
std::string complete_char,
std::string incomplete_char,
bool clear,
double show_after) :

first(true), format(format), total(total), current(0), count(0),
width(width), cursor_char(cursor_char), complete_char(complete_char),
Expand All @@ -57,6 +58,24 @@ class RProgress {
use_stderr = default_stderr();
}

RProgress(std::string format = "[:bar] :percent",
double total = 100,
int width = Rf_GetOptionWidth() - 2,
char complete_char = '=',
char incomplete_char = '-',
bool clear = true,
double show_after = 0.2) :

first(true), format(format), total(total), current(0), count(0),
width(width), cursor_char(&complete_char), complete_char(&complete_char),
incomplete_char(&incomplete_char), clear(clear), show_after(show_after),
last_draw(""), start(0), toupdate(false), complete(false) {

supported = is_supported();
use_stderr = default_stderr();
}


~RProgress() { }

void set_format(std::string format) { this->format = format; }
Expand Down Expand Up @@ -111,9 +130,9 @@ class RProgress {
int count; // Total number of calls
int width; // Width of progress bar
bool use_stderr; // Whether to print to stderr
const char* cursor_char; // Character for cursor tick
const char* complete_char; // Character for completed ticks
const char* incomplete_char; // Character for incomplete ticks
std::string cursor_char; // Character for cursor tick
std::string complete_char; // Character for completed ticks
std::string incomplete_char; // Character for incomplete ticks
bool clear; // Should we clear the line at the end?
double show_after; // Delay to show/increase the progress bar
std::string last_draw; // Last progress bar drawn
Expand Down Expand Up @@ -154,7 +173,7 @@ class RProgress {
buffer << "?";
} else {
double rate_num = elapsed_secs == 0 ? 0 : current / elapsed_secs;
buffer << pretty_bytes(lround(rate_num)) << "/s";
buffer << pretty_bytes(rate_num) << "/s";
}
replace_all(str, ":rate", buffer.str());
buffer.str(""); buffer.clear();
Expand All @@ -170,7 +189,7 @@ class RProgress {
buffer.str(""); buffer.clear();

// bytes
replace_all(str, ":bytes", pretty_bytes(lround(current)));
replace_all(str, ":bytes", pretty_bytes(current));

// spin
replace_all(str, ":spin", spin_symbol());
Expand Down Expand Up @@ -373,7 +392,13 @@ class RProgress {
return buffer.str();
}

static std::string pretty_bytes(long bytes) {
static std::string pretty_bytes(double rate) {

errno = 0;
long bytes = lround(rate);
if (errno == ERANGE) {
bytes = LONG_MAX;
}

if (bytes == 0) { return "0B"; }

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-cpp.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test_that("C++ API works", {

dir.create(lib <- tempfile())
on.exit(unlink(lib, recursive = TRUE), add = TRUE)
install.packages("progresstest_1.0.0.tar.gz", lib = lib, quiet = TRUE)
install.packages("progresstest_1.0.0.tar.gz", lib = lib, quiet = FALSE)

on.exit(unloadNamespace("progresstest"), add = TRUE)
withr::with_libpaths(lib, action = "prefix", {
Expand Down