Merge branch 'master' of ssh://apples.lambdacomplex.org/git/bus
[bus.git] / origin-src / serviceperiod.cc
1 #include <string.h>
2 #include <assert.h>
3 #include "serviceperiod.h"
4 #include "defuns.h"
5
6 using namespace std;
7
8
9 static time_t get_time_t(int tm_mday, int tm_mon, int tm_year)
10 {
11 struct tm t;
12 t.tm_sec = 0;
13 t.tm_min = 0;
14 t.tm_hour = 0;
15 t.tm_mday = tm_mday;
16 t.tm_mon = tm_mon;
17 t.tm_year = tm_year;
18 t.tm_wday = -1;
19 t.tm_yday = -1;
20 t.tm_isdst = -1;
21
22 return mktime(&t);
23 }
24
25
26 ServicePeriodException::ServicePeriodException(int32_t _tm_mday, int32_t _tm_mon,
27 int32_t _tm_year)
28 {
29 tm_mday = _tm_mday;
30 tm_mon = _tm_mon;
31 tm_year = _tm_year;
32 }
33
34
35 ServicePeriodException::ServicePeriodException()
36 {
37 tm_mday = 0;
38 tm_mon = 0;
39 tm_year = 0;
40 }
41
42
43 ServicePeriod::ServicePeriod(int32_t _id, int32_t _start_mday,
44 int32_t _start_mon, int32_t _start_year,
45 int32_t _end_mday, int32_t _end_mon,
46 int32_t _end_year, int32_t _duration,
47 bool _weekday, bool _saturday,
48 bool _sunday)
49 {
50 id = _id;
51
52 start_time = get_time_t(_start_mday, _start_mon, _start_year);
53 end_time = get_time_t(_end_mday, _end_mon, _end_year);
54
55 duration = _duration;
56 weekday = _weekday;
57 saturday = _saturday;
58 sunday = _sunday;
59 }
60
61
62 ServicePeriod::ServicePeriod(const ServicePeriod &s)
63 {
64 id = s.id;
65
66 start_time = s.start_time;
67 end_time = s.end_time;
68
69 duration = s.duration;
70 weekday = s.weekday;
71 saturday = s.saturday;
72 sunday = s.sunday;
73
74 for (vector<ServicePeriodException>::const_iterator i = s.exceptions_on.begin();
75 i != s.exceptions_on.end(); i++)
76 add_exception_on(i->tm_mday, i->tm_mon, i->tm_year);
77
78 for (vector<ServicePeriodException>::const_iterator i = s.exceptions_off.begin();
79 i != s.exceptions_off.end(); i++)
80 add_exception_off(i->tm_mday, i->tm_mon, i->tm_year);
81 }
82
83
84 ServicePeriod::ServicePeriod()
85 {
86 // blank service period object
87 start_time = 0;
88 end_time = 0;
89
90 duration = 0;
91 weekday = false;
92 saturday = false;
93 sunday = false;
94 }
95
96
97 ServicePeriod::ServicePeriod(FILE *fp)
98 {
99 assert(fread(&id, sizeof(int32_t), 1, fp) == 1);
100
101 assert(fread(&start_time, sizeof(time_t), 1, fp) == 1);
102 assert(fread(&end_time, sizeof(time_t), 1, fp) == 1);
103
104 assert(fread(&duration, sizeof(int32_t), 1, fp) == 1);
105 assert(fread(&weekday, sizeof(bool), 1, fp) == 1);
106 assert(fread(&saturday, sizeof(bool), 1, fp) == 1);
107 assert(fread(&sunday, sizeof(bool), 1, fp) == 1);
108
109 uint32_t num_exceptions_on;
110 assert(fread(&num_exceptions_on, sizeof(uint32_t), 1, fp) == 1);
111 for (int i=0; i < num_exceptions_on; i++)
112 {
113 ServicePeriodException e;
114 assert(fread(&e, sizeof(ServicePeriodException), 1, fp) == 1);
115 add_exception_on(e.tm_mday, e.tm_mon, e.tm_year);
116 }
117
118 uint32_t num_exceptions_off;
119 assert(fread(&num_exceptions_off, sizeof(uint32_t), 1, fp) == 1);
120 for (int i=0; i < num_exceptions_off; i++)
121 {
122 ServicePeriodException e;
123 assert(fread(&e, sizeof(ServicePeriodException), 1, fp) == 1);
124 add_exception_off(e.tm_mday, e.tm_mon, e.tm_year);
125 }
126 }
127
128
129 void ServicePeriod::write(FILE *fp)
130 {
131 assert(fwrite(&id, sizeof(int32_t), 1, fp) == 1);
132
133 assert(fwrite(&start_time, sizeof(time_t), 1, fp) == 1);
134 assert(fwrite(&end_time, sizeof(time_t), 1, fp) == 1);
135
136 assert(fwrite(&duration, sizeof(int32_t), 1, fp) == 1);
137 assert(fwrite(&weekday, sizeof(bool), 1, fp) == 1);
138 assert(fwrite(&saturday, sizeof(bool), 1, fp) == 1);
139 assert(fwrite(&sunday, sizeof(bool), 1, fp) == 1);
140
141 uint32_t num_exceptions_on = exceptions_on.size();
142 assert(fwrite(&num_exceptions_on, sizeof(uint32_t), 1, fp) == 1);
143 for (vector<ServicePeriodException>::iterator i = exceptions_on.begin();
144 i != exceptions_on.end(); i++)
145 {
146 ServicePeriodException &e = (*i);
147 assert(fwrite(&e, sizeof(ServicePeriodException), 1, fp) == 1);
148 }
149
150 uint32_t num_exceptions_off = exceptions_off.size();
151 assert(fwrite(&num_exceptions_off, sizeof(uint32_t), 1, fp) == 1);
152 for (vector<ServicePeriodException>::iterator i = exceptions_off.begin();
153 i != exceptions_off.end(); i++)
154 {
155 ServicePeriodException &e = (*i);
156 assert(fwrite(&e, sizeof(ServicePeriodException), 1, fp) == 1);
157 }
158 }
159
160
161 void ServicePeriod::add_exception_on(int32_t tm_mday, int32_t tm_mon, int32_t tm_year)
162 {
163 exceptions_on.push_back(ServicePeriodException(tm_mday, tm_mon, tm_year));
164 }
165
166
167 void ServicePeriod::add_exception_off(int32_t tm_mday, int32_t tm_mon, int32_t tm_year)
168 {
169 exceptions_off.push_back(ServicePeriodException(tm_mday, tm_mon, tm_year));
170 }
171
172
173 bool ServicePeriod::is_turned_on(int32_t tm_mday, int32_t tm_mon, int32_t tm_year)
174 {
175 for (vector<ServicePeriodException>::iterator i = exceptions_on.begin();
176 i != exceptions_on.end(); i++)
177 {
178 if ((*i).tm_mday == tm_mday && (*i).tm_mon == tm_mon &&
179 (*i).tm_year == tm_year)
180 return true;
181 }
182
183 return false;
184 }
185
186
187 bool ServicePeriod::is_turned_off(int32_t tm_mday, int32_t tm_mon, int32_t tm_year)
188 {
189 for (vector<ServicePeriodException>::iterator i = exceptions_off.begin();
190 i != exceptions_off.end(); i++)
191 {
192 if ((*i).tm_mday == tm_mday && (*i).tm_mon == tm_mon &&
193 (*i).tm_year == tm_year)
194 return true;
195 }
196
197 return false;
198 }
199