💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. * Copyright (C) 2017 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code 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 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "settings.hpp"
  20. #include <fstream>
  21. #include <sstream>
  22. #include <vector>
  23. #include <iostream>
  24. bool ParameterDict::load(const std::string& filename)
  25. {
  26. // Open parameter dict file
  27. std::ifstream file(filename.c_str());
  28. if (!file.is_open())
  29. {
  30. std::cerr << std::string("Failed to open file \"") << filename << std::string("\"") << std::endl;
  31. return false;
  32. }
  33. // Read file
  34. std::string line;
  35. std::size_t lineNumber = 0;
  36. while (file.good() && std::getline(file, line))
  37. {
  38. ++lineNumber;
  39. if (!line.empty() && line[line.size() - 1] == '\r')
  40. {
  41. line = line.substr(0, line.size() - 1);
  42. }
  43. // Tokenize line (tab-delimeted)
  44. std::vector<std::string> tokens;
  45. std::string token;
  46. std::istringstream linestream(line);
  47. while (std::getline(linestream, token, '\t'))
  48. tokens.push_back(token);
  49. if (tokens.empty() || tokens[0][0] == '#')
  50. continue;
  51. if (tokens.size() != 2)
  52. {
  53. std::cerr << std::string("Invalid line \"") << lineNumber << std::string("\" in file \"") << filename << std::string("\"") << std::endl;
  54. continue;
  55. }
  56. parameters[tokens[0]] = tokens[1];
  57. }
  58. file.close();
  59. return true;
  60. }
  61. bool ParameterDict::save(const std::string& filename)
  62. {
  63. std::ofstream file(filename.c_str());
  64. if (!file.is_open())
  65. {
  66. std::cerr << std::string("Failed to open file \"") << filename << std::string("\"") << std::endl;
  67. return false;
  68. }
  69. for (auto it = parameters.begin(); it != parameters.end(); ++it)
  70. file << it->first << std::string("\t") << it->second << std::endl;
  71. file.close();
  72. return true;
  73. }
  74. void ParameterDict::clear()
  75. {
  76. parameters.clear();
  77. }