SVN_SILENT made messages (.desktop file)
[kdeadmin.git] / kcron / ctunit.cpp
blob8afa53f6b6b999e4c9ae913598f59d939a33c4fa
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
15 #include "cti18n.h"
16 #include <vector>
17 #include <string>
18 #include <stdio.h> // sprintf
19 #include <ctype.h> // tolower
20 #include <stdlib.h>
22 using namespace std;
24 template<int min, int max>
25 CTUnit<min, max>::CTUnit(const string &tokStr)
27 initialize(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];
38 initialTokStr = "";
39 isDirty = true;
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];
52 isDirty = true;
53 return;
56 template<int min, int max>
57 void CTUnit<min, max>::initialize(const string &tokStr)
59 for (int i = 0; i <= max; i++)
60 enabled[i] = 0;
62 parse(tokStr);
64 for (int i = min; i <= max; i++)
65 initialEnabled[i] = enabled[i];
67 initialTokStr = tokStr;
68 isDirty = false;
70 return;
73 template<int min, int max>
74 void CTUnit<min, max>::parse(string tokStr)
76 // subelement is that which is between commas
77 string subelement;
78 int commapos, slashpos, dashpos;
79 int beginat, endat, step;
81 // loop through each subelement
82 tokStr += ",";
83 while ((commapos = tokStr.find(",")) > 0)
85 subelement = tokStr.substr(0,commapos);
87 // find "/" to determine step
88 slashpos = subelement.find("/");
89 if (slashpos == -1)
91 step = 1;
92 slashpos = subelement.length();
94 else
96 step = fieldToValue(subelement.substr(slashpos+1,
97 subelement.length()-slashpos-1));
98 if (step < 1)
99 step = 1;
102 // find "=" to determine range
103 dashpos = subelement.find("-");
104 if (dashpos == -1)
106 // deal with "*"
107 if (subelement.substr(0,slashpos) == "*")
109 beginat = min;
110 endat = max;
112 else
114 beginat = fieldToValue(subelement.substr(0,slashpos));
115 endat = beginat;
118 else
120 beginat = fieldToValue(subelement.substr(0,dashpos));
121 endat = fieldToValue(subelement.substr(dashpos+1,slashpos-dashpos-1));
124 // ignore out of range
125 if (beginat < 0)
126 beginat = 0;
127 if (endat > max)
128 endat = max;
130 // setup enabled
131 for (int i = beginat; i <= endat; i+=step)
132 enabled[i] = 1;
134 tokStr = tokStr.substr(commapos+1, tokStr.length()-commapos-1);
136 return;
139 template<int min, int max>
140 string CTUnit<min, max>::tokenize() const
142 if (!isDirty)
144 return initialTokStr;
146 else
148 int total(count());
149 int count(0);
150 int num(min);
151 string tmpStr;
153 while (num <= max)
155 if (enabled[num])
157 char cnum[3];
158 sprintf(cnum, "%u", num);
159 tmpStr += cnum;
160 if (++count < total)
161 tmpStr += ",";
163 num++;
165 if (count == (max - min + 1))
166 tmpStr = "*";
167 return tmpStr;
171 template<int min, int max>
172 string CTUnit<min, max>::describe(const string *label) const
174 int total(count());
175 int count(0);
176 string tmpStr;
177 for (int i = min; i <= max; i++)
179 if (enabled[i])
181 tmpStr += label[i];
182 count++;
183 switch (total - count)
185 case 0: break;
186 case 1: if (total > 2) tmpStr += (const char *)i18n(",").toLocal8Bit();
187 tmpStr += (const char *)i18n(" and ").toLocal8Bit();
188 break;
189 default: tmpStr += (const char *)i18n(", ").toLocal8Bit();
190 break;
194 return tmpStr;
197 template<int min, int max>
198 int CTUnit<min, max>::begin()
200 return min;
203 template<int min, int max>
204 int CTUnit<min, max>::end()
206 return max;
209 template<int min, int max>
210 bool CTUnit<min, max>::get(int pos) const
212 return enabled[pos];
215 template<int min, int max>
216 void CTUnit<min, max>::set(int pos, bool value)
218 enabled[pos] = value;
219 isDirty = true;
220 return;
223 template<int min, int max>
224 void CTUnit<min, max>::enable(int pos)
226 enabled[pos] = true;
227 isDirty = true;
228 return;
231 template<int min, int max>
232 void CTUnit<min, max>::disable(int pos)
234 enabled[pos] = false;
235 isDirty = true;
236 return;
239 template<int min, int max>
240 bool CTUnit<min, max>::dirty() const
242 return isDirty;
245 template<int min, int max>
246 int CTUnit<min, max>::count() const
248 int total(0);
249 for (int i = min; i <= max; i++)
250 total += (enabled[i] == true);
251 return total;
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];
260 isDirty = false;
261 return;
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];
269 isDirty = false;
270 return;
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
277 // by hand.
279 string lower("");
280 int length = entry.length();
281 for (int i = 0; i < length; i++)
282 lower += tolower(*(entry.substr(i, 1).c_str()));
284 // check for days
285 string days[7] =
287 "sun", "mon", "tue", "wed", "thu", "fri", "sat"
290 for (int day = 0; day < 7; day++)
292 if (lower == days[day])
294 char cnum[3];
295 sprintf(cnum, "%u", day);
296 entry = cnum;
300 // check for months
301 string months[13] =
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])
312 char cnum[3];
313 sprintf(cnum, "%u", month);
314 entry = cnum;
318 return atoi(entry.c_str());