[REBOL] Re: Fixed (width) dialect
From: al:bri:xtra at: 28-Aug-2002 17:15
Gregg wrote:
> Cool Andrew!
Thanks!
> Since the logic! definition doesnt' take a size, it's limited to single
char values? Boolean/logic values are something that always give me a
headache because it seems like nobobdy could agree on what chars or strings
to map to.
Currently, the DOS/Windows 3.1 application only use a single character to
store logic values, so I haven't implemented multicharacter versions. It's
fairly easy to add, I feel. Once you do so, please let me have a copy!
The latest version is attached (and I've sent a copy directly to you).
Andrew Martin
Super Cool Rebolutionary! :)
ICQ: 26227169 http://valley.150m.com/
-><-
-- Attached file included as plaintext by Listar --
-- File: Fixed.r
Rebol [
Name: 'Fixed
Title: "Fixed"
File: %Fixed.r
Author: "Andrew Martin"
eMail: [Al--Bri--xtra--co--nz]
Web: http://valley.150m.com
Date: 28/August/2002
Version: 1.1.0
Purpose: {Cuts up fixed width data file into Rebol values in an association.}
Category: [util 1]
]
Fixed: function [
{Cuts up fixed width data file into Rebol values in an association.}
Data [string! binary!] "The data file"
Dialect [block!] "The dialect controlling the cutting."
/Debug "Prints out the Fields and Values as they occur."
] [
Values Field Width Widths Value Type Previous With Line
] [
Values: make block! 200
parse Dialect [
any [
[
'skip (Width: 1) opt [set Width integer!] (
Data: next at Data Width
)
]
| [
set Field string! (Width: 1 Type: string!) [
[
[set Width integer! set Type 'binary!] (
Value: attempt [to-binary copy/part Data Width]
)
]
| [
[set Width integer! set Type ['issue! | 'integer! | 'date! | 'binary!]] (
Value: attempt [load trim to-string copy/part Data Width]
)
]
| [
[set Widths integer! 'char!] (
use [Block] [
Block: make block! Widths
loop Widths [
Value: trim to-string copy/part Data 1
Data: next Data
if not empty? Value [
append Block to-char Value
]
]
Value: Block
]
Width: 0
)
]
| [
['char!] (
Value: attempt [to-char trim to-string copy/part Data Width]
)
]
| [
['logic!] (
Value: switch/default trim to-string copy/part Data Width [
"Y" [true]
"N" [none]
] [
none
]
)
]
| [
[set Width integer! (With: none) opt [/with set With string!]] (
Value: to-string copy/part Data Width
either With [
trim/with Value With
][
trim Value
]
)
]
| [
[set Widths into [some integer!]] (
Value: make string! 100
foreach Width Widths [
Line: trim to-string copy/part Data Width
if not empty? Line [
append Line newline
]
Data: next at Data Width
append Value Line
]
Width: 0
)
]
] (
Data: at Data 1 + Width
Previous: associate? Values Field
either Previous: associate? Values Field [
either series? Previous [
append Previous Value
] [
if not empty? Value [
associate-many Values Field Value
]
]
] [
if any [
all [
series? Value
not empty? Value
]
not series? Value
] [
associate Values Field Value
]
]
if Debug [
print rejoin [Field ": " mold Value "."]
]
)
]
| [
set Value any-type! set Value1 any-type! (
print rejoin ["Error: Couldn't understand: " mold Value "&" mold Value1 "!"]
halt
)
]
]
end
]
reduce [Values Data]
]