Newbie: Make expr long safe

From: Ian McCall (ian_at_eruvia.org)
Date: 03/28/05


Date: Mon, 28 Mar 2005 02:05:20 +0100

Hello.

I'm entirely new (read: within the last half hour...) to Tcl and am
trying to fix some third-party code. The problem is, I believe, that an
expression overflows an int. I presume the answer is to make it return
a long, but I'm unaware of the syntax for doing so.

Here's the code:

proc rec_info_from_db { fsid } {
global info db ; set p 3
 outd $p "rec_info_from_db: recobj\{$fsid\}"
 set tsize 0 ; set info($fsid,sizes) "" ; set file_size 0 ; set
info($fsid,parts) "" ; set info($fsid,
 RetryTransaction {
  set rec [db $db openid $fsid]
  foreach item "State Showing Part" { set info($fsid,$item) [ntrim
[dbobj $rec get $item]] }
  foreach item "Date Time Program Station" { set info($fsid,$item)
[ntrim [dbobj $info($fsid,Showing)
  foreach item "Title EpisodeTitle OriginalAirDate" { set
info($fsid,$item) [ntrim [dbobj $info($fsid,
  if { $info($fsid,OriginalAirDate) != "" } { set info($fsid,OAD)
$info($fsid,OriginalAirDate) }
  set info($fsid,CallSign) [dbobj $info($fsid,Station) get CallSign]
  foreach part $info($fsid,Part) { lappend info($fsid,parts) [dbobj
$part get File] }
  foreach part $info($fsid,parts) {
   if { [catch { mfs streamsize $part } p_s ] } { set psize
[mfs_stream_size $part] } else { set psize
   lappend info($fsid,sizes) $psize
   set tsize [expr "$tsize" + "$psize.0"]
  }
outd -1 "Before expr\n"
  set tsize [string range $tsize 0 [expr [string first "." $tsize] -1]]
outd -1 "Got past expr call"
  set info($fsid,StreamFileSize) "$tsize"
  outd $p "$info($fsid,Title) $tsize k"
 }
 cache_xml_2_disk $fsid
 calc_tertiary_info $fsid
}

If I run it, I see "Before expr" but never "Got past expr call".
Instead, I see "value too big to be represented as integer". The for
loop has been included so that you can see how things build up, all the
function calls work as expected (I know this, since on most occasions
this routine works but for large values it will fail).

By the looks of it, the code only seems to care about getting the expr
answer as a string so I can state that's all I care about too. Code
formatted exactly as you see it, only the outd -1s have been added by
me to pinpoint the statement this fails on.

All help very much appreciated - as a reminder, I'm just trying to get
that expr statement working with long values.

Thanks in advance for any information,
Ian