ini/0000755000176200001440000000000013300165357011034 5ustar liggesusersini/tests/0000755000176200001440000000000013163742440012177 5ustar liggesusersini/tests/testthat.R0000644000176200001440000000006613163742440014164 0ustar liggesuserslibrary(testthat) library(ini) test_check("ini") ini/tests/testthat/0000755000176200001440000000000013300165357014036 5ustar liggesusersini/tests/testthat/test-write.ini.R0000644000176200001440000000051513163761263017054 0ustar liggesuserslibrary(ini) context("write ini") test_that("write.ini write ini to file", { iniFile <- tempfile(fileext = '.ini') on.exit(file.remove(iniFile)) ini <- list('Hello World'=c(list('Foo'='Bar'), list('Foo1'='Bar=345'))) write.ini(ini, iniFile) expect_equal(readLines(iniFile), readLines("writeini.txt")) }) ini/tests/testthat/test-read.ini.R0000644000176200001440000000033613163760524016635 0ustar liggesuserslibrary(ini) context("read.ini") test_that("read.ini parse ini files", { expect_equal(read.ini("writeini.txt"), list('Hello World'=c(list('Foo'='Bar'), list('Foo1'='Bar=345')))) expect_warning(read.ini("")) }) ini/tests/testthat/writeini.txt0000644000176200001440000000005013300013561016411 0ustar liggesusers[Hello World] Foo=Bar Foo1=Bar=345 ini/NAMESPACE0000644000176200001440000000012513163736434012260 0ustar liggesusers# Generated by roxygen2: do not edit by hand export(read.ini) export(write.ini) ini/NEWS.md0000644000176200001440000000010013300012613012103 0ustar liggesusers# ini 0.3.1 * Patch to fix unnecessary space between "=" (#2)ini/R/0000755000176200001440000000000013163736434011244 5ustar liggesusersini/R/ini.R0000644000176200001440000000660113300010455012126 0ustar liggesusers#' Read and parse .ini file to list #' #' @param filepath file to parse #' @param encoding Encoding of filepath parameter, will default to system #' encoding if not specifield #' #' @details Lines starting with '#' or ';' are comments and will not be parsed #' #' @seealso \code{\link{write.ini}} #' #' @return List with length equivalent to number of [sections], each section is #' a new list #' #' @examples #' ## Create a new temp ini for reading #' iniFile <- tempfile(fileext = '.ini') #' #' sink(iniFile) #' cat("; This line is a comment\n") #' cat("# This one too!\n") #' cat("[ Hello World]\n") #' cat("Foo = Bar \n") #' cat("Foo1 = Bar=345 \n") #' sink() #' #' ## Read ini #' checkini <- read.ini(iniFile) #' #' ## Check structure #' checkini #' checkini$`Hello World`$Foo #' #' @export #' read.ini <- function(filepath, encoding = getOption("encoding")) { index <- function(x, rune) { equalPosition = numeric(1) for(pos in 1:nchar(x)) { if (strsplit(x, '')[[1]][pos] == rune) { equalPosition = pos break } } return(equalPosition) } # internal helper function to find where a character occur sectionREGEXP <- '^\\s*\\[\\s*(.+?)\\s*]' # match section and capture section name keyValueREGEXP <- '^\\s*[^=]+=.+' # match "key = value" pattern ignoreREGEXP <- '^\\s*[;#]' # match lines with ; or # at start trim <- function(x) sub('^\\s*(.*?)\\s*$', '\\1', x) # amazing lack of trim at old versions of R ini <- list() con <- file(filepath, open = 'r', encoding = encoding) on.exit(close(con)) while ( TRUE ) { line <- readLines(con, n = 1, encoding = encoding, warn = F) if ( length(line) == 0 ) { break } if ( grepl(ignoreREGEXP, line) ) { next } if ( grepl(sectionREGEXP, line) ) { matches <- regexec(sectionREGEXP, line) lastSection <- regmatches(line, matches)[[1]][2] } if ( grepl(keyValueREGEXP, line) ) { key <- trim(paste0(strsplit(line, '')[[1]][1:(index(line, '=') - 1)], collapse = '')) value <- trim(paste0(strsplit(line, '')[[1]][(index(line, '=') + 1):nchar(line)], collapse = '')) ini[[ lastSection ]] <- c(ini[[ lastSection ]], list(key = value)) names(ini[[ lastSection ]])[ match('key', names(ini[[ lastSection ]])) ] <- key } } ini } #' Write list to .ini file #' #' @param x List with structure to be write at .ini file. #' #' @param filepath file to write #' @param encoding Encoding of filepath parameter, will default to system #' encoding if not specifield #' #' @seealso \code{\link{read.ini}} #' #' @examples #' ## Create a new temp ini for writing #' iniFile <- tempfile(fileext = '.ini') #' #' ## Create a new list holding our INI #' newini <- list() #' newini[[ "Hello World" ]] <- list(Foo = 'Bar') #' #' ## Write structure to file #' write.ini(newini, iniFile) #' #' ## Check file content #' \dontrun{ #' file.show(iniFile) #' } #' #' @export #' write.ini <- function(x, filepath, encoding = getOption("encoding")) { con <- file(filepath, open = 'w', encoding = encoding) on.exit(close(con)) for(section in names(x) ) { writeLines( paste0('[', section, ']'), con) for (key in x[ section ]) { writeLines( paste0(names(key), '=', key), con) } writeLines("", con) } } ini/README.md0000644000176200001440000000143213300130020012266 0ustar liggesusers# ini [![Travis-CI Build Status](https://travis-ci.org/dvdscripter/ini.svg?branch=master)](https://travis-ci.org/dvdscripter/ini) [![CRAN version](http://www.r-pkg.org/badges/version/ini)](https://cran.r-project.org/package=ini) [![CRAN downloads](http://cranlogs.r-pkg.org/badges/ini)](https://cran.r-project.org/package=ini) Parse simple '.ini' configuration files to an structured list. Users can manipulate this resulting list with lapply() functions. This same structured list can be used to write back to file after modifications. ## Installing Development version ``` library(devtools) install_github("dvdscripter/ini") ``` Prodution version ``` install.packages('ini') ``` ## Acknowledgments * SiBBr: first PoC was made to support another product ini/MD50000644000176200001440000000106313300165357011344 0ustar liggesusersd1c726df8949c0668ecb3db131333022 *DESCRIPTION f240ce12ce812afd9dde6a5cd3ec1a96 *NAMESPACE a4b92568012ab97d6123a7ddad92be9e *NEWS.md e13dd722e2d6856b3ed8cf5f0db2e987 *R/ini.R e9e521fe49b761d687a8c9bb82213f71 *README.md 4017b103a98cce01d74e4b66e9affdb6 *man/read.ini.Rd 5b9418810cbe9097410d5462acfd46c3 *man/write.ini.Rd ca69ee6f328efa26259a3178258b0aeb *tests/testthat.R 22ca2493cd69b1c4a6786fbf01f0741f *tests/testthat/test-read.ini.R 04da3e1c51987eecadea988f89fc4af4 *tests/testthat/test-write.ini.R 4f773df3c0df0cac8f447c7c24953dc8 *tests/testthat/writeini.txt ini/DESCRIPTION0000644000176200001440000000126313300165357012544 0ustar liggesusersPackage: ini Type: Package Title: Read and Write '.ini' Files Version: 0.3.1 Date: 2018-05-19 Author: David Valentim Dias Maintainer: David Valentim Dias Description: Parse simple '.ini' configuration files to an structured list. Users can manipulate this resulting list with lapply() functions. This same structured list can be used to write back to file after modifications. License: GPL-3 URL: https://github.com/dvdscripter/ini BugReports: https://github.com/dvdscripter/ini/issues LazyData: FALSE RoxygenNote: 6.0.1 Suggests: testthat NeedsCompilation: no Packaged: 2018-05-19 23:19:45 UTC; CLIENTE Repository: CRAN Date/Publication: 2018-05-20 03:26:39 UTC ini/man/0000755000176200001440000000000013163736434011616 5ustar liggesusersini/man/read.ini.Rd0000644000176200001440000000177613163766310013605 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/ini.R \name{read.ini} \alias{read.ini} \title{Read and parse .ini file to list} \usage{ read.ini(filepath, encoding = getOption("encoding")) } \arguments{ \item{filepath}{file to parse} \item{encoding}{Encoding of filepath parameter, will default to system encoding if not specifield} } \value{ List with length equivalent to number of [sections], each section is a new list } \description{ Read and parse .ini file to list } \details{ Lines starting with '#' or ';' are comments and will not be parsed } \examples{ ## Create a new temp ini for reading iniFile <- tempfile(fileext = '.ini') sink(iniFile) cat("; This line is a comment\\n") cat("# This one too!\\n") cat("[ Hello World]\\n") cat("Foo = Bar \\n") cat("Foo1 = Bar=345 \\n") sink() ## Read ini checkini <- read.ini(iniFile) ## Check structure checkini checkini$`Hello World`$Foo } \seealso{ \code{\link{write.ini}} } ini/man/write.ini.Rd0000644000176200001440000000150213163766310014007 0ustar liggesusers% Generated by roxygen2: do not edit by hand % Please edit documentation in R/ini.R \name{write.ini} \alias{write.ini} \title{Write list to .ini file} \usage{ write.ini(x, filepath, encoding = getOption("encoding")) } \arguments{ \item{x}{List with structure to be write at .ini file.} \item{filepath}{file to write} \item{encoding}{Encoding of filepath parameter, will default to system encoding if not specifield} } \description{ Write list to .ini file } \examples{ ## Create a new temp ini for writing iniFile <- tempfile(fileext = '.ini') ## Create a new list holding our INI newini <- list() newini[[ "Hello World" ]] <- list(Foo = 'Bar') ## Write structure to file write.ini(newini, iniFile) ## Check file content \dontrun{ file.show(iniFile) } } \seealso{ \code{\link{read.ini}} }