( load "txt.met" ) ;--------------------- ( load "seq.met" ) ;--------------------- ( load "ffs.met" ) ;--------------------- ;N io.cam - a standard i/o CAMILA library ;A J.N. Oliveira, J.J. Almeida (jno,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 = STR | INT | SYM. ;E ; ; 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. ; ( def _ops ( plus _ops ( makeff ( ( quote ioAwk2afs ) ( quote ( ( ( STR ) ( STR ) ) ( SET FF At At ) ) ) ) ) ) ) ;--------------------- ( def ioAwk2afs lambda ( fn fs ) ( let ( ( s ( elems ( ioReadPipe ( strcat "cat " fn ) ( strcat " -type tab -fs " fs ) ) ) ) ) ( ( lambda ( _y_ ) ( set ( seq2ff _x_ ) ( from _x_ _y_ ) ) ) s ) ) ) ;--------------------- ; ; ioAfs2awk(fn,s,fs) is the converse of ioAwk2afs(fn,fs), ; wwhere s is an INT to 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. ; ( def _ops ( plus _ops ( makeff ( ( quote ioAfs2awk ) ( quote ( ( ( STR ) ( SET FF INT At ) ( STR ) ) ( SYM ) ) ) ) ) ) ) ;--------------------- ( def ioAfs2awk lambda ( fn s fs ) ( let ( ( f ( lambda ( l ) ( append ( seqAddSep l fs ) ( makeseq "\n" ) ) ) ) ( t ( seq ( ff2seq x "" ) ( from x s ) ) ) ) ( ioTxt2File fn ( ( lambda ( _y_ ) ( seq ( f _x_ ) ( from _x_ _y_ ) ) ) t ) ) ) ) ;--------------------- ; ; ioTxtPrint(t) writes txt object t to the standard output. ; ( def _ops ( plus _ops ( makeff ( ( quote ioTxtPrint ) ( quote ( ( ( txt ) ) ( SYM ) ) ) ) ) ) ) ;--------------------- ( def ioTxtPrint lambda ( t ) ( let ( ( p ( append ( txtFlat t ) ( makeseq "\n" ) ) ) ) ( foreach x p ( if ( equal x "" ) ( princ "." ) ( fputs x ) ) ) ) ) ;--------------------- ; ; ioTxt2File(fn,t) writes txt object t into file fn. ; ( def _ops ( plus _ops ( makeff ( ( quote ioTxt2File ) ( quote ( ( ( STR ) ( txt ) ) ( SYM ) ) ) ) ) ) ) ;--------------------- ( def ioTxt2File lambda ( fn t ) ( let ( ( p ( txtFlat t ) ) ) ( progn ( sh ( strcat "rm " fn ) ) ( def _f ( fopen fn "w" ) ) ( foreach x p ( if ( equal x "" ) ( princ "." ) ( fputs x _f ) ) ) ( fclose _f ) ( sh ( strcat "ls -l " fn ) ) ) ) ) ;--------------------- ; ; ioAfsPrint(s) sends afs object to the standard output, after a ffs2txt ; conversion. ; ( def _ops ( plus _ops ( makeff ( ( quote ioAfsPrint ) ( quote ( ( ( SET FF At At ) ) ( SYM ) ) ) ) ) ) ) ;--------------------- ( def ioAfsPrint lambda ( s ) ( ioTxtPrint ( ffs2txt s ) ) ) ;--------------------- ; ; 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",""). ; ( def _ops ( plus _ops ( makeff ( ( quote ioReadPipe ) ( quote ( ( ( STR ) ( STR ) ) ( LIST A ) ) ) ) ) ) ) ;--------------------- ( def ioReadPipe lambda ( com opt ) ( let ( ( a ( popen ( strcat ( strcat com " | any2sexp " ) opt ) "r" ) ) ( b ( readf a ) ) ( __ ( pclose a ) ) ) b ) ) ;--------------------- ; ; ioObjSave(n,e) performs a textual save of any expression 'e' under name 'n' ; into file "n". ; ( def _ops ( plus _ops ( makeff ( ( quote ioObjSave ) ( quote ( ( ( SYM ) ( ANY ) ) ( SYM ) ) ) ) ) ) ) ;--------------------- ( def ioObjSave lambda ( n e ) ( let ( ( fn ( symstr n ) ) ) ( progn ( def _f ( fopen fn "w" ) ) ( fputs ( strcat fn " <- " ) _f ) ( nwrite _f e ) ( fputs ";\n" _f ) ( fclose _f ) ( sh ( strcat "ls -l " fn ) ) ) ) ) ;--------------------- ; ; ioObjLoad(n) loads object 'n' from file "n" where it was previously ; saved by ioObjSave. ; ( def _ops ( plus _ops ( makeff ( ( quote ioObjLoad ) ( quote ( ( ( SYM ) ) ( SYM ) ) ) ) ) ) ) ;--------------------- ( def ioObjLoad lambda ( n ) ( let ( ( fn ( symstr n ) ) ) ( nload fn ) ) ) ;--------------------- ( def bsh "\\" ) ;---------------------