[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

problem in Extending ruby

ravinder

3/21/2007 4:48:00 PM

I want to call C code from my ruby class. I am using SWIG for doing
this. I am able to compile the code and also able to invoke a C function
from ruby, but am unable to access a variable.
I have declared the variable as follows in "test.h" file:

typedef struct GenParams_s {
Version a_version; /**< Version */
BOOL keep_ignorable; /**< Keep Ignorable Whitespaces (Default:
FALSE) */
BOOL use_strtbl; /**< Generate String Table (Default: TRUE)
*/
} GenParams;


typedef enum Version_e {
VERSION_UNKNOWN = -1, /**< Unknown Version */
VERSION_10 = 0x00, /**< 1.0 Token */
VERSION_11 = 0x01, /**< 1.1 Token */
VERSION_12 = 0x02, /**< 1.2 Token */
VERSION_13 = 0x03 /**< 1.3 Token */
} Version;

#define BOOL unsigned char


Now I have another class "Convert.c", in which I have declared the
functions which I want to call from ruby. One of the method has
following signature:

A_DECLARE(Error) a_conv_a2b_withlen(UTINY *a, ULONG a_len, UTINY **b,
ULONG *b_len,
GenParams *params)



UTINY and ULONG are declared as unsigned char and unsigned long
respectively. I am importing "test.h" file in "Convert.c" file.

Next I compile "Convert.c" file using nmake etc...(as required by SWIG).
Now I want to access the method defined in Convert.c file, which I am
doing as follows:

require 'converter'

params = GenParams.new
convert = Converter.a_conv_a2b_withlen("a sample test", 13, "", 100,
params)

and getting following error:
"uninitialized constant GenParams (NameError)"


I'd appreciate if anyone can help me in this case or suggest me a
pointer as to where to look.

Thanks,
Ravinder Singh

--
Posted via http://www.ruby-....

2 Answers

Alexander Fleck

3/22/2007 7:38:00 AM

0

hi,
look at http://whytheluckystiff.net/rub...

There' re some issues to think about when you use structs and variables in
Ruby and C.

I don' t have the time to get deeper at the moment. Perhaps later.

regards,
Alex.

igor.sarzisartori@gmail.com

3/23/2007 10:08:00 PM

0

On Mar 21, 5:47 pm, ravinder <ravinder.1...@gmail.com> wrote:
> I want to call C code from my ruby class. I am using SWIG for doing
> this. I am able to compile the code and also able to invoke a C function
> from ruby, but am unable to access a variable.
> I have declared the variable as follows in "test.h" file:
>
> typedef struct GenParams_s {
> Version a_version; /**< Version */
> BOOL keep_ignorable; /**< Keep Ignorable Whitespaces (Default:
> FALSE) */
> BOOL use_strtbl; /**< Generate String Table (Default: TRUE)
> */
>
> } GenParams;
>
> typedef enum Version_e {
> VERSION_UNKNOWN = -1, /**< Unknown Version */
> VERSION_10 = 0x00, /**< 1.0 Token */
> VERSION_11 = 0x01, /**< 1.1 Token */
> VERSION_12 = 0x02, /**< 1.2 Token */
> VERSION_13 = 0x03 /**< 1.3 Token */
>
> } Version;
>
> #define BOOL unsigned char
>
> Now I have another class "Convert.c", in which I have declared the
> functions which I want to call from ruby. One of the method has
> following signature:
>
> A_DECLARE(Error) a_conv_a2b_withlen(UTINY *a, ULONG a_len, UTINY **b,
> ULONG *b_len,
> GenParams *params)
>
> UTINY and ULONG are declared as unsigned char and unsigned long
> respectively. I am importing "test.h" file in "Convert.c" file.
>
> Next I compile "Convert.c" file using nmake etc...(as required by SWIG).
> Now I want to access the method defined in Convert.c file, which I am
> doing as follows:
>
> require 'converter'
>
> params = GenParams.new
> convert = Converter.a_conv_a2b_withlen("a sample test", 13, "", 100,
> params)
>
> and getting following error:
> "uninitialized constant GenParams (NameError)"
>
> I'd appreciate if anyone can help me in this case or suggest me a
> pointer as to where to look.
>
> Thanks,
> Ravinder Singh
>
> --
> Posted viahttp://www.ruby-....

May be you have to use something like Converter::GenParams.new. I
suggest you to look inside the generated file from swig to know how
the structure GenParams is converted.