home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / p / plbin.zip / pl / boot / writef.pl < prev   
Text File  |  1992-05-26  |  5KB  |  190 lines

  1. /*  writef.pl,v 1.1.1.1 1992/05/26 11:51:24 jan Exp
  2.  
  3.     Copyright (c) 1990 Jan Wielemaker. All rights reserved.
  4.     jan@swi.psy.uva.nl
  5.  
  6.     Purpose: writef/1, writef/2, write_ln/1
  7. */
  8.  
  9. :- module($writef,
  10.     [ write_ln/1
  11.     , writef/1
  12.     , writef/2
  13.     , swritef/2
  14.     , swritef/3
  15.     ]).
  16.  
  17. %   Copied from Edinburgh C-Prolog. Original version by Byrd, changed
  18. %   many times since then.
  19. %
  20. %   Changes for version 1.3.0:
  21. %    
  22. %    - Format can be of type string.
  23. %    - Adjustment now allowed for arbitrary types.
  24. %    - Uses more appropriate SWI-Prolog built-in predicates.
  25. %    - Uses 0'<letter> to specify ascii values (more readable).
  26.  
  27. write_ln(X) :-
  28.     write(X), nl.
  29.  
  30. writef(Format) :-
  31.     writef(Format, []).
  32.  
  33. writef([F|String], List) :-
  34.     $writefs([F|String], List),
  35.     fail.                % clean up global stack
  36. writef(String, List) :-
  37.     string(String),
  38.     string_to_list(String, Fstring),
  39.     $writefs(Fstring, List),
  40.     fail.                % clean up global stack
  41. writef(Format, List) :-
  42.     atom(Format),
  43.     name(Format, Fstring),
  44.     $writefs(Fstring, List),
  45.     fail.                % clean up global stack
  46. writef(_, _).
  47.  
  48. swritef(Atom, Format, Arguments) :-
  49.     $write_on_string(writef(Format, Arguments), Atom).
  50. swritef(Atom, Format) :-
  51.     $write_on_string(writef(Format), Atom).
  52.  
  53.             % Formatted write for a string (i.e. a list of
  54.             % character codes).
  55.  
  56. $writefs([], _).
  57. $writefs([0'%, A|Rest], List) :-    %   %<$action'>
  58.     $action(A, List, More), !,
  59.     $writefs(Rest, More).
  60. $writefs([0'%, D|Rest], [Head|Tail]) :-    %   %<columns><just>
  61.     between(0'0, 0'9, D),
  62.     $getpad(Size, Just, [D|Rest], More),  !,
  63.     $padout(Head, Size, Just),
  64.     $writefs(More, Tail).
  65. $writefs([0'\, C|Rest], List) :-    %   \<special>
  66.     $special(C, Char), !,
  67.     put(Char),
  68.     $writefs(Rest, List).
  69. $writefs([0'\|Rest], List) :-        %   \<character code in decimal>
  70.     $getcode(Char, Rest, More), !,
  71.     put(Char),
  72.     $writefs(More, List).
  73. $writefs([Char|Rest], List) :-        %   <ordinary character>
  74.     put(Char),
  75.     $writefs(Rest, List).
  76.  
  77.  
  78. $action(0't, [Head|Tail], Tail) :-    %   Term
  79.     print(Head).
  80. $action(0'd, [Head|Tail], Tail) :-    %   Display
  81.     display(Head).
  82. $action(0'w, [Head|Tail], Tail) :-    %   Write
  83.     write(Head).
  84. $action(0'q, [Head|Tail], Tail) :-    %   Quoted
  85.     writeq(Head).
  86. $action(0'p,  [Head|Tail], Tail) :-    %   Print
  87.     print(Head).
  88. $action(0'f, List, List) :-        %   Flush
  89.     ttyflush.
  90. $action(0'n, [Char|Tail], Tail) :-    %   iNteger (character)
  91.     put(Char).
  92. $action(0'r, [Thing, Times|Tail], Tail) :-    %   Repeatedly
  93.     $writelots(Times, Thing).
  94. $action(0's, [Head|Tail], Tail) :-    %   String
  95.     $padout(Head).
  96.  
  97. $special(0'n, 10).        /*  n  */
  98. $special(0'l, 10).        /*  l  */
  99. $special(0'r, 10).        /*  r  */
  100. $special(0't,  9).        /*  t  */
  101. $special(0'\, 0'\).        /*  \  */
  102. $special(0'%, 0'%).        /*  %  */
  103.  
  104. $getcode(Char, In, Out) :-
  105.     $getdigits(3, Digits, In, Out),
  106.     name(Char, Digits),
  107.     Char < 128.
  108.  
  109. $getdigits(Limit, [Digit|Digits], In, Out) :-
  110.     Limit > 0,
  111.     $char(In, Digit, Out0),
  112.     between(0'0, 0'9, Digit),
  113.     Fewer is Limit - 1, !,
  114.     $getdigits(Fewer, Digits, Out0, Out).
  115. $getdigits(_, [], Out, Out).
  116.  
  117. $writelots(N, T) :-
  118.     N > 0, !,
  119.     write(T),
  120.     M is N - 1,
  121.     $writelots(M, T).
  122. $writelots(_, _).
  123.  
  124. /*  The new formats are %nC, %nL, and %nR for centered, left, and right
  125.     justified output of atoms, integers, and strings.  This is meant to
  126.     simplify the production of tabular output when it is appropriate.
  127.     At least one space will always precede/follow the item written.
  128. */
  129.  
  130. $getpad(Size, Just, In, Out) :-
  131.     $getdigits(3, Digits, In, Out0),
  132.     name(Size, Digits),
  133.     $char(Out0, Out1, Out),
  134.     $getpad(Out1, Just).
  135.  
  136. $getpad(0'r, r).        %  right justified
  137. $getpad(0'l, l).        %  left justified
  138. $getpad(0'c, c).        %  centered
  139. $getpad(0'R, r).        %  right justified
  140. $getpad(0'L, l).        %  left justified
  141. $getpad(0'C, c).        %  centered
  142.  
  143.  
  144.                 %   $padout(A, S, J) writes the item A in a
  145.                 %   field of S or more characters, Justified.
  146.  
  147. $padout(String, Size, Just) :-
  148.     $string(String), !,
  149.     name(Atom, String),
  150.     $padout(Atom, Size, Just).
  151. $padout(Term, Size, Just) :-
  152.     term_to_atom(Term, Atom),
  153.     atom_length(Atom, Length),
  154.     $padout(Just, Size, Length, Left, Right),
  155.     tab(Left),
  156.     write(Atom),
  157.     tab(Right).
  158.  
  159. $string(0) :- !, fail.
  160. $string([]) :- !.
  161. $string([H|T]) :-
  162.     $print(H), !,
  163.     $string(T).
  164.  
  165. $print(10).            % newline
  166. $print(9).            % tab
  167. $print(X) :-
  168.     between(32, 0'~, X).
  169.  
  170.  
  171.                 %   $padout(Just, Size, Length, Left, Right)
  172.                 %   calculates the number of spaces to put
  173.                 %   on the Left and Right of an item needing
  174.                 %   Length characters in a field of Size.
  175.  
  176. $padout(l, Size, Length, 0, Right) :- !,
  177.     Right is max(1, Size-Length).
  178. $padout(r, Size, Length, Left, 0) :- !,
  179.     Left is max(1, Size-Length).
  180. $padout(c, Size, Length, Left, Right) :-
  181.     Left is max(1, (Size - Length)/2),
  182.     Right is max(1, Size - Length - Left).
  183.  
  184.                 %   $padout(Str) writes a string.
  185.  
  186. $padout([Head|Tail]) :- !,
  187.     put(Head),
  188.     $padout(Tail).
  189. $padout([]).
  190.