From ce55ed022fd373af58f7bd25cdd3f092f5698c54 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Tue, 17 Mar 2009 01:19:43 +0100 Subject: Rubify behavoir of helper --- lib/chaos_calendar/ical_occurrences.c | 50 +++++++++++++++++++++++------------ lib/chaos_calendar/ical_occurrences.i | 2 +- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/lib/chaos_calendar/ical_occurrences.c b/lib/chaos_calendar/ical_occurrences.c index 2f36d8c..ba63a6a 100644 --- a/lib/chaos_calendar/ical_occurrences.c +++ b/lib/chaos_calendar/ical_occurrences.c @@ -1,12 +1,41 @@ #include #include +#include //#define RRULE "FREQ=MONTHLY;BYMONTH=1,2,3,4,5,6,7,8,9,10,11;BYDAY=-1WE;UNTIL=20091105T220000" //#define RRULE "FREQ=DAILY;UNTIL=20991111T220000;VFOO" -//#define DTSTART "20000101T010000" -VALUE occurrences( char *dtstart, char *dtend, char *rrule ) { - VALUE occurr = rb_ary_new(); +VALUE occurrences( VALUE dtstart, VALUE dtend, char *rrule ) { + struct icaltimetype start, end; + time_t tt; + VALUE tv_sec, occurr = rb_ary_new(); + + /* Get method ID for Time.tv_sec */ + ID time_tv_sec = rb_intern( "tv_sec" ); + ID time_to_time = rb_intern( "to_time" ); + + if( !rb_respond_to( dtstart, time_tv_sec ) ) { + if( rb_respond_to( dtstart, time_to_time ) ) + dtstart = rb_funcall( dtstart, time_to_time, 0 ); + else + rb_raise( rb_eTypeError, "Can't convert dtstart into a Time-like object." ); + } + + if( !rb_respond_to( dtend, time_tv_sec ) ) { + if( rb_respond_to( dtend, time_to_time ) ) + dtend = rb_funcall( dtend, time_to_time, 0 ); + else + rb_raise( rb_eTypeError, "Can't convert dtend into a Time-like object." ); + } + + /* Apply .tv_sec to our Time objects (if they are Times ...) */ + tv_sec = rb_funcall( dtstart, time_tv_sec, 0 ); + tt = NUM2INT( tv_sec ); + start = icaltime_from_timet( tt, 0 ); + + tv_sec = rb_funcall( dtend, time_tv_sec, 0 ); + tt = NUM2INT( tv_sec ); + end = icaltime_from_timet( tt, 0 ); icalerror_clear_errno(); icalerror_set_error_state( ICAL_MALFORMEDDATA_ERROR, ICAL_ERROR_NONFATAL); @@ -14,23 +43,12 @@ VALUE occurrences( char *dtstart, char *dtend, char *rrule ) { struct icalrecurrencetype recur = icalrecurrencetype_from_string( rrule ); if( icalerrno != ICAL_NO_ERROR ) { printf( "libical error: %i. -- 1\n", icalerrno ); - return occurr; - } - struct icaltimetype start = icaltime_from_string( dtstart ); - if( icalerrno != ICAL_NO_ERROR ) { - printf( "libical error: %i. -- 2\n", icalerrno ); - return occurr; - } - struct icaltimetype end = icaltime_from_string( dtend ); - if( icalerrno != ICAL_NO_ERROR ) { - printf( "libical error: %i. -- 3\n", icalerrno ); - return occurr; + return Qnil; } icalrecur_iterator* ritr = icalrecur_iterator_new( recur, start ); while(1) { -// char outbuf[1024] = {0}; struct icaltimetype next = icalrecur_iterator_next(ritr); if( icaltime_is_null_time(next) || ( icaltime_compare( next, end ) > 0 ) ) { @@ -39,8 +57,6 @@ VALUE occurrences( char *dtstart, char *dtend, char *rrule ) { } rb_ary_push( occurr, rb_time_new( icaltime_as_timet( next ), 0 ) ); -// print_datetime_to_string( outbuf, &next ); -// rb_ary_push( occurr, rb_str_new2( outbuf ) ); }; icalrecur_iterator_free(ritr); diff --git a/lib/chaos_calendar/ical_occurrences.i b/lib/chaos_calendar/ical_occurrences.i index 5f7efd5..3f87628 100644 --- a/lib/chaos_calendar/ical_occurrences.i +++ b/lib/chaos_calendar/ical_occurrences.i @@ -1,6 +1,6 @@ %module ical_occurrences %inline { - VALUE occurrences( char * dtstart, char * dtend, char * rrule ); + VALUE occurrences( VALUE dtstart, VALUE dtend, char * rrule ); } -- cgit v1.3