[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby C extension/wrapper around C function that uses scanf and printf

1gor

7/9/2006 4:49:00 PM

I need Ruby to access a set of external command line applications for
dataseries analysis. Each of these little apps simply reads data from
stdin and then outputs processed results to stdout (using scanf and
printf, they are all written in C).

These functions have to be accessed repeatedly when iterating over
large chunks of data, so accessing them through the pipe (popen or
popen3) doesn't work. Launching/closing an external process 3000 times
in a row results in errors. And that's not efficient. So I have to
write extension for each of those little C programs, a wrapper of
sorts.

I have learned how to write and compile an extension for Ruby. I can
import an extension, pass an array of data to it and return some data
from the extension. The problem is that I have no C experience and
dread thinking of having to replace each 'scanf' routine with my custom
array of imported data (memory allocation, writing iterator etc.). Each
input function here starts with:

while (scanf("%lf", &y) == 1) { ... etc.

and then follows to process input data, handle cases with input buffer
is too large etc. I would very much like to leave these input()
functions as they are and just feed them my data from Ruby code.

Is it possible to fill stdin buffer with data from Ruby code and then
simply call existing C function that uses 'scanf'? I couldn't simply
assign an array to $stdin variable in Ruby, gave TypeError (File
expected)...

5 Answers

Robert Klemme

7/9/2006 5:34:00 PM

0

1gor wrote:
> I need Ruby to access a set of external command line applications for
> dataseries analysis. Each of these little apps simply reads data from
> stdin and then outputs processed results to stdout (using scanf and
> printf, they are all written in C).
>
> These functions have to be accessed repeatedly when iterating over
> large chunks of data, so accessing them through the pipe (popen or
> popen3) doesn't work.

Do they need changing arguments? Otherwise you could just start an
instance of each via popen and then reuse that throughout the whole
program. Or do these programs detect end of input data via EOF on STDIN?

> Launching/closing an external process 3000 times
> in a row results in errors. And that's not efficient.

Yeah, true.

> So I have to
> write extension for each of those little C programs, a wrapper of
> sorts.
>
> I have learned how to write and compile an extension for Ruby. I can
> import an extension, pass an array of data to it and return some data
> from the extension. The problem is that I have no C experience and
> dread thinking of having to replace each 'scanf' routine with my custom
> array of imported data (memory allocation, writing iterator etc.). Each
> input function here starts with:
>
> while (scanf("%lf", &y) == 1) { ... etc.
>
> and then follows to process input data, handle cases with input buffer
> is too large etc. I would very much like to leave these input()
> functions as they are and just feed them my data from Ruby code.
>
> Is it possible to fill stdin buffer with data from Ruby code and then
> simply call existing C function that uses 'scanf'? I couldn't simply
> assign an array to $stdin variable in Ruby, gave TypeError (File
> expected)...

Hm, I'm not a seasoned C programmer either, so I cannot give you a
definitive answer to this. However, I doubt that you can simply inject
stuff into stdin.

Here are two more ideas:

1. Rewrite your programs to act as if they saw EOF from stdin when they
receive a certain signal (say SIGUSR1). That way you could use a single
process throughout your whole Ruby script with popen and just need to
send the signal at certain points in time. You could even change
behavior to use the first line read from the pipe / stdin as command
line arguments.

2. If it's feasible rewrite the processing in Ruby. I guess you want to
use a C program for certain reasons - these might be performance, not
having to rewrite the code or whatever. If it's not performance then
maybe you can replace the stuff with 10% LOC in Ruby.

Kind regards

robert

Laza

7/11/2006 1:50:00 AM

0

Go through the source code of your external apps, and replace each
scanf() with sscanf(). (sscanf() reads from a string instead from
standard input). Then you can make the Ruby wrappers and pass a string
to each function. For example:

void old_external_app()
{
float x;
scanf("%f %f...",&x,...);
....
printf("%f",x);
}

void new_external_app(char *input_str, char *output_str)
{
float x;
sscanf( input_str, "%f %f...",&x,...); // the same, except 1st arg
...
sprintf( output_str, "%f", x);
}


Robert Klemme wrote:
> 1gor wrote:
> > I need Ruby to access a set of external command line applications for
> > dataseries analysis. Each of these little apps simply reads data from
> > stdin and then outputs processed results to stdout (using scanf and
> > printf, they are all written in C).
> >
> > These functions have to be accessed repeatedly when iterating over
> > large chunks of data, so accessing them through the pipe (popen or
> > popen3) doesn't work.
>
> Do they need changing arguments? Otherwise you could just start an
> instance of each via popen and then reuse that throughout the whole
> program. Or do these programs detect end of input data via EOF on STDIN?
>
> > Launching/closing an external process 3000 times
> > in a row results in errors. And that's not efficient.
>
> Yeah, true.
>
> > So I have to
> > write extension for each of those little C programs, a wrapper of
> > sorts.
> >
> > I have learned how to write and compile an extension for Ruby. I can
> > import an extension, pass an array of data to it and return some data
> > from the extension. The problem is that I have no C experience and
> > dread thinking of having to replace each 'scanf' routine with my custom
> > array of imported data (memory allocation, writing iterator etc.). Each
> > input function here starts with:
> >
> > while (scanf("%lf", &y) == 1) { ... etc.
> >
> > and then follows to process input data, handle cases with input buffer
> > is too large etc. I would very much like to leave these input()
> > functions as they are and just feed them my data from Ruby code.
> >
> > Is it possible to fill stdin buffer with data from Ruby code and then
> > simply call existing C function that uses 'scanf'? I couldn't simply
> > assign an array to $stdin variable in Ruby, gave TypeError (File
> > expected)...
>
> Hm, I'm not a seasoned C programmer either, so I cannot give you a
> definitive answer to this. However, I doubt that you can simply inject
> stuff into stdin.
>
> Here are two more ideas:
>
> 1. Rewrite your programs to act as if they saw EOF from stdin when they
> receive a certain signal (say SIGUSR1). That way you could use a single
> process throughout your whole Ruby script with popen and just need to
> send the signal at certain points in time. You could even change
> behavior to use the first line read from the pipe / stdin as command
> line arguments.
>
> 2. If it's feasible rewrite the processing in Ruby. I guess you want to
> use a C program for certain reasons - these might be performance, not
> having to rewrite the code or whatever. If it's not performance then
> maybe you can replace the stuff with 10% LOC in Ruby.
>
> Kind regards
>
> robert

Captain America

9/19/2011 12:55:00 AM

0

On Sun, 18 Sep 2011 20:06:43 -0400, "Buster Norris (Kicks Shit Out Of
Wimpy Dems and Laughs)" <bustyourface@rocketmail.com> wrote:

>Summary: Not only the most prolific liar also the most prolific thief
>of other's writings. She is also a Stolen Valor stain on America.
>
>[][][][][][]
>
>
>The DemocRAT Hall Of Shame http://www.democrathallof... asks
>"Why do you always LIE?"
>
>[Courtesy of Buster Norris]
>
>On Mon, 20 Jun 2011 19:38:32 -0400, Kickin' Ass and Takin' Names
><PopUlist349@hotmail.com> wrote:
>>In California as in most other states.........
>
>I bet you hoped to get away with stealing that article...........
>
>HAAAAAAAAAAAAAAAAAAAAAAAAAA!!!!!!!!!!!!!!!!!!!!!!
>
>You stole it from:
>
>http://www.dailykos.com/story/2011/06/20/987073/-Wal-Mart-will-be-so-sorry-it...


Why don't you reply to the OP?

Could it be you are too damn ignorant?

Sounds like that's exactly the case.

Patriot Games DemocRATHallofShame.Com?

9/19/2011 11:58:00 AM

0

On Sun, 18 Sep 2011 20:54:59 -0400, The_Captain@America.com wrote:
>On Sun, 18 Sep 2011 20:06:43 -0400, "Buster Norris (Kicks Shit Out Of
>Wimpy Dems and Laughs)" <bustyourface@rocketmail.com> wrote:
>>Summary: Not only the most prolific liar also the most prolific thief
>>of other's writings. She is also a Stolen Valor stain on America.
>>[][][][][][]
>>The DemocRAT Hall Of Shame http://www.democrathallof... asks
>>"Why do you always LIE?"
>>[Courtesy of Buster Norris]
>>On Mon, 20 Jun 2011 19:38:32 -0400, Kickin' Ass and Takin' Names
>><PopUlist349@hotmail.com> wrote:
>>>In California as in most other states.........
>>I bet you hoped to get away with stealing that article...........
>>HAAAAAAAAAAAAAAAAAAAAAAAAAA!!!!!!!!!!!!!!!!!!!!!!
>>You stole it from:
>>http://www.dailykos.com/story/2011/06/20/987073/-Wal-Mart-will-be-so-sorry-it...
>Why don't you reply to the OP?

Buster did reply to the OP.

A rough translation of Buster's reply to the OP would be: 'Attention
one and all! The poster named Lickin' Ass and Fakin' Names is a proven
thief, a proven liar, a proven fraud and NOTHING the sociopath pervert
posts should EVER be read or replied to UNLESS you have the need to
piss in her open mouth in public.'

>Could it be you are too damn ignorant?
>Sounds like that's exactly the case.

Thanks for the opportunity to shit in your open mouth in public,
again...

Hahahahaha!!!

--
The DemocRATs Hall of Shame©!
http://www.democrathallof...

9/10/11: Galleries Updated! Over 300 New OBAMA Cartoons, Pics
100 Other 'RAT Cartoons Added!

4/8/11: Clipboard Manager v3.5.1! (Still FREE!)
Added Spam Warnings!

FREE Windows® Screensavers! Muzzy Screensaver, 15Mb; DemocRAT Screensaver, 18Mb!

FREE Windows® Gadgets! Including: Bumpersticker Slideshow, Obama
Cartoon Slideshow, Take Back America 2010 & 2012!, Are DemocRATs
Ugly?, Is Helen Thomas Ugly?, Is Nancy Pelosi Ugly?, Disco Muzzy
(1 & 2)!, Obama's Lies!, "Uh-Oh! Something's Burning!", and the
Racial Slur Database.

Learn the TRUTH about: BSWS, Bob LeChevalier, Bret Cahill, Brian Wraith,
Chom Noamsky, Clams Casino, Cop Welfare, Curly Surmudgeon, Dakota,
Dave Fritzinger, David Johnston, Freestyle, Gandalf Grey, Iarnrod,
Igor, Joe Steel, Juanjo, Kevin Cunningham, Kurt Lochner, Lorad,
Lamont Cranston, Lookout, Lickin Ass' and Fakin' Names, Malcolm Abel,
Lubow, Major Debacle, Michael Coburn, Mitchel Holman, Phlip, Peter
Principle, Ramon Herrera, Ramrod, Ray Fischer, Rightardia, RobW, Rod
Speed, Roneal, Sanders Kaufman, Scotius, ShittingDuck, Sid9, SilentOtto,
Siobhan Medeiros, Snakehawk, Spike Lee, Stile4aly, Tab182, Tater Gumfries,
Tim Crowley, Tim Howard, Tom Sr, Tom Wheat

Buster Norris

9/20/2011 2:34:00 AM

0

On Sun, 18 Sep 2011 20:54:59 -0400, The_Captain@America.com wrote:

>Why don't you let me suck out your butthole?

HAAAAAAAAAAAAAAAAAAAA!!!!!!!!!!!!!!