From f7b590cf27e36fa77e3cb86e3174aafd2dae83dc Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Mon, 16 Mar 2009 20:04:33 -0700 Subject: [PATCH] ruby ptql_re_impl --- bindings/ruby/rbsigar.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/bindings/ruby/rbsigar.c b/bindings/ruby/rbsigar.c index f5d4e543..adfddb1c 100644 --- a/bindings/ruby/rbsigar.c +++ b/bindings/ruby/rbsigar.c @@ -17,6 +17,7 @@ */ #include +#include #include #include "sigar.h" #include "sigar_fileinfo.h" @@ -42,6 +43,28 @@ static sigar_t *rb_sigar_get(VALUE obj) return sigar; } +static int rbsigar_ptql_re_impl(void *data, + char *haystack, char *needle) +{ + struct re_pattern_buffer *regex; + int len = strlen(haystack); + int retval; + const char *err; + + regex = ALLOC(struct re_pattern_buffer); + MEMZERO((char *)regex, struct re_pattern_buffer, 1); + /* XXX cache */ + if ((err = re_compile_pattern(needle, strlen(needle), regex))) { + re_free_pattern(regex); + rb_raise(rb_eRegexpError, "%s", err); + return 0; + } + + retval = re_match(regex, haystack, len, 0, NULL); + re_free_pattern(regex); + return retval > 0; +} + #define sigar_isdigit(c) \ (isdigit(((unsigned char)(c)))) @@ -69,7 +92,9 @@ static sigar_pid_t rb_sigar_pid_get(sigar_t *sigar, VALUE obj) if (status == SIGAR_OK) { sigar_pid_t qpid; + sigar_ptql_re_impl_set(sigar, NULL, rbsigar_ptql_re_impl); status = sigar_ptql_query_find_process(sigar, query, &qpid); + sigar_ptql_re_impl_set(sigar, NULL, NULL); sigar_ptql_query_destroy(query); if (status == SIGAR_OK) { return qpid; @@ -424,7 +449,9 @@ static VALUE rb_sigar_proc_list(int argc, VALUE *argv, VALUE obj) if (status != SIGAR_OK) { RB_SIGAR_RAISE(error.message); } + sigar_ptql_re_impl_set(sigar, NULL, rbsigar_ptql_re_impl); status = sigar_ptql_query_find(sigar, query, &list); + sigar_ptql_re_impl_set(sigar, NULL, NULL); sigar_ptql_query_destroy(query); if (status != SIGAR_OK) { RB_SIGAR_RAISE(sigar_strerror(sigar, status));