d7aaa90efc306adcb6b04cbad443ab9de43ebd20
[claws.git] / src / plugins / vcalendar / libical / libical / icalperiod.c
1 /* -*- Mode: C -*-
2   ======================================================================
3   FILE: icalperiod.c
4   CREATOR: eric 02 June 2000
5   
6   $Id$
7   $Locker$
8     
9  (C) COPYRIGHT 2000, Eric Busboom, http://www.softwarestudio.org
10
11  This program is free software; you can redistribute it and/or modify
12  it under the terms of either: 
13
14     The LGPL as published by the Free Software Foundation, version
15     2.1, available at: http://www.fsf.org/copyleft/lesser.html
16
17   Or:
18
19     The Mozilla Public License Version 1.0. You may obtain a copy of
20     the License at http://www.mozilla.org/MPL/
21
22  The Original Code is eric. The Initial Developer of the Original
23  Code is Eric Busboom
24
25
26  ======================================================================*/
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31
32 #include "icalperiod.h"
33
34 #include <assert.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38
39 #ifdef ICAL_NO_LIBICAL
40 #define icalerror_set_errno(x)
41 #define  icalerror_check_arg_rv(x,y)
42 #define  icalerror_check_arg_re(x,y,z)
43 #else
44 #include "icalerror.h"
45 #include "icalmemory.h"
46 #endif
47
48
49
50
51 struct icalperiodtype icalperiodtype_from_string (const char* str)
52 {
53     
54     struct icalperiodtype p, null_p;
55     char *s = icalmemory_strdup(str);
56     char *start, *end = s;
57     icalerrorstate es;
58
59     /* Errors are normally generated in the following code, so save
60        the error state for resoration later */
61
62     icalerrorenum e = icalerrno;
63
64     p.start = p.end = icaltime_null_time();
65     p.duration = icaldurationtype_from_int(0);
66
67     null_p = p;
68
69     if(s == 0) goto error;
70
71     start = s;
72     end = strchr(s, '/');
73
74     if(end == 0) goto error;
75
76     *end = 0;
77     end++;
78
79     p.start = icaltime_from_string(start);
80
81     if (icaltime_is_null_time(p.start)) goto error;
82
83     es = icalerror_get_error_state(ICAL_MALFORMEDDATA_ERROR);
84     icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,ICAL_ERROR_NONFATAL);
85
86     p.end = icaltime_from_string(end);
87
88     icalerror_set_error_state(ICAL_MALFORMEDDATA_ERROR,es);
89     
90
91     if (icaltime_is_null_time(p.end)){
92
93         p.duration = icaldurationtype_from_string(end);
94
95         if(icaldurationtype_as_int(p.duration) == 0) goto error;
96     } 
97
98     icalerrno = e;
99     icalmemory_free_buffer(s);
100     return p;
101
102  error:
103     icalmemory_free_buffer(s);
104     icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
105     return null_p;
106 }
107
108
109 const char* icalperiodtype_as_ical_string(struct icalperiodtype p)
110 {
111
112     const char* start;
113     const char* end;
114
115     char *buf;
116     size_t buf_size = 40;
117     char* buf_ptr = 0;
118
119     buf = (char*)icalmemory_new_buffer(buf_size);
120     buf_ptr = buf;
121     
122
123     start = icaltime_as_ical_string(p.start);
124
125     icalmemory_append_string(&buf, &buf_ptr, &buf_size, start); 
126
127     if(!icaltime_is_null_time(p.end)){
128         end = icaltime_as_ical_string(p.end);
129     } else {
130         end = icaldurationtype_as_ical_string(p.duration);
131     }
132
133     icalmemory_append_char(&buf, &buf_ptr, &buf_size, '/'); 
134
135     icalmemory_append_string(&buf, &buf_ptr, &buf_size, end); 
136    
137     icalmemory_add_tmp_buffer(buf); 
138
139     return buf;
140 }
141
142
143
144 struct icalperiodtype icalperiodtype_null_period() {
145     struct icalperiodtype p;
146     p.start = icaltime_null_time();
147     p.end = icaltime_null_time();
148     p.duration = icaldurationtype_null_duration();
149
150     return p;
151 }
152 int icalperiodtype_is_null_period(struct icalperiodtype p){
153     
154     if(icaltime_is_null_time(p.start) && 
155        icaltime_is_null_time(p.end) && 
156        icaldurationtype_is_null_duration(p.duration)){
157         return 1;
158     } else {
159         return 0;
160     }
161 }
162
163 int icalperiodtype_is_valid_period(struct icalperiodtype p){
164     if(icaltime_is_valid_time(p.start) && 
165        (icaltime_is_valid_time(p.end) || icaltime_is_null_time(p.end)) )
166         {
167             return 1;
168         }
169
170     return 0;
171 }
172