Skip to content

Commit

Permalink
bump version 0.13
Browse files Browse the repository at this point in the history
- allow reading maybe sav files (check the first four letters)
- allow the bias to be something else than 100
- warn if cflag = 1 and bias != 100
  • Loading branch information
JanMarvin committed Mar 4, 2019
1 parent b8a79fb commit d3b95d5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: readspss
Type: Package
Title: Importing SPSS Files
Version: 0.12
Version: 0.13
Authors@R: c(
person("Jan Marvin", "Garbuszus",
email = "[email protected]", role = c("aut", "cre")),
Expand Down
10 changes: 6 additions & 4 deletions src/read_sav_known_n.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ Rcpp::List read_sav_known_n (Rcpp::List& df, std::istream& sav,
Rcpp::NumericVector res,
std::vector<int> vartype,
const double lowest,
const double highest) {
const double highest,
const int bias) {

// final position
auto curpos = sav.tellg();
Expand Down Expand Up @@ -138,10 +139,10 @@ Rcpp::List read_sav_known_n (Rcpp::List& df, std::istream& sav,
if (val_b < lowest || val_b > highest)
val_b = NA_REAL;

REAL(VECTOR_ELT(df,kk))[nn] = val_b - 100;
REAL(VECTOR_ELT(df,kk))[nn] = val_b - bias;

if (debug)
Rprintf("val_b: %d\n", val_b - 100);
Rprintf("val_b: %d\n", val_b - bias);

break;
}
Expand Down Expand Up @@ -393,7 +394,8 @@ Rcpp::List read_sav_known_n (Rcpp::List& df, std::istream& sav,
{
val_d = readbin(val_d, sav, swapit);

if (val_d < lowest || val_d > highest)
// Not sure why, but -DBL_MAX is a missing in some sav-files
if (val_d < lowest || val_d > highest || val_d == -DBL_MAX)
val_d = NA_REAL;

REAL(VECTOR_ELT(df,kk))[nn] = val_d;
Expand Down
3 changes: 2 additions & 1 deletion src/read_sav_known_n.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Rcpp::List read_sav_known_n (Rcpp::List& df, std::istream& sav,
Rcpp::NumericVector res,
std::vector<int> vartype,
const double lowest,
const double highest);
const double highest,
const int bias);

#endif
20 changes: 14 additions & 6 deletions src/readsav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ List readsav(const char * filePath, const bool debug, std::string encStr,
std::ifstream sav(filePath, std::ios::in | std::ios::binary);
if (sav) {

bool is_spss = false, is_sav = false, is_zsav = false, swapit = false, autoenc = false;
bool is_spss = false, is_sav = false, is_zsav = false,
ml_sav = false, ml_zsav = false,
swapit = false, autoenc = false;

int64_t n = 0;
int32_t k = 0;
Expand All @@ -76,7 +78,11 @@ List readsav(const char * filePath, const bool debug, std::string encStr,

is_sav = std::regex_match(spss, std::regex("^\\$FL2@\\(#\\)$"));
is_zsav = std::regex_match(spss, std::regex("^\\$FL3@\\(#\\)$"));
is_spss = (is_sav == true) || (is_zsav == true);
ml_sav = std::regex_match(spss.substr(0,4), std::regex("^\\$FL2$"));
ml_zsav = std::regex_match(spss.substr(0,4), std::regex("^\\$FL3$"));
// most likely: "$FL2" can be followed by "SPSS"
is_spss = (is_sav == true) || (is_zsav == true) ||
(ml_sav == true) || (ml_zsav == true);



Expand Down Expand Up @@ -139,10 +145,12 @@ List readsav(const char * filePath, const bool debug, std::string encStr,
if (debug)
Rprintf("N: %d \n", n);

double bias = 0; // 100: compression bias
bias = readbin(bias, sav, swapit);
int bias = 0; double biasd = 0; // 100: compression bias
bias = readbin(biasd, sav, swapit);

if (bias!=100) Rcpp::stop("bias != 100. Stop.");
if ((cflag == 1) & (bias!=100))
Rcpp::warning("Cflag = 1. Found bias = %d. Using this. Expected 100.",
bias);

// creation date 9 dd_mmm_yy
std::string datestamp (9, '\0');
Expand Down Expand Up @@ -868,7 +876,7 @@ List readsav(const char * filePath, const bool debug, std::string encStr,

if (n > 0)
df = read_sav_known_n(df, sav, swapit, cflag, debug,
n, kv, vtyp, res, vartype, lowest, highest);
n, kv, vtyp, res, vartype, lowest, highest, bias);


// close file
Expand Down

0 comments on commit d3b95d5

Please sign in to comment.