summaryrefslogtreecommitdiffabout
path: root/libical/src/libical/icalperiod.c
Unidiff
Diffstat (limited to 'libical/src/libical/icalperiod.c') (more/less context) (ignore whitespace changes)
-rw-r--r--libical/src/libical/icalperiod.c170
1 files changed, 170 insertions, 0 deletions
diff --git a/libical/src/libical/icalperiod.c b/libical/src/libical/icalperiod.c
new file mode 100644
index 0000000..c74c157
--- a/dev/null
+++ b/libical/src/libical/icalperiod.c
@@ -0,0 +1,170 @@
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
51struct 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
100 return p;
101
102 error:
103 icalerror_set_errno(ICAL_MALFORMEDDATA_ERROR);
104 return null_p;
105}
106
107
108const char* icalperiodtype_as_ical_string(struct icalperiodtype p)
109{
110
111 const char* start;
112 const char* end;
113
114 char *buf;
115 size_t buf_size = 40;
116 char* buf_ptr = 0;
117
118 buf = (char*)icalmemory_new_buffer(buf_size);
119 buf_ptr = buf;
120
121
122 start = icaltime_as_ical_string(p.start);
123
124 icalmemory_append_string(&buf, &buf_ptr, &buf_size, start);
125
126 if(!icaltime_is_null_time(p.end)){
127 end = icaltime_as_ical_string(p.end);
128 } else {
129 end = icaldurationtype_as_ical_string(p.duration);
130 }
131
132 icalmemory_append_char(&buf, &buf_ptr, &buf_size, '/');
133
134 icalmemory_append_string(&buf, &buf_ptr, &buf_size, end);
135
136
137 return buf;
138}
139
140
141
142struct icalperiodtype icalperiodtype_null_period() {
143 struct icalperiodtype p;
144 p.start = icaltime_null_time();
145 p.end = icaltime_null_time();
146 p.duration = icaldurationtype_null_duration();
147
148 return p;
149}
150int icalperiodtype_is_null_period(struct icalperiodtype p){
151
152 if(icaltime_is_null_time(p.start) &&
153 icaltime_is_null_time(p.end) &&
154 icaldurationtype_is_null_duration(p.duration)){
155 return 1;
156 } else {
157 return 0;
158 }
159}
160
161int icalperiodtype_is_valid_period(struct icalperiodtype p){
162 if(icaltime_is_valid_time(p.start) &&
163 (icaltime_is_valid_time(p.end) || icaltime_is_null_time(p.end)) )
164 {
165 return 1;
166 }
167
168 return 0;
169}
170