base32hex.tcl

Go to the documentation of this file.
00001 /*  -*- tcl -*-*/
00002 /*  This code is hereby put into the public domain.*/
00003 /*  ### ### ### ######### ######### #########*/
00004 /*  Overview*/
00005 /*  Base32 encoding and decoding of small strings.*/
00006 /* */
00007 /*  Management code for switching between Tcl and C accelerated*/
00008 /*  implementations.*/
00009 /* */
00010 /*  RCS: @(#) $Id: base32hex.tcl,v 1.2 2007/03/23 17:31:17 andreas_kupries Exp $*/
00011 
00012 /*  @mdgen EXCLUDE: base32hex_c.tcl*/
00013 
00014 package require Tcl 8.4
00015 
00016 namespace ::base32::hex {}
00017 
00018 /*  ### ### ### ######### ######### #########*/
00019 /*  Management of base32 std implementations.*/
00020 
00021 /*  ::base32::hex::LoadAccelerator --*/
00022 /* */
00023 /*  Loads a named implementation, if possible.*/
00024 /* */
00025 /*  Arguments:*/
00026 /*  key Name of the implementation to load.*/
00027 /* */
00028 /*  Results:*/
00029 /*  A boolean flag. True if the implementation*/
00030 /*  was successfully loaded; and False otherwise.*/
00031 
00032 ret  ::base32::hex::LoadAccelerator (type key) {
00033     variable accel
00034     set isok 0
00035     switch -exact -- $key {
00036     critcl {
00037         # Critcl implementation of base32 requires Tcl 8.4.
00038         if {![package vsatisfies [package provide Tcl] 8.4]} {return 0}
00039         if {[catch {package require tcllibc}]} {return 0}
00040         set isok [llength [info commands ::base32::hex::critcl_encode]]
00041     }
00042     tcl {
00043         variable selfdir
00044         if {[catch {source [file join $selfdir base32hex_tcl.tcl]}]} {return 0}
00045         set isok [llength [info commands ::base32::hex::tcl_encode]]
00046     }
00047         default {
00048             return -code error "invalid accelerator $key:\
00049                 must be one of [join [KnownImplementations] {, }]"
00050         }
00051     }
00052     set accel($key) $isok
00053     return $isok
00054 }
00055 
00056 /*  ::base32::hex::SwitchTo --*/
00057 /* */
00058 /*  Activates a loaded named implementation.*/
00059 /* */
00060 /*  Arguments:*/
00061 /*  key Name of the implementation to activate.*/
00062 /* */
00063 /*  Results:*/
00064 /*  None.*/
00065 
00066 ret  ::base32::hex::SwitchTo (type key) {
00067     variable accel
00068     variable loaded
00069 
00070     if {[string equal $key $loaded]} {
00071     # No change, nothing to do.
00072     return
00073     } elseif {![string equal $key ""]} {
00074     # Validate the target implementation of the switch.
00075 
00076     if {![info exists accel($key)]} {
00077         return -code error "Unable to activate unknown implementation \"$key\""
00078     } elseif {![info exists accel($key)] || !$accel($key)} {
00079         return -code error "Unable to activate missing implementation \"$key\""
00080     }
00081     }
00082 
00083     # Deactivate the previous implementation, if there was any.
00084 
00085     if {![string equal $loaded ""]} {
00086     foreach c {encode decode} {
00087         rename ::base32::hex::$c ::base32::hex::${loaded}_$c
00088     }
00089     }
00090 
00091     # Activate the new implementation, if there is any.
00092 
00093     if {![string equal $key ""]} {
00094     foreach c {encode decode} {
00095         rename ::base32::hex::${key}_$c ::base32::hex::$c
00096     }
00097     }
00098 
00099     # Remember the active implementation, for deactivation by future
00100     # switches.
00101 
00102     set loaded $key
00103     return
00104 }
00105 
00106 /*  ::base32::hex::Implementations --*/
00107 /* */
00108 /*  Determines which implementations are*/
00109 /*  present, i.e. loaded.*/
00110 /* */
00111 /*  Arguments:*/
00112 /*  None.*/
00113 /* */
00114 /*  Results:*/
00115 /*  A list of implementation keys.*/
00116 
00117 ret  ::base32::hex::Implementations () {
00118     variable accel
00119     set res {}
00120     foreach n [array names accel] {
00121     if {!$accel($n)} continue
00122     lappend res $n
00123     }
00124     return $res
00125 }
00126 
00127 /*  ::base32::hex::KnownImplementations --*/
00128 /* */
00129 /*  Determines which implementations are known*/
00130 /*  as possible implementations.*/
00131 /* */
00132 /*  Arguments:*/
00133 /*  None.*/
00134 /* */
00135 /*  Results:*/
00136 /*  A list of implementation keys. In the order*/
00137 /*  of preference, most prefered first.*/
00138 
00139 ret  ::base32::hex::KnownImplementations () {
00140     return {critcl tcl}
00141 }
00142 
00143 /*  ### ### ### ######### ######### #########*/
00144 /*  Initialization: Data structures.*/
00145 
00146 namespace ::base32::hex {
00147     variable  selfdir [file dirname [info script]]
00148     variable  
00149 
00150     variable  accel
00151     array  accel =    {tcl 0 critcl 0}
00152 }
00153 
00154 /*  ### ### ### ######### ######### #########*/
00155 /*  Initialization: Choose an implementation,*/
00156 /*  most prefered first. Loads only one of the*/
00157 /*  possible implementations. And activates it.*/
00158 
00159 namespace ::base32::hex {
00160     variable e
00161     foreach e [KnownImplementations] {
00162     if {[LoadAccelerator $e]} {
00163         SwitchTo $e
00164         break
00165     }
00166     }
00167     un e = 
00168 
00169     namespace export encode decode
00170 }
00171 
00172 /*  ### ### ### ######### ######### #########*/
00173 /*  Ready*/
00174 
00175 package provide base32::hex 0.1
00176 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1