diff options
| author | erdgeist <erdgeist@bauklotz.local> | 2009-03-17 01:19:43 +0100 |
|---|---|---|
| committer | erdgeist <erdgeist@bauklotz.local> | 2009-03-17 01:19:43 +0100 |
| commit | ce55ed022fd373af58f7bd25cdd3f092f5698c54 (patch) | |
| tree | 4fd6252f80609720b76dde8d89cac03bafaa3743 /lib | |
| parent | 82741f65fc387e898e5e17f16a057b34bb6dddab (diff) | |
Rubify behavoir of helper
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/chaos_calendar/ical_occurrences.c | 50 | ||||
| -rw-r--r-- | 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 @@ | |||
| 1 | #include <ruby.h> | 1 | #include <ruby.h> |
| 2 | #include <ical.h> | 2 | #include <ical.h> |
| 3 | #include <time.h> | ||
| 3 | 4 | ||
| 4 | //#define RRULE "FREQ=MONTHLY;BYMONTH=1,2,3,4,5,6,7,8,9,10,11;BYDAY=-1WE;UNTIL=20091105T220000" | 5 | //#define RRULE "FREQ=MONTHLY;BYMONTH=1,2,3,4,5,6,7,8,9,10,11;BYDAY=-1WE;UNTIL=20091105T220000" |
| 5 | //#define RRULE "FREQ=DAILY;UNTIL=20991111T220000;VFOO" | 6 | //#define RRULE "FREQ=DAILY;UNTIL=20991111T220000;VFOO" |
| 6 | //#define DTSTART "20000101T010000" | ||
| 7 | 7 | ||
| 8 | VALUE occurrences( char *dtstart, char *dtend, char *rrule ) { | 8 | VALUE occurrences( VALUE dtstart, VALUE dtend, char *rrule ) { |
| 9 | VALUE occurr = rb_ary_new(); | 9 | struct icaltimetype start, end; |
| 10 | time_t tt; | ||
| 11 | VALUE tv_sec, occurr = rb_ary_new(); | ||
| 12 | |||
| 13 | /* Get method ID for Time.tv_sec */ | ||
| 14 | ID time_tv_sec = rb_intern( "tv_sec" ); | ||
| 15 | ID time_to_time = rb_intern( "to_time" ); | ||
| 16 | |||
| 17 | if( !rb_respond_to( dtstart, time_tv_sec ) ) { | ||
| 18 | if( rb_respond_to( dtstart, time_to_time ) ) | ||
| 19 | dtstart = rb_funcall( dtstart, time_to_time, 0 ); | ||
| 20 | else | ||
| 21 | rb_raise( rb_eTypeError, "Can't convert dtstart into a Time-like object." ); | ||
| 22 | } | ||
| 23 | |||
| 24 | if( !rb_respond_to( dtend, time_tv_sec ) ) { | ||
| 25 | if( rb_respond_to( dtend, time_to_time ) ) | ||
| 26 | dtend = rb_funcall( dtend, time_to_time, 0 ); | ||
| 27 | else | ||
| 28 | rb_raise( rb_eTypeError, "Can't convert dtend into a Time-like object." ); | ||
| 29 | } | ||
| 30 | |||
| 31 | /* Apply .tv_sec to our Time objects (if they are Times ...) */ | ||
| 32 | tv_sec = rb_funcall( dtstart, time_tv_sec, 0 ); | ||
| 33 | tt = NUM2INT( tv_sec ); | ||
| 34 | start = icaltime_from_timet( tt, 0 ); | ||
| 35 | |||
| 36 | tv_sec = rb_funcall( dtend, time_tv_sec, 0 ); | ||
| 37 | tt = NUM2INT( tv_sec ); | ||
| 38 | end = icaltime_from_timet( tt, 0 ); | ||
| 10 | 39 | ||
| 11 | icalerror_clear_errno(); | 40 | icalerror_clear_errno(); |
| 12 | icalerror_set_error_state( ICAL_MALFORMEDDATA_ERROR, ICAL_ERROR_NONFATAL); | 41 | icalerror_set_error_state( ICAL_MALFORMEDDATA_ERROR, ICAL_ERROR_NONFATAL); |
| @@ -14,23 +43,12 @@ VALUE occurrences( char *dtstart, char *dtend, char *rrule ) { | |||
| 14 | struct icalrecurrencetype recur = icalrecurrencetype_from_string( rrule ); | 43 | struct icalrecurrencetype recur = icalrecurrencetype_from_string( rrule ); |
| 15 | if( icalerrno != ICAL_NO_ERROR ) { | 44 | if( icalerrno != ICAL_NO_ERROR ) { |
| 16 | printf( "libical error: %i. -- 1\n", icalerrno ); | 45 | printf( "libical error: %i. -- 1\n", icalerrno ); |
| 17 | return occurr; | 46 | return Qnil; |
| 18 | } | ||
| 19 | struct icaltimetype start = icaltime_from_string( dtstart ); | ||
| 20 | if( icalerrno != ICAL_NO_ERROR ) { | ||
| 21 | printf( "libical error: %i. -- 2\n", icalerrno ); | ||
| 22 | return occurr; | ||
| 23 | } | ||
| 24 | struct icaltimetype end = icaltime_from_string( dtend ); | ||
| 25 | if( icalerrno != ICAL_NO_ERROR ) { | ||
| 26 | printf( "libical error: %i. -- 3\n", icalerrno ); | ||
| 27 | return occurr; | ||
| 28 | } | 47 | } |
| 29 | 48 | ||
| 30 | icalrecur_iterator* ritr = icalrecur_iterator_new( recur, start ); | 49 | icalrecur_iterator* ritr = icalrecur_iterator_new( recur, start ); |
| 31 | 50 | ||
| 32 | while(1) { | 51 | while(1) { |
| 33 | // char outbuf[1024] = {0}; | ||
| 34 | struct icaltimetype next = icalrecur_iterator_next(ritr); | 52 | struct icaltimetype next = icalrecur_iterator_next(ritr); |
| 35 | 53 | ||
| 36 | if( icaltime_is_null_time(next) || ( icaltime_compare( next, end ) > 0 ) ) { | 54 | if( icaltime_is_null_time(next) || ( icaltime_compare( next, end ) > 0 ) ) { |
| @@ -39,8 +57,6 @@ VALUE occurrences( char *dtstart, char *dtend, char *rrule ) { | |||
| 39 | } | 57 | } |
| 40 | 58 | ||
| 41 | rb_ary_push( occurr, rb_time_new( icaltime_as_timet( next ), 0 ) ); | 59 | rb_ary_push( occurr, rb_time_new( icaltime_as_timet( next ), 0 ) ); |
| 42 | // print_datetime_to_string( outbuf, &next ); | ||
| 43 | // rb_ary_push( occurr, rb_str_new2( outbuf ) ); | ||
| 44 | }; | 60 | }; |
| 45 | 61 | ||
| 46 | icalrecur_iterator_free(ritr); | 62 | 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 @@ | |||
| 1 | %module ical_occurrences | 1 | %module ical_occurrences |
| 2 | 2 | ||
| 3 | %inline { | 3 | %inline { |
| 4 | VALUE occurrences( char * dtstart, char * dtend, char * rrule ); | 4 | VALUE occurrences( VALUE dtstart, VALUE dtend, char * rrule ); |
| 5 | } | 5 | } |
| 6 | 6 | ||
