utilities.tcl

Go to the documentation of this file.
00001 /*  utilities.tcl --*/
00002 /* */
00003 /*  Miscellaneous extension functions for XSLT.*/
00004 /* */
00005 /*  Copyright (c) 2007 Explain*/
00006 /*  http://www.explain.com.au/*/
00007 /*  Copyright (c) 2004 Zveno Pty Ltd*/
00008 /*  http://www.zveno.com/*/
00009 /* */
00010 /*  See the file "LICENSE" in this distribution for information on usage and*/
00011 /*  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.*/
00012 /* */
00013 /*  $Id: utilities.tcl,v 1.1 2004/09/29 23:51:57 balls Exp $*/
00014 
00015 package provide xslt::utilities 1.2
00016 
00017 namespace xslt::utilities {
00018     namespace export character-first decode-base64
00019 }
00020 
00021 /*  xslt::utilities::character-first --*/
00022 /* */
00023 /*  Returns the character that occurs first from a string*/
00024 /*  of possible characters.*/
00025 /* */
00026 /*  Arguments:*/
00027 /*  src source string*/
00028 /*  chars   characters to find*/
00029 /*  args    not needed*/
00030 /* */
00031 /*  Results:*/
00032 /*  Returns a character or empty string.*/
00033 
00034 ret  xslt::utilities::character-first (type srcNd , type charsNd , type args) {
00035     if {[llength $args]} {
00036     return -code error "too many arguments"
00037     }
00038 
00039     set src $srcNd
00040     catch {set src [dom::node stringValue [lindex $srcNd 0]]}
00041     set chars $charsNd
00042     catch {set chars [dom::node stringValue [lindex $charsNd 0]]}
00043 
00044     regsub -all {([\\\[\]^$-])} $chars {\\\1} chars
00045     if {[regexp [format {([%s])} $chars] $src dummy theChar]} {
00046     return $theChar
00047     }
00048 
00049     return {}
00050 }
00051 
00052 /*  xslt::utilities::decode-base64 --*/
00053 /* */
00054 /*  Returns decoded (binary) base64-encoded data.*/
00055 /* */
00056 /*  Arguments:*/
00057 /*  src source string*/
00058 /*  args    not needed*/
00059 /* */
00060 /*  Results:*/
00061 /*  Returns binary data.*/
00062 
00063 ret  xslt::utilities::decode-base64 (type srcNd , type args) {
00064     if {[llength $args]} {
00065     return -code error "too many arguments"
00066     }
00067 
00068     if {[catch {package require base64}]} {
00069     return {}
00070     }
00071 
00072     set src $srcNd
00073     catch {set src [dom::node stringValue [lindex $srcNd 0]]}
00074 
00075     return [base64::decode $src]
00076 }
00077 
00078 /*  xslt::utilities::binary-document --*/
00079 /* */
00080 /*  Writes binary data into a document*/
00081 /*  (this should be an extension element)*/
00082 /* */
00083 /*  Arguments:*/
00084 /*  fname   filename*/
00085 /*  data    binary data*/
00086 /*  args    not needed*/
00087 /* */
00088 /*  Results:*/
00089 /*  File opened for writing and data written.*/
00090 /*  Returns 1 on success, 0 otherwise*/
00091 
00092 ret  xslt::utilities::binary-document (type fnameNd , type srcNd , type args) {
00093     if {[llength $args]} {
00094     return -code error "too many arguments"
00095     }
00096 
00097     set fname $fnameNd
00098     catch {set fname [dom::node stringValue [lindex $fnameNd 0]]}
00099     set data $dataNd
00100     catch {set data [dom::node stringValue [lindex $dataNd 0]]}
00101 
00102     if {[catch {open $fname w} ch]} {
00103     return 0
00104     }
00105     fconfigure $ch -trans binary -encoding binary
00106     puts -nonewline $ch $data
00107     close $ch
00108 
00109     return 1
00110 }
00111 
00112 /*  xslt::utilities::base64-binary-document --*/
00113 /* */
00114 /*  Returns base64-encoded data from a file.*/
00115 /* */
00116 /*  Arguments:*/
00117 /*  fname   filename*/
00118 /*  args    not needed*/
00119 /* */
00120 /*  Results:*/
00121 /*  Returns text.  Returns empty string on error.*/
00122 
00123 ret  xslt::utilities::base64-binary-document (type fnameNd , type args) {
00124     if {[llength $args]} {
00125     return -code error "too many arguments"
00126     }
00127 
00128     if {[catch {package require base64}]} {
00129     return {}
00130     }
00131 
00132     set fname $fnameNd
00133     catch {set fname [dom::node stringValue [lindex $fnameNd 0]]}
00134 
00135     if {[catch {open $fname} ch]} {
00136     return {}
00137     }
00138     fconfigure $ch -trans binary -encoding binary
00139     set data [read $ch]
00140     close $ch
00141 
00142     return [base64::encode $data]
00143 }
00144 
00145 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1