Skip to content

Commit

Permalink
Merge pull request #36 from sjewo/testing
Browse files Browse the repository at this point in the history
Fix type warnings in sprintf
  • Loading branch information
sjewo authored Sep 2, 2016
2 parents 95d765a + ecb3580 commit bdaa3da
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
Binary file added inst/extdata/statacar_strl.dta
Binary file not shown.
26 changes: 19 additions & 7 deletions src/read_dta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ List read_dta(FILE * file, const bool missing) {
// string of any length
case 32768:
{// strL 2*4bit or 2 + 6 bit
char val_strl[22];
//char val_strl[22];

// FixMe: Strl in 118
switch (release)
Expand All @@ -498,7 +498,10 @@ List read_dta(FILE * file, const bool missing) {
v = readbin(v, file, swapit);
o = readbin(o, file, swapit);

sprintf(val_strl, "%010d%010d", v, o);
stringstream ss;
ss << setfill('0') << setw(10) << v << setfill('0') << setw(10) << o;
string val_strl = ss.str();
//sprintf(val_strl, "%010d%010d", v, o);
as<CharacterVector>(df[i])[j] = val_strl;
break;
}
Expand All @@ -513,7 +516,10 @@ List read_dta(FILE * file, const bool missing) {
v = (uint16_t)z;
o = (z >> 16);

sprintf(val_strl, "%010d%010ld", v, o);
stringstream ss;
ss << setfill('0') << setw(10) << v << setfill('0') << setw(10) << o;
string val_strl = ss.str();
//sprintf(val_strl, "%010d%010llu", v, o);

as<CharacterVector>(df[i])[j] = val_strl;
break;
Expand Down Expand Up @@ -555,7 +561,7 @@ List read_dta(FILE * file, const bool missing) {
while(gso.compare(tags)==0)
{
CharacterVector strls(2);
char ref[22];
string ref;

// FixMe: Strl in 118
switch (release)
Expand All @@ -567,7 +573,10 @@ List read_dta(FILE * file, const bool missing) {
v = readbin(v, file, swapit);
o = readbin(o, file, swapit);

sprintf(ref, "%010d%010d", v, o);
stringstream ss;
ss << setfill('0') << setw(10) << v << setfill('0') << setw(10) << o;
ref.assign(ss.str());
//sprintf(ref, "%010d%010d", v, o);
break;
}
case 118:
Expand All @@ -579,8 +588,11 @@ List read_dta(FILE * file, const bool missing) {
o = readbin(o, file, swapit);
// z = readbin(z, file, swapit);

sprintf(ref, "%010d%010ld", v, o);
// sprintf(ref, "%010ld", z);
stringstream ss;
ss << setfill('0') << setw(10) << v << setfill('0') << setw(10) << o;
ref.assign(ss.str());
//sprintf(ref, "%010d%010ld", v, o);

break;
}
}
Expand Down
12 changes: 11 additions & 1 deletion tests/testthat/test_read.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,14 @@ test_that("encoding UTF-8 (Stata 14)", {
expect_true(datacompare(dd, ddutf_aE))
})

# rm(list = files)
test_that("Reading of strls", {
strl <- system.file("extdata", "statacar_strl.dta", package="readstata13")

ddstrlf <- read.dta13(strl, replace.strl = F)
ddstrlfref <- c("00000000130000000001", "00000000130000000002", "00000000130000000003", "00000000130000000004", "00000000130000000005", "00000000130000000006", "00000000130000000007", "00000000130000000008", "00000000130000000009", "00000000130000000010", "00000000130000000011", "00000000130000000012", "00000000130000000013", "00000000130000000014", "00000000130000000015", "00000000130000000016", "00000000130000000017", "00000000130000000018", "00000000130000000019", "00000000130000000020", "00000000130000000021", "00000000130000000022", "00000000130000000023", "00000000130000000024", "00000000130000000025", "00000000130000000026", "00000000130000000027", "00000000130000000028", "00000000130000000029", "00000000130000000030", "00000000130000000031", "00000000130000000032", "00000000130000000033", "00000000130000000034", "00000000130000000035", "00000000130000000036", "00000000130000000037", "00000000130000000038", "00000000130000000039", "00000000130000000040", "00000000130000000041", "00000000130000000042", "00000000130000000043", "00000000130000000044", "00000000130000000045", "00000000130000000046", "00000000130000000047", "00000000130000000048", "00000000130000000049", "00000000130000000050", "00000000130000000051", "00000000130000000052", "00000000130000000053", "00000000130000000054", "00000000130000000055", "00000000130000000056", "00000000130000000057", "00000000130000000058", "00000000130000000059", "00000000130000000060", "00000000130000000061", "00000000130000000062", "00000000130000000063", "00000000130000000064", "00000000130000000065", "00000000130000000066", "00000000130000000067", "00000000130000000068", "00000000130000000069", "00000000130000000070", "00000000130000000071", "00000000130000000072", "00000000130000000073", "00000000130000000074")
expect_equal(ddstrlf$mymake, ddstrlfref)

ddstrl <- read.dta13(strl, replace.strl = T)
expect_equal(ddstrl$mymake, ddstrl$make)
})

0 comments on commit bdaa3da

Please sign in to comment.