Search code examples
cschemeguile

Filtering C structures with GNU guile


I'm new to scheme/guile. I'd like to filter an array of C struct inside a C program using a user's scm script. Something like:

struct Date {
    int year;
    int month;
    int day;
    };

struct Person {
    char name[20];
    Date birth;
    Date death;
    };

void printPersons(Person* persons, size_t n_persons, const char* scm_file) {
    size_t i;

    /* (...) compile script */

    for(i=0; i< n_persons;++i)
        {
        int accept = /** call guile script with &persons[i] */
        if( accept) printf("%s\n",persons[i].name);
        }
    /* dispose script */
    } 

For now I'm lost in the manual. Where should I start ? Thanks.


Solution

  • To call a scheme script in a C program for evaluation with guile, you need to use scm_with_guile(). The function executed by scm_with_guile() then need then needs to call something like scm_c_eval_string() (to evaluate a string in scheme syntax) or scm_c_primitive_load() (to evaluate a file in scheme syntax). Such strings and files can themselves call the guile load and use-modules procedures using normal scheme syntax.

    If you happen to be using C++, this may give you some ideas: c++-gtk-utils/extension and extension source code. With regard to your particular code, you could initialize your 'accept' variable with the return value of Cgu::Extension::exec_shared() called with a Cgu::Extension::integer_to_long() translator, but since 'accept' is of int type, on a 64-bit system you would need to allow for long to int overflow. You would also need to destructure your person struct into individual values for the script.