FILE_IO.apl
I've corrected a bug in FILE_IO.apl and written a unit test workspace, FILE_test.apl both of which I'd like to contribute to gnu-apl. FILE_test.apl needs to be expanded to rest all the functions in FILE_IO and I'll work on this. Help would be appreciated. FILE_test.apl wants to create a directory, ls_test and populate it with test data. It is dependent on apl-libary. Use the function test∆FIO∆create_test_data for this purpose. Try function test∆FIO to run tests. w #!/usr/local/bin/apl --script -- ⍝ ⍝ ⎕FIO.apl 2014-07-29 15:40:42 (GMT+2) ⍝ ⍝ Copyright (C) 2008-2019 Dr. Jürgen Sauermann ⍝ Copyright (C) 2017 Christian Robert ⍝ Copyright (C) 2020 Bill Daly ⍝ This library contains APL wrapper functions the system function ⎕FIO ⍝ ⍝ The purpose is to give functions for file I/O meaningful names instead ⍝ of difficult-to-remember numbers. The function name is normally the name ⍝ of the C-function that is called by the native function. ⍝ ⍝ Legend: d - table of dirent structs ⍝ e - error code ⍝ i - integer ⍝ h - file handle (integer) ⍝ n - names (nested vector of strings) ⍝ s - string ⍝ A1, A2, ... nested vector with elements A1, A2, ... ∇Zi ← FIO∆errno ⍝⍝ errno (of last call) Zi ← ⎕FIO[1] '' ∇ ∇Zs ← FIO∆strerror Be ⍝⍝ strerror(Be) Zs ← ⎕FIO[2] Be ∇ ∇Zh ← As FIO∆fopen Bs ⍝⍝ fopen(Bs, As) filename Bs Zh ← As ⎕FIO[3] Bs ∇ ∇Zh ← FIO∆fopen_ro Bs ⍝⍝ fopen(Bs, "r") filename Bs Zh ← ⎕FIO[3] Bs ∇ ∇Ze ← FIO∆fclose Bh ⍝⍝ fclose(Bh) Ze ← ⎕FIO[4] Bh ∇ ∇Ze ← FIO∆file_errno Bh ⍝⍝ errno (of last call on Bh) Ze ← ⎕FIO[5] Bh ∇ ∇Zi ← Ai FIO∆fread Bh ⍝⍝ fread(Zi, 1, Ai, Bh) 1 byte per Zi ⍝⍝ read (at most) 5000 items if Ai is not defined →(0≠⎕NC 'Ai')/↑⎕LC+1 ◊ Ai←5000 Zi ← Ai ⎕FIO[ 6] Bh ∇ ∇Zi ← Ai FIO∆fwrite Bh ⍝⍝ fwrite(Ai, 1, ⍴Ai, Bh) 1 byte per Ai Zi ← Ai ⎕FIO[7] Bh ∇ ∇Zi ← Ai FIO∆fgets Bh ⍝⍝ fgets(Zi, Ai, Bh) 1 byte per Zi ⍝⍝ read (at most) 5000 items unless Ai is defined →(0≠⎕NC 'Ai')/↑⎕LC+1 ◊ Ai←5000 Zi ← Ai ⎕FIO[8] Bh ∇ ∇Zi ← FIO∆fgetc Bh ⍝⍝ fgetc(Zi, Bh) 1 byte per Zi Zi ← ⎕FIO[9] Bh ∇ ∇Zi ← FIO∆feof Bh ⍝⍝ feof(Bh) Zi ← ⎕FIO[10] Bh ∇ ∇Zi ← FIO∆ferror Bh ⍝⍝ ferror(Bh) Zi ← ⎕FIO[11] Bh ∇ ∇Zi ← FIO∆ftell Bh ⍝⍝ ftell(Bh) Zi ← ⎕FIO[12] Bh ∇ ∇Zi ← Ai FIO∆fseek_cur Bh ⍝⍝ fseek(Bh, Ai, SEEK_SET) Zi ← Ai ⎕FIO[13] Bh ∇ ∇Zi ← Ai FIO∆fseek_set Bh ⍝⍝ fseek(Bh, Ai, SEEK_CUR) Zi ← Ai ⎕FIO[14] Bh ∇ ∇Zi ← Ai FIO∆fseek_end Bh ⍝⍝ fseek(Bh, Ai, SEEK_END) Zi ← Ai ⎕FIO[15] Bh ∇ ∇Zi ← FIO∆fflush Bh ⍝⍝ fflush(Bh) Zi ← ⎕FIO[16] Bh ∇ ∇Zi ← FIO∆fsync Bh ⍝⍝ fsync(Bh) Zi ← ⎕FIO[17] Bh ∇ ∇Zi ← FIO∆fstat Bh ⍝⍝ fstat(Bh) Zi ← ⎕FIO[18] Bh ∇ ∇Zi ← FIO∆unlink Bh ⍝⍝ unlink(Bc) Zi ← ⎕FIO[19] Bh ∇ ∇Zi ← FIO∆mkdir_777 Bh ⍝⍝ mkdir(Bc, 0777) Zi ← ⎕FIO[20] Bh ∇ ∇Zi ← Ai FIO∆mkdir Bh ⍝⍝ mkdir(Bc, Ai) Zi ← Ai ⎕FIO[20] Bh ∇ ∇Zi ← FIO∆rmdir Bh ⍝⍝ rmdir(Bc) Zi ← ⎕FIO[21] Bh ∇ ∇Zi ← FIO∆printf B ⍝⍝ printf(B1, B2...) format B1 Zi ← B ⎕FIO[22] 1 ∇ ∇Zi ← FIO∆fprintf_stderr B ⍝⍝ fprintf(stderr, B1, B2...) format B1 Zi ← B ⎕FIO[22] 2 ∇ ∇Zi ← Ah FIO∆fprintf B ⍝⍝ fprintf(Ah, Bf, B2...) format Bf Zi ← B ⎕FIO[22] Ah ∇ ∇Zi ← Ac FIO∆fwrite_utf8 Bh ⍝⍝ fwrite(Ac, 1, ⍴Ac, Bh) Unicode Ac Output UTF-8 Zi ← Ac ⎕FIO[23] Bh ∇ ∇Zh ← As FIO∆popen Bs ⍝⍝ popen(Bs, As) command Bs mode As Zh ← As ⎕FIO[24] Bs ∇ ∇Zh ← FIO∆popen_ro Bs ⍝⍝ popen(Bs, "r") command Bs Zh ← ⎕FIO[24] Bs ∇ ∇Ze ← FIO∆pclose Bh ⍝⍝ pclose(Bh) Ze ← ⎕FIO[25] Bh ∇ ∇Ze←txt FIO∆pipeto cmd;ph ⍝⍝ Function pipes txt to shell command cmd ph←'w' FIO∆popen cmd Ze←txt ⎕fio[43] ph Ze←⎕fio[25] ph ∇ ∇txt←FIO∆pipefrom cmd;ph;Ze ⍝⍝ Function to pipe text from a shell command. Generally the function ⍝⍝ returns the stdout of the command. If error occur it returns an ⍝⍝ integer FIO∆strerror will give a hint of what went wrong. ph←'r' ⎕FIO[24] cmd txt←⍬ st: →(0=⍴sink←5000 ⎕fio[41] ph)/end txt←txt,sink →st end: txt←⎕ucs txt Ze←⎕fio[25] ph ⍎(0≠Ze)/'txt←Ze' ∇ ∇Zs ← FIO∆read_file Bs ⍝⍝ return entire file Bs as byte vector Zs ← ⎕FIO[26] Bs ∇ ∇Zs ← As FIO∆rename Bs ⍝⍝ rename file As to Bs Zs ← As ⎕FIO[27] Bs ∇ ∇Zd ← FIO∆read_directory Bs ⍝⍝ return content of directory Bs Zd ← ⎕FIO[28] Bs ∇ ∇Zn ← FIO∆files_in_directory Bs ⍝⍝ return file names in directory Bs Zn ← ⎕FIO[29] Bs ∇ ∇Zs ← FIO∆getcwd ⍝⍝ getcwd() Zs ← ⎕FIO 30 ∇ ∇Zn ← As FIO∆access Bs ⍝⍝ access(As, Bs) As ∈ 'RWXF' Zn ← As ⎕FIO[31] Bs ∇ ∇Zh ← FIO∆socket Bi ⍝⍝ socket(Bi). Bi is domain, type, protocol, e.g. ⍝⍝ Zh ← FIO∆socket 2 1 0 for an IPv4 TCP socket Zh ← ⎕FIO[32] Bi ∇ ∇Zi ← Ai FIO∆bind Bh ⍝⍝ bind(Bh, Ai). Ai is domain, local IPv4-address, local port, e.g. ⍝⍝ 2 (256⊥127 0 0 1) 80 bind Bh binds socket Bh to port 80 on ⍝⍝ localhost 127.0.0.1 (web server) Zi ← Ai ⎕FIO[33] Bh ∇ ∇Zi ← Ai FIO∆listen Bh ⍝⍝ listen(
Re: FILE_IO.apl
Hi Bill, thank you very much for your contribution. I have included it in SVN 1221. Best Regards, Jürgen Sauermann On 1/8/20 2:53 PM, Bill Daly wrote: I'd like to contribute my changes to FILE_IO.apl to the GNU APL project. A revised file, with my changes is attached. Bill Daly
FILE_IO.apl
I'd like to contribute my changes to FILE_IO.apl to the GNU APL project. A revised file, with my changes is attached. Bill Daly #!/usr/local/bin/apl --script -- ⍝ ⍝ ⎕FIO.apl 2014-07-29 15:40:42 (GMT+2) ⍝ ⍝ Copyright (C) 2008-2019 Dr. Jürgen Sauermann ⍝ Copyright (C) 2017 Christian Robert ⍝ Copyright (C) 2020 Bill Daly ⍝ This library contains APL wrapper functions the system function ⎕FIO ⍝ ⍝ The purpose is to give functions for file I/O meaningful names instead ⍝ of difficult-to-remember numbers. The function name is normally the name ⍝ of the C-function that is called by the native function. ⍝ ⍝ Legend: d - table of dirent structs ⍝ e - error code ⍝ i - integer ⍝ h - file handle (integer) ⍝ n - names (nested vector of strings) ⍝ s - string ⍝ A1, A2, ... nested vector with elements A1, A2, ... ∇Zi ← FIO∆errno ⍝⍝ errno (of last call) Zi ← ⎕FIO[1] '' ∇ ∇Zs ← FIO∆strerror Be ⍝⍝ strerror(Be) Zs ← ⎕FIO[2] Be ∇ ∇Zh ← As FIO∆fopen Bs ⍝⍝ fopen(Bs, As) filename Bs Zh ← As ⎕FIO[3] Bs ∇ ∇Zh ← FIO∆fopen_ro Bs ⍝⍝ fopen(Bs, "r") filename Bs Zh ← ⎕FIO[3] Bs ∇ ∇Ze ← FIO∆fclose Bh ⍝⍝ fclose(Bh) Ze ← ⎕FIO[4] Bh ∇ ∇Ze ← FIO∆file_errno Bh ⍝⍝ errno (of last call on Bh) Ze ← ⎕FIO[5] Bh ∇ ∇Zi ← Ai FIO∆fread Bh ⍝⍝ fread(Zi, 1, Ai, Bh) 1 byte per Zi ⍝⍝ read (at most) 5000 items if Ai is not defined →(0≠⎕NC 'Ai')/↑⎕LC+1 ◊ Ai←5000 Zi ← Ai ⎕FIO[ 6] Bh ∇ ∇Zi ← Ai FIO∆fwrite Bh ⍝⍝ fwrite(Ai, 1, ⍴Ai, Bh) 1 byte per Ai Zi ← Ai ⎕FIO[7] Bh ∇ ∇Zi ← Ai FIO∆fgets Bh ⍝⍝ fgets(Zi, Ai, Bh) 1 byte per Zi ⍝⍝ read (at most) 5000 items unless Ai is defined →(0≠⎕NC 'Ai')/↑⎕LC+1 ◊ Ai←5000 Zi ← Ai ⎕FIO[8] Bh ∇ ∇Zi ← FIO∆fgetc Bh ⍝⍝ fgetc(Zi, Bh) 1 byte per Zi Zi ← ⎕FIO[9] Bh ∇ ∇Zi ← FIO∆feof Bh ⍝⍝ feof(Bh) Zi ← ⎕FIO[10] Bh ∇ ∇Zi ← FIO∆ferror Bh ⍝⍝ ferror(Bh) Zi ← ⎕FIO[11] Bh ∇ ∇Zi ← FIO∆ftell Bh ⍝⍝ ftell(Bh) Zi ← ⎕FIO[12] Bh ∇ ∇Zi ← Ai FIO∆fseek_cur Bh ⍝⍝ fseek(Bh, Ai, SEEK_SET) Zi ← Ai ⎕FIO[13] Bh ∇ ∇Zi ← Ai FIO∆fseek_set Bh ⍝⍝ fseek(Bh, Ai, SEEK_CUR) Zi ← Ai ⎕FIO[14] Bh ∇ ∇Zi ← Ai FIO∆fseek_end Bh ⍝⍝ fseek(Bh, Ai, SEEK_END) Zi ← Ai ⎕FIO[15] Bh ∇ ∇Zi ← FIO∆fflush Bh ⍝⍝ fflush(Bh) Zi ← ⎕FIO[16] Bh ∇ ∇Zi ← FIO∆fsync Bh ⍝⍝ fsync(Bh) Zi ← ⎕FIO[17] Bh ∇ ∇Zi ← FIO∆fstat Bh ⍝⍝ fstat(Bh) Zi ← ⎕FIO[18] Bh ∇ ∇Zi ← FIO∆unlink Bh ⍝⍝ unlink(Bc) Zi ← ⎕FIO[19] Bh ∇ ∇Zi ← FIO∆mkdir_777 Bh ⍝⍝ mkdir(Bc, 0777) Zi ← ⎕FIO[20] Bh ∇ ∇Zi ← Ai FIO∆mkdir Bh ⍝⍝ mkdir(Bc, Ai) Zi ← Ai ⎕FIO[20] Bh ∇ ∇Zi ← FIO∆rmdir Bh ⍝⍝ rmdir(Bc) Zi ← ⎕FIO[21] Bh ∇ ∇Zi ← FIO∆printf B ⍝⍝ printf(B1, B2...) format B1 Zi ← B ⎕FIO[22] 1 ∇ ∇Zi ← FIO∆fprintf_stderr B ⍝⍝ fprintf(stderr, B1, B2...) format B1 Zi ← B ⎕FIO[22] 2 ∇ ∇Zi ← Ah FIO∆fprintf B ⍝⍝ fprintf(Ah, Bf, B2...) format Bf Zi ← B ⎕FIO[22] Ah ∇ ∇Zi ← Ac FIO∆fwrite_utf8 Bh ⍝⍝ fwrite(Ac, 1, ⍴Ac, Bh) Unicode Ac Output UTF-8 Zi ← Ac ⎕FIO[23] Bh ∇ ∇Zh ← As FIO∆popen Bs ⍝⍝ popen(Bs, As) command Bs mode As Zh ← As ⎕FIO[24] Bs ∇ ∇Zh ← FIO∆popen_ro Bs ⍝⍝ popen(Bs, "r") command Bs Zh ← ⎕FIO[24] Bs ∇ ∇Ze ← FIO∆pclose Bh ⍝⍝ pclose(Bh) Ze ← ⎕FIO[25] Bh ∇ ∇Ze←txt FIO∆pipeto cmd;ph ⍝⍝ Function pipes txt to shell command cmd ph←'w' FIO∆popen cmd →(0≠Ze←txt ⎕fio[43] ph)/0 ⍝return read error to caller Ze←⎕fio[25] ph ∇ ∇txt←FIO∆pipefrom cmd;ph;Ze ⍝⍝ Function to pipe text from a shell command. Generally the function ⍝⍝ returns the stdout of the command. If error occur it returns an ⍝⍝ integer FIO∆strerror will give a hint of what went wrong. ph←'r' ⎕FIO[24] cmd txt←⍬ st: →(0=⍴sink←5000 ⎕fio[41] ph)/end txt←txt,sink →st end: txt←⎕ucs txt Ze←⎕fio[25] ph ⍎(0≠Ze)/'txt←Ze' ∇ ∇Zs ← FIO∆read_file Bs ⍝⍝ return entire file Bs as byte vector Zs ← ⎕FIO[26] Bs ∇ ∇Zs ← As FIO∆rename Bs ⍝⍝ rename file As to Bs Zs ← As ⎕FIO[27] Bs ∇ ∇Zd ← FIO∆read_directory Bs ⍝⍝ return content of directory Bs Zd ← ⎕FIO[28] Bs ∇ ∇Zn ← FIO∆files_in_directory Bs ⍝⍝ return file names in directory Bs Zn ← ⎕FIO[29] Bs ∇ ∇Zs ← FIO∆getcwd ⍝⍝ getcwd() Zs ← ⎕FIO 30 ∇ ∇Zn ← As FIO∆access Bs ⍝⍝ access(As, Bs) As ∈ 'RWXF' Zn ← As ⎕FIO[31] Bs ∇ ∇Zh ← FIO∆socket Bi ⍝⍝ socket(Bi). Bi is domain, type, protocol, e.g. ⍝⍝ Zh ← FIO∆socket 2 1 0 for an IPv4 TCP socket Zh ← ⎕FIO[32] Bi ∇ ∇Zi ← Ai FIO∆bind Bh ⍝⍝ bind(Bh, Ai). Ai is domain, local IPv4-address, local port, e.g. ⍝⍝ 2 (256⊥127 0 0 1) 80 bind Bh binds socket Bh to port 80 on ⍝⍝ localhost 127.0.0.1 (web server) Zi ← Ai ⎕FIO[33] Bh ∇ ∇Zi ← Ai FIO∆listen Bh ⍝⍝ listen(Bh, Ai). Zi ← Ai ⎕FIO[34] Bh ∇ ∇Zh ← FIO∆accept Bh ⍝⍝ accept(Bh). ⍝⍝ Return errno or 4 integers: handle, domain, remote IPv4-address, remote port Zh ← ⎕FIO[35] Bh ∇ ∇Zh ← Aa FIO∆connect Bh ⍝⍝ connect(Bh, Aa). Aa is domain, remote IPv4-address, remote port, e.g. ⍝⍝ 2 (256⊥127 0 0 1) 80 connects to port 80 on localhost 1
Re: [Bug-apl] wslib5/FILE_IO.apl
Hi Bill, thanks, fixed in SVN 532. /// Jürgen On 02/14/2015 08:38 PM, Bill Heagy wrote: In the supplied script wslib5/FILE_IO.apl, the function "FIO∆fwrite" is defined twice, as FILE_IO[7] (binary) and as FILE_IO[23] (Unicode).
[Bug-apl] wslib5/FILE_IO.apl
In the supplied script wslib5/FILE_IO.apl, the function "FIO∆fwrite" is defined twice, as FILE_IO[7] (binary) and as FILE_IO[23] (Unicode). -- Bill Heagy