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.*/ |
||
w= 0; wb= 0; !.= 0; $= /*W (max width name), bits, names. */ |
|||
p.= 0; p.0= 1 /*P.α is structure bit position. */ |
|||
/* [↓] read the input text file (iFID)*/ |
|||
⚫ | |||
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 |
q= strip(q); if q=='' then iterate /*strip leading and trailing blanks. */ |
||
_L= left(q, 1); _R= right(q, 1) |
_L= left(q, 1); _R= right(q, 1) /*get extreme left and right characters*/ |
||
⚫ | |||
if _L=='+' then do |
if _L=='+' then do; if verify(q, '+-')\==0 then say er "(invalid grid):" q |
||
iterate /*skip this record, it's a single "+". */ |
|||
iterate /*skip this record, it's a single "+". */ |
|||
end |
end |
||
⚫ | |||
end |
|||
do until q=='|'; parse var q '|' n "|" -1 q; x= n /*parse record for names.*/ |
|||
n= strip(n); w= max(w, length(n) ); if n=='' then leave /*is N null? */ |
|||
if words(n)\==1 then do; say er'(invalid name): ' n; iterate j |
|||
end /* [↑] add more name validations. */ |
|||
$$= $; 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. */ |
|||
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, !.#) /* |
wb= max(wb, !.#) /*max # of bits; # names prev. to this.*/ |
||
prev= # - 1 |
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 |
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? */ |
||
L= length(test); if L==0 then exit /*stick a fork in it, we're all done. */ |
|||
bits= x2b(test) |
bits= x2b(test) /*convert test data to a bit string. */ |
||
wm=length( x2d( b2x( copies(1, wb) ) ) ) + 1 |
wm= length( x2d( b2x( copies(1, wb) ) ) ) + 1 /*used for displaying max width numbers*/ |
||
say 'test (hex)=' test " length=" |
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) |
||
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) |
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= when using the default input:}} |
{{out|output|text= when using the default input:}} |