From 372e04c634b4c3e51979e188ac12811b988cad82 Mon Sep 17 00:00:00 2001 From: koennecke Date: Thu, 7 Dec 2006 14:07:53 +0000 Subject: [PATCH] - Added a Tcl template processing facility to SICS --- tcl/ritaframe | 51 +++++++++++++++++++++++++++++++++++ tcl/tjxp | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++ tcl/tjxphelp | 38 +++++++++++++++++++++++++++ 3 files changed, 162 insertions(+) create mode 100755 tcl/ritaframe create mode 100755 tcl/tjxp create mode 100644 tcl/tjxphelp diff --git a/tcl/ritaframe b/tcl/ritaframe new file mode 100755 index 00000000..97b46a3e --- /dev/null +++ b/tcl/ritaframe @@ -0,0 +1,51 @@ +#!/usr/bin/tclsh +#-------------------------------------------------------------------------- +# script for extracting a frame of rita data from a file and to dump +# the frame into an ASCII file +# +# Mark Koennecke, November 2006 +#------------------------------------------------------------------------- +set loadnx "/afs/psi.ch/project/sinq/sl-linux/lib/" +load [file join $loadnx nxinter.so] + +if {$argc < 2} { + puts stdout "Usage:\n\tritaframe filename number" + exit 1 +} +set num [lindex $argv 1] + +set f [nx_open [lindex $argv 0] $NXACC_READ] +nx_openpath $f /entry1/data/counts +set info [nx_getinfo $f] +set dim1 [expr int([get_nxds_value $info 2])] +set dim2 [expr int([get_nxds_value $info 3])] +set nFrames [expr int([get_nxds_value $info 4])] +if {$num < 0 || $num > $nFrames-1} { + nx_close $f + puts stdout "Requested frame out of range" + exit1 +} +set start [create_nxds 1 $NX_INT32 3] +set end [create_nxds 1 $NX_INT32 3] +put_nxds_value $start 0 0 +put_nxds_value $start 0 1 +put_nxds_value $start $num 2 + +put_nxds_value $end $dim1 0 +put_nxds_value $end $dim2 1 +put_nxds_value $end 1 2 + +set data [nx_getslab $f $start $end] +for {set y 0} {$y < $dim2} {incr y} { + for {set x 0} {$x < $dim1} {incr x} { + set val [expr int([get_nxds_value $data $x $y])] + puts -nonewline stdout [format " %8d" $val] + } + puts stdout "" +} +drop_nxds $start +drop_nxds $end +drop_nxds $data + +nx_close $f +exit 0 diff --git a/tcl/tjxp b/tcl/tjxp new file mode 100755 index 00000000..3f7682c8 --- /dev/null +++ b/tcl/tjxp @@ -0,0 +1,73 @@ +#!/usr/bin/tclsh +#---------------------------------------------------------------------- +# This is a Tcl template processor in the style of JSP tags. Unmarked +# text is left alone. But there is special markup: +# <% script %> execute Tcl script and output result +# <%=var%> print The Tcl variable var +# <%! script%> execute the script and print nothing +# +# copyright: GPL +# +# Mark Koennecke, November 2006 +#---------------------------------------------------------------------- +proc loadTemplate {input} { + return [read $input] +} +#--------------------------------------------------------------------- +proc processScript {script} { + set startChar [string index $script 0] + if {[string equal $startChar =] == 1 } { + set varName [string trim [string range $script 1 end]] + set cmd [format "return \$%s" $varName] + return [uplevel #0 $cmd] + } elseif {[string equal $startChar !] == 1} { + set script [string range $script 1 end] + uplevel #0 $script + } else { + return [uplevel #0 $script] + } + return "" +} +#---------------------------------------------------------------------- +# process The template: read template from input, +# write to output channel +#---------------------------------------------------------------------- +proc processTemplate {input output} { + set template [loadTemplate $input] + set current 0 + set start [string first "<%" $template] + set end [string first "%>" $template $start] + while {$start >= 0} { + if {$end < 0} { + error "Found start tag but no end in $template" + } + puts -nonewline $output [string range $template $current \ + [expr $start -1]] + set script [string range $template [expr $start +2] \ + [expr $end -1]] + set txt [processScript $script] + if {[string length $txt] >= 1} { + puts -nonewline $output $txt + } + set template [string range $template [expr $end +2] end] + set start [string first "<%" $template] + set end [string first "%>" $template $start] + + } + puts -nonewline $output $template +} +#================ MAIN ================================================ +if {$argc < 2} { + puts stdout "Usage:\n\ttjxp infile outfile" + puts stdout "\t Outfile can be - for stdout" + exit 1 +} +set in [open [lindex $argv 0] r] +set outfile [lindex $argv 1] +if {[string equal [string trim $outfile] -] == 1} { + set out stdout +} else { + set out [open $outfile w] +} +processTemplate $in $out +exit 0 diff --git a/tcl/tjxphelp b/tcl/tjxphelp new file mode 100644 index 00000000..c5847ac1 --- /dev/null +++ b/tcl/tjxphelp @@ -0,0 +1,38 @@ + + + Tcl Template Processing System. + + This is a test file and help text for my Tcl template processing + system. It was inspired by JSP and JXP. But is in my favourite + scripting language Tcl. Basically it allows to mix Tcl scripts with + text. The text can contain special marks which are then executed as + Tcl scripts in a variety of ways. Three tags are supported: + + <%! set var waschmaschine %> + + This tag executes the Tcl script but prints nothing, except may + be error messages. Please note that this can be used to source + more Tcl files which contains procedures you need for doing your + work. + + <%=var %> prints the value of the Tcl variable var. When processed, + this should print waschmaschine. + + <% set a [list 1 2 3] + join $a + %> + executes the Tcl code within and prints the result. This should be + 1 2 3. + + All Tcl code is executed at global level. There is nothing more to + this. All this was done in 75 lines of Tcl, including comments! You + should be able to process this file through tjxp to see what you get. + Txjp is brough to you by: + + Mark Koennecke, Mark.Koennecke@psi.ch + + txjp is copyrighted under the GNU Public Licence 2.0, which you can + find elsewhere. + + Enjoy! + \ No newline at end of file