base32.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 {}
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 ret ::base32::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::critcl_encode]]
00041 }
00042 tcl {
00043 variable selfdir
00044 if {[catch {source [file join $selfdir base32_tcl.tcl]}]} {return 0}
00045 set isok [llength [info commands ::base32::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::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::$c ::base32::${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::${key}_$c ::base32::$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::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::KnownImplementations () {
00140 return {critcl tcl}
00141 }
00142
00143 ret ::base32::Names () {
00144 return {
00145 critcl {tcllibc based}
00146 tcl {pure Tcl}
00147 }
00148 }
00149
00150
00151
00152
00153 namespace ::base32 {
00154 variable selfdir [file dirname [info script]]
00155 variable
00156
00157 variable accel
00158 array accel = {tcl 0 critcl 0}
00159 }
00160
00161
00162
00163
00164
00165
00166 namespace ::base32 {
00167 variable e
00168 foreach e [KnownImplementations] {
00169 if {[LoadAccelerator $e]} {
00170 SwitchTo $e
00171 break
00172 }
00173 }
00174 un e =
00175
00176 namespace export encode decode
00177 }
00178
00179
00180
00181
00182 package provide base32 0.1
00183