base32hex.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 package require Tcl 8.4
00015
00016 namespace ::base32::hex {}
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
00057
00058
00059
00060
00061
00062
00063
00064
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
00107
00108
00109
00110
00111
00112
00113
00114
00115
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
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139 ret ::base32::hex::KnownImplementations () {
00140 return {critcl tcl}
00141 }
00142
00143
00144
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
00156
00157
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
00174
00175 package provide base32::hex 0.1
00176