lib: Fixing checking for incorrect return value in while loop.
[barry.git] / src / r_recur_base.cc
blobe3b40e933ab65a7906cb2ac2584cb9af0269fcda
1 ///
2 /// \file r_recur_base.cc
3 /// Base class for recurring calendar event data.
4 ///
6 /*
7 Copyright (C) 2005-2012, 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>
29 #include <iomanip>
31 #define __DEBUG_MODE__
32 #include "debug.h"
34 using namespace std;
35 using namespace Barry::Protocol;
38 #define FIELDCODE_RECURRENCE_DATA 0x0c
40 namespace Barry {
43 ///////////////////////////////////////////////////////////////////////////////
44 // RecurBase class, static members
46 unsigned char RecurBase::WeekDayProto2Rec(uint8_t raw_field)
48 // Note: this simple copy is only possible since
49 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
50 // If this ever changes, this code will need to change.
51 return raw_field;
54 uint8_t RecurBase::WeekDayRec2Proto(unsigned char weekdays)
56 // Note: this simple copy is only possible since
57 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
58 // If this ever changes, this code will need to change.
59 return weekdays;
63 ///////////////////////////////////////////////////////////////////////////////
64 // RecurBase class
66 RecurBase::RecurBase()
68 Clear();
71 RecurBase::~RecurBase()
75 bool RecurBase::ParseField(uint8_t type,
76 const unsigned char *data,
77 size_t size,
78 const IConverter *ic)
80 // handle special cases
81 switch( type )
83 case FIELDCODE_RECURRENCE_DATA:
84 if( size >= CALENDAR_RECURRENCE_DATA_FIELD_SIZE ) {
85 // good data
86 ParseRecurrenceData(data);
88 else {
89 // not enough data!
90 throw Error("RecurBase::ParseField: not enough data in recurrence data field");
92 return true;
95 // unknown field
96 return false;
99 // this function assumes the size has already been checked
100 void RecurBase::ParseRecurrenceData(const void *data)
102 const CalendarRecurrenceDataField *rec =
103 (const CalendarRecurrenceDataField*) data;
105 Interval = btohs(rec->interval);
106 if( Interval < 1 )
107 Interval = 1; // must always be >= 1
109 if( rec->endTime == 0xffffffff ) {
110 Perpetual = true;
112 else {
113 RecurringEndTime.Time = min2time(rec->endTime);
114 Perpetual = false;
117 switch( rec->type )
119 case CRDF_TYPE_DAY:
120 RecurringType = Day;
121 // no extra data
122 break;
124 case CRDF_TYPE_MONTH_BY_DATE:
125 RecurringType = MonthByDate;
126 DayOfMonth = rec->u.month_by_date.monthDay;
127 break;
129 case CRDF_TYPE_MONTH_BY_DAY:
130 RecurringType = MonthByDay;
131 DayOfWeek = rec->u.month_by_day.weekDay;
132 WeekOfMonth = rec->u.month_by_day.week;
133 break;
135 case CRDF_TYPE_YEAR_BY_DATE:
136 RecurringType = YearByDate;
137 DayOfMonth = rec->u.year_by_date.monthDay;
138 MonthOfYear = rec->u.year_by_date.month;
139 break;
141 case CRDF_TYPE_YEAR_BY_DAY:
142 RecurringType = YearByDay;
143 DayOfWeek = rec->u.year_by_day.weekDay;
144 WeekOfMonth = rec->u.year_by_day.week;
145 MonthOfYear = rec->u.year_by_day.month;
146 break;
148 case CRDF_TYPE_WEEK:
149 RecurringType = Week;
150 WeekDays = WeekDayProto2Rec(rec->u.week.days);
151 break;
153 default:
154 eout("Unknown recurrence data type: 0x"
155 << setbase(16) << (unsigned int) rec->type);
156 throw Error("Unknown recurrence data type");
159 Recurring = true;
162 void RecurBase::Validate() const
166 // this function assumes there is CALENDAR_RECURRENCE_DATA_FIELD_SIZE bytes
167 // available in data
168 void RecurBase::BuildRecurrenceData(time_t StartTime, void *data) const
170 if( !Recurring )
171 throw Error("RecurBase::BuildRecurrenceData: Attempting to build recurrence data on non-recurring record.");
173 CalendarRecurrenceDataField *rec = (CalendarRecurrenceDataField*) data;
175 // set all to zero
176 memset(data, 0, CALENDAR_RECURRENCE_DATA_FIELD_SIZE);
178 rec->interval = htobs(Interval);
179 rec->startTime = time2min(StartTime);
180 if( Perpetual )
181 rec->endTime = 0xffffffff;
182 else
183 rec->endTime = time2min(RecurringEndTime.Time);
185 switch( RecurringType )
187 case Day:
188 rec->type = CRDF_TYPE_DAY;
189 // no extra data
190 break;
192 case MonthByDate:
193 rec->type = CRDF_TYPE_MONTH_BY_DATE;
194 rec->u.month_by_date.monthDay = DayOfMonth;
195 break;
197 case MonthByDay:
198 rec->type = CRDF_TYPE_MONTH_BY_DAY;
199 rec->u.month_by_day.weekDay = DayOfWeek;
200 rec->u.month_by_day.week = WeekOfMonth;
201 break;
203 case YearByDate:
204 rec->type = CRDF_TYPE_YEAR_BY_DATE;
205 rec->u.year_by_date.monthDay = DayOfMonth;
206 rec->u.year_by_date.month = MonthOfYear;
207 break;
209 case YearByDay:
210 rec->type = CRDF_TYPE_YEAR_BY_DAY;
211 rec->u.year_by_day.weekDay = DayOfWeek;
212 rec->u.year_by_day.week = WeekOfMonth;
213 rec->u.year_by_day.month = MonthOfYear;
214 break;
216 case Week:
217 rec->type = CRDF_TYPE_WEEK;
218 rec->u.week.days = WeekDayRec2Proto(WeekDays);
219 break;
221 default:
222 eout("RecurBase::BuildRecurrenceData: "
223 "Unknown recurrence data type: 0x"
224 << setbase(16) << (unsigned int) rec->type);
225 throw Error("RecurBase::BuildRecurrenceData: Unknown recurrence data type");
229 uint8_t RecurBase::RecurringFieldType() const
231 return FIELDCODE_RECURRENCE_DATA;
234 void RecurBase::Clear()
236 Recurring = false;
237 RecurringType = RecurBase::Week;
238 Interval = 1;
239 RecurringEndTime.clear();
240 Perpetual = false;
241 DayOfWeek = WeekOfMonth = DayOfMonth = MonthOfYear = 0;
242 WeekDays = 0;
245 void RecurBase::Dump(std::ostream &os) const
247 ios_format_state state(os);
249 static const char *DayNames[] = { "Sun", "Mon", "Tue", "Wed",
250 "Thu", "Fri", "Sat" };
251 static const char *MonthNames[] = { "Jan", "Feb", "Mar", "Apr",
252 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
254 // FIXME - need a "check all data" function that make sure that all
255 // recurrence data is within range. Then call that before using
256 // the data, such as in Build and in Dump.
258 // print recurrence data if available
259 os << " Recurring: " << (Recurring ? "yes" : "no") << "\n";
260 if( Recurring ) {
261 switch( RecurringType )
263 case Day:
264 os << " Every day.\n";
265 break;
267 case MonthByDate:
268 os << " Every month on the "
269 << dec
270 << DayOfMonth
271 << (DayOfMonth == 1 ? "st" : "")
272 << (DayOfMonth == 2 ? "nd" : "")
273 << (DayOfMonth == 3 ? "rd" : "")
274 << (DayOfMonth > 3 ? "th" : "")
275 << "\n";
276 break;
278 case MonthByDay:
279 os << " Every month on the "
280 << dec
281 << DayNames[DayOfWeek]
282 << " of week "
283 << WeekOfMonth
284 << "\n";
285 break;
287 case YearByDate:
288 os << " Every year on "
289 << dec
290 << MonthNames[MonthOfYear-1]
291 << " " << DayOfMonth << "\n";
292 break;
294 case YearByDay:
295 os << " Every year in " << MonthNames[MonthOfYear-1]
296 << dec
297 << " on "
298 << DayNames[DayOfWeek]
299 << " of week " << WeekOfMonth << "\n";
300 break;
302 case Week:
303 os << " Every week on: ";
304 if( WeekDays & CAL_WD_SUN ) os << "Sun ";
305 if( WeekDays & CAL_WD_MON ) os << "Mon ";
306 if( WeekDays & CAL_WD_TUE ) os << "Tue ";
307 if( WeekDays & CAL_WD_WED ) os << "Wed ";
308 if( WeekDays & CAL_WD_THU ) os << "Thu ";
309 if( WeekDays & CAL_WD_FRI ) os << "Fri ";
310 if( WeekDays & CAL_WD_SAT ) os << "Sat ";
311 os << "\n";
312 break;
314 default:
315 os << " Unknown recurrence type\n";
316 break;
319 os << dec << " Interval: " << Interval << "\n";
321 if( Perpetual )
322 os << " Ends: never\n";
323 else
324 os << " Ends: " << RecurringEndTime << "\n";
329 } // namespace Barry