| 1 |
|
| 2 |
renv_properties_read <- function(path = NULL, |
| 3 |
text = NULL, |
| 4 |
delimiter = ":", |
| 5 |
dequote = TRUE, |
| 6 |
trim = TRUE) |
| 7 |
{ |
| 8 |
renv_scope_options(warn = -1L) |
| 9 |
|
| 10 |
# read file |
| 11 |
contents <- paste(text %||% readLines(path, warn = FALSE), collapse = "\n") |
| 12 |
|
| 13 |
# split on newlines; allow spaces to continue a value |
| 14 |
parts <- strsplit(contents, "\\n(?=\\S)", perl = TRUE)[[1L]] |
| 15 |
|
| 16 |
# remove comments and blank lines |
| 17 |
parts <- grep("^\\s*(?:#|$)", parts, perl = TRUE, value = TRUE, invert = TRUE) |
| 18 |
|
| 19 |
# split into key / value pairs |
| 20 |
index <- regexpr(delimiter, parts, fixed = TRUE) |
| 21 |
|
| 22 |
# if we couldn't match a delimiter, treat the whole thing as a key |
| 23 |
missed <- index == -1 |
| 24 |
index[missed] <- nchar(parts)[missed] + 1L |
| 25 |
|
| 26 |
# perform the subsetting |
| 27 |
keys <- substring(parts, 1L, index - 1L) |
| 28 |
vals <- substring(parts, index + 1L) |
| 29 |
|
| 30 |
# trim whitespace when requested |
| 31 |
if (trim) { |
| 32 |
keys <- trimws(keys) |
| 33 |
vals <- gsub("\n\\s*", " ", trimws(vals), perl = TRUE) |
| 34 |
} |
| 35 |
|
| 36 |
# strip quotes if requested |
| 37 |
if (dequote) { |
| 38 |
keys <- dequote(keys) |
| 39 |
vals <- dequote(vals) |
| 40 |
} |
| 41 |
|
| 42 |
# return as named list |
| 43 |
storage.mode(vals) <- "list" |
| 44 |
names(vals) <- keys |
| 45 |
|
| 46 |
vals |
| 47 |
|
| 48 |
} |