George Ogata
4/17/2005 7:59:00 AM
Dave Sims <davsims@gmail.com> writes:
> I'm trying to write an interface to the Ruby regular expressions
> engine using a C dll, but I'm neither a Ruby nor C wiz. Can someone
> tell me what's wrong with the following:
>
> #include <ruby.h>
> #include <windows.h>
>
> typedef struct RRegexp RegExp;
>
> int regExMatch(int pos, char *pattern, char *text)
> {
> RegExp *re = RREGEXP(rb_reg_new(pattern, 0, 1));
> result = rb_funcall(re, rb_intern("match"), 1, *text);
> // re will be a Ruby nil if no match is found.
> return RTEST(result);
> }
Your compiler should probably have given you a good number of
warnings/errors. `result' is undeclared, for instance. Did you
actually compile this code?
This worked for me:
int regExMatch(int pos, char *pattern, char *text)
{
VALUE re, str, result;
re = rb_reg_new(pattern, strlen(pattern), 1);
str = rb_str_new2(text);
result = rb_funcall(re, rb_intern("match"), 2, str, INT2NUM(pos));
// re will be a Ruby nil if no match is found.
return RTEST(result);
}
> I get a GPF on the first line of regExMatch.
How are you using it? If you use anything in the ruby API, you'll
probably need to have the ruby interpreter initialized (ruby_init())
before doing so. I don't know if you can use the regexp engine on its
own.
> I just need a C method equivalent of Regexp.match that I can export in
> a dll. Is this even the best way to go about such a thing or is there
> something better?
If you just want to use regexps in C code, it seems overkill to link
the whole ruby library. A regexp library like libpcre may suffice.
HTH.