From 17c46bdd5555180ef6918d07ae0dd330d3f8f854 Mon Sep 17 00:00:00 2001 From: Arni Magnusson Date: Thu, 24 Oct 2024 03:27:04 +1100 Subject: [PATCH] Add argument 'force' to dos2unix() and unix2dos() --- NEWS.md | 5 +++-- R/dos2unix.R | 21 +++++++++++++++++++-- R/unix2dos.R | 13 +++++++++++-- man/dos2unix.Rd | 13 +++++++++++-- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/NEWS.md b/NEWS.md index 2b82fe6..0decf4e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -8,10 +8,11 @@ * Added function taf.libraries() to load all packages from TAF library. Code contributed by Iago Mosqueira. +* Added argument 'force' to dos2unix() and unix2dos(). + * Added argument 'quiet' to make(). Renamed argument 'debug' to 'details'. -* Added argument 'gitignore' to taf.skeleton() for writing a .gitignore file, - enabled by default. +* Added argument 'gitignore' to taf.skeleton(). * Improved draft.software() so it supports packages from r-universe. Code contributed by Iago Mosqueira. diff --git a/R/dos2unix.R b/R/dos2unix.R index f5304b0..799ff62 100644 --- a/R/dos2unix.R +++ b/R/dos2unix.R @@ -3,6 +3,14 @@ #' Convert line endings in a text file between Dos (CRLF) and Unix (LF) format. #' #' @param file a filename. +#' @param force whether to proceed with the conversion when the file is not a +#' standard text file. +#' +#' @details +#' The default value of \code{force = FALSE} is a safety feature that can avoid +#' corrupting files that are not standard text files, such as binary files. A +#' standard text file is one that can be read using \code{\link{readLines}} +#' without producing warnings. #' #' @return No return value, called for side effects. #' @@ -27,9 +35,18 @@ #' #' @export -dos2unix <- function(file) +dos2unix <- function(file, force=FALSE) { - txt <- readLines(file) + if(!force) + { + owarn <- options(warn=2) # treat warnings from readLines() as errors + on.exit(options(owarn)) + } + + txt <- try(readLines(file), silent=TRUE) + if(inherits(txt, "try-error")) + stop("file is not a standard text file") + con <- file(file, open="wb") writeLines(txt, con, sep="\n") close(con) diff --git a/R/unix2dos.R b/R/unix2dos.R index 4a3c28e..1fadc3d 100644 --- a/R/unix2dos.R +++ b/R/unix2dos.R @@ -2,9 +2,18 @@ #' #' @export -unix2dos <- function(file) +unix2dos <- function(file, force=FALSE) { - txt <- readLines(file) + if(!force) + { + owarn <- options(warn=2) # treat warnings from readLines() as errors + on.exit(options(owarn)) + } + + txt <- try(readLines(file), silent=TRUE) + if(inherits(txt, "try-error")) + stop("file is not a standard text file") + con <- file(file, open="wb") writeLines(txt, con, sep="\r\n") close(con) diff --git a/man/dos2unix.Rd b/man/dos2unix.Rd index 4b05b71..8551bc5 100644 --- a/man/dos2unix.Rd +++ b/man/dos2unix.Rd @@ -5,12 +5,15 @@ \alias{unix2dos} \title{Convert Line Endings} \usage{ -dos2unix(file) +dos2unix(file, force = FALSE) -unix2dos(file) +unix2dos(file, force = FALSE) } \arguments{ \item{file}{a filename.} + +\item{force}{whether to proceed with the conversion when the file is not a +standard text file.} } \value{ No return value, called for side effects. @@ -18,6 +21,12 @@ No return value, called for side effects. \description{ Convert line endings in a text file between Dos (CRLF) and Unix (LF) format. } +\details{ +The default value of \code{force = FALSE} is a safety feature that can avoid +corrupting files that are not standard text files, such as binary files. A +standard text file is one that can be read using \code{\link{readLines}} +without producing warnings. +} \examples{ \dontrun{ file <- "test.txt"