Debian Codegraph @

R/import-internal-spss-syntax.R

raw
1 rofile <- function(filename) .Call("rofile",as.character(filename))
2
3 readfixed <- function(file,what,nlines,start,stop)
4 .Call("readfixed",file,what,nlines,start,stop)
5
6 readfixedslice <- function(file,what,j,i,start,stop)
7 .Call("readfixedslice",file,what,j,i,start,stop)
8
9 readfixedchunk <- function(file,what,j,n,start,stop)
10 .Call("readfixedchunk",file,what,j,n,start,stop)
11
12 roreadline <- function(file)
13 .Call("rofreadline",file)
14
15 # print.rofile <- function(x,...)
16 # cat("Read-only file",dQuote(attr(x,"filename")),"\n")
17
18 roftell <- function(f) .Call("roftell",f)
19 rofseek <- function(f,pos=0,whence=0) .Call("rofseek",f,pos=pos,whence=whence)
20
21 spss.parse.data.spec <- function(file){
22
23 text <- paste(readLines(file,n=-1,warn=FALSE),collapse=" ")
24
25 keyword <- .pattern("data\\s+list\\s+",text,ignore.case=TRUE)
26 if(!length(keyword)) return(NULL)
27
28 header <- .pattern("/",text,before=TRUE,ignore.case=TRUE)
29 text <- .remainder(header)
30
31 skip <- .pattern("skip\\s*=\\s*[0-9]+",header,ignore.case = TRUE)
32 if(!length(skip)) skip <- 0L
33 else {
34
35 skip <- .pattern("[0-9]+",skip)
36 skip <- as.integer(skip)
37 }
38
39 varnames <- character()
40 start <- integer()
41 end <- integer()
42 types <- integer()
43
44 repeat {
45
46 if(grepl("^[ \t\r\n]*[.]",text)){
47 #message("found closing dot")
48 break
49 }
50 vn <- .getvarnames(text)
51 if(!length(vn)) {
52 break
53 }
54 text <- .remainder(vn)
55
56 start1 <- .pattern("^[0-9]+",text)
57 text <- .remainder(start1)
58 start1 <- as.integer(start1)
59 text <- .remainder(.pattern("^\\s*-\\s*",text))
60 end1 <- .pattern("^[0-9]+",text)
61 text <- .remainder(end1)
62 end1 <- as.integer(end1)
63 type1 <- .pattern("^[(][aA0-9]+[])]",text)
64 if(!length(type1))
65 type1 <- 1
66 else {
67 text <- .remainder(type1)
68 type1 <- .pattern("[aA]",type1)
69 if(length(type1))
70 type1 <- 2
71 else
72 type1 <- 1
73 }
74
75 varnames <- c(varnames,vn)
76 types <- c(types,type1)
77 start <- c(start,start1)
78 end <- c(end,end1)
79 }
80 names(types) <- varnames
81 list(
82 types=types,
83 start=start,
84 stop=end,
85 skip=skip
86 )
87 }
88
89 spss.parse.variable.labels <- function(file,iconv=TRUE,
90 encoded=getOption("spss.syntax.encoding","cp1252")){
91 text <- paste(readLines(file,n=-1,warn=FALSE),collapse=" ")
92 if(iconv)
93 text <- iconv(text,from=encoded,to="")
94 text <- gsub("\"\"","'",text)
95 text <- gsub("''","\"",text)
96 res <- character()
97
98 repeat{
99 keyword <- .pattern("variable\\s+labels\\s+",text,ignore.case=TRUE)
100 if(!length(keyword)) break
101 text <- .remainder(keyword)
102 vl <- spss.parse1.variable.labels(text)
103 text <- .remainder(vl)
104 .remainder(vl) <- NULL
105 res <- c(res,vl)
106 }
107 res
108 }
109
110 spss.parse1.variable.labels <- function(text){
111
112
113 names <- character()
114 labels <- character()
115
116 # Remove leading slash stuff
117 text <- sub("^\\s*/\\s*","",text)
118 repeat {
119 if(grepl("^[ \t\r\n]*[.]",text)){
120 #message("found closing dot")
121 break
122 }
123 varnames <- .getvarnames(text)
124 text <- .remainder(varnames)
125 lab <- .getquoted(text)
126 text <- .remainder(lab)
127
128 names <- c(names,varnames)
129 labels <- c(labels,lab)
130 #res <- c(res,list(list(varnames=varnames,vallabs=vallabs)))
131 if(nchar(text)<1) break
132 }
133
134 names(labels) <- names
135 structure(labels,
136 remainder=text)
137 }
138
139 spss.parse.value.labels <- function(file,iconv=TRUE,
140 encoded=getOption("spss.syntax.encoding","cp1252")){
141 text <- paste(readLines(file,n=-1,warn=FALSE),collapse=" ")
142 if(iconv)
143 text <- iconv(text,from=encoded,to="")
144 text <- gsub("\"\"","'",text)
145 text <- gsub("''","\"",text)
146 res <- list()
147 repeat{
148 keyword <- .pattern("value\\s+labels\\s+",text,ignore.case=TRUE)
149 if(!length(keyword)) break
150 text <- .remainder(keyword)
151 vl <- spss.parse1.value.labels(text)
152 text <- .remainder(vl)
153 .remainder(vl) <- NULL
154 res <- c(res,vl)
155 }
156 res
157 }
158
159 spss.parse1.value.labels <- function(text){
160
161 res <- list()
162 repeat {
163
164 if(grepl("^[ \t\r\n]*[.]",text)){
165 #message("found closing dot")
166 break
167 }
168 varnames <- .getvarnames(text)
169 text <- .remainder(varnames)
170 .remainder(varnames) <- NULL
171 vallabs <- .getvallabs(text)
172 text <- .remainder(vallabs)
173 labs <- vallabs$labels
174 vallabs <- vallabs$values
175 names(vallabs) <- labs
176
177 vallabs <- rep(list(vallabs),length(varnames))
178 names(vallabs) <- varnames
179 res <- c(res,vallabs)
180
181 if(nchar(text)<1) break
182 }
183 structure(res,
184 remainder=text)
185 }
186
187
188 spss.parse.missing.values <- function(file){
189
190 text <- paste(readLines(file,n=-1,warn=FALSE),collapse=" ")
191 res <- list()
192 repeat{
193
194 keyword <- .pattern("missing\\s+values\\s+",text,ignore.case=TRUE)
195 if(!length(keyword)) break
196 text <- .remainder(keyword)
197 mv <- spss.parse1.missing.values(text)
198 text <- .remainder(mv)
199 .remainder(mv) <- NULL
200 res <- c(res,mv)
201 }
202 res
203 }
204
205 spss.parse1.missing.values <- function(text){
206
207 missvals <- list()
208
209 repeat{
210
211 if(grepl("^[ \t\r\n]*[.]",text)){
212 #message("found closing dot")
213 break
214 }
215 vn <- .getvarnames(text)
216 text <- .remainder(vn)
217 if(!length(vn)) break
218
219 mvspec <- .pattern("^[(].*?[)]",text)
220 if(!length(mvspec)) break
221 text <- .remainder(mvspec)
222
223 mvspec <- gsub("[()]","",mvspec)
224 .remainder(mvspec) <- NULL
225
226 if(grepl("thru",mvspec,ignore.case=TRUE)){
227 lo <- .pattern("thru",mvspec,before=TRUE,ignore.case=TRUE)
228 hi <- .remainder(lo)
229 suppressWarnings(lo <- as.numeric(lo))
230 if(!is.finite(lo)) lo <- -Inf
231
232 if(grepl(",",hi)){
233 hi <- .pattern(",",hi,before=TRUE)
234 vals <- .remainder(hi)
235 vals <- as.numeric(vals)
236 }
237 else vals <- NULL
238 suppressWarnings(hi <- as.numeric(hi))
239 if(!is.finite(hi)) hi <- Inf
240 range <- c(lo,hi)
241 }
242 else if(!grepl("\"",mvspec)) {
243 range <- NULL
244 vals <- numeric()
245 for(ii in 1:3){
246 v <- .pattern(",",mvspec,before=TRUE)
247 mvspec <- .remainder(v)
248 vals <- c(vals, as.numeric(v))
249 if(!length(mvspec)) break
250 }
251 }
252 else {
253 range <- NULL
254 vals <- character()
255 for(ii in 1:3){
256 v <- .pattern(",",mvspec,before=TRUE)
257 mvspec <- .remainder(v)
258 vals <- c(vals, gsub("\"","",v))
259 if(!length(mvspec)) break
260 }
261 }
262 mv <- list(values=vals,range=range)
263
264 mv <- rep(list(mv),length(vn))
265 names(mv) <- vn
266
267 missvals <- c(missvals,mv)
268 }
269 return(structure(missvals,
270 remainder=text))
271 }
272
273
274 .getvarnames <- function(text){
275
276 varnames <- character(0)
277 text <- sub("^\\s*","",text)
278 text <- sub("^[/;,]\\s*","",text)
279
280 repeat {
281 vn.pos <- regexpr("^[A-Za-z][A-Za-z0-9_]*",text)
282 if(vn.pos < 0) return(structure(varnames,remainder=text))
283 vn.length <- attr(vn.pos,"match.length")
284 vn <- substring(text,first=1,last=vn.length)
285 varnames <- c(varnames,vn)
286 text <- substring(text,first=vn.length+1,last=nchar(text))
287 text <- trimws(text,right=FALSE)
288 text <- gsub("^[;,][ \t\n\r]*","",text)
289 }
290 }
291
292 .getvallabs <- function(text){
293 text <- trimws(text,right=FALSE)
294 if(grepl("^[\"']",text)) .getvallabs1(text,strings=TRUE)
295 else .getvallabs1(text)
296 }
297
298 .getvallabs1 <- function(text,strings=FALSE){
299
300 if(strings)
301 vals <- character(0)
302 else
303 vals <- numeric(0)
304 labs <- character(0)
305 repeat{
306 text <- trimws(text,right=FALSE)
307 if(grepl("^[/|.|;]",text)) return(structure(
308 list(values=vals,labels=labs),
309 remainder=text))
310
311 if(strings){
312 val <- .getquoted(text)
313 text <- .remainder(val)
314 }
315 else{
316 val <- .getnotquoted(text)
317 text <- .remainder(val)
318 val <- as.numeric(val)
319 }
320 .remainder(val) <- NULL
321 lab <- .getquoted(text)
322 text <- .remainder(lab)
323 .remainder(lab) <- NULL
324
325 vals <- c(vals,val)
326 labs <- c(labs,lab)
327 }
328 }
329
330 .getnotquoted <- function(text){
331 text <- trimws(text,right=FALSE)
332 if(!nzchar(text)) return(NULL)
333 tok.pos <- regexpr("[^ \t\r\n]+",text)
334 tok.len <- attr(tok.pos,"match.length")
335 tok <- substring(text,first=1,last=tok.len)
336 text <- substring(text,first=tok.len+1,last=nchar(text))
337 structure(tok,remainder=text)
338 }
339
340 .getquoted <- function(text){
341 text <- trimws(text,right=FALSE)
342 if(!nzchar(text)) stop("empty string")
343 if(!grepl("^[\"']",text)) stop("missing quotation mark")
344 qm <- substring(text,first=1,last=1)
345 text <- substring(text,first=2)
346 qm.pos <- regexpr(qm,text)
347 tok <- substring(text,first=1,last=qm.pos-1)
348 text <- substring(text,first=qm.pos+1,last=nchar(text))
349 structure(tok,remainder=text)
350 }
351
352 .pattern <- function(pattern,text,before=FALSE,...){
353 attributes(text) <- NULL
354 pat.pos <- regexpr(pattern,text,...)
355 if(pat.pos < 1) {
356 if(before) return(text)
357 else return(NULL)
358 }
359 pat.len <- attr(pat.pos,"match.len")
360 if(before)
361 pat.found <- substring(text,first=1,last=pat.pos-1)
362 else
363 pat.found <- substring(text,first=pat.pos,last=pat.pos+pat.len-1)
364 text <- substring(text,first=pat.pos+pat.len,last=nchar(text))
365 structure(pat.found,remainder=text)
366 }
367
368 .remainder <- function(x){
369 y <- attr(x,"remainder")
370 attributes(y) <- NULL
371 if(length(y)) sub('^[ \t\r\n]+', '',y)
372 else y
373 }
374
375 ".remainder<-" <- function(x,value){
376 attr(x,"remainder") <- value
377 x
378 }