Posted to tcl by aspect at Fri Jul 08 02:38:06 GMT 2011view raw

  1. package provide parse
  2.  
  3. namespace eval ::parse {
  4. proc date {data} { ;# non-unix date format
  5. if {[string length $data] != 7} {
  6. error "Incorrect input!"
  7. }
  8. ::binary scan $data Iccccc Y m d H M S
  9. clock scan "$Y-$M-$D $H:$M:$S"
  10. }
  11.  
  12. proc csv {s args} {
  13. if {[llength $args] == 1} { set args [lindex $args 0] }
  14. array set res {}
  15. foreach key $args value [split $s ,] {
  16. set res($key) $value
  17. }
  18. array get res
  19. }
  20.  
  21. proc binary {s args} {
  22. array set typemap {
  23. u64 Wu
  24. u32 Iu
  25. u16 Su
  26. u8 cu
  27. s64 W
  28. s32 I
  29. s16 S
  30. s8 c
  31. }
  32. if {[llength $args] == 1} { set args [lindex $args 0] }
  33. set bs {}
  34. set targets {}
  35. foreach {type name} $args {
  36. if {[array names typemap $type] != ""} {
  37. append bs $typemap($type)
  38. } else {
  39. append bs $type
  40. }
  41. lappend targets rec($name)
  42. }
  43. array set rec {}
  44. ::binary scan $s $bs {*}$targets
  45. array get rec
  46. }
  47. }
  48.