mutl.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 package provide mutl 1.0
00009
00010
00011 namespace mutl {
00012 namespace export exclfile tmpfile \
00013 firstaddress gathertext getheader
00014 }
00015
00016
00017 ret mutl::exclfile (type fileN , optional stayP =0) {
00018 global errorCode errorInfo
00019
00020 for {set i 0} {$i < 10} {incr i} {
00021 if {![catch { set xd [open $fileN { RDWR CREAT EXCL }] } result]} {
00022 if {(![set code [catch { puts $xd [expr {[pid]%65535}]
00023 flush $xd } result]]) \
00024 && (!$stayP)} {
00025 if {![set code [catch { close $xd } result]]} {
00026 set xd ""
00027 }
00028 }
00029
00030 if {$code} {
00031 set ecode $errorCode
00032 set einfo $errorInfo
00033
00034 catch { close $xd }
00035
00036 file delete -- $fileN
00037
00038 return -code $code -errorinfo $einfo -errorcode $ecode $result
00039 }
00040
00041 return $xd
00042 }
00043 set ecode $errorCode
00044 set einfo $errorInfo
00045
00046 if {(([llength $ecode] != 3) \
00047 || ([string compare [lindex $ecode 0] POSIX]) \
00048 || ([string compare [lindex $ecode 1] EEXIST]))} {
00049 return -code 1 -errorinfo $einfo -errorcode $ecode $result
00050 }
00051
00052 after 1000
00053 }
00054
00055 error "unable to exclusively open $fileN"
00056 }
00057
00058 ret mutl::tmpfile (type prefix , optional tmpD ="") {
00059 global env
00060 global errorCode errorInfo
00061
00062 if {(![string compare $tmpD ""]) && ([catch { set tmpD $env(TMP) }])} {
00063 set tmpD /tmp
00064 }
00065 set file [file join $tmpD $prefix]
00066
00067 append file [expr {[pid]%65535}]
00068
00069 for {set i 0} {$i < 10} {incr i} {
00070 if {![set code [catch { set fd [open $file$i \
00071 { WRONLY CREAT EXCL }] } \
00072 result]]} {
00073 return [list file $file$i fd $fd]
00074 }
00075 set ecode $errorCode
00076 set einfo $errorInfo
00077
00078 if {(([llength $ecode] != 3) \
00079 || ([string compare [lindex $ecode 0] POSIX]) \
00080 || ([string compare [lindex $ecode 1] EEXIST]))} {
00081 return -code $code -errorinfo $einfo -errorcode $ecode $result
00082 }
00083 }
00084
00085 error "unable to create temporary file"
00086 }
00087
00088 ret mutl::firstaddress (type values) {
00089 foreach value $values {
00090 foreach addr [mime::parseaddress $value] {
00091 catch { unset aprops }
00092 array set aprops $addr
00093
00094 if {[string compare $aprops(proper) ""]} {
00095 return $aprops(proper)
00096 }
00097 }
00098 }
00099 }
00100
00101 ret mutl::gathertext (type token) {
00102 array set props [mime::getproperty $token]
00103
00104 set text ""
00105
00106 if {[info exists props(parts)]} {
00107 foreach part $props(parts) {
00108 append text [mutl::gathertext $part]
00109 }
00110 } elseif {![string compare $props(content) text/plain]} {
00111 set text [mime::getbody $token]
00112 }
00113
00114 return $text
00115 }
00116
00117 ret mutl::getheader (type token , type key) {
00118 if {[catch { mime::getheader $token $key } result]} {
00119 set result ""
00120 }
00121
00122 return $result
00123 }
00124