|
From: かいお (Kaio) <ca...@de...> - 2008-12-10 05:18:49
|
Haoyu Bai wrote:
> On Mon, Dec 8, 2008 at 3:33 PM, "かいお (Kaio)" <ca...@de...> wrote:
>> Hi,
>>
>> Nice to meet you. My name is Caius from I18N team, Red Hat.
>>
>> I am currently developing a module on ibus, a multi-language input
>> method platform. However, since the library (libtranslate) I am reusing
>> is written in C but ibus is written in Python, I tried to use swig to
>> wrap the libraryrecommended by ibus author.
>>
>> My question is, I have a C method which its return data type is char*,
>> and I want the string to be passed to something written in Python. I
>> have created the interface .i file which is like:
>>
>> ===BEGIN .i file
>> %module trans_bridge
>> %include typemaps.i
>>
>> #include <translate.h>
>> extern char *translate(char *from, char *to, char *text);
>> ===END .i file
>>
>> I proved the return of the method 'translate()' is valid as a char*
>> string by a 'main()' method in the same .c file.
>>
>> Then I have the Python .py as follows:
>>
>> ===BEGIN .py file
>> ...
>> import trans_bridge
>> ...
>> def __commit_string(self, text):
>> self.__reset()
>> translated_text = trans_bridge.translate("en", "ja", text)
>> self.commit_string(translated_text)
>> self.__invalidate()
>> ...
>> ===END .py file
>>
>> I checked the translated_text after the trans_beidge() is called. There
>> was no data be assigned.
>>
>> I am wondering if I am implementing the char* passing correctly? Do I
>> need to do some extra codes because I have not found info about passing
>> char*.
>>
>> Thank you very much.
>>
>> Best Regards,
>> Caius.
>>
>> -- Caius Chance, Soft Eng, I18N, Red Hat APAC, cchance AT redhat DOT com
>> JP (Qual), RHCE, MCSE, CCNA, JLPT4, http://apac.redhat.com/disclaimer
>
> Hi,
>
> Try to write your .i file like this:
>
> ===BEGIN .i file
> %module trans_bridge
> %include typemaps.i
>
> %{
> #include <translate.h>
> %}
>
> extern char *translate(char *from, char *to, char *text);
> ===END .i file
>
> So that the "#include <translate.h>" line would be put into the
> generated _wrap.c file.
>
> However, the char * typemap should be applied automatically.
>
> -- Haoyu Bai
Hi Haoyu,
I have changed that but seems there is still the value returned is
invalid. I coded the python file like following:
>> ===BEGIN .py file
>> ...
>> import trans_bridge
>> ...
>> def __commit_string(self, text):
>> self.__reset()
>> translated_text = trans_bridge.translate("en", "ja", text)
>> self.commit_string(translated_text)
>> self.__invalidate()
>> ...
>> ===END .py file
Will trans_bridge.translate() return char* and translated_text will be
assigned with the char* like a string?
self.commit_string() is tested handling string correctly (e.g. "this is
string").
Best Regards,
Caio.
|