[ruby-dl] extern a global variable?

S

Simon Strandgaard

I am wrapping ncursesw for ruby.
In ncurses.h it has declared the following variable:
extern int COLORS;

I would like to access COLORS from ruby (if possible).
I have tried extern 'int COLORS' but without luck.


shell> nm -a libncursesw.a
[snip]
00000000 B COLOR_PAIRS
00000004 B COLORS
U curscr
U cur_term
0000001c t default_bg
[snip]


I guess its accessable from the outside.. just how to do it?

Thanks in advance.
 
S

Simon Strandgaard

I am wrapping ncursesw for ruby.
In ncurses.h it has declared the following variable:
extern int COLORS;

I would like to access COLORS from ruby (if possible).
I have tried extern 'int COLORS' but without luck.

shell> nm -a libncursesw.a
[snip]
00000000 B COLOR_PAIRS
00000004 B COLORS
U curscr
U cur_term
0000001c t default_bg
[snip]

I guess its accessable from the outside.. just how to do it?


I have made some more progress.. but still I cannot access the value..


COLORS = symbol 'COLORS'
Int = struct ["int value"]

...

Later I then can access Curses::COLORS which gives me an PtrData instance.
After I have invoked Curses.start_color I try access the variable.

i = Curses::Int.new(Curses::COLORS).value
p i

It results in 0. Im not sure if this is the right way to access variables?




btw: wouldn't it be easier if one could type extern 'int COLORS' and
accomplish the same?
 
F

Florian Gross

Simon said:
I am wrapping ncursesw for ruby.
In ncurses.h it has declared the following variable:
extern int COLORS;

I would like to access COLORS from ruby (if possible).
I have tried extern 'int COLORS' but without luck.

Does this work?

module MyLib
extend DL::Importable
dlload "ncurses.so"

Colors = symbol "COLORS"
end
 
S

Simon Strandgaard

Does this work?

module MyLib
extend DL::Importable
dlload "ncurses.so"

Colors = symbol "COLORS"
end

No, it seems to return a PtrData instance.


I have made something that seems to work.. feels ackward though.
I can now access Curses.COLORS and Curses.ESCDELAY.

module Curses
extend DL::Importable
dlload 'libncursesw.so'

Int = struct ["int value"]
COLORS_PTR = symbol 'COLORS'
ESCDELAY_PTR = symbol 'ESCDELAY'
def self.COLORS
Int.new(COLORS_PTR).value
end
def self.ESCDELAY
Int.new(ESCDELAY_PTR).value
end
end


Im curious to if this can be done smarter?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top