PreviousUpNext

15.4.837  src/lib/prettyprint/big/src/out/plain-file-prettyprint-output-stream-avoiding-pointless-file-rewrites.pkg

## plain-file-prettyprint-output-stream-avoiding-pointless-file-rewrites.pkg
#
#    A simple prettyprinter output stream that eventually writes to
#    a text file unless the current contents of that file coincide
#    with what was written.

# Compiled by:
#     src/lib/prettyprint/big/prettyprinter.lib


stipulate
    package fil =  file__premicrothread;                                # file__premicrothread  is from   src/lib/std/src/posix/file--premicrothread.pkg
herein

    package plain_file_prettyprint_output_stream_avoiding_pointless_file_rewrites
    : (weak)
    api {
        include api Prettyprint_Output_Stream;                          # Prettyprint_Output_Stream     is from   src/lib/prettyprint/big/src/out/prettyprint-output-stream.api

        make_plain_file_prettyprinter_output_stream_avoiding_pointless_file_rewrites
            :
            String -> Prettyprint_Output_Stream;                        # Arg is filename.

#       close:  Prettyprint_Output_Stream -> Void;
    }
    {
        Prettyprint_Output_Stream
            =
            PRETTYPRINT_OUTPUT_STREAM
              {
                filename:       String,
                output_stream:  Ref(  List(  String ) )
              };

        Texttraits = Void;

        fun same_texttraits    _ = TRUE;
        fun push_texttraits    _ = ();
        fun pop_texttraits     _ = ();
        fun default_texttraits _ = ();


        #  Allocate an empty output stream and remember the file name. 

        fun make_plain_file_prettyprinter_output_stream_avoiding_pointless_file_rewrites
              f
            =
            PRETTYPRINT_OUTPUT_STREAM
              {
                filename => f,
                output_stream   => REF []
              };

        # Calculate the final output and
        # compare it with the current
        # contents of the file.
        #
        # If they differ, write the file:

        fun close (PRETTYPRINT_OUTPUT_STREAM { output_stream => REF l, filename, ... } )
            =
            {   s =   cat (reverse l);

                fun write ()
                    =
                    {   f = fil::open_for_write filename;
                        fil::write (f, s);
                        fil::close_output f;
                    };

                {   f = fil::open_for_read filename;
                    s' = fil::read_all f;

                    fil::close_input f;

                    if (s != s')    write ();    fi;
                }
                except
                    _ = write ();
            };

        fun put_string (PRETTYPRINT_OUTPUT_STREAM { output_stream, ... }, s)                    # Write a string in the current texttraits to the output stream.
            =
            output_stream := s ! *output_stream;

        fun flush d = ();
#       fun close d = ();
    };
end;


## (C) 2002, Lucent Technologies, Bell Labs
## author: Matthias Blume (blume@research.bell-labs.com)


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext