R/reactor.r

Defines functions check.has.pubkey check.is.interactive check.is.flag check.is.posint check.is.natnum check.is.inty check.is.string is.posint is.flag is.string is.annoying is.negative is.zero is.inty is.badval

is.badval = function(x)
{
  is.na(x) || is.nan(x) || is.infinite(x)
}

is.inty = function(x)
{
  abs(x - round(x)) < 1e-10
}

is.zero = function(x)
{
  abs(x) < 1e-10
}

is.negative = function(x)
{
  x < 0
}

is.annoying = function(x)
{
  length(x) != 1 || is.badval(x)
}

is.string = function(x)
{
  is.character(x) && !is.annoying(x)
}

is.flag = function(x)
{
  is.logical(x) && !is.annoying(x)
}

is.posint = function(x)
{
  is.numeric(x) && !is.annoying(x) && is.inty(x) && !is.negative(x) && !is.zero(x)
}




check.is.string = function(x)
{
  if (!is.string(x))  
	{
    nm = deparse(substitute(x))
    stop(paste0("argument '", nm, "' must be a single string"), call.=FALSE)
  }
	
  invisible(TRUE)
}

check.is.inty = function(x)
{
  if (is.annoying(x) && !is.inty(x))
  {
    nm = deparse(substitute(x))
    stop(paste0("argument '", nm, "' must be an integer"), call.=FALSE)
  }
  
  invisible(TRUE)
}

check.is.natnum = function(x)
{
  if (!is.numeric(x) || is.annoying(x) || !is.inty(x) || is.negative(x))  
  {
    nm = deparse(substitute(x))
    stop(paste0("argument '", nm, "' must be a natural number (0 or positive integer)"), call.=FALSE)
  }
  
  invisible(TRUE)
}

check.is.posint = function(x)
{
  if (!is.posint(x))
  {
    nm = deparse(substitute(x))
    stop(paste0("argument '", nm, "' must be a positive integer"), call.=FALSE)
  }
  
  invisible(TRUE)
}

check.is.flag = function(x)
{
  if (!is.flag(x))
  {
    nm = deparse(substitute(x))
    stop(paste0("argument '", nm, "' must be TRUE or FALSE"), call.=FALSE)
  }
  
  invisible(TRUE)
}

check.is.interactive = function()
{
  if (!isTRUE(interactive()))
    stop("this can only be used interactively")
  
  invisible(TRUE)
}

check.has.pubkey = function()
{
  pubkey_test = tryCatch(openssl::my_pubkey(), error=identity)
  if (inherits(pubkey_test, "simpleError"))
    stop("no valid ssh key found")
  
  invisible(TRUE)
}
wrathematics/rotp documentation built on June 29, 2020, 12:56 a.m.