1 /*****
2 *
3 * Copyright (C) 2008-2015 CS-SI. All Rights Reserved.
4 * Author: Yoann Vandoorselaere <yoann@prelude-ids.com>
5 *
6 * This file is part of the Prelude library.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 *****/
23 
24 #include "prelude-error.hxx"
25 #include "idmef-time.hxx"
26 
27 using namespace Prelude;
28 
29 
30 IDMEFTime::~IDMEFTime()
31 {
32         if ( _time )
33                 idmef_time_destroy(_time);
IDMEFPath(const char * buffer)34 }
35 
36 
37 IDMEFTime::IDMEFTime(const IDMEFTime &time)
38 {
39         _time = (time._time) ? idmef_time_ref(time._time) : NULL;
40 }
41 
42 
43 IDMEFTime::IDMEFTime()
44 {
IDMEFPath(IDMEF & idmef,const char * buffer)45         int ret;
46 
47         ret = idmef_time_new_from_gettimeofday(&_time);
48         if ( ret < 0 )
49                 throw PreludeError(ret);
50 
51 }
52 
53 
54 IDMEFTime::IDMEFTime(idmef_time_t *time)
55 {
56         _time = time;
IDMEFPath(idmef_path_t * path)57 }
58 
59 
60 IDMEFTime::IDMEFTime(time_t time)
61 {
62         int ret;
63 
64         ret = idmef_time_new_from_time(&_time, &time);
65         if ( ret < 0 )
66                 throw PreludeError(ret);
67 }
68 
~IDMEFPath()69 
70 IDMEFTime::IDMEFTime(const char *str)
71 {
72         int ret;
73 
74         ret = idmef_time_new_from_string(&_time, str);
75         if ( ret < 0 )
76                 throw PreludeError(ret);
77 }
78 
79 
80 IDMEFTime::IDMEFTime(const struct timeval *tv)
81 {
82         int ret;
83 
84         ret = idmef_time_new_from_timeval(&_time, tv);
85         if ( ret < 0 )
86                 throw PreludeError(ret);
87 }
88 
89 
90 
91 void IDMEFTime::set(const time_t *t)
getValueType(int depth) const92 {
93         idmef_time_set_from_time(_time, t);
94 }
95 
96 
97 void IDMEFTime::set(const struct timeval *tv)
checkOperator(idmef_criterion_operator_t op) const98 {
99         int ret;
100 
101         ret = idmef_time_set_from_timeval(_time, tv);
102         if ( ret < 0 )
103                 throw PreludeError(ret);
104 }
getApplicableOperators() const105 
106 
107 
108 void IDMEFTime::set(const char *str)
109 {
110         int ret;
111 
112         ret = idmef_time_set_from_string(_time, str);
113         if ( ret < 0 )
114                 throw PreludeError(ret);
115 }
116 
117 
118 void IDMEFTime::set()
set(IDMEF & message,const std::vector<IDMEF> & value) const119 {
120         int ret;
121 
122         ret = idmef_time_set_from_gettimeofday(_time);
123         if ( ret < 0 )
124                 throw PreludeError(ret);
125 }
126 
127 
128 void IDMEFTime::setSec(uint32_t sec)
129 {
set(IDMEF & message,IDMEF * value) const130         idmef_time_set_sec(_time, sec);
131 }
132 
133 
134 void IDMEFTime::setUSec(uint32_t usec)
135 {
136         idmef_time_set_usec(_time, usec);
137 }
138 
139 
140 void IDMEFTime::setGmtOffset(int32_t gmtoff)
141 {
142         idmef_time_set_gmt_offset(_time, gmtoff);
143 }
set(IDMEF & message,IDMEFValue * value) const144 
145 
146 uint32_t IDMEFTime::getSec() const
147 {
148         return idmef_time_get_sec(_time);
149 }
150 
151 
152 uint32_t IDMEFTime::getUSec() const
153 {
154         return idmef_time_get_usec(_time);
155 }
156 
157 
set(IDMEF & message,const std::vector<IDMEFValue> & value) const158 int32_t IDMEFTime::getGmtOffset() const
159 {
160         return idmef_time_get_gmt_offset(_time);
161 }
162 
163 
164 IDMEFTime IDMEFTime::clone() const
165 {
166         int ret;
167         idmef_time_t *clone;
168 
set(IDMEF & message,IDMEFValue & value) const169         ret = idmef_time_clone(_time, &clone);
170         if ( ret < 0 )
171                 throw PreludeError(ret);
172 
173         return IDMEFTime(clone);
174 }
175 
176 
177 const std::string IDMEFTime::toString() const
178 {
set(IDMEF & message,const std::string & value) const179         int ret;
180         std::string cs;
181         prelude_string_t *str = NULL;
182 
183         ret = prelude_string_new(&str);
184         if ( ret < 0 )
185                 throw PreludeError(ret);
186 
187         ret = idmef_time_to_string(_time, str);
188         if ( ret < 0 )
set(IDMEF & message,const char * value) const189                 throw PreludeError(ret);
190 
191         cs = prelude_string_get_string(str);
192         prelude_string_destroy(str);
193 
194         return cs;
195 }
196 
197 
198 bool IDMEFTime::operator <= (const IDMEFTime &time) const
199 {
200         return ( (double) *this <= (double) time );
201 }
202 
203 
set(IDMEF & message,int32_t value) const204 bool IDMEFTime::operator < (const IDMEFTime &time) const
205 {
206         return ( (double) *this < (double) time );
207 }
208 
209 
210 bool IDMEFTime::operator >= (const IDMEFTime &time) const
211 {
212         return ( (double) *this >= (double) time );
213 }
214 
set(IDMEF & message,int64_t value) const215 
216 bool IDMEFTime::operator > (const IDMEFTime &time) const
217 {
218         return ( (double) *this > (double) time );
219 }
220 
221 
222 bool IDMEFTime::operator != (const IDMEFTime &time) const
223 {
224         return ( (double) *this != (double) time );
set(IDMEF & message,uint64_t value) const225 }
226 
227 
228 bool IDMEFTime::operator == (const IDMEFTime &time) const
229 {
230         return ( (double) *this == (double) time );
231 }
232 
233 
234 IDMEFTime::operator int() const
set(IDMEF & message,float value) const235 {
236         return getSec();
237 }
238 
239 
240 IDMEFTime::operator long() const
241 {
242         return getSec();
243 }
244 
set(IDMEF & message,double value) const245 
246 IDMEFTime::operator double() const
247 {
248         return getSec() + (getUSec() * 1e-6);
249 }
250 
251 
252 IDMEFTime::operator const std::string() const
253 {
254         return toString();
set(IDMEF & message,IDMEFTime & time) const255 }
256 
257 
258 IDMEFTime::operator idmef_time_t *() const
259 {
260         return _time;
261 }
262 
263 
264 IDMEFTime &IDMEFTime::operator=(const IDMEFTime &time)
265 {
getClass(int depth) const266         if ( this != &time && _time != time._time ) {
267                 if ( _time )
268                         idmef_time_destroy(_time);
269 
270                 _time = (time._time) ? idmef_time_ref(time._time) : NULL;
271         }
setIndex(unsigned int index,int depth)272 
273         return *this;
274 }
275 
276