Skip to content

Commit

Permalink
Add argument 'force' to dos2unix() and unix2dos()
Browse files Browse the repository at this point in the history
  • Loading branch information
arni-magnusson committed Oct 23, 2024
1 parent c4520de commit 17c46bd
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
5 changes: 3 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
21 changes: 19 additions & 2 deletions R/dos2unix.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#'
Expand All @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions R/unix2dos.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions man/dos2unix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 17c46bd

Please sign in to comment.