File: writef.pl

package info (click to toggle)
swi-prolog 8.0.2+dfsg-3+deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 72,036 kB
  • sloc: ansic: 349,612; perl: 306,654; java: 5,208; cpp: 4,436; sh: 3,042; ruby: 1,594; yacc: 845; makefile: 136; xml: 82; sed: 12; sql: 6
file content (278 lines) | stat: -rw-r--r-- 9,913 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*  Part of SWI-Prolog

    Author:        Jan Wielemaker
    E-mail:        J.Wielemaker@vu.nl
    WWW:           http://www.swi-prolog.org
    Copyright (c)  2011-2013, University of Amsterdam
                              VU University Amsterdam
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions
    are met:

    1. Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in
       the documentation and/or other materials provided with the
       distribution.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
*/

:- module(writef,
          [ writef/1,                   % +Format
            writef/2,                   % +Format, +Args
            swritef/2,                  % -String, +Format
            swritef/3                   % -String, +Format, +Args
          ]).
:- set_prolog_flag(generate_debug_info, false).

/** <module> Old-style formatted write

This library provides writef/1 and   friends. These predicates originate
from Edinburgh C-Prolog and and provided for compatibility purposes. New
code should use format/1, format/2  and   friends,  which  are currently
supported by more Prolog implementations.

The   writef-family   of   predicates   conflicts    with   the   modern
_|character-esacapes|_ flag about  the   interpretation  of \-sequences.
This can be avoided by

  1. Disable character escapes (not recommended unless one wants to
  run really outdated code unmodified).
  2. Double the \ for conflicting interpretations
  3. Use ISO compliant alternatives for conflicting interpretations

@copyright      Copied from Edinburgh C-Prolog. Original version by Byrd,
                changed many times since.
*/

%!  writef(+Format) is det.
%!  writef(+Format, +Arguments) is det.
%
%   Formatted write to the  =current_output=.   Format  is  a format
%   specifier. Some escape sequences require  arguments that must be
%   provided in the list Arguments. There   are  two types of escape
%   sequences: special characters  start  with   =|\|=  and  include
%   arguments start with =|%|=. The special character sequences are:
%
%       | =|\n|= | Output a newline character |
%       | =|\l|= | Output a line separator (same as =|\n|=) |
%       | =|\r|= | Output a carriage-return character (ASCII 13) |
%       | =|\r|= | Output a TAB character (ASCII 9) |
%       | =|\\|= | Output =|\|= |
%       | =|\%|= | Output =|%|= |
%       | =|\nnn|= | Output character <nnn>. <nnn> is a 1-3 decimal number |
%
%   Escape sequences to include arguments  from Arguments. Each time
%   a %-escape sequence is found in   Format  the next argument from
%   Arguments is formatted according to the specification.
%
%       | =|%t|= | print/1 the next item (mnemonic: term) |
%       | =|%w|= | write/1 the next item |
%       | =|%q|= | writeq/1 the next item  |
%       | =|%d|= | display/1 the next item |
%       | =|%n|= | Put the next item as a character |
%       | =|%r|= | Write the next item N times where N is the second item (an integer) |
%       | =|%s|= | Write the next item as a String (so it must be a list of characters) |
%       | =|%f|= |Perform a ttyflush/0 (no items used) |
%       | =|%Nc|= | Write the next item Centered in N columns. |
%       | =|%Nl|= | Write the next item Left justified in N columns. |
%       | =|%Nr|= | Write the next item Right justified in N columns. |
%
%   @deprecated New code should use format/1, format/2, etc.

writef(Format) :-
    writef(Format, []).

writef([F|String], List) :-
    '$writefs'([F|String], List),
    fail.                           % clean up global stack
writef(String, List) :-
    string(String),
    string_codes(String, Fstring),
    '$writefs'(Fstring, List),
    fail.                           % clean up global stack
writef(Format, List) :-
    atom(Format),
    name(Format, Fstring),
    '$writefs'(Fstring, List),
    fail.                           % clean up global stack
writef(_, _).

%!  swritef(-String, +Format) is det.
%!  swritef(-String, +Format, +Arguments) is det.
%
%   Use writef/1 or writef/2 and  write   the  result to a _string_.
%   Note that this is a  string   in  the sense of string_codes/2,
%   _not_ a list of character(-code)s.
%
%   @deprecated.  See format/2,3 and/or with_output_to/2.

swritef(String, Format, Arguments) :-
    with_output_to(string(String), writef(Format, Arguments)).
swritef(String, Format) :-
    with_output_to(string(String), writef(Format)).

                        % Formatted write for a string (i.e. a list of
                        % character codes).

