ASCII art diagram converter: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added/changed whitespace and comments, optimized the code.)
Line 1,134: Line 1,134:
<lang rexx>/*REXX program interprets an ASCII art diagram for names and their bit length(s). */
<lang rexx>/*REXX program interprets an ASCII art diagram for names and their bit length(s). */
numeric digits 100 /*be able to handle large numbers. */
numeric digits 100 /*be able to handle large numbers. */
er = '***error*** illegal input txt' /*a literal used for error messages. */
parse arg iFID test . /*obtain optional input─FID & test─data*/
parse arg iFID test . /*obtain optional input─FID & test─data*/
if iFID=='' | iFID=="," then iFID= 'ASCIIART.TXT' /*use the default iFID.*/
if iFID=='' | iFID=="," then iFID= 'ASCIIART.TXT' /*use the default iFID.*/
if test=='' | test=="," then test= 'cafe8050800000808080000a' /* " " " data.*/
if test=='' | test=="," then test= 'cafe8050800000808080000a' /* " " " data.*/
e = '***error*** illegal input txt' /*a literal used for error messages. */
w= 0; wb= 0; !.= 0; $= /*W (max width name), bits, names. */
w= 0; wb= 0; !.= 0; $= /*W (max width name), bits, names. */
p.= 0; p.0= 1 /*P.α is structure bit position. */
p.= 0; p.0= 1 /*P.α is structure bit position. */
/* [↓] read the input text file (iFID)*/
/* [↓] read the input text file. */
do j=1 while lines(iFID)\==0; q= linein(iFID); say '■■■■■text►'q
do j=1 while lines(iFID)\==0; q= linein(iFID); say '■■■■■text►'q
q=strip(q); if q=='' then iterate /*strip leading and trailing blanks. */
q= strip(q); if q=='' then iterate /*strip leading and trailing blanks. */
_L= left(q, 1); _R= right(q, 1) /*get extreme left and right characters*/
_L= left(q, 1); _R= right(q, 1) /*get extreme left and right characters*/
/* [↓] is this record an "in-between"?*/

if _L=='+' then do /*is this record an "in-between" ? */
if _L=='+' then do; if verify(q, '+-')\==0 then say er "(invalid grid):" q
if verify(q, '+-')\==0 then say e "(invalid grid):" q
iterate /*skip this record, it's a single "+". */
iterate /*skip this record, it's a single "+". */
end
end
if _L\=='|' | _R\=="|" then do; say er '(boundary): ' q; iterate

if _L\=='|' | _R\=="|" then do; say e '(boundary): ' q; iterate; end
end
do until q=='|'; parse var q '|' n "|" -1 q; x= n /*parse record for names.*/

do until q=='|' /* [↓] parse a record for names. */
n= strip(n); w= max(w, length(n) ); if n=='' then leave /*is N null? */
parse var q '|' n "|" -1 q; x=n; n=strip(n); w=max(w, length(n))
if words(n)\==1 then do; say er'(invalid name): ' n; iterate j
if n=='' then leave /*Is N null? We're done scanning. */
end /* [↑] add more name validations. */
if words(n)\==1 then do; say e'(invalid name): ' n; iterate j; end
$$= $; nn= n; upper $$ n /*N could be a mixed─case name. */
if wordpos(nn, $$)\==0 then do; say er '(dup name):' n; iterate j
/* [↑] add more name validations. */
$$= $; nn= n; upper $$ n /*N can be a mixed─case name. */
end
if wordpos(nn, $$)\==0 then do; say e '(dup name):' n; iterate j; end
$= $ n /*add the N (name) to the $ list. */
$= $ n /*add the N (name) to the $ list. */
#= words($); !.#= (length(x) + 1) % 3 /*assign the number of bits for N. */
#= words($); !.#= (length(x) + 1) % 3 /*assign the number of bits for N. */
wb= max(wb, !.#) /*find the maximum number of bits. */
wb= max(wb, !.#) /*max # of bits; # names prev. to this.*/
prev= # - 1 /*number of names previous to this name*/
prev= # - 1; p.#= p.prev + !.prev /*number of names previous to this name*/
p.#= p.prev + !.prev /*calculate the structure bit position.*/
end /*until*/
end /*until*/
end /*j*/
end /*j*/


if j==1 then do; say e '(file not found): ' iFID; exit 12; end
if j==1 then do; say er '(file not found): ' iFID; exit 12
end
say
say
do k=1 for words($)
do k=1 for words($)
Line 1,173: Line 1,171:
end /*k*/
end /*k*/
say /* [↓] Any (hex) data to test? */
say /* [↓] Any (hex) data to test? */
if test=='' then exit /*stick a fork in it, we're all done. */
L= length(test); if L==0 then exit /*stick a fork in it, we're all done. */
bits= x2b(test); L= length(test) /*convert test data to a bit string. */
bits= x2b(test) /*convert test data to a bit string. */
wm=length( x2d( b2x( copies(1, wb) ) ) ) + 1 /*used for displaying maximum numbers. */
wm= length( x2d( b2x( copies(1, wb) ) ) ) + 1 /*used for displaying max width numbers*/
say 'test (hex)=' test " length=" L 'hexadecimal digits.'
say 'test (hex)=' test " length=" L 'hexadecimal digits.'
say
say
do r=1 by 8+8 to L*4
do r=1 by 8+8 to L*4; _1= substr(bits, r, 8, 0); _2= substr(bits, r+8, 8, 0)
_1= substr(bits, r, 8, 0); _2= substr(bits, r+8, 8, 0)
say 'test (bit)=' _1 _2 " hex=" b2x(_1) b2x(_2)
say 'test (bit)=' _1 _2 " hex=" b2x(_1) b2x(_2)
end /*r*/
end /*r*/
say
say
do m=1 for words($) /* [↓] display hex string in lowercase*/
do m=1 for words($) /* [↓] display hex string in lowercase*/
_= translate( b2x( substr( bits, p.m, !.m) ), 'abcdef', "ABCDEF" )
_= translate( b2x( substr( bits, p.m, !.m) ), 'abcdef', "ABCDEF" )
say right( word($, m), w+2) ' decimal='right( x2d(_), wm) ' hex=' _
say right( word($, m), w+2) ' decimal='right( x2d(_), wm) " hex=" _
end /*m*/ /*stick a fork in it, we're all done. */</lang>
end /*m*/ /*stick a fork in it, we're all done. */</lang>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}