Search code examples
crrulelibical

can't get C method icalrecur_expand_recurrence to work


This is a bit frustrating. I've been working on this for a while now, and I can't seem to get this method to work like it says it does.

#include "icalrecur.h"
#include <time.h> /* for time() */
#include <stdio.h>

int get_occurrences(char* rrule, time_t start, int count)
{
        //char*        rule; /* rule string */
       // *rule = PG_GETARG_CHAR(0);

        time_t   *result[count]; /* output array */

        icalrecur_expand_recurrence(rrule, start, count, *result);

        return (time_t) *result;
}


//time_t *output[5*8];

void main() {
        time_t right_now = time(0);
        char *_rrule = "FREQ=WEEKLY;INTERVAL=2;COUNT=8;WKST=SU;BYDAY=TU,TH";
        get_occurrences(_rrule, right_now, 5);
}

I save this test file as ical_recur.h. Then I type in bash:

gcc -I/usr/local/libical/include/libical -L/usr/local/libical/lib/ -lical -o hello ical_recur.c

To include the libical.a libraries. The include/libical directory has icalrecur.h in it, so I really don't even need to be including the whole ical library.

~: ./hello
Segmentation fault

Anytime I change around any pointers, it starts complaining about something during compilation. Can anyone get this to work?? Source files are from Marketcircle on github.


Solution

  • Looking at the documentation it seems that you have an unwanted extra level of indirection - you need to change:

        time_t   *result[count]; /* output array */
    
        icalrecur_expand_recurrence(rrule, start, count, *result);
    

    to:

        time_t   result[count]; /* output array */
    
        icalrecur_expand_recurrence(rrule, start, count, result);
    

    Also you're passing a read-only string literal to a function which expects a char * - this should at least give you a compiler warning (hint: always use gcc -Wall ..., read the warnings carefully, understand them and fix them). main() should look more like this:

    int main() {
        time_t right_now = time(0);
        char _rrule[] = "FREQ=WEEKLY;INTERVAL=2;COUNT=8;WKST=SU;BYDAY=TU,TH";
        get_occurrences(_rrule, right_now, 5);
        return 0;
    }
    

    Some more problems:

    • this line doesn't do anything useful, but it's not clear what you're trying to achieve:

      char _size = (char)(((int)'0') + sizeof(result));
      

      all those casts are a "code smell" and this should tell you that you're doing something very wrong here

    • your function is defined as returning an int, but you're trying to cast your array to a time_t and return that, which also makes no sense - again, turn on compiler warnings - let the compiler help you find and fix your mistakes.