Advertisement
pycache

Untitled

Jul 29th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <cmath>
  2. #include <string>
  3. #include <iostream>
  4. #include <fstream>
  5. #include <windows.h>
  6. #include <vector>
  7. #include <iomanip>
  8. #include <sstream>
  9. #include <set>
  10.  
  11. double pi(int n) { return n == 0 ? 4 : 4 * pow(-1, n) / (2.0 * n + 1.0) + pi(n - 1); }
  12.  
  13. class CIniWriter {
  14. private:
  15.     void AppendNewline(std::string szSection) {
  16.         if (!m_vecUsedCategories.empty() && m_vecUsedCategories.find(szSection) == m_vecUsedCategories.end()) {
  17.             m_vecUsedCategories.emplace(szSection);
  18.             m_sFileStream.seekp(0, std::ios_base::end);
  19.             m_sFileStream << "\n";
  20.             m_sFileStream.seekp(0, std::ios_base::end);
  21.         }
  22.     }
  23.  
  24. public:
  25.     CIniWriter(std::string szFileName) {
  26.         m_szFileName = szFileName;
  27.         m_sFileStream = std::ofstream(szFileName);
  28.     }
  29.  
  30.     template<typename T> void WriteValue(std::string szSection, std::string szKey, T tValue, bool hex = false) {
  31.         AppendNewline(szSection);
  32.         std::string val;
  33.         if constexpr (std::is_same_v<T, const char*> || std::is_same_v<T, std::string>)
  34.             val = tValue;
  35.         else
  36.             val = std::to_string(tValue);
  37.         if constexpr (std::is_same_v<T, int>) {
  38.             std::stringstream vss;
  39.             vss << (hex ? std::hex : std::dec) << (hex ? "0x" : "") << tValue;
  40.             val = vss.str();
  41.         }
  42.         WritePrivateProfileString(szSection.c_str(), szKey.c_str(), val.c_str(), m_szFileName.c_str());
  43.     }
  44.  
  45. private:
  46.     std::string m_szFileName;
  47.     std::set<std::string> m_vecUsedCategories;
  48.     std::ofstream m_sFileStream;
  49. };
  50.  
  51. int main() {
  52.     auto writer = CIniWriter("C:/Users/M/Desktop/test.ini");
  53.     writer.WriteValue("Category", "Bool1", true);
  54.     writer.WriteValue("Category", "Int1", 100);
  55.     writer.WriteValue("Category", "Int2", 100, true);
  56.     writer.WriteValue("Category2", "Bool1", false);
  57.     writer.WriteValue("Category2", "Str1", "roflcopter");
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement