forked from ben-crowhurst/crontab-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
122 lines (99 loc) · 2.39 KB
/
main.cpp
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
#include <map>
#include <cstdio>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
using namespace std;
const int MINUTESPERHOUR = 60;
const int HOURESPERDAY = 24;
const int DAYSPERWEEK = 7;
const int MONTHSPERYEAR = 12;
const int DAYSPERMONTH = 31;
vector< string > split( const string& value, const char delimiter )
{
vector< string > result;
string token = "";
stringstream stream( value );
while( getline( stream, token, delimiter ) )
{
result.push_back( token );
}
return result;
}
string expand( const string& expression )
{
string result = "";
if ( expression.find( "-" ) not_eq string::npos )
{
vector< string > rangearray = split( expression, '-' );
int start = stoi( rangearray[ 0 ] );
int stop = stoi( rangearray[ 1 ] );
for(int index = start; index <= stop; index++ )
{
result.append( to_string( index ) + "," );
}
}
else
{
result = expression + ",";
}
return result;
}
vector< string > parse(string expression, int maximum, int minimum)
{
vector< string > subexpressions;
if ( expression.find( "," ) not_eq string::npos )
{
subexpressions = split( expression, ',' );
}
else
{
subexpressions.push_back(expression);
}
string rangeitems;
for (const auto& subexpression : subexpressions)
{
if (subexpression.find("/") not_eq string::npos) // handle */N syntax
{
for (int a = 1; a <= maximum; a++)
{
if (a % stoi(subexpression.substr(subexpression.find("/")+1)) == 0)
{
if(a == maximum)
{
rangeitems.append(to_string(minimum));
}
else
{
rangeitems.append(to_string(a));
}
rangeitems.append(",");
}
}
}
else
{
if(subexpression == "*")
{
rangeitems.append(expand(to_string(minimum) + "-" + to_string(maximum)));
}
else
{
rangeitems.append(expand(subexpression));
}
}
}
return split( rangeitems, ',' );
}
int main( int argc, char** argv )
{
//bug: 3-59/15
auto result = parse("3-59/15", MINUTESPERHOUR, 0);
cout << "size: " << result.size( ) << endl;
for (auto item : result)
{
cout << "first: " << item << endl;
}
return EXIT_SUCCESS;
}