reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
DateCategory.cxx
Go to the documentation of this file.
1#include "rutil/compat.hxx"
2
3#include <string.h>
4#include <ctype.h>
5#include <time.h>
6
8
10#include "rutil/Data.hxx"
11#include "rutil/DnsUtil.hxx"
12#include "rutil/Logger.hxx"
13#include "rutil/ParseBuffer.hxx"
14#include "rutil/Socket.hxx"
15//#include "rutil/WinLeakCheck.hxx" // not compatible with placement new used below
16
17using namespace resip;
18using namespace std;
19
20namespace resip
21{
22// Implemented in gen/DayOfWeekHash.cxx
23struct days { const char *name; DayOfWeek type; };
25{
26private:
27 static inline unsigned int hash (const char *str, GPERF_SIZE_TYPE len);
28public:
29 static const struct days *in_word_set (const char *str, GPERF_SIZE_TYPE len);
30};
31
32// Implemented in gen/MonthHash.cxx
33struct months { const char *name; Month type; };
35{
36private:
37 static inline unsigned int hash (const char *str, GPERF_SIZE_TYPE len);
38public:
39 static const struct months *in_word_set (const char *str, GPERF_SIZE_TYPE len);
40};
41}
42
43#define RESIPROCATE_SUBSYSTEM Subsystem::SIP
44
45//====================
46// Date
47//====================
48
50{
51 "Sun",
52 "Mon",
53 "Tue",
54 "Wed",
55 "Thu",
56 "Fri",
57 "Sat"
58};
59
61{
62 "Jan",
63 "Feb",
64 "Mar",
65 "Apr",
66 "May",
67 "Jun",
68 "Jul",
69 "Aug",
70 "Sep",
71 "Oct",
72 "Nov",
73 "Dec"
74};
75
76
79 mDayOfWeek(Sun),
80 mDayOfMonth(),
81 mMonth(Jan),
82 mYear(0),
83 mHour(0),
84 mMin(0),
85 mSec(0)
86{
87 time_t now;
88 time(&now);
89 if (now == ((time_t)-1))
90 {
91 int e = getErrno();
92 DebugLog (<< "Failed to get time: " << strError(e));
94 return;
95 }
96
97 setDatetime(now);
98}
99
101 : ParserCategory(),
102 mDayOfWeek(Sun),
103 mDayOfMonth(),
104 mMonth(Jan),
105 mYear(0),
106 mHour(0),
107 mMin(0),
108 mSec(0)
109{
110 setDatetime(datetime);
111}
112
114 Headers::Type type,
115 PoolBase* pool)
116 : ParserCategory(hfv, type, pool),
117 mDayOfWeek(Sun),
118 mDayOfMonth(),
119 mMonth(Jan),
120 mYear(0),
121 mHour(0),
122 mMin(0),
123 mSec(0)
124{}
125
127 PoolBase* pool)
128 : ParserCategory(rhs, pool),
129 mDayOfWeek(rhs.mDayOfWeek),
130 mDayOfMonth(rhs.mDayOfMonth),
131 mMonth(rhs.mMonth),
132 mYear(rhs.mYear),
133 mHour(rhs.mHour),
134 mMin(rhs.mMin),
135 mSec(rhs.mSec)
136{}
137
140{
141 if (this != &rhs)
142 {
144 mDayOfWeek = rhs.mDayOfWeek;
145 mDayOfMonth = rhs.mDayOfMonth;
146 mMonth = rhs.mMonth;
147 mYear = rhs.mYear;
148 mHour = rhs.mHour;
149 mMin = rhs.mMin;
150 mSec = rhs.mSec;
151 }
152 return *this;
153}
154
155bool
157{
158 struct tm gmt;
159#if defined(WIN32)
160 if (gmtime_s(&gmt, &datetime) != 0)
161 {
162 int e = getErrno();
163 DebugLog(<< "Failed to convert to gmt: " << strError(e));
165 return false;
166 }
167#else
168 // gmtime_r is available on Linux, Solaris, and other POSIX platforms
169 if (gmtime_r(&datetime, &gmt) == 0)
170 {
171 int e = getErrno();
172 DebugLog(<< "Failed to convert to gmt: " << strError(e));
174 return false;
175 }
176#endif
177
178 mDayOfWeek = static_cast<DayOfWeek>(gmt.tm_wday);
179 mDayOfMonth = gmt.tm_mday;
180 mMonth = static_cast<Month>(gmt.tm_mon);
181 mYear = gmt.tm_year + 1900;
182 mHour = gmt.tm_hour;
183 mMin = gmt.tm_min;
184 mSec = gmt.tm_sec;
185 DebugLog (<< "Set date: day=" << mDayOfWeek
186 << " month=" << mMonth
187 << " year=" << mYear
188 << " " << mHour << ":" << mMin << ":" << mSec);
189 return true;
190}
191
194{
195 const char *str = dow.data();
196 Data::size_type len = dow.size();
197
198 const struct days* _day = DayOfWeekHash::in_word_set(str, len);
199 if(_day != 0)
200 {
201 return _day->type;
202 }
203 else
204 {
205 return Sun;
206 }
207}
208
209Month
211{
212 const char *str = mon.data();
213 Data::size_type len = mon.size();
214
215 const struct months* _month = MonthHash::in_word_set(str, len);
216 if(_month != 0)
217 {
218 return _month->type;
219 }
220 else
221 {
222 return Jan;
223 }
224}
225
226const DayOfWeek&
228{
229 checkParsed();
230 return mDayOfWeek;
231}
232
233int&
235{
236 checkParsed();
237 return mDayOfMonth;
238}
239
240int
242{
243 checkParsed();
244 return mDayOfMonth;
245}
246
247Month&
249{
250 checkParsed();
251 return mMonth;
252}
253
254Month
256{
257 checkParsed();
258 return mMonth;
259}
260
261int&
263{
264 checkParsed();
265 return mYear;
266}
267
268int
270{
271 checkParsed();
272 return mYear;
273}
274
275int&
277{
278 checkParsed();
279 return mHour;
280}
281
282int
284{
285 checkParsed();
286 return mHour;
287}
288
289int&
291{
292 checkParsed();
293 return mMin;
294}
295
296int
298{
299 checkParsed();
300 return mMin;
301}
302
303int&
305{
306 checkParsed();
307 return mSec;
308}
309
310int
312{
313 checkParsed();
314 return mSec;
315}
316
317void
319{
320 // Mon, 04 Nov 2002 17:34:15 GMT
321 // Note: Day of Week is optional, so this is also valid: 04 Nov 2002 17:34:15 GMT
322
323 const char* anchor = pb.skipWhitespace();
324
325 // If comma is present, then DayOfWeek is present
327 if (!pb.eof())
328 {
330 pb.data(dayOfWeek, anchor);
332
334 }
335 else
336 {
337 pb.reset(pb.start());
338 mDayOfWeek = DayOfWeek::NA;
339 }
340
341 pb.skipWhitespace();
342
343 mDayOfMonth = pb.integer();
344
345 anchor = pb.skipWhitespace();
347
348 Data month;
349 pb.data(month, anchor);
351
352 pb.skipWhitespace();
353 mYear = pb.integer();
354
355 pb.skipWhitespace();
356
357 mHour = pb.integer();
359 mMin = pb.integer();
361 mSec = pb.integer();
362
363 pb.skipWhitespace();
364 pb.skipChar('G');
365 pb.skipChar('M');
366 pb.skipChar('T');
367
368 pb.skipWhitespace();
369 pb.assertEof();
370}
371
374{
375 return new DateCategory(*this);
376}
377
379DateCategory::clone(void* location) const
380{
381 return new (location) DateCategory(*this);
382}
383
386{
387 return new (pool) DateCategory(*this, pool);
388}
389
390static void pad2(const int x, EncodeStream& str)
391{
392 if (x < 10)
393 {
394 str << Symbols::ZERO[0];
395 }
396 str << x;
397}
398
401{
402 if (mDayOfWeek != DayOfWeek::NA)
403 {
404 str << DayOfWeekData[mDayOfWeek] // Mon
405 << Symbols::COMMA[0] << Symbols::SPACE[0];
406 }
407
408 pad2(mDayOfMonth, str); // 04
409
410 str << Symbols::SPACE[0]
411 << MonthData[mMonth] << Symbols::SPACE[0] // Nov
412 << mYear << Symbols::SPACE[0]; // 2002
413
414 pad2(mHour, str);
415 str << Symbols::COLON[0];
416 pad2(mMin, str);
417 str << Symbols::COLON[0];
418 pad2(mSec, str);
419 str << " GMT";
420
421 return str;
422}
423
424
425/* ====================================================================
426 * The Vovida Software License, Version 1.0
427 *
428 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
429 *
430 * Redistribution and use in source and binary forms, with or without
431 * modification, are permitted provided that the following conditions
432 * are met:
433 *
434 * 1. Redistributions of source code must retain the above copyright
435 * notice, this list of conditions and the following disclaimer.
436 *
437 * 2. Redistributions in binary form must reproduce the above copyright
438 * notice, this list of conditions and the following disclaimer in
439 * the documentation and/or other materials provided with the
440 * distribution.
441 *
442 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
443 * and "Vovida Open Communication Application Library (VOCAL)" must
444 * not be used to endorse or promote products derived from this
445 * software without prior written permission. For written
446 * permission, please contact vocal@vovida.org.
447 *
448 * 4. Products derived from this software may not be called "VOCAL", nor
449 * may "VOCAL" appear in their name, without prior written
450 * permission of Vovida Networks, Inc.
451 *
452 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
453 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
454 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
455 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
456 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
457 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
458 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
459 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
460 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
461 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
462 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
463 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
464 * DAMAGE.
465 *
466 * ====================================================================
467 *
468 * This software consists of voluntary contributions made by Vovida
469 * Networks, Inc. and many individuals on behalf of Vovida Networks,
470 * Inc. For more information on Vovida Networks, Inc., please see
471 * <http://www.vovida.org/>.
472 *
473 */
static void pad2(const int x, EncodeStream &str)
#define DebugLog(args_)
Definition Logger.hxx:128
Handles cross-platform sockets compatibility.
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
size_t size_type
Definition Data.hxx:94
size_type size() const noexcept
Returns the number of bytes in this Data.
Definition Data.hxx:599
const char * data() const noexcept
Returns a pointer to the contents of this Data.
Definition Data.hxx:608
Represents the "SIP-date" element in the RFC 3261 grammar.
virtual EncodeStream & encodeParsed(EncodeStream &str) const
virtual void parse(ParseBuffer &pb)
static DayOfWeek DayOfWeekFromData(const Data &)
const DayOfWeek & dayOfWeek() const
DateCategory & operator=(const DateCategory &)
virtual ParserCategory * clone() const
virtual bool setDatetime(time_t datetime)
static Month MonthFromData(const Data &)
static const struct days * in_word_set(const char *str, GPERF_SIZE_TYPE len)
void checkParsed() const
static const struct months * in_word_set(const char *str, GPERF_SIZE_TYPE len)
Provides string-parsing functionality with protection from buffer overflow.
CurrentPosition skipNonWhitespace()
Pointer start() const
CurrentPosition skipWhitespace()
CurrentPosition skipToChar(char c)
void reset(const char *pos)
CurrentPosition skipChar()
void data(Data &data, const char *start) const
make the passed in data share memory with the buffer (uses Data::Share)
void assertEof() const
Base class for all SIP grammar elements that can have parameters.
ParserCategory & operator=(const ParserCategory &rhs)
static const char * ZERO
Definition Symbols.hxx:36
static const char * SPACE
Definition Symbols.hxx:34
static const char * COLON
Definition Symbols.hxx:15
static const char * COMMA
Definition Symbols.hxx:35
static void error(int e)
Generates a generic log for the platform specific socket error number.
This file is used to handle compatibility fixes/tweaks so reSIProcate can function on multiple platfo...
#define GPERF_SIZE_TYPE
Definition compat.hxx:7
dcm! – add UnusedChecking(_enum) below;
Data DayOfWeekData[]
int getErrno()
Definition Socket.hxx:97
std::string strError(int err)
Definition compat.hxx:274
Data MonthData[]
#define EncodeStream
const char * name
DayOfWeek type
const char * name