Files
mercury/samples/diff/file.m
Andrew Bromage bc0a109801 Modified files
Estimated hours taken: 30

Modified files
--------------

samples/diff/README:
	Info about this new version.  Put the existing READMEs in
	reverse order, so that they'll be in the most sensible order
	for someone reading top-down.

samples/diff/Mmakefile:
	Supply more detail about which bit of this program GCC 2.7.2
	under Digital Unix 3.2 doesn't compile properly.

samples/diff/diff.m:
	Slight reorganisation.  Accept options, remove some high
	coupling betweem diff__do_diff and lcss.m.

samples/diff/file.m:
	Add support for attaching a filename to a file.

samples/diff/lcss.m:
	Add more documentation, one slight performance improvement.
	lcss__to_diff now works on the reversed LCSS, to avoid going
	to the trouble of reversing it.

New files
---------

samples/diff/TODO:
	Meaning should be obvious.

samples/diff/diff_out.m:
	Replacement for diffs.m (which was never a very good name).
	Now contains code to display a diff in lots more output
	styles than previously supported.

samples/diff/difftype.m:
	Replacement for lcsstype.m.  Contains the type of a diff (but
	not of an lcss, which is now local to lcss.m, hence the
	renaming).

samples/diff/filter.m:
	Contains code to filter a diff before being displayed.  This
	is only required if the user specified that they didn't want
	to see part of the diff (e.g. with --ignore-blank-lines).

samples/diff/globals.m:
samples/diff/options.m:
	Support for option handling.

Removed files
-------------

samples/diff/diffs.m:
	Functionality moved to diff_out.m.

samples/diff/lcsstype.m:
	Functionality moved to difftype.m.
1998-01-13 00:52:08 +00:00

145 lines
4.8 KiB
Mathematica

%-----------------------------------------------------------------------------%
% Copyright (C) 1995-1998 The University of Melbourne.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%-----------------------------------------------------------------------------%
% Main author: bromage
% Simplified by Marnix Klooster <marnix@worldonline.nl>
% This module provides file input. One can read a file entirely,
% select a single line from a read file, get the number of lines
% in a read file, and convert a read file to a list of strings.
%
% Every file has a filename attached to it.
%-----------------------------------------------------------------------------%
:- module file.
:- interface.
:- import_module io, list, string.
:- type file.
% file__read_file reads a file from a filename.
:- pred file__read_file(string, io__res(file), io__state, io__state).
:- mode file__read_file(in, out, di, uo) is det.
% file__read_input reads a file from the input
% stream.
:- pred file__read_input(string, io__res(file), io__state, io__state).
:- mode file__read_input(in, out, di, uo) is det.
% file__get_line retrieves a line from a file.
% (Lines are numbered from 0.)
% Fails if the line is out of bounds.
:- pred file__get_line(file, int, string).
:- mode file__get_line(in, in, out) is semidet.
% file__get_numlines returns the number of lines
% in a file.
:- pred file__get_numlines(file, int).
:- mode file__get_numlines(in, out) is det.
% file__from_list converts a list of lines to a file.
:- pred file__from_list(string, list(string), file).
:- mode file__from_list(in, in, out) is det.
% file__to_list converts a file into a list of
% lines.
:- pred file__to_list(file, list(string)).
:- mode file__to_list(in, out) is det.
% file__get_file_name returns the name of the file.
:- pred file__get_file_name(file, string).
:- mode file__get_file_name(in, out) is det.
% file__set_file_name sets the name of the file.
:- pred file__set_file_name(file, string, file).
:- mode file__set_file_name(in, in, out) is det.
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module array, require, int, bool.
%-----------------------------------------------------------------------------%
:- type file
---> file(
string, % File name
array(string) % Contents
).
% Open the stream, read from the stream, then close
% the stream.
file__read_file(FileName, File) -->
io__open_input(FileName, Res),
( { Res = ok(InputStream) },
file__read_stream(InputStream, Contents),
io__close_input(InputStream),
{ File = ok(file(FileName, Contents)) }
; { Res = error(Error) },
{ File = error(Error) }
).
% Get the input stream, then read from it.
file__read_input(FileName, ok(file(FileName, Contents))) -->
io__input_stream(InputStream),
file__read_stream(InputStream, Contents).
% file__read_stream is the "real" file reader.
:- pred file__read_stream(io__input_stream, array(string),
io__state, io__state).
:- mode file__read_stream(in, array_uo, di, uo) is det.
file__read_stream(Stream, File) -->
file__read_stream2(Stream, 0, File).
% Given a Stream from which LinesIn lines have already been
% read, fill File[LinesIn] to File[LinesOut-1] with the rest
% of the lines. LinesOut is the number of lines in the file.
% (Note that line numbering starts at zero.)
:- pred file__read_stream2(io__input_stream, int, array(string),
io__state, io__state).
:- mode file__read_stream2(in, in, array_uo, di, uo) is det.
file__read_stream2(Stream, LineNo, File) -->
io__read_line(Stream, Res),
( { Res = eof },
{ array__init(LineNo, "", File) }
; { Res = ok(Line) },
{ string__from_char_list(Line, Line1) },
{ LineNo1 is LineNo + 1 },
file__read_stream2(Stream, LineNo1, File1),
{ array__set(File1, LineNo, Line1, File) }
; { Res = error(Error) },
{ io__error_message(Error, Msg) },
{ error(Msg) }
).
%-----------------------------------------------------------------------------%
file__get_line(file(_, Contents), LineNo, Line) :-
array__semidet_lookup(Contents, LineNo, Line).
file__get_numlines(file(_, Contents), NumLines) :-
array__bounds(Contents, _, NumLines1),
NumLines is NumLines1 + 1.
%-----------------------------------------------------------------------------%
file__to_list(file(_, Contents), List) :-
array__to_list(Contents, List).
file__from_list(FileName, List, file(FileName, Contents)) :-
array__from_list(List, Contents).
%-----------------------------------------------------------------------------%
file__get_file_name(file(FileName, _), FileName).
file__set_file_name(file(_, B), FileName, file(FileName, B)).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%