💿🐜 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.

79 lines
2.0 KiB

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 << "Failed to open file \"" << filename << "\"" << std::endl;
  31. return false;
  32. }
  33. // Read file
  34. std::string line;
  35. while (file.good() && std::getline(file, line))
  36. {
  37. // Tokenize line (tab-delimeted)
  38. std::vector<std::string> tokens;
  39. std::string token;
  40. std::istringstream linestream(line);
  41. while (std::getline(linestream, token, '\t'))
  42. tokens.push_back(token);
  43. if (tokens.empty() || tokens[0][0] == '#')
  44. continue;
  45. if (tokens.size() != 2)
  46. {
  47. std::cerr << "Invalid line \"" << line << "\" in file \"" << filename << "\"" << std::endl;
  48. continue;
  49. }
  50. parameters[tokens[0]] = tokens[1];
  51. }
  52. file.close();
  53. return true;
  54. }
  55. bool ParameterDict::save(const std::string& filename)
  56. {
  57. std::ofstream file(filename.c_str());
  58. if (!file.is_open())
  59. {
  60. std::cerr << "Failed to open file \"" << filename << "\"" << std::endl;
  61. return false;
  62. }
  63. for (auto it = parameters.begin(); it != parameters.end(); ++it)
  64. file << it->first << "\t" << it->second << std::endl;
  65. file.close();
  66. return true;
  67. }