Arjen Markus (24 september 2002) I wish to create a test program for a library of C routines that I am developing. These C routines are meant to examine the contents of a file and to read particular data from this file. So, the test program I want to write, in Tcl, must manage these routines and test whether the information returned is correct (in my case: the C routines are new and more flexible versions of existing routines).
The C routines, however, fill arrays of strings, integers and floats and I need a suitable form to represent these arrays on the Tcl side. As I prefer to put as much code on the Tcl side as possible, I came up with the following solution (the basic idea was proposed in the Tcl chatroom): Translate Tcl lists into C arrays and vice versa.
The easiest method that I could imagine was:
There are a few caveats:
Most of these points can be solved easily as shown in the code below
The C code, as generated via Critcl, includes the following statements:
_data = (int*) Tcl_GetByteArrayFromObj(ov[1], NULL); Tcl_InvalidateStringRep(ov[1]) ;
In essence: the "data" argument is a pointer to a bytearray. In C we can use it as a pointer to an array of integers.
# # Use critcl to do the hard part # package require critcl # # Define the C routine to interface with # ::critcl::cproc getfibonaci { int maxsize int* data } ok { int error ; int i ; error = TCL_OK ; data[0] = 1 ; data[1] = 1 ; for ( i = 2 ; i < maxsize ; i ++ ) { data[i] = data[i-2] +data[i-1] ; } return error ; } # # Tcl interface to the routine - for clean operation # proc getSeries { maxdata data } { upvar $data _data # # Create a list with the correct number of integer elements # for { set i 0 } { $i < $maxdata } { incr i } { lappend _data 0 } # # Convert the list to a byte array # set c_data [intsToByteArray $_data] # # Call the C routine - that will fill the byte array # set error [getfibonaci $maxdata $c_data] # # Convert the byte array into an ordinary list # set _data [byteArrayToInts $c_data] }
The above script requires a number of support procedures, here they are:
# # Generic routine to convert a list into a bytearray # proc listToByteArray { valuetype list {elemsize 0} } { if { $valuetype == "i" || $valuetype == "I" } { if { $::tcl_platform(byteOrder) == "littleEndian" } { set valuetype "i" } else { set valuetype "I" } } switch -- $valuetype { f - d - i - I { set result [binary format ${valuetype}* $list] } s { set result {} foreach elem $list { append result [binary format a$elemsize $elem] } } default { error "Unknown value type: $valuetype" } } return $result } interp alias {} stringsToByteArray {} listToByteArray s interp alias {} intsToByteArray {} listToByteArray i interp alias {} floatsToByteArray {} listToByteArray f interp alias {} doublesToByteArray {} listToByteArray d # # Generic routine to convert a bytearray into a list # proc byteArrayToList { valuetype bytearray {elemsize 0} } { if { $valuetype == "i" || $valuetype == "I" } { if { $::tcl_platform(byteOrder) == "littleEndian" } { set valuetype "i" } else { set valuetype "I" } } switch -- $valuetype { f - d - i - I { binary scan $bytearray ${valuetype}* result } s { set result {} set length [string length $bytearray] set noelems [expr {$length/$elemsize}] for { set i 0 } { $i < $noelems } { incr i } { set elem [string range $bytearray \ [expr {$i*$elemsize}] [expr {($i+1)*$elemsize-1}]] set posnull [string first "\000" $elem] if { $posnull != -1 } { set elem [string range $elem 0 [expr {$posnull-1}]] } lappend result $elem } } default { error "Unknown value type: $valuetype" } } return $result } interp alias {} byteArrayToStrings {} byteArrayToList s interp alias {} byteArrayToInts {} byteArrayToList i interp alias {} byteArrayToFloats {} byteArrayToList f interp alias {} byteArrayToDoubles {} byteArrayToList d # # Test the routine # getSeries 10 data puts $data
While I haven't fully understood the intention of Arjen's code above I found the Tcl_NewByteArrayObj() https://www.tcl-lang.org/man/tcl8.4/TclLib/ByteArrObj.htm functions and related helpful when handling binary data in TCL objects in C/C++. [email protected]