Help! Getting binary string variable from C. 
Author Message
 Help! Getting binary string variable from C.

I am writing a DLL under TCL 8.1 using Windows 95. I want to get the content
of a jpg file from a TCL variable and manipulate it in C in my DLL.
Here is how to suppose to work

%load mydll
% set fp [ open 1.jpg ]
% fconfigure $fp -translation binary
% set b [ read $fp ]
% CreateJPGObject b
....

Attached at the end is the C side code ( I skip some error handling stuff to
make it simple ). With this code I got the right data size and was able to
create file but the content was screwed up. Strang thing is : in the above
TCL script, after all the above instructions, if I do the following:
%set fout [ open another.jpg w ]
%puts $fout $b
%close $fout
Then "another.jpg" is totally fine. This made me believe the internal data
screwed when I call Tcl lib functions, but how to fix this ? Please advise.

{
     int      dataSize, ret;
     Tcl_Obj *myStrVal;
     char    *content;
     char     buffer[100];

     sprintf(buffer, "set %s", argv[startIndex+1]);
     if ( Tcl_Eval(interp, buffer) != TCL_OK )
      return TCL_ERROR;
     myStrVal = Tcl_GetObjResult(interp);
     content = Tcl_GetStringFromObj(myStrVal, &dataSize);
     if ( ( content ) == NULL )
     {
      return TCL_ERROR;
     }
    {
       FILE *fp;

       fp = fopen("haha.jpg", "wb");
       fwrite(content, sizeof(char), dataSize, fp);
       fclose(fp);
      }
    }



Tue, 09 Jul 2002 03:00:00 GMT  
 Help! Getting binary string variable from C.

Quote:

> I am writing a DLL under TCL 8.1 using Windows 95. I want to get the content
> of a jpg file from a TCL variable and manipulate it in C in my DLL.
        ...
>      content = Tcl_GetStringFromObj(myStrVal, &dataSize);

You want Tcl_GetByteArrayFromObj, as strings in Tcl are UTF-8, and
that's what you get with the above (the UTF-8 representation of
the JPEG data).

--
   Jeffrey Hobbs                          The Tcl Guy
   jeffrey.hobbs at scriptics.com         Scriptics Corp.



Tue, 09 Jul 2002 03:00:00 GMT  
 Help! Getting binary string variable from C.

Quote:

>      content = Tcl_GetStringFromObj(myStrVal, &dataSize);

This should be:
       content = Tcl_GetByteArrayFromObj(myStrVal, &dataSize);

Another difference is that content shoud be (unsigned char *),
not (char *). The Tcl_GetStringFromObj() function assumes that
the object contains UTS-8 data, which indeed screws everything!!!

Regards,
--
Jan Nijtmans, CMG Arnhem B.V.


url:   http://purl.oclc.org/net/nijtmans/



Wed, 10 Jul 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Getting a variable format specification, and character strings

2. Reading binary files, unflatten binary string

3. convert binary number to binary string

4. Converting Binary To String (with out binary representation)

5. HELP! ADA and Binary Strings

6. Getting a string from the keyboard - Help

7. Help - Converting gets string to integer

8. How to convert binary string to char string ?

9. Converting a Hexadecimal String to a Binary String

10. Getting segfaults when calling Tcl_EvalFile from C.

11. String length of a binary string?

12. getting variable name from the variable itself?

 

 
Powered by phpBB® Forum Software