Rind пре 3 година
родитељ
комит
6525ef8e06
2 измењених фајлова са 34 додато и 0 уклоњено
  1. 31 0
      common/strutil.cpp
  2. 3 0
      common/strutil.h

+ 31 - 0
common/strutil.cpp

@@ -1,4 +1,5 @@
 #include <algorithm>
+#include <fstream>
 #include <stdarg.h>
 #include "strutil.h"
 
@@ -173,3 +174,33 @@ std::string strFormatByteSize(uint64_t s, unsigned int dec)
 
 	return szBuf;
 }
+
+//////////////////////////////////////////////////////////////////////////////////
+// readFile
+
+bool readFile(const char *pszFilePath, std::string &str)
+{
+	std::ifstream stm(pszFilePath);
+	bool bGood = stm.good();
+
+	if(bGood)
+	{
+		stm.seekg(0, std::ios::end);
+		str.reserve(stm.tellg());
+		stm.seekg(0, std::ios::beg);
+		str.assign((std::istreambuf_iterator<char>(stm)), std::istreambuf_iterator<char>());
+	}
+
+	return bGood;
+}
+
+//////////////////////////////////////////////////////////////////////////////////
+// writeFile
+
+bool writeFile(const char *pszFilePath, const std::string &str)
+{
+	std::ofstream stm(pszFilePath);
+	stm << str;
+	stm.close();
+	return true;
+}

+ 3 - 0
common/strutil.h

@@ -9,6 +9,7 @@
 #include <string>
 #include <vector>
 #include <regex>
+#include <functional>
 
 /////////////////////////////////////////////////////////////////////////////
 // strutil.h - Declarations:
@@ -30,6 +31,8 @@ std::string trim(const std::string &s, const char *sep);
 int strsplit(const std::string &str, const char *sep, std::vector<std::string> &vec);
 std::string strFormatByteSize(uint64_t s, unsigned int dec = 1);
 std::string formatString(const char *fmt, ...);
+bool readFile(const char *pszFilePath, std::string &str);
+bool writeFile(const char *pszFilePath, const std::string &str);
 
 /////////////////////////////////////////////////////////////////////////////
 #endif	//	__cplusplus