config.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "Win32.h"
  2. #include "Config.h"
  3. #include "Function.h"
  4. #include "SharedValue.h"
  5. #include <vector>
  6. //basic
  7. static std::vector<std::string> ConfigRead()
  8. {
  9. std::ifstream read("server.properties");
  10. Err(&read, _T("打开配置文件失败"));
  11. std::vector<std::string> configs;
  12. std::string config;
  13. while (getline(read, config))
  14. {
  15. configs.push_back(config);
  16. }
  17. read.close();
  18. return configs;
  19. }
  20. static void ConfigWrite(std::vector<std::string> &configText)
  21. {
  22. std::ofstream write("server.properties", std::ios::out);
  23. Err(&write, _T("写入配置文件失败"));
  24. for (const auto& in : configText)
  25. {
  26. write << in << "\n";
  27. }
  28. write.close();
  29. }
  30. //normal
  31. static void ConfigSet_EDIT(std::vector<std::string> &configText, HWND hWnd1, std::string target, int id)
  32. {
  33. char newText[1024] = { 0 };
  34. GetWindowTextA(GetDlgItem(hWnd1, id), newText, 1024);
  35. if (newText != "不修改")
  36. {
  37. for (auto& l : configText)
  38. {
  39. if (l.find(target) != std::string::npos)
  40. {
  41. l = target + '=' + newText;
  42. }
  43. return;
  44. }
  45. }
  46. }
  47. static void ConfigSet_CHECK_1(std::vector<std::string>& configText, HWND hWnd1, std::string target, int id, int no_id)
  48. {
  49. if (IsDlgButtonChecked(hWnd1, no_id) != BST_CHECKED)
  50. {
  51. for (auto& l : configText)
  52. {
  53. if (l.find(target) != std::string::npos)
  54. {
  55. if(IsDlgButtonChecked(hWnd1, id) != BST_CHECKED)
  56. l = target + "=true";
  57. else l = target + "=false";
  58. return;
  59. }
  60. }
  61. }
  62. }
  63. static void ConfigSet_CHECK_1(std::vector<std::string>& configText, HWND hWnd1, std::string target, int id, int no_id)
  64. {
  65. if (IsDlgButtonChecked(hWnd1, no_id) != BST_CHECKED)
  66. {
  67. for (auto& l : configText)
  68. {
  69. if (l.find(target) != std::string::npos)
  70. {
  71. if (IsDlgButtonChecked(hWnd1, id) != BST_CHECKED)
  72. l = target + "=false";
  73. else l = target + "=true";
  74. return;
  75. }
  76. }
  77. }
  78. }
  79. //special
  80. //whole
  81. void Submit(HWND hWnd1)
  82. {
  83. std::vector<std::string> configText = ConfigRead();
  84. ConfigSet_EDIT(configText, hWnd1,
  85. "server-name", CONF_ServerName);
  86. ConfigWrite(configText);
  87. MessageBox(hWnd,
  88. L"已尝试提交", TITLE,
  89. MB_OK);
  90. }