js-stream-sas7bdat 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/binding.gyp +58 -0
- package/package.json +4 -2
- package/src/binding/ReadStat/LICENSE +19 -0
- package/src/binding/ReadStat/README.md +483 -0
- package/src/binding/ReadStat/src/CKHashTable.c +309 -0
- package/src/binding/ReadStat/src/CKHashTable.h +37 -0
- package/src/binding/ReadStat/src/readstat.h +627 -0
- package/src/binding/ReadStat/src/readstat_bits.c +69 -0
- package/src/binding/ReadStat/src/readstat_bits.h +20 -0
- package/src/binding/ReadStat/src/readstat_convert.c +36 -0
- package/src/binding/ReadStat/src/readstat_convert.h +2 -0
- package/src/binding/ReadStat/src/readstat_error.c +126 -0
- package/src/binding/ReadStat/src/readstat_iconv.h +15 -0
- package/src/binding/ReadStat/src/readstat_io_unistd.c +147 -0
- package/src/binding/ReadStat/src/readstat_io_unistd.h +11 -0
- package/src/binding/ReadStat/src/readstat_malloc.c +34 -0
- package/src/binding/ReadStat/src/readstat_malloc.h +4 -0
- package/src/binding/ReadStat/src/readstat_metadata.c +53 -0
- package/src/binding/ReadStat/src/readstat_parser.c +121 -0
- package/src/binding/ReadStat/src/readstat_strings.h +6 -0
- package/src/binding/ReadStat/src/readstat_value.c +178 -0
- package/src/binding/ReadStat/src/readstat_variable.c +123 -0
- package/src/binding/ReadStat/src/readstat_writer.c +677 -0
- package/src/binding/ReadStat/src/readstat_writer.h +21 -0
- package/src/binding/ReadStat/src/sas/ieee.c +420 -0
- package/src/binding/ReadStat/src/sas/ieee.h +6 -0
- package/src/binding/ReadStat/src/sas/readstat_sas.c +528 -0
- package/src/binding/ReadStat/src/sas/readstat_sas.h +131 -0
- package/src/binding/ReadStat/src/sas/readstat_sas7bcat_read.c +515 -0
- package/src/binding/ReadStat/src/sas/readstat_sas7bcat_write.c +218 -0
- package/src/binding/ReadStat/src/sas/readstat_sas7bdat_read.c +1304 -0
- package/src/binding/ReadStat/src/sas/readstat_sas7bdat_write.c +812 -0
- package/src/binding/ReadStat/src/sas/readstat_sas_rle.c +286 -0
- package/src/binding/ReadStat/src/sas/readstat_sas_rle.h +8 -0
- package/src/binding/ReadStat/src/sas/readstat_xport.c +28 -0
- package/src/binding/ReadStat/src/sas/readstat_xport.h +47 -0
- package/src/binding/ReadStat/src/sas/readstat_xport_parse_format.c +265 -0
- package/src/binding/ReadStat/src/sas/readstat_xport_parse_format.h +4 -0
- package/src/binding/ReadStat/src/sas/readstat_xport_parse_format.rl +68 -0
- package/src/binding/ReadStat/src/sas/readstat_xport_read.c +777 -0
- package/src/binding/ReadStat/src/sas/readstat_xport_write.c +561 -0
- package/src/binding/readstat_binding.cc +393 -0
package/binding.gyp
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"targets": [
|
|
3
|
+
{
|
|
4
|
+
"target_name": "readstat_binding",
|
|
5
|
+
"sources": [
|
|
6
|
+
"src/binding/readstat_binding.cc",
|
|
7
|
+
"src/binding/ReadStat/src/CKHashTable.c",
|
|
8
|
+
"src/binding/ReadStat/src/readstat_bits.c",
|
|
9
|
+
"src/binding/ReadStat/src/readstat_convert.c",
|
|
10
|
+
"src/binding/ReadStat/src/readstat_error.c",
|
|
11
|
+
"src/binding/ReadStat/src/readstat_io_unistd.c",
|
|
12
|
+
"src/binding/ReadStat/src/readstat_malloc.c",
|
|
13
|
+
"src/binding/ReadStat/src/readstat_metadata.c",
|
|
14
|
+
"src/binding/ReadStat/src/readstat_parser.c",
|
|
15
|
+
"src/binding/ReadStat/src/readstat_value.c",
|
|
16
|
+
"src/binding/ReadStat/src/readstat_variable.c",
|
|
17
|
+
"src/binding/ReadStat/src/readstat_writer.c",
|
|
18
|
+
"src/binding/ReadStat/src/sas/ieee.c",
|
|
19
|
+
"src/binding/ReadStat/src/sas/readstat_sas.c",
|
|
20
|
+
"src/binding/ReadStat/src/sas/readstat_sas7bcat_read.c",
|
|
21
|
+
"src/binding/ReadStat/src/sas/readstat_sas7bcat_write.c",
|
|
22
|
+
"src/binding/ReadStat/src/sas/readstat_sas7bdat_read.c",
|
|
23
|
+
"src/binding/ReadStat/src/sas/readstat_sas7bdat_write.c",
|
|
24
|
+
"src/binding/ReadStat/src/sas/readstat_sas_rle.c",
|
|
25
|
+
"src/binding/ReadStat/src/sas/readstat_xport.c",
|
|
26
|
+
"src/binding/ReadStat/src/sas/readstat_xport_parse_format.c",
|
|
27
|
+
"src/binding/ReadStat/src/sas/readstat_xport_read.c",
|
|
28
|
+
"src/binding/ReadStat/src/sas/readstat_xport_write.c"
|
|
29
|
+
],
|
|
30
|
+
"include_dirs": [
|
|
31
|
+
"<!@(node -p \"require('node-addon-api').include\")",
|
|
32
|
+
"src/binding/ReadStat/src"
|
|
33
|
+
],
|
|
34
|
+
"dependencies": [
|
|
35
|
+
"<!(node -p \"require('node-addon-api').gyp\")"
|
|
36
|
+
],
|
|
37
|
+
"cflags!": ["-fno-exceptions"],
|
|
38
|
+
"cflags_cc!": ["-fno-exceptions"],
|
|
39
|
+
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"],
|
|
40
|
+
"conditions": [
|
|
41
|
+
["OS=='win'", {
|
|
42
|
+
"msvs_settings": {
|
|
43
|
+
"VCCLCompilerTool": {
|
|
44
|
+
"ExceptionHandling": 1
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}],
|
|
48
|
+
["OS=='mac'", {
|
|
49
|
+
"xcode_settings": {
|
|
50
|
+
"GCC_ENABLE_CPP_EXCEPTIONS": "YES",
|
|
51
|
+
"CLANG_CXX_LIBRARY": "libc++",
|
|
52
|
+
"MACOSX_DEPLOYMENT_TARGET": "10.15"
|
|
53
|
+
}
|
|
54
|
+
}]
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
]
|
|
58
|
+
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "js-stream-sas7bdat",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Stream SAS7BDAT files using ReadStat C library",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"files": [
|
|
8
|
-
"/dist"
|
|
8
|
+
"/dist",
|
|
9
|
+
"binding.gyp",
|
|
10
|
+
"/src/binding"
|
|
9
11
|
],
|
|
10
12
|
"scripts": {
|
|
11
13
|
"test": "jest",
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
Copyright (c) 2013-2016 Evan Miller (except where otherwise noted)
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
5
|
+
in the Software without restriction, including without limitation the rights
|
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
[](https://github.com/WizardMac/ReadStat/actions)
|
|
2
|
+
[](https://ci.appveyor.com/project/evanmiller/readstat/branch/master)
|
|
3
|
+
[](https://codecov.io/gh/WizardMac/ReadStat)
|
|
4
|
+
[](https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened&can=1&q=proj:readstat)
|
|
5
|
+
|
|
6
|
+
ReadStat: Read (and write) data sets from SAS, Stata, and SPSS
|
|
7
|
+
==
|
|
8
|
+
|
|
9
|
+
Originally developed for [Wizard](https://www.wizardmac.com/), ReadStat is a
|
|
10
|
+
command-line tool and MIT-licensed C library for reading files from popular
|
|
11
|
+
stats packages. Supported data formats include:
|
|
12
|
+
|
|
13
|
+
* SAS: SAS7BDAT (binary file) and XPORT (transport file)
|
|
14
|
+
* Stata: DTA (binary file) versions 104-119
|
|
15
|
+
* SPSS: POR (portable file), SAV (binary file), and ZSAV (compressed binary)
|
|
16
|
+
|
|
17
|
+
Supported metadata formats include:
|
|
18
|
+
|
|
19
|
+
* SAS: SAS7BCAT (catalog file) and .sas (command file)
|
|
20
|
+
* Stata: .dct (dictionary file)
|
|
21
|
+
* SPSS: .sps (command file)
|
|
22
|
+
|
|
23
|
+
There is also write support for all the data formats, but not the metadata
|
|
24
|
+
formats. *The produced SAS7BDAT files still cannot be read by SAS*, but feel
|
|
25
|
+
free to contribute your binary-format expertise here.
|
|
26
|
+
|
|
27
|
+
For reading in R data files, please see the related
|
|
28
|
+
[librdata](https://github.com/WizardMac/librdata) project.
|
|
29
|
+
|
|
30
|
+
Installation on Unix / macOS
|
|
31
|
+
--
|
|
32
|
+
|
|
33
|
+
Grab the latest [release](https://github.com/WizardMac/ReadStat/releases) and
|
|
34
|
+
then proceed as usual:
|
|
35
|
+
|
|
36
|
+
./configure
|
|
37
|
+
make
|
|
38
|
+
sudo make install
|
|
39
|
+
|
|
40
|
+
If you're cloning the repository, first make sure you have autotools installed,
|
|
41
|
+
and then run `./autogen.sh` to generate the configure file.
|
|
42
|
+
|
|
43
|
+
If you're on Mac and see errors about `AM_ICONV` when you run `./autogen.sh`,
|
|
44
|
+
you'll need to install [gettext](https://www.gnu.org/software/gettext/).
|
|
45
|
+
|
|
46
|
+
Installation on Windows
|
|
47
|
+
--
|
|
48
|
+
|
|
49
|
+
ReadStat now includes a Microsoft Visual Studio project file that includes
|
|
50
|
+
build targets for the library and tests. See the [VS17](./VS17) folder in
|
|
51
|
+
the downloaded release for a "one-click" Windows build.
|
|
52
|
+
|
|
53
|
+
Alternatively, you can build ReadStat on the command line using an
|
|
54
|
+
[msys2](https://msys2.github.io/) environment. After installing msys2,
|
|
55
|
+
download some other packages:
|
|
56
|
+
|
|
57
|
+
pacman -S autoconf automake libtool make mingw-w64-x86_64-toolchain mingw-w64-x86_64-cmake mingw-w64-x86_64-libiconv
|
|
58
|
+
|
|
59
|
+
Then start a MINGW command line (not the msys2 prompt!) and follow the UNIX
|
|
60
|
+
install instructions above for this package.
|
|
61
|
+
|
|
62
|
+
Language Bindings
|
|
63
|
+
--
|
|
64
|
+
|
|
65
|
+
* Julia: [ReadStat.jl](https://github.com/queryverse/ReadStat.jl)
|
|
66
|
+
* Perl 6: [ReadStat.pm6](https://github.com/WizardMac/ReadStat.pm6)
|
|
67
|
+
* Python: [pyreadstat](https://github.com/Roche/pyreadstat)
|
|
68
|
+
* R: [haven](https://github.com/tidyverse/haven)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
Docker
|
|
72
|
+
--
|
|
73
|
+
|
|
74
|
+
A dockerized version is available [here](https://github.com/jbn/readstat)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
Command-line Usage
|
|
79
|
+
--
|
|
80
|
+
|
|
81
|
+
Standard usage:
|
|
82
|
+
|
|
83
|
+
readstat [-f] <input file> <output file>
|
|
84
|
+
|
|
85
|
+
Where:
|
|
86
|
+
|
|
87
|
+
* `<input file>` ends with `.dta`, `.por`, `.sav`, `.sas7bdat`, or `.xpt`and
|
|
88
|
+
* `<output file>` ends with `.dta`, `.por`, `.sav`, `.sas7bdat`, `.xpt` or `.csv`
|
|
89
|
+
|
|
90
|
+
If [libxlsxwriter](http://libxlsxwriter.github.io) is found at compile-time, an
|
|
91
|
+
XLSX file (ending in `.xlsx`) can be written instead.
|
|
92
|
+
|
|
93
|
+
If zlib is found at compile-time, compressed SPSS files (`.zsav`) can be read
|
|
94
|
+
and written as well.
|
|
95
|
+
|
|
96
|
+
Use the `-f` option to overwrite an existing output file.
|
|
97
|
+
|
|
98
|
+
If you have a plain-text file described by a Stata dictionary file, a SAS
|
|
99
|
+
command file, or an SPSS command file, a second invocation style is supported:
|
|
100
|
+
|
|
101
|
+
readstat <input file> <dictionary file> <output file>
|
|
102
|
+
|
|
103
|
+
Where:
|
|
104
|
+
|
|
105
|
+
* `<input file>` can be anything
|
|
106
|
+
* `<dictionary file>` ends with `.dct`, `.sas`, or `.sps`
|
|
107
|
+
* `<output file>` ends with `.dta`, `.por`, `.sav`, `.xpt`, or `.csv`
|
|
108
|
+
|
|
109
|
+
If you have a SAS catalog file containing the data set's value labels, you
|
|
110
|
+
can use the same invocation:
|
|
111
|
+
|
|
112
|
+
readstat <input file> <catalog file> <output file>
|
|
113
|
+
|
|
114
|
+
Except where:
|
|
115
|
+
|
|
116
|
+
* `<input file>` ends with `.sas7bdat`
|
|
117
|
+
* `<catalog file>` ends with `.sas7bcat`
|
|
118
|
+
* `<output file>` ends with `.dta`, `.por`, `.sav`, `.xpt`, or `.csv`
|
|
119
|
+
|
|
120
|
+
If the file conversion succeeds, ReadStat will report the number of rows and
|
|
121
|
+
variables converted, e.g.
|
|
122
|
+
|
|
123
|
+
Converted 111 variables and 160851 rows in 12.36 seconds
|
|
124
|
+
|
|
125
|
+
At the moment value labels are supported, but the finer nuances of converting
|
|
126
|
+
format strings (e.g. `%8.2g`) are not.
|
|
127
|
+
|
|
128
|
+
Command-line Usage with CSV input
|
|
129
|
+
--
|
|
130
|
+
|
|
131
|
+
A prerequisite for CSV input is that the [libcsv](https://github.com/rgamble/libcsv)
|
|
132
|
+
library is found at compile time.
|
|
133
|
+
|
|
134
|
+
CSV input is supported together with a metadata file describing the data:
|
|
135
|
+
|
|
136
|
+
readstat <input file.csv> <input metadata.json> <output file>
|
|
137
|
+
|
|
138
|
+
The `<output file>` should end with `.dta`, `.sav`, or `.csv`.
|
|
139
|
+
|
|
140
|
+
The `<input file.csv>` is a regular CSV file.
|
|
141
|
+
|
|
142
|
+
The `<input metadata.json>` is a JSON file describing column types, value
|
|
143
|
+
labels and missing values. The easiest way to create such a metadata file is to
|
|
144
|
+
use the provided `extract_metadata` program on an existing file:
|
|
145
|
+
|
|
146
|
+
$ extract_metadata <input file.(dta|sav|sas7bcat)>
|
|
147
|
+
|
|
148
|
+
The schema of this JSON file is fully described in
|
|
149
|
+
[variablemetadata_schema.json](variablemetadata_schema.json) using [JSON
|
|
150
|
+
Schema](http://json-schema.org/).
|
|
151
|
+
|
|
152
|
+
The following is an example of a valid metadata file:
|
|
153
|
+
|
|
154
|
+
{
|
|
155
|
+
"type": "SPSS",
|
|
156
|
+
"variables": [
|
|
157
|
+
{
|
|
158
|
+
"type": "NUMERIC",
|
|
159
|
+
"name": "citizenship",
|
|
160
|
+
"label": "Citizenship of respondent",
|
|
161
|
+
"categories": [
|
|
162
|
+
{
|
|
163
|
+
"code": 1,
|
|
164
|
+
"label": "Afghanistan"
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
"code": 2,
|
|
168
|
+
"label": "Albania"
|
|
169
|
+
},
|
|
170
|
+
{
|
|
171
|
+
"code": 98,
|
|
172
|
+
"label": "No answer"
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
"code": 99,
|
|
176
|
+
"label": "Not applicable"
|
|
177
|
+
}
|
|
178
|
+
],
|
|
179
|
+
"missing": {
|
|
180
|
+
"type": "DISCRETE",
|
|
181
|
+
"values": [
|
|
182
|
+
98,
|
|
183
|
+
99
|
|
184
|
+
]
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
]
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
Here the column `citizenship` is a numeric column with four possible values `1`, `2`, `98`, and `99`.
|
|
191
|
+
`1` has the label `Afghanistan`, `2` has `Albania`, `98` has `No answer` and `99` has `Not applicable`.
|
|
192
|
+
`98` and `99` are defined as missing values.
|
|
193
|
+
|
|
194
|
+
Other column types are `STRING` and `DATE`.
|
|
195
|
+
All values in `DATE` columns are expected to conform to [ISO 8601 date](https://en.wikipedia.org/wiki/ISO_8601).
|
|
196
|
+
Here is an example of `DATE` metadata:
|
|
197
|
+
|
|
198
|
+
{
|
|
199
|
+
"type": "SPSS",
|
|
200
|
+
"variables": [
|
|
201
|
+
{
|
|
202
|
+
"type": "DATE",
|
|
203
|
+
"name": "startdate",
|
|
204
|
+
"label": "Start date",
|
|
205
|
+
"categories": [
|
|
206
|
+
{
|
|
207
|
+
"code": "6666-01-01",
|
|
208
|
+
"label": "no date available"
|
|
209
|
+
}
|
|
210
|
+
],
|
|
211
|
+
"missing": {
|
|
212
|
+
"type": "DISCRETE",
|
|
213
|
+
"values": [
|
|
214
|
+
"6666-01-01",
|
|
215
|
+
"9999-01-01"
|
|
216
|
+
]
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
]
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
Value labels are supported for `DATE`.
|
|
223
|
+
|
|
224
|
+
The last column type is `STRING`:
|
|
225
|
+
|
|
226
|
+
{
|
|
227
|
+
"type": "SPSS",
|
|
228
|
+
"variables": [
|
|
229
|
+
{
|
|
230
|
+
"type": "STRING",
|
|
231
|
+
"name": "somestring",
|
|
232
|
+
"label": "Label of column",
|
|
233
|
+
"missing": {
|
|
234
|
+
"type": "DISCRETE",
|
|
235
|
+
"values": [
|
|
236
|
+
"NA",
|
|
237
|
+
"N/A"
|
|
238
|
+
]
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
]
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
Value labels are not supported for `STRING`.
|
|
245
|
+
|
|
246
|
+
Library Usage: Reading Files
|
|
247
|
+
--
|
|
248
|
+
|
|
249
|
+
The ReadStat API is callback-based. It uses very little memory, and is suitable
|
|
250
|
+
for programs with progress bars. ReadStat uses
|
|
251
|
+
[iconv](https://en.wikipedia.org/wiki/Iconv) to automatically transcode
|
|
252
|
+
text data into UTF-8, so you don't have to worry about character encodings.
|
|
253
|
+
|
|
254
|
+
See src/readstat.h for the complete API. In general you'll provide a filename
|
|
255
|
+
and a set of optional callback functions for handling various information and
|
|
256
|
+
data found in the file. It's up to the user to store this information in an
|
|
257
|
+
appropriate data structure. If a context pointer is passed to the parse_* functions,
|
|
258
|
+
it will be made available to the various callback functions.
|
|
259
|
+
|
|
260
|
+
Callback functions should return `READSTAT_HANDLER_OK` (zero) on success.
|
|
261
|
+
Returning `READSTAT_HANDLER_ABORT` will abort the parsing process.
|
|
262
|
+
|
|
263
|
+
Example: Return the number of records in a DTA file.
|
|
264
|
+
|
|
265
|
+
```c
|
|
266
|
+
#include "readstat.h"
|
|
267
|
+
|
|
268
|
+
int handle_metadata(readstat_metadata_t *metadata, void *ctx) {
|
|
269
|
+
int *my_count = (int *)ctx;
|
|
270
|
+
|
|
271
|
+
*my_count = readstat_get_row_count(metadata);
|
|
272
|
+
|
|
273
|
+
return READSTAT_HANDLER_OK;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
int main(int argc, char *argv[]) {
|
|
277
|
+
if (argc != 2) {
|
|
278
|
+
printf("Usage: %s <filename>\n", argv[0]);
|
|
279
|
+
return 1;
|
|
280
|
+
}
|
|
281
|
+
int my_count = 0;
|
|
282
|
+
readstat_error_t error = READSTAT_OK;
|
|
283
|
+
readstat_parser_t *parser = readstat_parser_init();
|
|
284
|
+
readstat_set_metadata_handler(parser, &handle_metadata);
|
|
285
|
+
|
|
286
|
+
error = readstat_parse_dta(parser, argv[1], &my_count);
|
|
287
|
+
|
|
288
|
+
readstat_parser_free(parser);
|
|
289
|
+
|
|
290
|
+
if (error != READSTAT_OK) {
|
|
291
|
+
printf("Error processing %s: %d\n", argv[1], error);
|
|
292
|
+
return 1;
|
|
293
|
+
}
|
|
294
|
+
printf("Found %d records\n", my_count);
|
|
295
|
+
return 0;
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
Example: Convert a DTA to a tab-separated file.
|
|
300
|
+
|
|
301
|
+
```c
|
|
302
|
+
#include "readstat.h"
|
|
303
|
+
|
|
304
|
+
int handle_metadata(readstat_metadata_t *metadata, void *ctx) {
|
|
305
|
+
int *my_var_count = (int *)ctx;
|
|
306
|
+
|
|
307
|
+
*my_var_count = readstat_get_var_count(metadata);
|
|
308
|
+
|
|
309
|
+
return READSTAT_HANDLER_OK;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
int handle_variable(int index, readstat_variable_t *variable,
|
|
313
|
+
const char *val_labels, void *ctx) {
|
|
314
|
+
int *my_var_count = (int *)ctx;
|
|
315
|
+
|
|
316
|
+
printf("%s", readstat_variable_get_name(variable));
|
|
317
|
+
if (index == *my_var_count - 1) {
|
|
318
|
+
printf("\n");
|
|
319
|
+
} else {
|
|
320
|
+
printf("\t");
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
return READSTAT_HANDLER_OK;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
int handle_value(int obs_index, readstat_variable_t *variable, readstat_value_t value, void *ctx) {
|
|
327
|
+
int *my_var_count = (int *)ctx;
|
|
328
|
+
int var_index = readstat_variable_get_index(variable);
|
|
329
|
+
readstat_type_t type = readstat_value_type(value);
|
|
330
|
+
if (!readstat_value_is_system_missing(value)) {
|
|
331
|
+
if (type == READSTAT_TYPE_STRING) {
|
|
332
|
+
printf("%s", readstat_string_value(value));
|
|
333
|
+
} else if (type == READSTAT_TYPE_INT8) {
|
|
334
|
+
printf("%hhd", readstat_int8_value(value));
|
|
335
|
+
} else if (type == READSTAT_TYPE_INT16) {
|
|
336
|
+
printf("%hd", readstat_int16_value(value));
|
|
337
|
+
} else if (type == READSTAT_TYPE_INT32) {
|
|
338
|
+
printf("%d", readstat_int32_value(value));
|
|
339
|
+
} else if (type == READSTAT_TYPE_FLOAT) {
|
|
340
|
+
printf("%f", readstat_float_value(value));
|
|
341
|
+
} else if (type == READSTAT_TYPE_DOUBLE) {
|
|
342
|
+
printf("%lf", readstat_double_value(value));
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
if (var_index == *my_var_count - 1) {
|
|
346
|
+
printf("\n");
|
|
347
|
+
} else {
|
|
348
|
+
printf("\t");
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return READSTAT_HANDLER_OK;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
int main(int argc, char *argv[]) {
|
|
355
|
+
if (argc != 2) {
|
|
356
|
+
printf("Usage: %s <filename>\n", argv[0]);
|
|
357
|
+
return 1;
|
|
358
|
+
}
|
|
359
|
+
int my_var_count = 0;
|
|
360
|
+
readstat_error_t error = READSTAT_OK;
|
|
361
|
+
readstat_parser_t *parser = readstat_parser_init();
|
|
362
|
+
readstat_set_metadata_handler(parser, &handle_metadata);
|
|
363
|
+
readstat_set_variable_handler(parser, &handle_variable);
|
|
364
|
+
readstat_set_value_handler(parser, &handle_value);
|
|
365
|
+
|
|
366
|
+
error = readstat_parse_dta(parser, argv[1], &my_var_count);
|
|
367
|
+
|
|
368
|
+
readstat_parser_free(parser);
|
|
369
|
+
|
|
370
|
+
if (error != READSTAT_OK) {
|
|
371
|
+
printf("Error processing %s: %d\n", argv[1], error);
|
|
372
|
+
return 1;
|
|
373
|
+
}
|
|
374
|
+
return 0;
|
|
375
|
+
}
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
Library Usage: Writing Files
|
|
379
|
+
--
|
|
380
|
+
|
|
381
|
+
ReadStat can write data sets to a number of file formats, and uses largely the
|
|
382
|
+
same API for each of them. Files are written incrementally, with the header
|
|
383
|
+
written first, followed by individual rows of data, and ending with some kind
|
|
384
|
+
of trailer. (So the full data file never resides in memory.) Unlike like the
|
|
385
|
+
callback-based API for reading files, the writer API consists of function that
|
|
386
|
+
the developer must call in a particular order. The complete API can be found in
|
|
387
|
+
[readstat.h](./src/readstat.h).
|
|
388
|
+
|
|
389
|
+
Basic usage:
|
|
390
|
+
|
|
391
|
+
```c
|
|
392
|
+
#include "readstat.h"
|
|
393
|
+
|
|
394
|
+
/* A callback for writing bytes to your file descriptor of choice */
|
|
395
|
+
/* The ctx argument comes from the readstat_begin_writing_xxx function */
|
|
396
|
+
static ssize_t write_bytes(const void *data, size_t len, void *ctx) {
|
|
397
|
+
int fd = *(int *)ctx;
|
|
398
|
+
return write(fd, data, len);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
int main(int argc, char *argv[]) {
|
|
402
|
+
readstat_writer_t *writer = readstat_writer_init();
|
|
403
|
+
readstat_set_data_writer(writer, &write_bytes);
|
|
404
|
+
readstat_writer_set_file_label(writer, "My data set");
|
|
405
|
+
|
|
406
|
+
int row_count = 1;
|
|
407
|
+
|
|
408
|
+
readstat_variable_t *variable = readstat_add_variable(writer, "Var1", READSTAT_TYPE_DOUBLE, 0);
|
|
409
|
+
readstat_variable_set_label(variable, "First variable");
|
|
410
|
+
|
|
411
|
+
/* Call one of:
|
|
412
|
+
* readstat_begin_writing_dta
|
|
413
|
+
* readstat_begin_writing_por
|
|
414
|
+
* readstat_begin_writing_sas7bdat
|
|
415
|
+
* readstat_begin_writing_sav
|
|
416
|
+
* readstat_begin_writing_xport
|
|
417
|
+
*/
|
|
418
|
+
|
|
419
|
+
int fd = open("something.dta", O_CREAT | O_WRONLY);
|
|
420
|
+
readstat_begin_writing_dta(writer, &fd, row_count);
|
|
421
|
+
|
|
422
|
+
int i;
|
|
423
|
+
for (i=0; i<row_count; i++) {
|
|
424
|
+
readstat_begin_row(writer);
|
|
425
|
+
readstat_insert_double_value(writer, variable, 1.0 * i);
|
|
426
|
+
readstat_end_row(writer);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
readstat_end_writing(writer);
|
|
430
|
+
readstat_writer_free(writer);
|
|
431
|
+
close(fd);
|
|
432
|
+
|
|
433
|
+
return 0;
|
|
434
|
+
}
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
Fuzz Testing
|
|
438
|
+
--
|
|
439
|
+
|
|
440
|
+
To assist in fuzz testing, ReadStat ships with target files designed to work
|
|
441
|
+
with [libFuzzer](http://llvm.org/docs/LibFuzzer.html). Clang 6 or later is
|
|
442
|
+
required.
|
|
443
|
+
|
|
444
|
+
1. `./configure --enable-fuzz-testing` turns on useful sanitizer and sanitizer-coverage flags
|
|
445
|
+
1. `make` will create a new binary called `generate_corpus`. Running this
|
|
446
|
+
program will use the ReadStat test suite to create a corpus of test files in
|
|
447
|
+
`corpus/`. There is a subdirectory for each sub-format (`dta104`, `dta105`,
|
|
448
|
+
etc.). Currently a total of 468 files are created.
|
|
449
|
+
1. If fuzz-testing has been enabled, `make` will also create fourteen fuzzer
|
|
450
|
+
targets, one for each of seven file formats, six for internally used
|
|
451
|
+
grammars, and two fuzzers for testing the compression routines.
|
|
452
|
+
* `fuzz_format_dta`
|
|
453
|
+
* `fuzz_format_por`
|
|
454
|
+
* `fuzz_format_sas7bcat`
|
|
455
|
+
* `fuzz_format_sas7bdat`
|
|
456
|
+
* `fuzz_format_sav`
|
|
457
|
+
* `fuzz_format_xport`
|
|
458
|
+
* `fuzz_format_stata_dictionary`
|
|
459
|
+
* `fuzz_grammar_dta_timestamp`
|
|
460
|
+
* `fuzz_grammar_por_double`
|
|
461
|
+
* `fuzz_grammar_sav_date`
|
|
462
|
+
* `fuzz_grammar_sav_time`
|
|
463
|
+
* `fuzz_grammar_spss_format`
|
|
464
|
+
* `fuzz_grammar_xport_format`
|
|
465
|
+
* `fuzz_compression_sas_rle`
|
|
466
|
+
* `fuzz_compression_sav`
|
|
467
|
+
|
|
468
|
+
For best results, each sub-directory of the corpus should be passed to the relevant fuzzer, e.g.:
|
|
469
|
+
|
|
470
|
+
* `./fuzz_format_dta corpus/dta104`
|
|
471
|
+
* `./fuzz_format_dta corpus/dta110`
|
|
472
|
+
* ...
|
|
473
|
+
* `./fuzz_format_sav corpus/sav`
|
|
474
|
+
* `./fuzz_format_sav corpus/zsav`
|
|
475
|
+
* `./fuzz_format_xport corpus/xpt5`
|
|
476
|
+
* `./fuzz_format_xport corpus/xpt8`
|
|
477
|
+
|
|
478
|
+
Finally, the compression fuzzers can be invoked without a corpus:
|
|
479
|
+
|
|
480
|
+
* `./fuzz_compression_sas_rle`
|
|
481
|
+
* `./fuzz_compression_sav`
|
|
482
|
+
|
|
483
|
+
|