rulecomp.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <iostream>
  2. #include <set>
  3. #include <vector>
  4. #include <algorithm>
  5. #include "Lexer.h"
  6. #include "rulecomp.h"
  7. using namespace std;
  8. vector<vector<string> > weekDayString
  9. {
  10. {"sunday", "sun"},
  11. {"monday", "mon"},
  12. {"tuesday", "tue"},
  13. {"wednesday", "wed"},
  14. {"thursday", "thu"},
  15. {"friday", "fri"},
  16. {"saturday", "sat"}
  17. };
  18. vector<vector<string> > monthString
  19. {
  20. {"dummy"},
  21. {"january", "jan"},
  22. {"febrary", "feb"},
  23. {"march", "mar"},
  24. {"april", "apr"},
  25. {"may"},
  26. {"june", "jun"},
  27. {"july", "jul"},
  28. {"august", "aug"},
  29. {"september", "sep"},
  30. {"october", "oct"},
  31. {"november", "nov"},
  32. {"december", "dec"}
  33. };
  34. int getNr(const string& w, const vector<vector<string>>& dict)
  35. {
  36. int res = -1;
  37. for (unsigned int i = 0; res < 0 && i < dict.size(); ++i)
  38. {
  39. const vector<string>& entry = dict[i];
  40. if (find(entry.begin(), entry.end(), w) != entry.end())
  41. res = i;
  42. }
  43. if (res < 0)
  44. throw Exception("Range", "unknown identifier: " + w);
  45. return res;
  46. }
  47. int getNr(Lexer& p, int altstring)
  48. {
  49. if (p.type == Lexer::integer)
  50. return p.getInt();
  51. else if (altstring != 0 && p.type == Lexer::identifier)
  52. {
  53. string w = p.getWord();
  54. int res = getNr(w, altstring == 1 ? weekDayString : monthString);
  55. return res;
  56. }
  57. else
  58. throw Exception("getNr", "int or identifier expected");
  59. }
  60. std::set<int> getValues(Lexer& p, int first, int last, const int altstring)
  61. {
  62. int mod = 1;
  63. set<int> values;
  64. if (p.token == "*")
  65. {
  66. p.nextToken();
  67. if (p.token == "/")
  68. {
  69. p.nextToken();
  70. mod = p.getInt();
  71. if (mod <= 0)
  72. throw Exception("Range", string("Modulo value invalid ") + to_string(mod));
  73. }
  74. for (int i = first; i <= last; i += mod)
  75. values.insert(i);
  76. }
  77. else
  78. {
  79. int s = getNr(p, altstring);
  80. if (s < first || s > last)
  81. throw Exception("Range", "value out of range");
  82. if (p.token != "-")
  83. values.insert(s);
  84. else
  85. {
  86. p.nextToken();
  87. // cout << p.token << endl;
  88. int e = getNr(p, altstring);
  89. if (e < s || e > last)
  90. throw Exception("Range", "end value out of range");
  91. if (p.token == "/")
  92. {
  93. p.nextToken();
  94. mod = p.getInt();
  95. }
  96. for (int i = s; i <= e; i += mod)
  97. values.insert(i);
  98. }
  99. }
  100. return values;
  101. }
  102. set<int> getValues(const string& range, int first, int last, int altstringmode) // range is string without spaces
  103. {
  104. if (range.empty())
  105. throw Exception("Range", "Description is empty");
  106. Lexer p(range);
  107. return getValues(p, first, last, altstringmode);
  108. }