1 /***************************************************************************
2 * CT Unit of Time Interval Implementation *
3 * -------------------------------------------------------------------- *
4 * Copyright (C) 1999, Gary Meyer <gary@meyer.net> *
5 * -------------------------------------------------------------------- *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 ***************************************************************************/
12 // Do not introduce any Qt or KDE dependencies into the "CT"-prefixed classes.
13 // I want to be able to reuse these classes with another GUI toolkit. -GM 11/99
18 #include <stdio.h> // sprintf
19 #include <ctype.h> // tolower
24 template<int min
, int max
>
25 CTUnit
<min
, max
>::CTUnit(const string
&tokStr
)
30 template<int min
, int max
>
31 CTUnit
<min
, max
>::CTUnit(const CTUnit
& source
)
33 for (int i
= 0; i
<= max
; i
++)
35 initialEnabled
[i
] = 0;
36 enabled
[i
] = source
.enabled
[i
];
42 template<int min
, int max
>
43 CTUnit
<min
, max
>::~CTUnit()
47 template<int min
, int max
>
48 void CTUnit
<min
, max
>::operator = (const CTUnit
<min
,max
>& unit
)
50 for (int i
= 0; i
<= max
; i
++)
51 enabled
[i
] = unit
.enabled
[i
];
56 template<int min
, int max
>
57 void CTUnit
<min
, max
>::initialize(const string
&tokStr
)
59 for (int i
= 0; i
<= max
; i
++)
64 for (int i
= min
; i
<= max
; i
++)
65 initialEnabled
[i
] = enabled
[i
];
67 initialTokStr
= tokStr
;
73 template<int min
, int max
>
74 void CTUnit
<min
, max
>::parse(string tokStr
)
76 // subelement is that which is between commas
78 int commapos
, slashpos
, dashpos
;
79 int beginat
, endat
, step
;
81 // loop through each subelement
83 while ((commapos
= tokStr
.find(",")) > 0)
85 subelement
= tokStr
.substr(0,commapos
);
87 // find "/" to determine step
88 slashpos
= subelement
.find("/");
92 slashpos
= subelement
.length();
96 step
= fieldToValue(subelement
.substr(slashpos
+1,
97 subelement
.length()-slashpos
-1));
102 // find "=" to determine range
103 dashpos
= subelement
.find("-");
107 if (subelement
.substr(0,slashpos
) == "*")
114 beginat
= fieldToValue(subelement
.substr(0,slashpos
));
120 beginat
= fieldToValue(subelement
.substr(0,dashpos
));
121 endat
= fieldToValue(subelement
.substr(dashpos
+1,slashpos
-dashpos
-1));
124 // ignore out of range
131 for (int i
= beginat
; i
<= endat
; i
+=step
)
134 tokStr
= tokStr
.substr(commapos
+1, tokStr
.length()-commapos
-1);
139 template<int min
, int max
>
140 string CTUnit
<min
, max
>::tokenize() const
144 return initialTokStr
;
158 sprintf(cnum
, "%u", num
);
165 if (count
== (max
- min
+ 1))
171 template<int min
, int max
>
172 string CTUnit
<min
, max
>::describe(const string
*label
) const
177 for (int i
= min
; i
<= max
; i
++)
183 switch (total
- count
)
186 case 1: if (total
> 2) tmpStr
+= (const char *)i18n(",").toLocal8Bit();
187 tmpStr
+= (const char *)i18n(" and ").toLocal8Bit();
189 default: tmpStr
+= (const char *)i18n(", ").toLocal8Bit();
197 template<int min
, int max
>
198 int CTUnit
<min
, max
>::begin()
203 template<int min
, int max
>
204 int CTUnit
<min
, max
>::end()
209 template<int min
, int max
>
210 bool CTUnit
<min
, max
>::get(int pos
) const
215 template<int min
, int max
>
216 void CTUnit
<min
, max
>::set(int pos
, bool value
)
218 enabled
[pos
] = value
;
223 template<int min
, int max
>
224 void CTUnit
<min
, max
>::enable(int pos
)
231 template<int min
, int max
>
232 void CTUnit
<min
, max
>::disable(int pos
)
234 enabled
[pos
] = false;
239 template<int min
, int max
>
240 bool CTUnit
<min
, max
>::dirty() const
245 template<int min
, int max
>
246 int CTUnit
<min
, max
>::count() const
249 for (int i
= min
; i
<= max
; i
++)
250 total
+= (enabled
[i
] == true);
254 template<int min
, int max
>
255 void CTUnit
<min
, max
>::apply()
257 initialTokStr
= tokenize();
258 for (int i
= min
; i
<= max
; i
++)
259 initialEnabled
[i
] = enabled
[i
];
264 template<int min
, int max
>
265 void CTUnit
<min
, max
>::cancel()
267 for (int i
= min
; i
<= max
; i
++)
268 enabled
[i
] = initialEnabled
[i
];
273 template<int min
, int max
>
274 int CTUnit
<min
, max
>::fieldToValue(string entry
) const
276 // GNU C++ STL has no String::toLower() so we have to lower
280 int length
= entry
.length();
281 for (int i
= 0; i
< length
; i
++)
282 lower
+= tolower(*(entry
.substr(i
, 1).c_str()));
287 "sun", "mon", "tue", "wed", "thu", "fri", "sat"
290 for (int day
= 0; day
< 7; day
++)
292 if (lower
== days
[day
])
295 sprintf(cnum
, "%u", day
);
304 "jan", "feb", "mar", "apr", "may", "jun",
305 "jul", "aug", "sep", "oct", "nov", "dec"
308 for (int month
= 1; month
< 13; month
++)
310 if (lower
== months
[month
])
313 sprintf(cnum
, "%u", month
);
318 return atoi(entry
.c_str());