Posted to tcl by aspect at Fri Jul 08 02:38:06 GMT 2011view raw
- package provide parse
- namespace eval ::parse {
- proc date {data} { ;# non-unix date format
- if {[string length $data] != 7} {
- error "Incorrect input!"
- }
- ::binary scan $data Iccccc Y m d H M S
- clock scan "$Y-$M-$D $H:$M:$S"
- }
- proc csv {s args} {
- if {[llength $args] == 1} { set args [lindex $args 0] }
- array set res {}
- foreach key $args value [split $s ,] {
- set res($key) $value
- }
- array get res
- }
- proc binary {s args} {
- array set typemap {
- u64 Wu
- u32 Iu
- u16 Su
- u8 cu
- s64 W
- s32 I
- s16 S
- s8 c
- }
- if {[llength $args] == 1} { set args [lindex $args 0] }
- set bs {}
- set targets {}
- foreach {type name} $args {
- if {[array names typemap $type] != ""} {
- append bs $typemap($type)
- } else {
- append bs $type
- }
- lappend targets rec($name)
- }
- array set rec {}
- ::binary scan $s $bs {*}$targets
- array get rec
- }
- }