#include txt.cam #include seq.cam #include ffs.cam ;N io.cam - a standard i/o CAMILA library ;A J.N. Oliveira (jno@di.uminho.pt), J.J. Almeida (jj@di.uminho.pt) ;D ; This library contains a few i/o functions, including reading from UNIX pipes. ; This includes output functions for the (A->B)-set functor in ffs.cam to the particular case where both A and B are atomic, that is, A = B = Atom, where Atom = STR | INT | SYM | NAT. ; Last Update: 1999.11.11 ;E FUNC ioReadPipe(com:STR,opt:STR): A-seq ; ; ioReadPipe(com,opt) opens, reads and closes a Unix pipe created by process ; "com", the output of which is passed through the 'any2sexp' (Perl) filter. ; See the documentation of 'any2sexp' for the options string you may add ; to the command. In many situations str="" will do, eg. ; ioReadPipe("ls *.cam",""). ; RETURN let (a = popen(com ++ " | any2sexp " ++ opt , "r"), b = readf(a), = pclose(a)) in b; FUNC ioAwk2afs(fn:STR,fs:STR):(Atom->Atom)-set ; ; ioAwk2afs(fn,fs) - read "afs from awk-like file" - reads file fn which is ; supposed to be in awk-like format, where RS = newline and FS = fs ; and converts it to an STR-afs. ; RETURN let (s=elems(ioReadPipe("cat " ++ fn , " -type tab -fs " ++ fs ))) in seq2ff-set(s); FUNC ioAfs2awk(fn:STR,s:(NAT->Atom)-set,fs:STR):SYM ; ; ioAfs2awk(fn,s,fs) is the converse of ioAwk2afs(fn,fs), ; where s is an NAT->Atom finite mapping set to be stored in file fn ; in awk-like format such that FS = fs. ; The domain of every entry in s should be an initial segment. ; Otherwise, "holes" will be filled in by the empty string. ; RETURN let (f=lambda(l).seqAddSep(l,fs) ^ <"\n">, t=< ff2seq("",x) | x <- s>) in ioTxt2File(fn,f-seq(t)); FUNC ioTxtPrint(t:txt):SYM ; ; ioTxtPrint(t) writes txt object t to the standard output. ; RETURN let (p=txtFlat(t)^<"\n">) in foreach(x,p,if x=="" then princ(".") else fputs(x)); FUNC ioTxt2File(fn:STR,t:txt):SYM ; ; ioTxt2File(fn,t) writes txt object t into file fn. ; RETURN let (p=txtFlat(t)) in do(sh(strcat("rm ",fn)), _f<- fopen(fn,"w"), foreach(x,p,if x=="" then princ(".") else fputs(x,_f)), fclose(_f), sh(strcat("ls -l ",fn)), ); FUNC ioAfsPrint(s:(Atom->Atom)-set):SYM ; ; ioAfsPrint(s) sends afs object to the standard output, after a ffs2txt ; conversion. ; RETURN ioTxtPrint(ffs2txt(s)); FUNC ioObjSave(n:SYM,e:ANY):SYM ; ; ioObjSave(n,e) performs a textual save of any expression 'e' under name 'n' ; into file "n". ; RETURN let (fn=symstr(n)) in do( _f<-fopen(fn,"w"), fputs(fn ++ " <- ",_f), nwrite(_f,e), fputs(";\n",_f), fclose(_f), sh(strcat("ls -l ",fn)) ); FUNC ioObjLoad(n:SYM):SYM ; ; ioObjLoad(n) loads object 'n' from file "n" where it was previously ; saved by ioObjSave. ; RETURN let (fn=symstr(n)) in nload(fn); bsh<-"\\";