# R program to create .csv file from cgiwrite output # This program is indended to be called from the shell file "cgiread". # It creates cgireadlog.tmp with comments. # Shell file "cgiread" writes user's request to "cgireadDBfile.tmp" # Read filename from this file and verify that it exists. if (!file.exists("cgireadDBfile.tmp")) { write("Can't open cgireadDBfile.tmp", file="cgireadlog.tmp") quit(save="no") } filename=scan("cgireadDBfile.tmp", what="", quiet=TRUE) if (!file.exists(filename)) { write(paste("Can't open",filename), file="cgireadlog.tmp") quit(save="no") } # Read data from file (one field per line) lines=scan(filename, sep="\n", strip.white=TRUE, what="", quiet=TRUE) lines=lines[nchar(lines)>0] nl=length(lines) if (nl==0) { write(paste(filename, "is empty."), file="cgireadlog.tmp") quit(save="no") } # Record lines with equal signs (start of field) and positions of equal signs eqloc=as.numeric(regexpr("\=",lines)) while (length(eqloc)>1 && eqloc[1]==-1) { eqloc=eqloc[-1] lines=lines[-1] } nl=length(lines) if (length(eqloc[1])==0) { write(paste(filename,"appears to have the wrong format"), file="cgireadlog.tmp") quit(save="no") } # Assume first observed field name starts all records firstname=substring(lines[1],1,eqloc[1]) startrec=regexpr(paste("^",firstname,sep=""),lines) startrec=seq(along=startrec)[startrec>0] nrec=length(startrec) # Get all field names allnames=unique(substring(lines[eqloc>0],1,eqloc[eqloc>0])) nfields=length(allnames) # Construct matrix of input, concatenating as needed # First drop trailing characters of field names trimnames=allnames for (i in seq(along=trimnames)) { tmp=trimnames[i] j=nchar(tmp) while (j>1 && (substring(tmp,j,j)==" " || substring(tmp,j,j)=="=")) { tmp=substring(tmp,1,j-1) j=j-1 } trimnames[i]=tmp } mat=matrix("", nrec, nfields, dimnames=list(1:nrec, trimnames)) startlines=seq(along=eqloc)[eqloc>0] # named fields start here startlinesX=c(startlines,nl+1) startrecX=c(startrec,nl+1) for (row in seq(along=startrec)) { n1=startlines[startlines>=startrec[row] & startlinesnfields) { write(paste("Bad logic (#2) -- see Howard"), file="cgireadlog.tmp") #quit(save="no") } if (nchar(this1)==e) { tmp="" } else { tmp=substring(this1,1+e) } while (nchar(tmp)>0 && substring(tmp,1,1)==" ") tmp=substring(tmp,2) if (nn[field]>n1[field]) tmp=paste(tmp,paste(lines[(n1[field]+1):(nn[field])],collapse=" "),collapse=" ") mat[row,col]=tmp } } # Get basename for creating new filenames basename=filename i=nc=nchar(filename) while (i>1) { if (substring(filename,i,i)==".") { basename=substring(filename,1,i-1) break } i=i-1 } # Write csv file csvname=paste(basename,".csv",sep="") write.table(data.frame(mat), file=csvname, sep=",", row=FALSE, col=TRUE, quote=TRUE) write(paste("Wrote",csvname), file="cgireadlog.tmp")