1% ---------------------------------------------------------------------- 2% BEGIN LICENSE BLOCK 3% Version: CMPL 1.1 4% 5% The contents of this file are subject to the Cisco-style Mozilla Public 6% License Version 1.1 (the "License"); you may not use this file except 7% in compliance with the License. You may obtain a copy of the License 8% at www.eclipse-clp.org/license. 9% 10% Software distributed under the License is distributed on an "AS IS" 11% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 12% the License for the specific language governing rights and limitations 13% under the License. 14% 15% The Original Code is The ECLiPSe Constraint Logic Programming System. 16% The Initial Developer of the Original Code is Cisco Systems, Inc. 17% Portions created by the Initial Developer are 18% Copyright (C) 1990-2006 Cisco Systems, Inc. All Rights Reserved. 19% 20% Contributor(s): ECRC GmbH 21% 22% END LICENSE BLOCK 23% 24% System: ECLiPSe Constraint Logic Programming System 25% Version: $Id: pretty_print.pl,v 1.2 2009/07/16 09:11:24 jschimpf Exp $ 26% ---------------------------------------------------------------------- 27 28/* 29 * SEPIA PROLOG SOURCE MODULE 30 * 31 * IDENTIFICATION: pretty_print.pl 32 * 33 * DESCRIPTION: Utility predicates, for user convenience. 34 * 35 * CONTENTS: 36 * 37 */ 38 39 40:- module(pretty_print). 41 42:- comment(categories, ["Development Tools"]). 43:- comment(summary, "Pretty-printing of complex terms"). 44:- comment(author, "Micha Meier, ECRC Munich"). 45:- comment(copyright, "Cisco Systems, Inc"). 46:- comment(date, "$Date: 2009/07/16 09:11:24 $"). 47:- comment(pretty_print/3, [template:"pretty_print(+Stream, +Term, +Max)", 48 summary:"Print a term on the given stream, split it if its width exceeds Max" 49 ]). 50 51:- export pretty_print/3. 52 53% Print a term on the given stream, split it if its size exceeds Max 54pretty_print(Stream, Term, Max) :- 55 open(string(""), write, S), 56 pp(S, Term, 0, Max), 57 get_stream_info(S, name, String), 58 close(S), 59 write(Stream, String). 60 61% First try to print the term using write/2, if it is too big, 62% split it onto separate lines 63pp(S, Term, Offset, MaxSize) :- 64 at(S, Start), 65 write(S, Term), 66 (at(S) < Start + MaxSize -> 67 true 68 ; 69 seek(S, Start), % rewind the output 70 NewOffset is Offset + 3, 71 functor(Term, F, N), 72 printf(S, "%a(\n", F), 73 pp_arg(S, 1, N, Term, NewOffset, MaxSize), 74 printf(S, "%*c)", [Offset, 0' ]) 75 ). 76 77pp_arg(S, N, N, Term, Off, MaxSize) :- 78 !, 79 printf(S, "%*c", [Off, 0' ]), 80 arg(N, Term, Arg), 81 pp(S, Arg, Off, MaxSize), 82 nl(S). 83pp_arg(S, I, N, Term, Off, MaxSize) :- 84 printf(S, "%*c", [Off, 0' ]), 85 arg(I, Term, Arg), 86 pp(S, Arg, Off, MaxSize), 87 write(S, ',\n'), 88 I1 is I + 1, 89 pp_arg(S, I1, N, Term, Off, MaxSize). 90 91