home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activetcltk / ActiveTcl8.3.4.1-8.win32-ix86.exe / ActiveTcl8.3.4.1-win32-ix86 / demos / TclXML / xmlwc < prev   
Encoding:
Text File  |  2001-10-22  |  1.5 KB  |  86 lines

  1. #!/bin/sh
  2. # \
  3. exec tclsh8.3 "$0" "$@"
  4.  
  5. package require xml
  6.  
  7. set bytes 0
  8. set chars 0
  9. set words 0
  10. set lines 0
  11.  
  12. proc cdata {data args} {
  13.     global bytes chars words lines
  14.  
  15.     incr bytes [string bytelength $data]
  16.     incr chars [string length $data]
  17.     incr lines [regsub -all \n $data {} discard]
  18.     regsub -all "\[ \t\r\n\]+" [string trim $data] { } data
  19.     incr words [regsub -all { } $data {} discard]
  20.  
  21.     return {}
  22. }
  23.  
  24. set format {%1$7d%2$8d%3$8d %5$10s}
  25.  
  26. set input {}
  27. foreach opt $argv {
  28.     switch -- $opt {
  29.     --bytes {
  30.         set format {%4$7d}
  31.     }
  32.  
  33.     -c -
  34.     --chars {
  35.         set format {%3$7d}
  36.     }
  37.  
  38.     -w -
  39.     --words {
  40.         set format {%2$7d}
  41.     }
  42.  
  43.     -l -
  44.     --lines {
  45.         set format {%1$7d}
  46.     }
  47.  
  48.     -h -
  49.     --help {
  50.         puts stderr "$argv0 \[-clw\] \[--bytes\] \[--chars\] \[--words\] \[--lines\] \[--help\] \[--version\] \[file...\]"
  51.         puts stderr "Counts number of bytes, characters, words and lines in an XML document"
  52.     }
  53.     --version {
  54.         puts stderr "xmlwc version 1.0"
  55.     }
  56.  
  57.     default {
  58.         lappend input $opt
  59.     }
  60.     }
  61. }
  62.  
  63. if {![llength $input]} {
  64.     set p [xml::parser -characterdatacommand cdata]
  65.     if {[catch {$p parse [read stdin]} err]} {
  66.     puts stderr $err
  67.     exit 1
  68.     }
  69. } else {
  70.     foreach in $input {
  71.     if {[catch {open $in} ch]} {
  72.         puts stderr "unable to open file \"$in\""
  73.         exit 1
  74.     }
  75.     set p [xml::parser -characterdatacommand cdata]
  76.     if {[catch {$p parse [read $ch]} err]} {
  77.         puts stderr $err
  78.         exit 1
  79.     }
  80.     catch {close $ch}
  81.     }
  82. }
  83.  
  84. puts [format $format $lines $words $chars $bytes $input]
  85. exit 0
  86.