Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Diff of /sml/trunk/src/cml/src/IO/text-io-fn.sml
ViewVC logotype

Diff of /sml/trunk/src/cml/src/IO/text-io-fn.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 843, Tue Jun 19 21:53:04 2001 UTC revision 844, Wed Jun 20 20:39:15 2001 UTC
# Line 30  Line 30 
30      structure SV = SyncVar      structure SV = SyncVar
31    
32    (* assign to an MVar *)    (* assign to an MVar *)
33      fun mUpdate (mv, x) = (SV.mTake mv; SV.mPut(mv, x))      fun mUpdate (mv, x) = ignore(SV.mSwap(mv, x))
34    
35    (* an element for initializing buffers *)    (* an element for initializing buffers *)
36      val someElem = #"\000"      val someElem = #"\000"
# Line 331  Line 331 
331                          else (SV.mPut(more, next); false)                          else (SV.mPut(more, next); false)
332                      end                      end
333                (* end case *))                (* end case *))
334          fun mkInstream' (reader, data) = let          fun mkInstream' (reader, optData) = let
335                val PIO.RD{readVec, readVecEvt, getPos, setPos, ...} = reader                val PIO.RD{readVec, readVecEvt, getPos, setPos, ...} = reader
336                val getPos = (case (getPos, setPos)                val getPos = (case (getPos, setPos)
337                       of (SOME f, SOME _) => (fn () => SOME(f()))                       of (SOME f, SOME _) => (fn () => SOME(f()))
# Line 345  Line 345 
345                        closed = closedFlg, getPos = getPos,                        closed = closedFlg, getPos = getPos,
346                        tail = SV.mVarInit more, cleanTag = tag                        tail = SV.mVarInit more, cleanTag = tag
347                      }                      }
348  (** What should we do about the position when there is initial data?? **)                val buf = (case optData
349                         of NONE => IBUF{
350                                basePos = getPos(), data=empty,
351                                info=info, more=more
352                              }
353    (** What should we do about the position in this case ?? **)
354  (** Suggestion: When building a stream with supplied initial data,  (** Suggestion: When building a stream with supplied initial data,
355   ** nothing can be said about the positions inside that initial   ** nothing can be said about the positions inside that initial
356   ** data (who knows where that data even came from!).   ** data (who knows where that data even came from!).
357   **)   **)
358                val basePos = if (V.length data = 0)                        | (SOME v) => IBUF{
359                      then getPos()                              basePos = NONE, data=v,
360                      else NONE                              info=info, more=more}
361                val buf = IBUF{                      (* end case *))
                       basePos = basePos, data = data, info = info, more = more  
                     }  
