Guest User

Untitled

a guest
Jan 21st, 2018
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. # This is a comment
  2.  
  3. log =/var/log/secure
  4.  
  5. max_tries=5
  6.  
  7. max_reports=2
  8.  
  9. std::string buf;
  10. smatch matches;
  11. int line_number = 0; // keeping track of the line number allows for error identification
  12. std::unordered_map<std::string, std::string> parsed_options;
  13.  
  14. while (std::getline(is, buf)) {
  15. // is the line a comment or blank?
  16. if ((buf.find_first_of('#') == 0) || (std::all_of(buf.begin(), buf.end(), isspace)) || (buf.empty())) {
  17. } else {
  18. std::istringstream configs(buf);
  19. std::string option;
  20. if (std::getline(configs, option, '=')) {
  21. std::string val;
  22. if (std::getline(configs, val)) {
  23. option.erase(std::remove_if(option.begin(), option.end(), isspace), option.end());
  24. val.erase(std::remove_if(val.begin(), val.end(), isspace), val.end());
  25. parsed_options.emplace(std::make_pair(option, val));
  26. }
  27.  
  28. } else {
  29. std::stringstream se;
  30. se << "Coud not parse option at line " << line_number;
  31. throw std::runtime_error(se.str());
  32. }
  33. }
  34. ++line_number;
  35. }
  36.  
  37. for (auto i : parsed_options) {
  38. if (i.first == "log") {
  39. auth_log_ = i.second;
  40. } else if (i.first == "max_tries") {
  41. max_attempts_ = std::stoi(i.second);
  42. } else if (i.first == "max_reports") {
  43. max_reports_ = std::stoi(i.second);
  44. } else if (i.first == "interval") {
  45. interval_ == std::stoi(i.second);
  46. } else {
  47. std::cout << """ << i.first << "" " << i.second << 'n';
  48. std::stringstream ss1;
  49. ss1 << "Invalid option at line " << line_number;
  50. throw std::runtime_error(ss1.str());
  51. }
  52. }
  53.  
  54. # This is a comment
  55.  
  56. [section1]
  57. log =/var/log/secure
  58.  
  59. max_tries=5
  60.  
  61. max_reports=2
  62.  
  63. [section2]
  64. log =/var/log/secure
  65.  
  66. max_tries=5
  67.  
  68. max_reports=2
Add Comment
Please, Sign In to add comment