home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / mosmllib / BinIO.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  4.2 KB  |  129 lines  |  [TEXT/R*ch]

  1. (* BinIO -- SML Basis Library *)
  2.  
  3. type elem   = Word8.word
  4. type vector = Word8Vector.vector
  5.  
  6. (* The only way BinIO.instream and BinIO.outstream differ from
  7.  * TextIO.instream and TextIO.outstream is in the way they were
  8.  * opened.  Hence we call on the TextIO functions to implement most of
  9.  * the BinIO functions too (except openIn, openOut, openAppend, of
  10.  * course.  Some `conversion' functions:
  11.  *)
  12.  
  13. prim_val fromString : string -> vector = 1 "identity"
  14. prim_val toString   : vector -> string = 1 "identity"
  15.  
  16. prim_val fromChar   : char -> elem = 1 "identity"
  17. prim_val toChar     : elem -> char = 1 "identity"
  18.  
  19. prim_type in_channel and out_channel;
  20.  
  21. (* Opening and closing files in binary mode: *)
  22.  
  23. local 
  24.     datatype open_flag =
  25.     O_RDONLY                       (* `open' read-only *)
  26.       | O_WRONLY                       (* `open' write-only *)
  27.       | O_RDWR                         (* `open' for reading and writing *)
  28.       | O_APPEND                       (* `open' for appending *)
  29.       | O_CREAT                        (* create the file if nonexistent *)
  30.       | O_TRUNC                        (* truncate the file to 0 if exists *)
  31.       | O_EXCL                         (* fails if the file exists *)
  32.       | O_BINARY                       (* `open' in binary mode *)
  33.       | O_TEXT                         (* `open' in text mode *)
  34.  
  35.     type file_perm = int;    
  36.     prim_val sys_open : string -> open_flag list -> file_perm -> int 
  37.                         = 3 "sys_open"
  38.  
  39.     prim_val open_descriptor_in  : int -> in_channel  = 1 "open_descriptor";
  40.     prim_val open_descriptor_out : int -> out_channel = 1 "open_descriptor";
  41.  
  42.     fun caml_open_in_gen  mode rights filename =
  43.     open_descriptor_in  (sys_open filename mode rights);
  44.     fun caml_open_out_gen mode rights filename =
  45.     open_descriptor_out (sys_open filename mode rights);
  46.  
  47.     prim_val s_irall : file_perm = 0 "s_irall";
  48.     prim_val s_iwall : file_perm = 0 "s_iwall";
  49. in
  50.     val caml_open_in_bin = 
  51.     caml_open_in_gen [O_RDONLY, O_BINARY] 0;
  52.     
  53.     val caml_open_out_bin =
  54.     caml_open_out_gen [O_WRONLY, O_TRUNC,  O_CREAT, O_BINARY] 
  55.                       (s_irall + s_iwall);
  56.  
  57.     val caml_open_out_bin_append =
  58.     caml_open_out_gen [O_WRONLY, O_APPEND, O_CREAT, O_BINARY]
  59.                       (s_irall + s_iwall)
  60. end
  61.  
  62. (* Binary input: *)
  63.  
  64. type instream = { closed: bool, ic: in_channel, name : string } ref;
  65.  
  66. prim_val inToText : instream -> TextIO.instream = 1 "identity"
  67.  
  68. fun raiseIo fcn nam exn = 
  69.     raise Io {function = fcn, name = nam, cause = exn};
  70.  
  71. fun openIn (s : string) : instream =
  72.     ref {closed=false, ic=caml_open_in_bin s, name=s}
  73.     handle exn as SysErr _ => raiseIo "openIn" s exn;
  74.  
  75. fun closeIn (is : instream) : unit = 
  76.     TextIO.closeIn (inToText is)
  77.  
  78. fun input (is : instream) : vector = 
  79.     fromString (TextIO.input (inToText is))
  80.  
  81. fun inputAll (is : instream) : vector =
  82.     fromString (TextIO.inputAll (inToText is))
  83.  
  84. fun inputNoBlock (is : instream) : vector option =
  85.     raise Fail "not implemented";
  86.  
  87. fun input1 (is : instream) : elem option =
  88.     case TextIO.input1 (inToText is) of
  89.     NONE   => NONE
  90.       | SOME c => SOME (fromChar c);
  91.  
  92. fun inputN (is : instream, n : int) : vector =
  93.     fromString (TextIO.inputN (inToText is, n));
  94.  
  95. fun endOfStream (is : instream) : bool =
  96.     TextIO.endOfStream (inToText is);
  97.  
  98. fun lookahead (is : instream) : elem option =
  99.     case TextIO.lookahead (inToText is) of
  100.     NONE   => NONE
  101.       | SOME c => SOME (fromChar c);
  102.     
  103.  
  104. (* Binary output: *)
  105.  
  106. type outstream = { closed: bool, oc: out_channel, name : string } ref;
  107.  
  108. prim_val outToText : outstream -> TextIO.outstream = 1 "identity"
  109.  
  110. fun openOut (s : string) : outstream =
  111.     ref {closed=false, oc=caml_open_out_bin s, name=s}
  112.     handle exn as SysErr _ => raiseIo "openOut" s exn;
  113.  
  114. fun openAppend s : outstream =
  115.     ref { closed=false, oc=caml_open_out_bin_append s, name=s }
  116.     handle exn as SysErr _ => raiseIo "openAppend" s exn;
  117.  
  118. fun closeOut (os : outstream) : unit =
  119.     TextIO.closeOut (outToText os);
  120.  
  121. fun output(os : outstream, vec : vector) : unit =
  122.     TextIO.output(outToText os, toString vec);
  123.  
  124. fun output1(os : outstream, w : elem) : unit =
  125.     TextIO.output1(outToText os, toChar w);
  126.  
  127. fun flushOut(os : outstream) : unit =
  128.     TextIO.flushOut(outToText os);
  129.