Skip to content

Commit

Permalink
Merge pull request #357 from R-Lum/consolidate_read_BIN2R
Browse files Browse the repository at this point in the history
Consolidate the reading of the first few BIN/BINX fields [skip ci]
  • Loading branch information
mcol authored Oct 28, 2024
2 parents 56d869a + 58c2c23 commit d5e732f
Showing 1 changed file with 34 additions and 46 deletions.
80 changes: 34 additions & 46 deletions R/read_BIN2R.R
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,11 @@ read_BIN2R <- function(
seek.connection(con, 1, origin = "current")

## get record LENGTH
if(temp.VERSION == 06 | temp.VERSION == 07 | temp.VERSION == 08){
length.size <- 4
}else{
length.size <- 2
}

temp.LENGTH <- readBin(con, what = "integer", 1, size = length.size,
int.size <- if (temp.VERSION >= 05) 4 else 2
temp.LENGTH <- readBin(con, what = "integer", 1, size = int.size,
endian = "little")
num.toread <- max(0, temp.LENGTH - length.size - 2)

num.toread <- max(0, temp.LENGTH - int.size - 2)
if (num.toread > 0) {
seek.connection(con, num.toread, origin = "current")
} else {
Expand Down Expand Up @@ -589,25 +585,26 @@ read_BIN2R <- function(
#empty byte position
seek.connection(con, 1, origin = "current")

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# BINX FORMAT SUPPORT -----------------------------------------------------
if(temp.VERSION == 05 | temp.VERSION == 06 | temp.VERSION == 07 | temp.VERSION == 08){
##(1) Header size and structure
##LENGTH, PREVIOUS, NPOINTS, LTYPE
temp <- readBin(con, what = "int", 3, size = 4, endian = "little")
temp.LENGTH <- temp[1]
temp.PREVIOUS <- temp[2]
temp.NPOINTS <- temp[3]

## skip record if not selected
## the first condition boosts the speed of reading if n.records is not
## used; otherwise for each record the condition is checked whether
## used or not.
if(!is.null(n.records) && !(temp.ID + 1) %in% n.records) {
temp.ID <- temp.ID + 1
seek.connection(con, temp.LENGTH - 14, origin = "current")
next()
}
## (1) Header size and structure
## LENGTH, PREVIOUS, NPOINTS
int.size <- if (temp.VERSION >= 05) 4 else 2
temp <- readBin(con, what = "int", 3, size = int.size, endian = "little")
temp.LENGTH <- temp[1]
temp.PREVIOUS <- temp[2]
temp.NPOINTS <- temp[3]

## skip record if not selected in n.records
## the first condition boosts the speed of reading if n.records is not used
if (!is.null(n.records) && !(temp.ID + 1) %in% n.records) {
temp.ID <- temp.ID + 1
seek.connection(con, temp.LENGTH - 3 * int.size - 2, origin = "current")
next()
}

## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## BINX FORMAT SUPPORT
if (temp.VERSION == 05 || temp.VERSION == 06 ||
temp.VERSION == 07 || temp.VERSION == 08) {

#for temp.VERSION == 08
#RECTYPE
Expand Down Expand Up @@ -904,21 +901,11 @@ read_BIN2R <- function(
}
}# end RECTYPE 128
}
}else if(temp.VERSION == 04 | temp.VERSION == 03){
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
##START BIN FILE FORMAT SUPPORT (vers. 03 and 04)
##LENGTH, PREVIOUS, NPOINTS, LTYPE
temp <- readBin(con, what="int", 3, size=2, endian="little")
temp.LENGTH <- temp[1]
temp.PREVIOUS <- temp[2]
temp.NPOINTS <- temp[3]

## set temp ID if within select
if(!is.null(n.records) && !(temp.ID + 1) %in% n.records) {
temp.ID <- temp.ID + 1
seek.connection(con, temp.LENGTH - 8, origin = "current")
next()
}
}

## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## BIN FILE FORMAT SUPPORT
else if (temp.VERSION == 03 || temp.VERSION == 04) {

##LTYPE
temp.LTYPE<-readBin(con, what="int", 1, size=1, endian="little")
Expand Down Expand Up @@ -1095,10 +1082,11 @@ read_BIN2R <- function(
temp.PTENABLED <- temp[1]
temp.RESERVED2 <- temp[2:11]
}
} else {
## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## Unrecognised version
##
}

## ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
## Unrecognised version
else {
## We should already have raised a warning that the file is corrupt
## during the first scan of the BIN/BINX file: at that point we have
## set `n.records` so that we would stop reading before encountering
Expand Down

0 comments on commit d5e732f

Please sign in to comment.