'$writefs'([], _).
'$writefs'([0'%, A|Rest], List) :-      %   %<$action'>
    '$action'(A, List, More),
    !,
    '$writefs'(Rest, More).
'$writefs'([0'%, D|Rest], [Head|Tail]) :-       %   %<columns><just>
    between(0'0, 0'9, D),
    '$getpad'(Size, Just, [D|Rest], More),
    !,
    '$padout'(Head, Size, Just),
    '$writefs'(More, Tail).
'$writefs'([0'\\, C|Rest], List) :-     %   \<special>
    '$special'(C, Char),
    !,
    put(Char),
    '$writefs'(Rest, List).
'$writefs'([0'\\|Rest], List) :-        %   \<character code in decimal>
    '$getcode'(Char, Rest, More),
    !,
    put(Char),
    '$writefs'(More, List).
'$writefs'([Char|Rest], List) :-        %   <ordinary character>
    put(Char),
    '$writefs'(Rest, List).


'$action'(0't, [Head|Tail], Tail) :-    %   Term
    print(Head).
'$action'(0'd, [Head|Tail], Tail) :-    %   Display
    write_canonical(Head).
'$action'(0'w, [Head|Tail], Tail) :-    %   Write
    write(Head).
'$action'(0'q, [Head|Tail], Tail) :-    %   Quoted
    writeq(Head).
'$action'(0'p,  [Head|Tail], Tail) :-   %   Print
    print(Head).
'$action'(0'f, List, List) :-           %   Flush
    ttyflush.
'$action'(0'n, [Char|Tail], Tail) :-    %   iNteger (character)
    put(Char).
'$action'(0'r, [Thing, Times|Tail], Tail) :-    %   Repeatedly
    '$writelots'(Times, Thing).
'$action'(0's, [Head|Tail], Tail) :-    %   String
    '$padout'(Head).

'$special'(0'n, 10).            /*  n  */
'$special'(0'l, 10).            /*  l  */
'$special'(0'r, 10).            /*  r  */
'$special'(0't,  9).            /*  t  */
'$special'(0'\\, 0'\\).         /*  \  */
'$special'(0'%, 0'%).           /*  %  */

'$getcode'(Char, In, Out) :-
    '$getdigits'(3, Digits, In, Out),
    Digits = [_|_],
    name(Char, Digits),
    Char < 128.

'$getdigits'(Limit, [Digit|Digits], [Digit|Out0], Out) :-
    Limit > 0,
    between(0'0, 0'9, Digit),
    Fewer is Limit - 1,
    !,
    '$getdigits'(Fewer, Digits, Out0, Out).
'$getdigits'(_, [], Out, Out).

'$writelots'(N, T) :-
    N > 0,
    !,
    write(T),
    M is N - 1,
    '$writelots'(M, T).
'$writelots'(_, _).

/*  The new formats are %nC, %nL, and %nR for centered, left, and right
    justified output of atoms, integers, and strings.  This is meant to
    simplify the production of tabular output when it is appropriate.
    At least one space will always precede/follow the item written.
*/

'$getpad'(Size, Just, In, Out) :-
    '$getdigits'(3, Digits, In, [Out1|Out]),
    name(Size, Digits),
    '$getpad'(Out1, Just).

'$getpad'(0'r, r).              %  right justified
'$getpad'(0'l, l).              %  left justified
'$getpad'(0'c, c).              %  centered
'$getpad'(0'R, r).              %  right justified
'$getpad'(0'L, l).              %  left justified
'$getpad'(0'C, c).              %  centered


                                %   '$padout'(A, S, J) writes the item A in a
                                %   field of S or more characters, Justified.

'$padout'(String, Size, Just) :-
    '$string'(String),
    !,
    name(Atom, String),
    '$padout'(Atom, Size, Just).
'$padout'(Term, Size, Just) :-
    format(string(Atom), Term, Atom),
    atom_length(Atom, Length),
    '$padout'(Just, Size, Length, Left, Right),
    tab(Left),
    write(Atom),
    tab(Right).

'$string'(0) :- !, fail.
'$string'([]) :- !.
'$string'([H|T]) :-
    '$print'(H),
    !,
    '$string'(T).

'$print'(10).                   % newline
'$print'(9).                    % tab
'$print'(X) :-
    integer(X),
    between(32, 0'~, X).


                                %   '$padout'(Just, Size, Length, Left, Right)
                                %   calculates the number of spaces to put
                                %   on the Left and Right of an item needing
                                %   Length characters in a field of Size.

'$padout'(l, Size, Length, 0, Right) :-
    !,
    Right is max(1, Size-Length).
'$padout'(r, Size, Length, Left, 0) :-
    !,
    Left is max(1, Size-Length).
'$padout'(c, Size, Length, Left, Right) :-
    Left is max(1, round((Size - Length)/2)),
    Right is max(1, Size - Length - Left).

                                %   '$padout'(Str) writes a string.

'$padout'([Head|Tail]) :-
    !,
    put(Head),
    '$padout'(Tail).
'$padout'([]).