-
Notifications
You must be signed in to change notification settings - Fork 14
/
PassByDigitsHelper.h
206 lines (182 loc) · 5.28 KB
/
PassByDigitsHelper.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#ifndef PASSBYDIGITSHELPER_H
#define PASSBYDIGITSHELPER_H
NSString * stringFromDateAndFormat(NSDate * date, NSString * format)
{
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format];
NSString * string = [formatter stringFromDate:date];
[formatter release];
return string;
}
NSDate * dateFromStringAndFormat(NSString * string, NSString * format)
{
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format];
NSDate * date = [formatter dateFromString:string];
[formatter release];
return date;
}
BOOL evalDateTimeHelper(NSString * format, char d0, char d1, BOOL reversed)
{
if (reversed) {
char tmp = d0;
d0 = d1;
d1 = tmp;
}
NSString * dateString =
stringFromDateAndFormat(
[NSDate dateWithTimeIntervalSinceNow:timeShift * 60],
format
);
return d0 == [dateString characterAtIndex:0]
&& d1 == [dateString characterAtIndex:1];
}
struct Digits {
BOOL (* eval) (struct Digits *, char, char);
char digit0, digit1;
BOOL reversed, isGracePeriod;
};
static BOOL evalCustom(struct Digits * config, char d0, char d1)
{
return config->reversed
? d1 == config->digit0 && d0 == config->digit1
: d0 == config->digit0 && d1 == config->digit1;
}
static BOOL evalTimeH(struct Digits * config, char d0, char d1)
{
return evalDateTimeHelper(
use24hFormat ? @"HH" : @"hh",
d0, d1, config->reversed
);
}
static BOOL evalTimeM(struct Digits * config, char d0, char d1)
{
return evalDateTimeHelper(@"mm", d0, d1, config->reversed);
}
static BOOL evalDateD(struct Digits * config, char d0, char d1)
{
return evalDateTimeHelper(@"dd", d0, d1, config->reversed);
}
static BOOL evalDateM(struct Digits * config, char d0, char d1)
{
return evalDateTimeHelper(@"MM", d0, d1, config->reversed);
}
static BOOL evalBattR(struct Digits * config, char d0, char d1)
{
if (config->reversed) {
char tmp = d0;
d0 = d1;
d1 = tmp;
}
int level = (int) ([[UIDevice currentDevice] batteryLevel] * 100.0f);
return d0 == '0' + ((level / 10) % 10)
&& d1 == '0' + (level % 10);
}
static BOOL evalBattU(struct Digits * config, char d0, char d1)
{
if (config->reversed) {
char tmp = d0;
d0 = d1;
d1 = tmp;
}
int level = 100 - (int) ([[UIDevice currentDevice] batteryLevel] * 100.0f);
return d0 == '0' + ((level / 10) % 10)
&& d1 == '0' + (level % 10);
}
static BOOL evalGraceP(struct Digits * config, char d0, char d1)
{
if (config->reversed) {
char tmp = d0;
d0 = d1;
d1 = tmp;
}
digitsGracePeriod = (d0 - '0') * 60 + (d1 - '0') * 10;
return YES;
}
enum DigitConfig : int {
CONFIG_TIMEH = 0,
CONFIG_TIMEM = 1,
CONFIG_DATED = 2,
CONFIG_DATEM = 3,
CONFIG_BATTR = 4,
CONFIG_BATTU = 5,
CONFIG_GRACEP = 6,
CONFIG_CUSTOM = 7
};
void parseDigitsConfiguration(struct Digits * digits,
NSString * custom, int config, BOOL reversed)
{
digits->reversed = reversed;
digits->isGracePeriod = NO;
switch(config) {
case CONFIG_TIMEH:
digits->eval = evalTimeH;
return;
case CONFIG_TIMEM:
digits->eval = evalTimeM;
return;
case CONFIG_DATED:
digits->eval = evalDateD;
return;
case CONFIG_DATEM:
digits->eval = evalDateM;
return;
case CONFIG_BATTR:
digits->eval = evalBattR;
return;
case CONFIG_BATTU:
digits->eval = evalBattU;
return;
case CONFIG_GRACEP:
digits->eval = evalGraceP;
digits->isGracePeriod = YES;
return;
case CONFIG_CUSTOM:
default:
if ([custom length] == 2) {
digits->digit0 = [custom characterAtIndex:0];
digits->digit1 = [custom characterAtIndex:1];
} else {
digits->digit1 = digits->digit0 = '0';
}
digits->eval = evalCustom;
return;
}
}
struct Time {
int hours, minutes;
};
BOOL parseTime(struct Time * time, NSString * timeString)
{
char h0, h1, m0, m1;
if ([timeString length] == 4) {
h0 = '0';
h1 = [timeString characterAtIndex:0];
m0 = [timeString characterAtIndex:2];
m1 = [timeString characterAtIndex:3];
} else if ([timeString length] == 5) {
h0 = [timeString characterAtIndex:0];
h1 = [timeString characterAtIndex:1];
m0 = [timeString characterAtIndex:3];
m1 = [timeString characterAtIndex:4];
} else {
return NO;
}
if (h0 >= '0' && h0 <= '2'
&& h1 >= '0' && h1 <= (h0 == 2 ? '3' : '9')
&& m0 >= '0' && m0 <= '5'
&& m1 >= '0' && m1 <= '9'
) {
time->hours = (h0 - '0') * 10 + h1 - '0';
time->minutes = (m0 - '0') * 10 + m1 - '0';
return YES;
}
return NO;
}
#else
#error "File already included"
#endif // PASSBYDIGITSHELPER_H