| 1 |
|
| 2 |
# similar to base::read.dcf(), but: |
| 3 |
# - allows for whitespace between fields |
| 4 |
# - allows for non-indented field continuations |
| 5 |
# - always keeps whitespace |
| 6 |
renv_dcf_read <- function(file, text = NULL, ...) { |
| 7 |
|
| 8 |
# read file |
| 9 |
contents <- text %||% renv_dcf_read_impl(file, ...) |
| 10 |
|
| 11 |
# split on newlines |
| 12 |
parts <- strsplit(contents, "\\r?\\n(?=\\S)", perl = TRUE)[[1L]] |
| 13 |
|
| 14 |
# remove embedded newlines |
| 15 |
parts <- gsub("\\r?\\n\\s*", " ", parts, perl = TRUE) |
| 16 |
|
| 17 |
# split into key / value pairs |
| 18 |
index <- regexpr(":", parts, fixed = TRUE) |
| 19 |
keys <- substring(parts, 1L, index - 1L) |
| 20 |
vals <- substring(parts, index + 1L) |
| 21 |
|
| 22 |
# trim whitespace |
| 23 |
vals <- trimws(vals) |
| 24 |
|
| 25 |
# return early if everything looks fine |
| 26 |
ok <- nzchar(keys) |
| 27 |
if (all(ok)) { |
| 28 |
storage.mode(vals) <- "list" |
| 29 |
names(vals) <- keys |
| 30 |
return(vals) |
| 31 |
} |
| 32 |
|
| 33 |
# otherwise, fix up bad continuations |
| 34 |
starts <- which(ok) |
| 35 |
ends <- c(tail(starts - 1L, n = -1L), length(keys)) |
| 36 |
vals <- .mapply( |
| 37 |
function(start, end) paste(vals[start:end], collapse = " "), |
| 38 |
list(starts, ends), |
| 39 |
NULL |
| 40 |
) |
| 41 |
|
| 42 |
# set up names |
| 43 |
names(vals) <- keys[ok] |
| 44 |
|
| 45 |
# done |
| 46 |
vals |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
renv_dcf_read_impl_encoding <- function(bytes) { |
| 51 |
|
| 52 |
# try to find encoding -- if none is declared, assume native encoding? |
| 53 |
start <- 0L |
| 54 |
while (TRUE) { |
| 55 |
|
| 56 |
# find 'Encoding' |
| 57 |
start <- grepRaw("Encoding:", bytes, fixed = TRUE, offset = start + 1L) |
| 58 |
if (length(start) == 0L) |
| 59 |
return(NULL) |
| 60 |
|
| 61 |
# check for preceding newline, or start of file |
| 62 |
if (start == 1L || bytes[[start - 1L]] == 0x0A) { |
| 63 |
start <- start + 9L |
| 64 |
break |
| 65 |
} |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
# find the end of the encoding field |
| 70 |
end <- grepRaw("\\r?\\n", bytes, offset = start + 1L) |
| 71 |
if (length(end) == 0L) |
| 72 |
end <- length(bytes) |
| 73 |
|
| 74 |
# pull it out |
| 75 |
field <- rawToChar(bytes[start:end]) |
| 76 |
trimws(field) |
| 77 |
|
| 78 |
} |
| 79 |
|
| 80 |
renv_dcf_read_impl <- function(file, ...) { |
| 81 |
|
| 82 |
# suppress warnings in this scope |
| 83 |
renv_scope_options(warn = -1L) |
| 84 |
|
| 85 |
# first, read the file as bytes to get encoding |
| 86 |
# use a guess for the file size to avoid expensive lookup, but fallback |
| 87 |
# if necessary |
| 88 |
bytes <- readBin(file, what = "raw", n = 8192L) |
| 89 |
if (length(bytes) == 8192L) { |
| 90 |
n <- renv_file_size(file) |
| 91 |
bytes <- readBin(con = file, what = "raw", n = n) |
| 92 |
} |
| 93 |
|
| 94 |
# try to guess the encoding |
| 95 |
encoding <- renv_dcf_read_impl_encoding(bytes) |
| 96 |
|
| 97 |
# try a bunch of candidate encodings |
| 98 |
encodings <- c(encoding, "UTF-8", "latin1", "") |
| 99 |
for (encoding in unique(encodings)) { |
| 100 |
result <- iconv(list(bytes), from = encoding, to = "UTF-8") |
| 101 |
if (!is.na(result)) |
| 102 |
return(result) |
| 103 |
} |
| 104 |
|
| 105 |
# all else fails, just pretend it's in the native encoding |
| 106 |
rawToChar(bytes) |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
renv_dcf_write <- function(x, file = "") { |
| 111 |
|
| 112 |
# NOTE: Older versions of write.dcf() will coerce the value into a data.frame |
| 113 |
# without setting 'optional = TRUE'; make sure we do this ourselves first |
| 114 |
value <- as_data_frame(x) |
| 115 |
|
| 116 |
keep.white <- c("Description", "Authors@R", "Author", "Built", "Packaged") |
| 117 |
result <- write.dcf(value, file = file, indent = 4L, width = 80L, keep.white = keep.white) |
| 118 |
|
| 119 |
renv_filebacked_invalidate(file) |
| 120 |
|
| 121 |
invisible(result) |
| 122 |
|
| 123 |
} |