362                val strm = ISTRM(buf, 0)                val strm = ISTRM(buf, 0)
363                in                in
364                  (tag, strm)                  (tag, strm)
# Line 382  Line 385 
385                    else (reader, V.concat(getData more))                    else (reader, V.concat(getData more))
386                end                end
387    
388  (** NOTE: this code works for Unix TextIO, but not for Windows!! **)        (** Position operations on instreams **)
389          fun filePosIn (ISTRM(buf, pos)) = (case buf          datatype in_pos = INP of {
390                base : pos,
391                offset : int,
392                info : info
393              }
394    
395            fun getPosIn (ISTRM(buf, pos)) = (case buf
396                 of IBUF{basePos=NONE, info, ...} =>                 of IBUF{basePos=NONE, info, ...} =>
397                      inputExn (info, "filePosIn", IO.RandomAccessNotSupported)                      inputExn (info, "getPosIn", IO.RandomAccessNotSupported)
398                  | IBUF{basePos=SOME base, info, ...} =>                  | IBUF{basePos=SOME p, info, ...} => INP{
399                      Position.+(base, Position.fromInt pos)                        base = p, offset = pos, info = info
400                        }
401                (* end case *))                (* end case *))
402            fun filePosIn (INP{base, offset, ...}) =
403                  Position.+(base, Position.fromInt offset)
404            fun setPosIn (pos as INP{info as INFO{reader, ...}, ...}) = let
405                  val fpos = filePosIn pos
406                  val (PIO.RD rd) = reader
407                  in
408                    terminate info;
409                    valOf (#setPos rd) fpos;
410                    mkInstream (PIO.RD rd, NONE)
411                  end
412    
413        (** Text stream specific operations **)        (** Text stream specific operations **)
414          fun inputLine (ISTRM(buf as IBUF{data, ...}, pos)) = let          fun inputLine (ISTRM(buf as IBUF{data, ...}, pos)) = let
# Line 793  Line 813 
813            in            in
814              SV.mPut (strm, strm'); v              SV.mPut (strm, strm'); v
815            end            end
816      fun input1Evt _ = raise Fail "input1Evt unimplemented"  
817      fun inputEvt _ = raise Fail "inputEvt unimplemented"    (* event-value constructors *)
818      fun inputNEvt _ = raise Fail "inputNEvt unimplemented"      local
819      fun inputAllEvt _ = raise Fail "inputAllEvt unimplemented"        datatype 'a result = RES of 'a | EXN of exn
820          fun sendEvt (ch, v) = CML.sendEvt(ch, RES v)
821          fun sendExnEvt (ch, exn) = CML.sendEvt(ch, EXN exn)
822          fun recvEvt ch =
823                CML.wrap(CML.recvEvt ch, fn (RES v) => v | (EXN exn) => raise exn)
824          fun doInput inputEvt (strm : instream) nack = let
825                val replyCh = CML.channel()
826                fun inputThread () = let
827                      val strm' = SV.mTake strm
828                      val nackEvt = CML.wrap(nack, fn _ => SV.mPut(strm, strm'))
829                      fun handleInput (result, strm'') = CML.select [
830                              CML.wrap (sendEvt(replyCh, result),
831                                fn _ => SV.mPut(strm, strm'')),
832                              nackEvt
833                            ]
834                      in
835                        (CML.select [
836                            CML.wrap (inputEvt strm', handleInput),
837                            nackEvt
838                          ]) handle exn => CML.select [
839                              CML.wrap (sendExnEvt(replyCh, exn),
840                                fn _ => SV.mPut(strm, strm')),
841                              nackEvt
842                            ]
843                      end
844                in
845                  ignore (CML.spawn inputThread);
846                  recvEvt replyCh
847                end
848        in
849        fun input1Evt (strm : instream) = let
850              fun inputEvt (strm : StreamIO.instream) = CML.wrap (
851                    StreamIO.input1Evt strm,
852                    fn NONE => (NONE, strm) | SOME(s, strm') => (SOME s, strm'))
853              in
854                CML.withNack (doInput inputEvt strm)
855              end
856        fun inputEvt strm = CML.withNack (doInput StreamIO.inputEvt strm)
857        fun inputNEvt (strm, n) =
858              CML.withNack (doInput (fn strm' => StreamIO.inputNEvt(strm', n)) strm)
859        fun inputAllEvt Strm = CML.withNack (doInput StreamIO.inputAllEvt Strm)
860        end (* local *)
861    
862      fun canInput (strm, n) = StreamIO.canInput (SV.mGet strm, n)      fun canInput (strm, n) = StreamIO.canInput (SV.mGet strm, n)
863      fun lookahead (strm : instream) = (case StreamIO.input1(SV.mGet strm)      fun lookahead (strm : instream) = (case StreamIO.input1(SV.mGet strm)
864             of NONE => NONE             of NONE => NONE
# Line 810  Line 872 
872              SV.mPut(strm, StreamIO.findEOS buf)              SV.mPut(strm, StreamIO.findEOS buf)
873            end            end
874      fun endOfStream strm = StreamIO.endOfStream(SV.mGet strm)      fun endOfStream strm = StreamIO.endOfStream(SV.mGet strm)
875        fun getPosIn strm = StreamIO.getPosIn(SV.mGet strm)
876        fun setPosIn (strm, p) = mUpdate(strm, StreamIO.setPosIn p)
877    
878    (** Output operations **)    (** Output operations **)
879      fun output (strm, v) = StreamIO.output(SV.mGet strm, v)      fun output (strm, v) = StreamIO.output(SV.mGet strm, v)
# Line 835  Line 899 
899    
900    (** Open files **)    (** Open files **)
901      fun openIn fname =      fun openIn fname =
902            mkInstream(StreamIO.mkInstream(OSPrimIO.openRd fname, empty))            mkInstream(StreamIO.mkInstream(OSPrimIO.openRd fname, NONE))
903              handle ex => raise IO.Io{function="openIn", name=fname, cause=ex}              handle ex => raise IO.Io{function="openIn", name=fname, cause=ex}
904      fun openOut fname = let      fun openOut fname = let
905            val wr = OSPrimIO.openWr fname            val wr = OSPrimIO.openWr fname
# Line 854  Line 918 
918            end            end
919      fun outputSubstr (strm, ss) = StreamIO.outputSubstr (SV.mGet strm, ss)      fun outputSubstr (strm, ss) = StreamIO.outputSubstr (SV.mGet strm, ss)
920      fun openString src =      fun openString src =
921            mkInstream(StreamIO.mkInstream(OSPrimIO.strReader src, empty))            mkInstream(StreamIO.mkInstream(OSPrimIO.strReader src, NONE))
922              handle ex => raise IO.Io{function="openIn", name="<string>", cause=ex}              handle ex => raise IO.Io{function="openIn", name="<string>", cause=ex}
923    
924      structure ChanIO = ChanIOFn(      structure ChanIO = ChanIOFn(
# Line 864  Line 928 
928    
929    (* open an instream that is connected to the output port of a channel. *)    (* open an instream that is connected to the output port of a channel. *)
930      fun openChanIn ch =      fun openChanIn ch =
931            mkInstream(StreamIO.mkInstream(ChanIO.mkReader ch, empty))            mkInstream(StreamIO.mkInstream(ChanIO.mkReader ch, NONE))
932    
933    (* open an outstream that is connected to the input port of a channel. *)    (* open an outstream that is connected to the input port of a channel. *)
934      fun openChanOut ch =      fun openChanOut ch =
# Line 874  Line 938 
938      local      local
939        structure SIO = StreamIO        structure SIO = StreamIO
940        fun mkStdIn rebind = let        fun mkStdIn rebind = let
941              val (tag, strm) = SIO.mkInstream'(OSPrimIO.stdIn(), empty)              val (tag, strm) = SIO.mkInstream'(OSPrimIO.stdIn(), NONE)
942              in              in
943                if rebind                if rebind
944                  then CleanIO.rebindCleaner (tag, dummyCleaner)                  then CleanIO.rebindCleaner (tag, dummyCleaner)

Legend:
Removed from v.843  
changed lines
  Added in v.844

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0