dsource.tcl

Go to the documentation of this file.
00001 /*  -*- tcl -*-*/
00002 /*  ### ### ### ######### ######### #########*/
00003 /** 
00004  */
00005 
00006 /*  Class for the handling of stream sources.*/
00007 
00008 /*  ### ### ### ######### ######### #########*/
00009 /*  Requirements*/
00010 
00011 package require transfer::copy ; /*  Data transmission core*/
00012 package require snit
00013 
00014 /*  ### ### ### ######### ######### #########*/
00015 /*  Implementation*/
00016 
00017 snit::type ::transfer::data::source {
00018 
00019     /*  ### ### ### ######### ######### #########*/
00020     /*  API*/
00021 
00022     /*                        Source is ...*/
00023     option -string   {} ; /*  a string.*/
00024     option -channel  {} ; /*  an open & readable channel.*/
00025     option -file     {} ; /*  a file.*/
00026     option -variable {} ; /*  a string held by the named variable.*/
00027 
00028     option -size -1     ; /*  number of characters to transfer.*/
00029 
00030     ret  type  () {}
00031     ret  data  () {}
00032     ret  size  () {}
00033     ret  valid (type mv) {}
00034 
00035     ret  transmit (type sock , type blocksize , type done) {}
00036 
00037     /*  ### ### ### ######### ######### #########*/
00038     /*  Implementation*/
00039 
00040     ret  type () {
00041     return $xtype
00042     }
00043 
00044     ret  data () {
00045     switch -exact -- $etype {
00046         undefined {
00047         return -code error "Data source is undefined"
00048         }
00049         string - chan {
00050         return $value
00051         }
00052         variable {
00053         upvar \#0 $value thevalue
00054         return $thevalue
00055         }
00056         file {
00057         return [open $value r]
00058         }
00059     }
00060     }
00061 
00062     ret  size () {
00063     if {$options(-size) < 0} {
00064         switch -exact -- $etype {
00065         undefined {
00066             return -code error "Data source is undefined"
00067         }
00068         string {
00069             return [string length $value]
00070         }
00071         variable {
00072             upvar \#0 $value thevalue
00073             return [string length $thevalue]
00074         }
00075         chan - file {
00076             # Nothing, -1 passes through
00077             # We do not use [file size] for a file, as a
00078             # user-specified encoding may distort the
00079             # counting.
00080         }
00081         }
00082     }
00083 
00084     return $options(-size)
00085     }
00086 
00087     ret  valid (type mv) {
00088     upvar 1 $mv message
00089 
00090     switch -exact -- $etype {
00091         undefined {
00092         set message "Data source is undefined"
00093         return 0
00094         }
00095         string - variable {
00096         if {[$self size] > [string length [$self data]]} {
00097             set message "Not enough data to transmit"
00098             return 0
00099         }
00100         }
00101         chan {
00102         # Additional check of option ?
00103         }
00104         file {
00105         # Additional check of option ?
00106         }
00107     }
00108     return 1
00109     }
00110 
00111     ret  transmit (type sock , type blocksize , type done) {
00112     ::transfer::copy::do \
00113         [$self type] [$self data] $sock \
00114         -size      [$self size] \
00115         -blocksize $blocksize \
00116         -command   $done
00117     return
00118     }
00119 
00120     /*  ### ### ### ######### ######### #########*/
00121     /*  Internal helper commands.*/
00122 
00123     onconfigure -string {newvalue} {
00124      etype =  string
00125      xtype =  string
00126      value =  $newvalue
00127     return
00128     }
00129 
00130     onconfigure -variable {newvalue} {
00131      etype =  variable
00132      xtype =  string
00133 
00134     if {![uplevel \/* 0 {info exists $newvalue}]} {*/
00135         return -code error "Bad variable \"$newvalue\", does not exist"
00136     }
00137 
00138      value =  $newvalue
00139     return
00140     }
00141 
00142     onconfigure -channel {newvalue} {
00143     if {![llength [file channels $newvalue]]} {
00144         return -code error "Bad channel handle \"$newvalue\", does not exist"
00145     }
00146      etype =  chan
00147      xtype =  chan
00148      value =  $newvalue
00149     return
00150     }
00151 
00152     onconfigure -file {newvalue} {
00153     if {![file exists $newvalue]} {
00154         return -code error "File \"$newvalue\" does not exist"
00155     }
00156     if {![file readable $newvalue]} {
00157         return -code error "File \"$newvalue\" not readable"
00158     }
00159     if {![file isfile $newvalue]} {
00160         return -code error "File \"$newvalue\" not a file"
00161     }
00162      etype =  file
00163      xtype =  chan
00164      value =  $newvalue
00165     return
00166     }
00167 
00168     /*  ### ### ### ######### ######### #########*/
00169     /*  Data structures*/
00170 
00171     variable etype undefined
00172     variable xtype undefined
00173     variable value
00174 
00175     /** 
00176      * ### ### ### ######### ######### #########
00177  */
00178 }
00179 
00180 /*  ### ### ### ######### ######### #########*/
00181 /*  Ready*/
00182 
00183 package provide transfer::data::source 0.1
00184 

Generated on 21 Sep 2010 for Gui by  doxygen 1.6.1