Version 0 of quickio

Updated 2007-10-23 12:18:16 by suchenwi

Richard Suchenwirth] 2007-10-23 : In the Tcl chatroom we discussed that Tcl is much slower than Perl in doing simple things like reading a text file line-by-line.

Here is an experiment with tcc to expose C's "raw" file I/O directly to Tcl. No encoding or line-end treatmant takes place, so files written by these routines are always UTF-8-encided (of which ASCII) is a subset.

With maybe some slight modifications, it should be usable with Critcl and Odyce as well.

 package require tcc
 namespace import tcc::*

 cproc fopen {char* name char* mode} char* {
    static char handle[16];
    FILE* fp = fopen(name, mode);
    sprintf(handle,"%p",fp);
    return &handle;
 } 
 cproc fgets {char* handle} char* {
    FILE* fp;
    static char buf[4096];
    sscanf(handle,"%p",&fp);
    fgets(&buf, sizeof(buf)-1, fp);
    buf[strlen(buf)-1] = '\0';      /* chomp trailing newline */
    return &buf;
 }
 cproc fputs {char* handle char* str} ok {
    FILE* fp;
    sscanf(handle,"%p",&fp);
    fputs(str,fp);
    fputs("\n",fp);
    return TCL_OK;
 }
 cproc fclose {char* handle} ok {
    FILE* fp;
    sscanf(handle,"%p",&fp);
    fclose(fp);
    return TCL_OK;
 }
 cproc feof {char* handle} int {
    FILE* fp;
    sscanf(handle,"%p",&fp);
    return feof(fp);
 }

#-- Testing:

 set f [fopen test.txt w]
 fputs $f hällo,
 fputs $f world!
 fclose $f

 set f [open test.txt]
 fconfigure $f -encoding utf-8
 puts >>[read $f][close $f]

 set f [fopen test.txt r]
 while 1 {
    set line [fgets $f]
    if [feof $f] break
    puts >>$line<<
 }

Category Example Category Performance