Minor change to buildgen.sh for personal ctags use
[barry/progweb.git] / src / r_recur_base.cc
blob1fd7aca9b211c75f2722dc6a378a2bd019d50ad2
1 ///
2 /// \file r_recur_base.cc
3 /// Base class for recurring calendar event data.
4 ///
6 /*
7 Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "r_recur_base.h"
23 #include "protostructs.h"
24 #include "error.h"
25 #include "endian.h"
26 #include "time.h"
27 #include "ios_state.h"
28 #include <string.h>
30 #define __DEBUG_MODE__
31 #include "debug.h"
33 using namespace std;
34 using namespace Barry::Protocol;
37 #define FIELDCODE_RECURRENCE_DATA 0x0c
39 namespace Barry {
42 ///////////////////////////////////////////////////////////////////////////////
43 // RecurBase class, static members
45 unsigned char RecurBase::WeekDayProto2Rec(uint8_t raw_field)
47 // Note: this simple copy is only possible since
48 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
49 // If this ever changes, this code will need to change.
50 return raw_field;
53 uint8_t RecurBase::WeekDayRec2Proto(unsigned char weekdays)
55 // Note: this simple copy is only possible since
56 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
57 // If this ever changes, this code will need to change.
58 return weekdays;
62 ///////////////////////////////////////////////////////////////////////////////
63 // RecurBase class
65 RecurBase::RecurBase()
67 Clear();
70 RecurBase::~RecurBase()
74 bool RecurBase::ParseField(uint8_t type,
75 const unsigned char *data,
76 size_t size,
77 const IConverter *ic)
79 // handle special cases
80 switch( type )
82 case FIELDCODE_RECURRENCE_DATA:
83 if( size >= CALENDAR_RECURRENCE_DATA_FIELD_SIZE ) {
84 // good data
85 ParseRecurrenceData(data);
87 else {
88 // not enough data!
89 throw Error("RecurBase::ParseField: not enough data in recurrence data field");
91 return true;
94 // unknown field
95 return false;
98 // this function assumes the size has already been checked
99 void RecurBase::ParseRecurrenceData(const void *data)
101 const CalendarRecurrenceDataField *rec =
102 (const CalendarRecurrenceDataField*) data;
104 Interval = btohs(rec->interval);
105 if( Interval < 1 )
106 Interval = 1; // must always be >= 1
108 if( rec->endTime == 0xffffffff ) {
109 Perpetual = true;
111 else {
112 RecurringEndTime = min2time(rec->endTime);
113 Perpetual = false;
116 switch( rec->type )
118 case CRDF_TYPE_DAY:
119 RecurringType = Day;
120 // no extra data
121 break;
123 case CRDF_TYPE_MONTH_BY_DATE:
124 RecurringType = MonthByDate;
125 DayOfMonth = rec->u.month_by_date.monthDay;
126 break;
128 case CRDF_TYPE_MONTH_BY_DAY:
129 RecurringType = MonthByDay;
130 DayOfWeek = rec->u.month_by_day.weekDay;
131 WeekOfMonth = rec->u.month_by_day.week;
132 break;
134 case CRDF_TYPE_YEAR_BY_DATE:
135 RecurringType = YearByDate;
136 DayOfMonth = rec->u.year_by_date.monthDay;
137 MonthOfYear = rec->u.year_by_date.month;
138 break;
140 case CRDF_TYPE_YEAR_BY_DAY:
141 RecurringType = YearByDay;
142 DayOfWeek = rec->u.year_by_day.weekDay;
143 WeekOfMonth = rec->u.year_by_day.week;
144 MonthOfYear = rec->u.year_by_day.month;
145 break;
147 case CRDF_TYPE_WEEK:
148 RecurringType = Week;
149 WeekDays = WeekDayProto2Rec(rec->u.week.days);
150 break;
152 default:
153 eout("Unknown recurrence data type: 0x"
154 << setbase(16) << (unsigned int) rec->type);
155 throw Error("Unknown recurrence data type");
158 Recurring = true;
161 // this function assumes there is CALENDAR_RECURRENCE_DATA_FIELD_SIZE bytes
162 // available in data
163 void RecurBase::BuildRecurrenceData(time_t StartTime, void *data) const
165 if( !Recurring )
166 throw Error("RecurBase::BuildRecurrenceData: Attempting to build recurrence data on non-recurring record.");
168 CalendarRecurrenceDataField *rec = (CalendarRecurrenceDataField*) data;
170 // set all to zero
171 memset(data, 0, CALENDAR_RECURRENCE_DATA_FIELD_SIZE);
173 rec->interval = htobs(Interval);
174 rec->startTime = time2min(StartTime);
175 if( Perpetual )
176 rec->endTime = 0xffffffff;
177 else
178 rec->endTime = time2min(RecurringEndTime);
180 switch( RecurringType )
182 case Day:
183 rec->type = CRDF_TYPE_DAY;
184 // no extra data
185 break;
187 case MonthByDate:
188 rec->type = CRDF_TYPE_MONTH_BY_DATE;
189 rec->u.month_by_date.monthDay = DayOfMonth;
190 break;
192 case MonthByDay:
193 rec->type = CRDF_TYPE_MONTH_BY_DAY;
194 rec->u.month_by_day.weekDay = DayOfWeek;
195 rec->u.month_by_day.week = WeekOfMonth;
196 break;
198 case YearByDate:
199 rec->type = CRDF_TYPE_YEAR_BY_DATE;
200 rec->u.year_by_date.monthDay = DayOfMonth;
201 rec->u.year_by_date.month = MonthOfYear;
202 break;
204 case YearByDay:
205 rec->type = CRDF_TYPE_YEAR_BY_DAY;
206 rec->u.year_by_day.weekDay = DayOfWeek;
207 rec->u.year_by_day.week = WeekOfMonth;
208 rec->u.year_by_day.month = MonthOfYear;
209 break;
211 case Week:
212 rec->type = CRDF_TYPE_WEEK;
213 rec->u.week.days = WeekDayRec2Proto(WeekDays);
214 break;
216 default:
217 eout("RecurBase::BuildRecurrenceData: "
218 "Unknown recurrence data type: 0x"
219 << setbase(16) << (unsigned int) rec->type);
220 throw Error("RecurBase::BuildRecurrenceData: Unknown recurrence data type");
224 uint8_t RecurBase::RecurringFieldType() const
226 return FIELDCODE_RECURRENCE_DATA;
229 void RecurBase::Clear()
231 Recurring = false;
232 RecurringType = RecurBase::Week;
233 Interval = 1;
234 RecurringEndTime = 0;
235 Perpetual = false;
236 DayOfWeek = WeekOfMonth = DayOfMonth = MonthOfYear = 0;
237 WeekDays = 0;
240 void RecurBase::Dump(std::ostream &os) const
242 ios_format_state state(os);
244 static const char *DayNames[] = { "Sun", "Mon", "Tue", "Wed",
245 "Thu", "Fri", "Sat" };
246 static const char *MonthNames[] = { "Jan", "Feb", "Mar", "Apr",
247 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
249 // FIXME - need a "check all data" function that make sure that all
250 // recurrence data is within range. Then call that before using
251 // the data, such as in Build and in Dump.
253 // print recurrence data if available
254 os << " Recurring: " << (Recurring ? "yes" : "no") << "\n";
255 if( Recurring ) {
256 switch( RecurringType )
258 case Day:
259 os << " Every day.\n";
260 break;
262 case MonthByDate:
263 os << " Every month on the "
264 << DayOfMonth
265 << (DayOfMonth == 1 ? "st" : "")
266 << (DayOfMonth == 2 ? "nd" : "")
267 << (DayOfMonth == 3 ? "rd" : "")
268 << (DayOfMonth > 3 ? "th" : "")
269 << "\n";
270 break;
272 case MonthByDay:
273 os << " Every month on the "
274 << DayNames[DayOfWeek]
275 << " of week "
276 << WeekOfMonth
277 << "\n";
278 break;
280 case YearByDate:
281 os << " Every year on "
282 << MonthNames[MonthOfYear-1]
283 << " " << DayOfMonth << "\n";
284 break;
286 case YearByDay:
287 os << " Every year in " << MonthNames[MonthOfYear-1]
288 << " on "
289 << DayNames[DayOfWeek]
290 << " of week " << WeekOfMonth << "\n";
291 break;
293 case Week:
294 os << " Every week on: ";
295 if( WeekDays & CAL_WD_SUN ) os << "Sun ";
296 if( WeekDays & CAL_WD_MON ) os << "Mon ";
297 if( WeekDays & CAL_WD_TUE ) os << "Tue ";
298 if( WeekDays & CAL_WD_WED ) os << "Wed ";
299 if( WeekDays & CAL_WD_THU ) os << "Thu ";
300 if( WeekDays & CAL_WD_FRI ) os << "Fri ";
301 if( WeekDays & CAL_WD_SAT ) os << "Sat ";
302 os << "\n";
303 break;
305 default:
306 os << " Unknown recurrence type\n";
307 break;
310 os << " Interval: " << Interval << "\n";
312 if( Perpetual )
313 os << " Ends: never\n";
314 else
315 os << " Ends: " << ctime(&RecurringEndTime);
320 } // namespace Barry