PreviousUpNext

15.4.1173  src/lib/std/src/posix/winix-text-file-io-driver-for-posix–premicrothread.pkg

## winix-text-file-io-driver-for-posix--premicrothread.pkg
#
# Platform-specific textfile I/O for posix.
#
# This file gets combined with the platform-agnostic code in
# winix_text_file_for_os_g__premicrothread                                              # winix_text_file_for_os_g__premicrothread                      is from   src/lib/std/src/io/winix-text-file-for-os-g--premicrothread.pkg
# to produce winix_text_file_for_posix__premicrothread                                  # winix_text_file_for_posix__premicrothread                     is from   src/lib/std/src/posix/winix-text-file-for-posix--premicrothread.pkg
# which is also published as the 'file' package,                                        # file__premicrothread                                          is from   src/lib/std/src/posix/file--premicrothread.pkg
# the main standard.lib user textfile I/O resource.

# Compiled by:
#     src/lib/std/src/standard-core.sublib




stipulate
    package str =  string_guts;                                                         # string_guts                                                   is from   src/lib/std/src/string-guts.pkg
    package int =  int_guts;                                                            # int_guts                                                      is from   src/lib/std/src/int-guts.pkg
    package iox =  io_exceptions;                                                       # io_exceptions                                                 is from   src/lib/std/src/io/io-exceptions.pkg
    package pos =  file_position_guts;                                                  # file_position_guts                                            is from   src/lib/std/src/bind-position-31.pkg
    package psx =  posixlib;                                                            # posixlib                                                      is from   src/lib/std/src/psx/posixlib.pkg
    #
herein

    package winix_text_file_io_driver_for_posix__premicrothread
    : (weak)
    api {
        include api Winix_Extended_File_Io_Driver_For_Os__Premicrothread;               # Winix_Extended_File_Io_Driver_For_Os__Premicrothread          is from   src/lib/std/src/io/winix-extended-file-io-driver-for-os--premicrothread.api

        stdin:   Void -> drv::Filereader;
        stdout:  Void -> drv::Filewriter;
        stderr:  Void -> drv::Filewriter;

        string_reader:  String -> drv::Filereader;

    } {
        package drv   = winix_base_text_file_io_driver_for_posix__premicrothread;       # winix_base_text_file_io_driver_for_posix__premicrothread      is from   src/lib/std/src/io/winix-base-text-file-io-driver-for-posix--premicrothread.pkg

        File_Descriptor =  psx::File_Descriptor;

        best_io_quantum = 4096;                                                         # Reading/writing files 4K at a time should be fairly efficient.

        make_filereader =  psx::make_text_filereader;                                   # make_text_filereader          def in    src/lib/std/src/psx/posix-io.pkg
        make_filewriter =  psx::make_text_filewriter;                                   # make_text_filewriter          def in    src/lib/std/src/psx/posix-io.pkg

        fun announce s x y
            =
            {
    # print "Posix: "; print (s: String); print "\n"; 
                x y;
            };

        fun open_for_read  filename
            =
            make_filereader
                { file_descriptor =>  announce "openf"   psx::openf (filename, psx::O_RDONLY, psx::o::flags []),
                  filename,
                  ok_to_block =>  TRUE
                };

        standard_mode
            =
            psx::s::flags
                [       #  mode 0666 
                  psx::s::irusr, psx::s::iwusr,
                  psx::s::irgrp, psx::s::iwgrp,
                  psx::s::iroth, psx::s::iwoth
                ];
                                                                                        # createf                       def in    src/lib/std/src/psx/posix-file.pkg
        fun create_file (filename, mode, flags)
            =
            announce "createf"   psx::createf (filename, mode, flags, standard_mode);

        fun open_for_write  filename
            =
            make_filewriter
                { file_descriptor =>  create_file (filename, psx::O_WRONLY, psx::o::trunc),
                  filename,
                  ok_to_block   =>  TRUE,
                  append_mode   =>  FALSE,
                  best_io_quantum
                };

        fun open_for_append  filename
            =
            make_filewriter
                { file_descriptor =>  create_file (filename, psx::O_WRONLY, psx::o::append),
                  filename,
                  ok_to_block   =>  TRUE,
                  append_mode   =>  TRUE,
                  best_io_quantum
                };

        fun stdin ()
            =
            make_filereader
              {
                file_descriptor =>  psx::stdin,         # psx::stdin is just int 0 as an opaque type.
                filename        =>  "<stdin>",
                ok_to_block     =>  TRUE                #  Bug!  Should check!  XXX BUGGO FIXME
              };

        fun stdout ()
            =
            make_filewriter
              {
                file_descriptor =>  psx::stdout,        # psx::stdout is just int 1 as an opaque type.
                filename        =>  "<stdout>",
                #
                ok_to_block     =>  TRUE,               # Bug!  Should check! XXX BUGGO FIXME
                append_mode     =>  FALSE,              # Bug!  Should check! XXX BUGGO FIXME
                best_io_quantum
              };

        fun stderr ()
            =
            make_filewriter
              {
                file_descriptor =>  psx::stderr,        # psx::stderr is just int 2 as an opaque type.
                filename        =>  "<stderr>",
                #
                ok_to_block     =>  TRUE,               # Bug!  Should check! XXX BUGGO FIXME
                append_mode     =>  FALSE,              # Bug!  Should check! XXX BUGGO FIXME
                best_io_quantum
              };

        fun string_reader src
            =
            {
                pos    =  REF 0;
                closed =  REF FALSE;

                fun raise_exception_if_file_is_closed ()
                    =
                    if  *closed    raise exception iox::CLOSED_IO_STREAM;  fi;

                len  =  str::length_in_bytes    src;
                plen =  pos::from_int  len;

                fun avail ()
                    =
                    len - *pos;

                fun read_ro n
                    =
                    {   p = *pos;
                        m = int::min (n, len-p);
                        #
                        raise_exception_if_file_is_closed ();
                        #
                        pos := p+m;
                        str::substring (src, p, m);       # Could use unchecked operations here.
                      };

                fun get_file_position ()
                    =
                    {   raise_exception_if_file_is_closed();
                        #
                        pos::from_int *pos;
                    };

                fun set_file_position p
                    =
                    {   raise_exception_if_file_is_closed ();
                        #
                        if (p < 0  or  p > plen)   raise exception INDEX_OUT_OF_BOUNDS;   fi;

                        pos :=  pos::to_int  p;
                    };

                drv::FILEREADER
                  {
                    filename                   => "<string>", 
                    best_io_quantum            => len,
                    #
                    read_vector                =>  read_ro,
                    #
                    blockx                     => THE (raise_exception_if_file_is_closed),
                    can_readx                  => THE (\\ () = { raise_exception_if_file_is_closed(); TRUE;}),
                    #
                    avail                      => THE o avail,
                    #
                    get_file_position          => THE get_file_position,
                    set_file_position          => THE set_file_position,
                    end_file_position          => THE (\\ () = { raise_exception_if_file_is_closed(); plen;}),
                    #
                    verify_file_position       => THE get_file_position,
                    close                      => \\ () = closed := TRUE,
                    #
                    io_descriptor   => NULL
                  };
              };

    };                                                                  # package winix_text_file_io_driver_for_posix__premicrothread 
end;



Comments and suggestions to: bugs@mythryl.org

PreviousUpNext