]> git.stg.codes - stg.git/blobdiff - stglibs/common.lib/common.cpp
Merge branch 'stg-2.409-radius'
[stg.git] / stglibs / common.lib / common.cpp
index 1068cfa12ed085351486dd462b844cc7603a4d73..fcea5ebc23a59b2cc85ca37574b4eb681ae59320 100644 (file)
@@ -32,7 +32,8 @@
 // Like FreeBSD4
 #include <sys/types.h>
 #include <sys/time.h>
-#include <unistd.h>
+#include <pwd.h>
+#include <grp.h>
 
 #include <sys/select.h>
 
@@ -47,6 +48,8 @@
 
 #include <iconv.h>
 
+#include <algorithm>
+
 #include <cstdlib>
 #include <cstdarg>
 #include <cstdio>
@@ -512,6 +515,22 @@ uint32_t inet_strington(const std::string & value)
     return result;
 }
 //-----------------------------------------------------------------------------
+std::string TimeToString(time_t time)
+{
+struct tm brokenTime;
+
+brokenTime.tm_wday = 0;
+brokenTime.tm_yday = 0;
+brokenTime.tm_isdst = 0;
+
+gmtime_r(&time, &brokenTime);
+
+char buf[32];
+strftime(buf, 32, "%Y-%m-%d %H:%M:%S", &brokenTime);
+
+return buf;
+}
+//-----------------------------------------------------------------------------
 int ParseTariffTimeStr(const char * str, int &h1, int &m1, int &h2, int &m2)
 {
 char hs1[10], ms1[10], hs2[10], ms2[10];
@@ -731,6 +750,38 @@ void SwapBytes(int64_t & value)
     value = temp;
 }
 //---------------------------------------------------------------------------
+std::string formatTime(time_t ts)
+{
+char buf[32];
+struct tm brokenTime;
+
+brokenTime.tm_wday = 0;
+brokenTime.tm_yday = 0;
+brokenTime.tm_isdst = 0;
+
+gmtime_r(&ts, &brokenTime);
+
+strftime(buf, 32, "%Y-%m-%d %H:%M:%S", &brokenTime);
+
+return buf;
+}
+//---------------------------------------------------------------------------
+time_t readTime(const std::string & ts)
+{
+if (ts == "0000-00-00 00:00:00")
+    return 0;
+
+struct tm brokenTime;
+
+brokenTime.tm_wday = 0;
+brokenTime.tm_yday = 0;
+brokenTime.tm_isdst = 0;
+
+stg_strptime(ts.c_str(), "%Y-%m-%d %H:%M:%S", &brokenTime);
+
+return stg_timegm(&brokenTime);
+}
+//---------------------------------------------------------------------------
 int str2x(const std::string & str, int32_t & x)
 {
 x = static_cast<int32_t>(strtol(str.c_str(), NULL, 10));
@@ -820,6 +871,24 @@ std::string & Trim(std::string & val)
 return TrimR(TrimL(val));
 }
 //---------------------------------------------------------------------------
+std::string Trim(const std::string & val)
+{
+std::string res(val);
+return TrimR(TrimL(res));
+}
+//---------------------------------------------------------------------------
+std::string ToLower(std::string value)
+{
+    std::transform(value.begin(), value.end(), value.begin(), ::tolower);
+    return value;
+}
+//---------------------------------------------------------------------------
+std::string ToUpper(std::string value)
+{
+    std::transform(value.begin(), value.end(), value.begin(), ::toupper);
+    return value;
+}
+//---------------------------------------------------------------------------
 #ifdef WIN32
 static int is_leap(unsigned y)
 {
@@ -885,7 +954,7 @@ strncpy(inBuf, source.c_str(), source.length());
 
 inBuf[source.length()] = 0;
 
-#if defined(FREE_BSD) || defined(FREE_BSD5) || defined(WIN32)
+#if defined(CONST_ICONV)
 const char * srcPos = inBuf;
 #else
 char * srcPos = inBuf;
@@ -1060,3 +1129,37 @@ std::string ToPrintable(const std::string & src)
 
     return dest;
 }
+
+uid_t str2uid(const std::string& name)
+{
+    const passwd* res = getpwnam(name.c_str());
+    if (res == NULL)
+        return -1;
+    return res->pw_uid;
+}
+
+gid_t str2gid(const std::string& name)
+{
+    const group* res = getgrnam(name.c_str());
+    if (res == NULL)
+        return -1;
+    return res->gr_gid;
+}
+
+mode_t str2mode(const std::string& name)
+{
+    if (name.length() < 3 || name.length() > 4)
+        return -1;
+
+    if (name.length() == 4 && name[0] != '0')
+        return -1;
+
+    mode_t res = 0;
+    for (size_t i = 0; i < name.length(); ++i)
+    {
+        if (name[i] > '7' || name[i] < '0')
+            return -1;
+        res = (res << 3) + (name[i] - '0');
+    }
+    return res;
+}