â Quad_FIO.tc â ---------------------------------- â same as File_IO_1.tc but using FILE_IO.apl library â This is the recommeded way to use file_io.so â â This file demonstrates the use of the FILE_IO.apl library â and checks that the wrapper functions in FILE_IO.apl are working â âââ âââ If this segfaults then re-install GNU APL !!! âââ )COPY 5 FILE_IO.apl DUMPED ³ â fopen this file readonly â Filenameâ†'testcases/File_IO_1.tc' ⎕â†Handleâ†FIO∆fopen_ro Filename â° â get statistics â ⎕â†FIO∆fstat Handle â° â° â° â° â° â° â° â° â° â° â° â° â° â close this file â ⎕â†FIO∆fclose Handle 0 â close this file again (which is an error) â FIO∆fclose Handle DOMAIN ERROR+ FIO∆fclose[2] Zeâ†âŽ•FIO[4]Bh ^ ^ → â create new file â Filename↠"FILE_IO.test" ⎕â†Handleâ†'w' FIO∆fopen Filename â° FIO∆errno 0 FIO∆strerror FIO∆errno Success â write 3 lines â ⎕â†(⎕UCS "Hello\n") FIO∆fwrite Handle 6 ⎕â†(⎕UCS "World\n") FIO∆fwrite Handle 6 ⎕â†(⎕UCS "Line 3...\n") FIO∆fwrite Handle 10 â close the file â FIO∆fclose Handle 0 â print the file â )HOST cat FILE_IO.test Hello World Line 3... 0 â open file again for reading â ⎕â†Handle↠FIO∆fopen_ro Filename â° â read one line (default max_len) â Z↠FIO∆fgets Handle ⎕UCS Z Hello â read another line (max_len 200) â Zâ†200 FIO∆fgets Handle ⎕UCS Z World â´Z 6 â end of file ? (no) â FIO∆feof Handle 0 â read more bytes â Zâ†200 FIO∆fread Handle â´Z 10 â end of file ? (yes) â FIO∆feof Handle 1 â get statistics â ⎕â†FSTATâ†FIO∆fstat Handle â—Š 0 8 8 8 ⊤ FSTAT[3] â° â° â° 1 â° â° 0 22 â° â° â° â° â° 64 6 â° â° â close the file â FIO∆fclose Handle 0 â delete the file â FIO∆unlink Filename 0 â delete the file again (should fail) â Errorâ†-FIO∆unlink Filename FIO∆strerror Error No such file or directory ⎕FIO['strerror'] Error â monadic with axis string No such file or directory â printf â )ERASE FORMAT FORMATâ†âŠ‚"⎕everything %d Pi %e String %s\n" FIO∆printf FORMAT, 42 (â—‹1) 'Hello' 44 â fwrite with UCS in UTF8 out â Filename↠'FILE_IO.test1' ⎕â†Handleâ†'w' FIO∆fopen Filename â° 'HELLO ⋠⌽ â’ â´ âµ' FIO∆fwrite_utf8 Handle 25 FIO∆fclose Handle â close file 0 )HOST cat FILE_IO.test1 HELLO ⋠⌽ â’ â´ âµ 0 â popen "r" â Commandâ†'ls FILE_IO*' Handle↠FIO∆popen_ro Command Zâ†20000 FIO∆fread Handle â´Z â° FIO∆pclose Handle â close command (returning its exit code) 0 â read entire file â ⎕UCS FIO∆read_file 'FILE_IO.test1' 72 69 76 76 79 32 226 141 139 32 226 140 189 32 226 141 146 32 226 141 180 32 226 141 181 â socket communication: send 'Hello' from one socket to another socket â and send 'World!' back. â AF_INET ↠2 â see /usr/include/.../bits/socket.h SOCK_STREAM ↠1 â see /usr/include/.../bits/socket_type.h IPPROTO_IP ↠0 â see /usr/include/netinet/in.h LOCALHOSTâ†256⊥127 0 0 1 â 127.0.0.1 in host byte order Port ↠22222 Serverâ†AF_INET, LOCALHOST, Port â socket(AF_INET, SOCK_STREAM, IPPROTO_IP) â 3 ≤ Handleâ†FIO∆socket AF_INET, SOCK_STREAM, IPPROTO_IP 1 â setsockopt(Handle, SOL_SOCKET, SO_REUSEADDR, 1) â SOL_SOCKETâ†1 â see /usr/include/asm-generic/socket.h SO_REUSEADDRâ†2 â see /usr/include/asm-generic/socket.h (SOL_SOCKET, SO_REUSEADDR, 1) FIO∆setsockopt Handle 0 â bind(AF_INET, LOCALHOST, Port) â Server FIO∆bind Handle 0 â listen(10) â 10 FIO∆listen Handle 0 â socket(AF_INET, SOCK_STREAM, IPPROTO_IP) â 4 ≤ Handle1â†FIO∆socket AF_INET, SOCK_STREAM, IPPROTO_IP 1 â connect() Handle1 to Handle â Server FIO∆connect Handle1 0 â accept() returns Handle, remote address â Handle2 ↠↑ FIO∆accept Handle â send("Hello") â 'Hello' FIO∆send_utf8 Handle2 5 â recv() â ⎕UCS 200 FIO∆recv Handle1 Hello â send("World!") â 'World!' FIO∆send_utf8 Handle1 6 â recv() â ⎕UCS 200 FIO∆recv Handle2 World! ⎕â†FIO∆fclose Handle2 0 ⎕â†FIO∆fclose Handle1 0 ⎕â†FIO∆fclose Handle 0 â fscanf â Filename↠'FILE_IO.test1' ⎕â†Handleâ†'w' FIO∆fopen Filename â° â´DATAâ†'int 22 perc % doub 3.14 char C chars DEFGH qqq str stâ´ing' 57 FMTâ†'int %d perc %% doub %f char %c chars %5c qqq str %s %n' DATA FIO∆fwrite_utf8 Handle 59 FIO∆fclose Handle â close file 0 ⎕â†Handleâ†'r' FIO∆fopen Filename â° FMT FIO∆fscanf Handle 22 3.14 C DEFGH stâ´ing 59 FIO∆fclose Handle â close file 0 â sscanf â â´DATAâ†'int 22 perc % doub 3.14 char C chars DEFGH qqq str stâ´ing' 57 FMTâ†'int %d perc %% doub %f char %c chars %5c qqq str %s %n' FMT FIO∆sscanf DATA 22 3.14 C DEFGH stâ´ing 57 FMT ⎕FIO['sscanf'] DATA â dyadic with axis string 22 3.14 C DEFGH stâ´ing 57 â execve â âFilenameâ†'/usr/local/lib/apl/TLV_server' â whereever TLV_server lives Filenameâ†'../tools/TLV_server' Handle ↠FIO∆execve Filename â start & connect TLV_server TLV ↠33 ⎕CR 42,'Forty-Two' â encode a TLV buffer, Tag 42 ⊣TLV ⎕FIO[43] Handle â send TLV buffer to TLV_server TL ↠8 ⎕FIO[6] Handle â read tag/lenght from TLV_server Value ↠(256⊥4↓TL) ⎕FIO[6] Handle â read value from TLV_server 34 ⎕CR TL,Value â display response tag and value ¯42 owT-ytroF TLV ↠33 ⎕CR 99,'quit' â encode stop command, Tag 99 ⊣TLV ⎕FIO[43] Handle â send TLV buffer to TLV_server ⊣(⎕FIO[4] Handle) â close connection (stops TLV_server) FIO∆mkdir_777 FIO∆getcwd,'/test_data' 0 DATAâ†'Kiss,n. A word invented by poets as a rhyme for bliss.' DATA FIO∆pipeto 'wc>test_data/wc.txt' 0 FIO∆read_file 'test_data/wc.txt' 0 11 54 FIO∆pipefrom 'ls test_data' wc.txt FIO∆unlink 'test_data/wc.txt' 0 FIO∆rmdir 'test_data' 0 )ERASE FORMAT Handle Handle1 Handle2 Command AF_INET SOCK_STREAM )ERASE Error FSTAT Filename LOCALHOST Port Z Server IPPROTO_IP )ERASE FMT DATA TL TLV Value â )VARS â ==================================