|
From: Kevin S. <Kev...@sa...> - 2014-06-20 21:15:18
|
On Jun 20, 2014, at 11:53 AM, Kevin D Smith <Kev...@sa...> wrote:
> We are using SWIG to handle data that isn't NULL-terminated. It is stored in records of fixed length and blank padded. The problem is that when we wrap a function to return one of these strings, we have to allocate a new string, copy the non-blank characters in, and NULL terminate it in order for SWIG to know the length of it when passing it back to other languages. This is extremely inefficient, especially since the string is deleted immediately after the scripting language makes its own copy. I see that there are typemaps for character pointers with size, but I'm not sure if these will work for me. I would like to be able to wrap something like this that would return the string pointer and populate an output variable with the resulting length.
>
> char *output getstringfield( other, arguments, &output_length );
>
> Then SWIG would do:
>
> SWIG_FromCharPtrAndSize( output, output_length );
>
> Is something like this possible?
I've gotten really close to what I need with the following typemaps. I created a new data type called "string" that I use to trigger the typemap for strings that have a specified length. I can then override the SWIG_FromCharPtr with SWIG_FromCharPtrAndSize. The only problem is that SWIG adds an argnum at the end of the temporary variable names, and it doesn't appear to be available in the out typemap context. I get an error about "temp" not being defined; because it's defined as "temp4" in this case. I either need to get rid of the number in the in typemap, or figure out how to add the number in the out typemap.
%typemap(in,numinputs=0,noblock=1) int *string_l (int temp) { $1 = &temp; }
%typemap(out,noblock=1) string { $result = SWIG_FromCharPtrAndSize((const char *)$1, (int)
temp); }
Kevin D Smith
Kev...@sa...
|