; XMAS
;
; Program for Proteus
;
; (C) 2000-2004 Simone Zanella Productions
;
; Print a text in shape of a tree, with a single character at the top, 3 characters in the
; second row, 5 characters in the third, etc.
#!proteus -z
IF LT(ARGC, 6)
CONSOLELN "Syntax: " ARGV(1) " " ARGV(2) " source.txt destination.txt"
CONSOLELN ""
CONSOLELN "Purpose: writes to destination.txt the contents of source.txt,"
CONSOLELN "in shape of a tree."
ABORT 0
FI
; Load file to a string
S = FLOAD(ARGV(5))
IF ISEMPTY(S)
CONSOLELN "File " ARGV(5) " not found or empty!"
ABORT 1
FI
; Open destination
D = FOPEN(ARGV(6), 28)
IF EQ(D, -1)
CONSOLELN "Could not create " ARGV(6) "!"
ABORT 2
FI
; Replace end-of-line with a single blank
STRTRAN(@S, EOL, " ")
; Replace multiple blanks with a single blank
P = STRSTR(S, " ")
WHILE NEQ(P, 0)
STRTRAN(@S, " ", " ")
P = STRSTR(S, " ")
LOOP
; Create tree rows
Q = QUEUENEW()
MAXLEN = 1
WHILE STRLEN(S)
ENQUEUE(Q, LEFT(S, MAXLEN))
RESTFROM(@S, INC(MAXLEN))
ADD(@MAXLEN, 2)
LOOP
; Print tree
WHILE QUEUELEN(Q)
FWRITELN(D, RTRIM(CENTER(DEQUEUE(Q), MAXLEN), " "))
LOOP
QUEUEFREE(Q)
FCLOSE(D)
ABORT 0