⍝ File_IO_1.tc
⍝ ----------------------------------

      ⍝ same as File_IO.tc but WITHOUT using FILE_IO.apl library
      ⍝
      ⍝ This is NOT the recommeded way to use file_io.so but a way to
      ⍝ separate faults in file_io.cc from faults in FILE_IO.apl

      ⍝ 1. Fix native  function FILE_IO 
      ⍝

      ⍝⍝⍝
      ⍝⍝⍝ If this segfaults then re-install GNU APL !!!
      ⍝⍝⍝

      'lib_file_io.so' ⎕FX 'FILE_IO'
FILE_IO

      ⍝ fopen THIS file readonly
      ⍝       ----
      ⎕←Handle←FILE_IO[3] Filename←'testcases/File_IO_1.tc'
⁰

      ⍝ get statistics
      ⍝
      ⎕←FILE_IO[18] Handle
⁰ ⁰ ⁰ ⁰ ⁰ ⁰ ⁰ ⁰ ⁰ ⁰ ⁰ ⁰ ⁰

      ⍝ close this file
      ⍝
      ⎕←FILE_IO[4] Handle
0

      ⍝ close THIS file again (which is an error)
      ⍝
      FILE_IO[4] Handle
DOMAIN ERROR+
      FILE_IO[4]Handle
      ^         ^

      →

      ⍝ create new file
      ⍝
      ⎕←Handle←'w' FILE_IO[3] Filename← 'FILE_IO_1.test1'
⁰

      ⍝ write 3 lines
      ⍝
      ⎕←(⎕UCS "Hello\n") FILE_IO[7] Handle
6
      ⎕←(⎕UCS "World\n") FILE_IO[7] Handle
6
      ⎕←(⎕UCS "Line 3...\n") FILE_IO[7] Handle
10

      ⍝ close the file
      ⍝
      FILE_IO[4] Handle
0

      ⍝ print the file
      ⍝
      )HOST cat FILE_IO_1.test1
Hello
World
Line 3...

0 

      ⍝ open file again for reading
      ⍝
      ⎕←Handle← FILE_IO[3] Filename
⁰

      ⍝ read one line (default max_len)
      ⍝
      Z←FILE_IO[8] Handle
      ⎕UCS Z
Hello

      ⍝ read another line (max_len 200)
      ⍝
      Z←200 FILE_IO[8] Handle
      ⎕UCS Z
World

      ⍴Z
6

      ⍝ end of file ? (no)
      ⍝
      FILE_IO[10] Handle
0

      ⍝ read more bytes
      ⍝
      Z←FILE_IO[6] Handle
      ⍴Z
10

      ⍝ end of file ? (yes)
      ⍝
      FILE_IO[10] Handle
1

      ⍝ get statistics
      ⍝
      ⎕←FSTAT←FILE_IO[18] Handle ◊ 0 8 8 8 ⊤ FSTAT[3]
⁰ ⁰ ⁰ 1 ⁰ ⁰ 0 22 ⁰ ⁰ ⁰ ⁰ ⁰
64 6 ⁰ ⁰

      ⍝ close the file
      ⍝
      FILE_IO[4] Handle
0

      ⍝ delete the file
      ⍝
      FILE_IO[19] Filename
0

      ⍝ delete the file again (should fail)
      ⍝
      Error←-FILE_IO[19] Filename
      FILE_IO[2] Error
No such file or directory

      ⍝ printf
      ⍝
      )ERASE FORMAT
      FORMAT←"⎕everything %d Pi %e String %s\n" 
      FORMAT 42 (○1) 'Hello'  FILE_IO[22] 1
44

      ⍝ fwrite with UCS in UTF8 out
      ⍝
      ⎕←Handle←'w' FILE_IO[3] Filename← 'FILE_IO_1.test2'
⁰
      'HELLO ⍋ ⌽ ⍒ ⍴ ⍵' FILE_IO[23] Handle
25

      FILE_IO[4] Handle ⍝ close file
0

      )HOST cat FILE_IO_1.test2 
HELLO ⍋ ⌽ ⍒ ⍴ ⍵
0 

      ⍝ popen "r"
      ⍝
      Command←'ls FILE_IO*'
      Handle← FILE_IO[24] Command
      Z←20000 FILE_IO[6] Handle
      ⍴Z
⁰
      FILE_IO[25] Handle ⍝ close command (returning its exit code)
0

      ⍝ read entire file
      ⍝
      ⎕UCS FILE_IO[26] Filename
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
      ⍝
      AF_INET     ← 2
      SOCK_STREAM ← 1
      PROTO       ← 0
      LOCALHOST←256⊥127 0 0 1
      Port ← 22222
      Server←AF_INET, LOCALHOST, Port

      ⍝ socket(AF_INET, SOCK_STREAM, PROTO)
      ⍝
      3 ≤ Handle←FILE_IO[32] AF_INET, SOCK_STREAM, PROTO
1

      ⍝ setsockopt(Handle, SOL_SOCKET, SO_REUSEADDR, 1)
      ⍝
      SOL_SOCKET←1
      SO_REUSEADDR←2
      (SOL_SOCKET, SO_REUSEADDR, 1) FILE_IO[47] Handle
0

      ⍝ bind(AF_INET, LOCALHOST, Port)
      ⍝
      Server FILE_IO[33] Handle
0

      ⍝ listen(10)
      ⍝
      10 FILE_IO[34] Handle
0

      ⍝ socket(AF_INET, SOCK_STREAM, PROTO)
      ⍝
      4 ≤ Handle1←FILE_IO[32] AF_INET, SOCK_STREAM, PROTO
1

      ⍝ connect() Handle1 to Handle
      ⍝
      Server FILE_IO[36] Handle1
0

      ⍝ accept()
      ⍝
      Handle2 ← ↑ FILE_IO[35] Handle

      ⍝ send("Hello")
      ⍝
      'Hello' FILE_IO[39] Handle2
5

      ⍝ recv()
      ⍝
      ⎕UCS FILE_IO[37] Handle1
Hello

      ⍝ send("World!")
      ⍝
      'World!' FILE_IO[39] Handle1
6

      ⍝ recv()
      ⍝
      ⎕UCS FILE_IO[37] Handle2
World!

      ⎕←FILE_IO[4] Handle2
0
      ⎕←FILE_IO[4] Handle1
0
      ⎕←FILE_IO[4] Handle
0

      )ERASE FORMAT Handle Handle1 Handle2 Command AF_INET SOCK_STREAM PROTO
      )ERASE Error FSTAT Filename LOCALHOST Port Z Server

⍝     )VARS

⍝ ==================================

