Skip to content

Commit

Permalink
[misc] sync with openssl (#32)
Browse files Browse the repository at this point in the history
* [misc] update readspss.Rproj

* [misc] sync with openssl 2.3.0 by @jeroen

* [misc] update Rbuildignore

* [write] restore writing character variables longer than 8 characters
  • Loading branch information
JanMarvin authored Dec 20, 2024
1 parent 3ba9cc2 commit a83b9eb
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 34 deletions.
6 changes: 6 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@
.lintr
^configure.log$
^README.Rmd$
^windows$
^src/Makevars$
.*\.so
.*\.dll
.*\.o
.*\.a
55 changes: 36 additions & 19 deletions R/writesav.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
resize_vartyp <- function(vec, var = NULL) {

out <- NULL
for (i in seq_along(vec)) {

val <- vec[i]

if (is.null(var)) {
if (val <= 8) {
out <- c(out, val)
} else {
out <- c(out, c(val, rep(-1, (ceiling(val / 8) - 1))))
}
} else {
if (val <= 8) {
out <- c(out, var[i])
} else {
out <- c(out, c(var[i], rep(var[i], (ceiling(val / 8) - 1))))
}
}

}

out
}

#' write.sav
#'
#' Function to write an SPSS sav or zsav file from a data.frame().
Expand Down Expand Up @@ -93,25 +119,7 @@ write.sav <- function(dat, filepath, label, add.rownames = FALSE,

vtyp[vtyp > 255] <- 255

fun <- function(vec) {

vartypes <- NULL
for (i in seq_along(vec)) {

val <- vtyp[i]

if (val <= 8) {
vartypes <- c(vartypes, val)
} else {
vartypes <- c(vartypes, c(val, rep(-1, (ceiling(val / 8) - 1))))
}
}

vartypes

}

vartypes <- fun(vtyp)
vartypes <- resize_vartyp(vtyp)

vartypes[vartypes > 255] <- 255

Expand Down Expand Up @@ -264,6 +272,15 @@ write.sav <- function(dat, filepath, label, add.rownames = FALSE,
# make it flat
disppar <- c(t(disppar))

# resize vartyp for long strings
if (length(vartyp) != length(vartypes)) {
vartyp <- resize_vartyp(vtyp, vartyp)
}

if (length(label) != length(vartypes)) {
label <- resize_vartyp(vtyp, label)
}

attr(dat, "vtyp") <- vtyp
attr(dat, "vartyp") <- vartyp
attr(dat, "vartypes") <- vartypes
Expand Down
5 changes: 0 additions & 5 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ PKG_CFLAGS=""
# Build against a specific openssl version
# export PKG_CONFIG_PATH="/usr/local/opt/openssl@1.1/lib/pkgconfig"

# On MacOS we avoid legacy versions of openssl
if [ `uname` = "Darwin" ]; then
MINVERSION="--atleast-version=3"
fi

# Use pkg-config if available
pkg-config ${PKG_CONFIG_NAME} ${MINVERSION} 2>/dev/null
if [ $? -eq 0 ]; then
Expand Down
1 change: 1 addition & 0 deletions readspss.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: ee071c55-283a-4646-997f-e2262c923500

RestoreWorkspace: Default
SaveWorkspace: Default
Expand Down
23 changes: 15 additions & 8 deletions src/Makevars.win
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
PKG_CONFIG ?= $(BINPREF)pkg-config
OPENSSL_LIBS := $(shell $(PKG_CONFIG) --libs openssl)

ifneq ($(OPENSSL_LIBS),)
$(info using OpenSSL from Rtools)
OPENSSL_CFLAGS := $(shell $(PKG_CONFIG) --cflags openssl)
else
RWINLIB = ../windows/libssl
TARGET = lib$(subst gcc,,$(COMPILED_BY))$(R_ARCH)
PKG_CPPFLAGS = -I$(RWINLIB)/include -DOPENSSL_SUPPRESS_DEPRECATED
OPENSSL_CFLAGS = -I$(RWINLIB)/include -DOPENSSL_SUPPRESS_DEPRECATED
OPENSSL_LIBS = -L$(RWINLIB)/$(TARGET) -L$(RWINLIB)/lib -lssl -lcrypto -lws2_32 -lcrypt32
endif

PKG_LIBS = \
-L$(RWINLIB)/$(TARGET) \
-L$(RWINLIB)/lib \
-lssl -lcrypto -lz -lws2_32 -lcrypt32
PKG_CPPFLAGS = $(OPENSSL_CFLAGS) -DOPENSSL_SUPPRESS_DEPRECATED
PKG_LIBS = $(OPENSSL_LIBS) -lz

#all: clean
all: $(SHLIB)

$(OBJECTS): winlibs
$(OBJECTS): $(RWINLIB)

$(SHLIB): $(OBJECTS)

winlibs:
$(RWINLIB):
"${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" "../tools/winlibs.R"

clean:
rm -f $(SHLIB) $(OBJECTS)

.PHONY: all clean winlibs
.PHONY: all clean
4 changes: 2 additions & 2 deletions tests/testthat/test_write.R
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ dd <- data.frame(
stringsAsFactors = FALSE
)

write.sav(dd, "data/dd_u.sav", compress = FALSE)
write.sav(dd, "data/dd_c.sav", compress = TRUE)
write.sav(dd, "data/dd_u.sav", label = c("A numeric", "A not so long string", "A long string"), compress = FALSE)
write.sav(dd, "data/dd_c.sav", label = c("A numeric", "A not so long string", "A long string"), compress = TRUE)

write.por(dd, "data/dd_p.por")

Expand Down

0 comments on commit a83b9eb

Please sign in to comment.