]> git.stg.codes - stg.git/blobdiff - stglibs/common.lib/include/stg/common.h
Added special function to read/write all data (for TCP).
[stg.git] / stglibs / common.lib / include / stg / common.h
index 0a4ef9495939d0fff36a31f2e20f504bc20fbe67..0791f0938e9e89c327ba255f07f2b1f97f9a358d 100644 (file)
@@ -33,6 +33,7 @@
 #include <ctime>
 #endif
 #include <string>
+#include <sstream>
 
 #include "stg/os_int.h"
 #include "stg/const.h"
@@ -96,7 +97,38 @@ std::string &   TrimL(std::string & val);
 std::string &   TrimR(std::string & val);
 std::string &   Trim(std::string & val);
 
-std::string     IconvString(const std::string & source, const std::string & from, const std::string & to);
+template <typename C, typename F>
+C Split(const std::string & value, char delim, F conv)
+{
+C res;
+size_t startPos = 0;
+size_t pos = value.find_first_of(delim);
+while (pos != std::string::npos)
+    {
+    res.push_back(conv(value.substr(startPos, pos - startPos)));
+    startPos = pos + 1;
+    pos = value.find_first_of(delim, pos + 1);
+    }
+res.push_back(conv(value.substr(startPos, pos - startPos)));
+return res;
+}
+
+template <typename T>
+T FromString(const std::string & value)
+{
+T res;
+std::istringstream stream(value);
+stream >> res;
+return res;
+}
+
+template <typename C>
+C Split(const std::string & value, char delim)
+{
+    return Split<C>(value, delim, FromString);
+}
+
+std::string IconvString(const std::string & source, const std::string & from, const std::string & to);
 
 int ParseInt(const std::string & str, int * val);
 int ParseUnsigned(const std::string & str, unsigned * val);
@@ -107,6 +139,9 @@ int ParseYesNo(const std::string & str, bool * val);
 
 bool WaitPackets(int sd);
 
+bool ReadAll(int sd, void * dest, size_t size);
+bool WriteAll(int sd, const void * source, size_t size);
+
 //-----------------------------------------------------------------------------
 int str2x(const std::string & str, int32_t & x);
 int str2x(const std::string & str, uint32_t & x);