Forum OpenACS Q&A: tcl question

Collapse
Posted by Nick Carroll on
Hi, I have a tcl library procedure that reads a log entry from a log file. The log entry is appended to a variable of type string. When the procedure has completed reading the file, it returns the variable.
ad_proc get_chunk { } { } { 
    set filename "/some/file" 

    set file [open $filename "r"] 

    set out "" 
    while { [gets $file line] >= 0 } { 
	append out $line 
	append out "
" 
	if { [string length $out] >= 8192 } { 
	    ns_write $out 
	    set out "" 
	} 
    } 

    close $file
    return $out
}
From another tcl file, when I call this method, I want to read each line and process it in a loop. Do I use the tcl gets command? eg
while { [gets get_chunk line] >= 0 } {
    #process line
}
Or am I suppose to open a stream or channel somehow?
Collapse
2: Response to tcl question (response to 1)
Posted by Nick Carroll on
Or should I use the get_chunk method as follows...

<pre>
set chunkhandle [ open {get_chunk} r]
while { [gets chunkhandle line] >= 0 } {
    #process line
}
</pre>

Collapse
3: Response to tcl question (response to 1)
Posted by Nick Carroll on
Forgot to set the format to HTML... Or should I use the get_chunk method as follows...
set chunkhandle [ open {get_chunk} r] 
while { [gets chunkhandle line] >= 0 } { 
    #process line } 
Collapse
4: Response to tcl question (response to 1)
Posted by Nick Carroll on
Its cool... decided to store the data temporarily in the database instead of a file.  Which is probably the best thing to do.