ReadConfig.cpp 960 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <cwctype>
  6. void trim(std::wstring& str) {
  7. str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](wchar_t ch) {
  8. return !std::iswspace(ch);
  9. }));
  10. str.erase(std::find_if(str.rbegin(), str.rend(), [](wchar_t ch) {
  11. return !std::iswspace(ch);
  12. }).base(), str.end());
  13. }
  14. std::wstring ReadConfig(const std::wstring& filename, const std::wstring& key)
  15. {
  16. std::wstring configValue;
  17. std::wstring line;
  18. std::wifstream file(filename);
  19. while (std::getline(file, line)) {
  20. trim(line);
  21. if (line.find(key) == 0) {
  22. std::size_t position = line.find(L"=", key.size());
  23. if (position != std::string::npos) {
  24. configValue = line.substr(position + 1);
  25. trim(configValue);
  26. break;
  27. }
  28. }
  29. }
  30. file.close();
  31. return configValue;
  32. }