logplus/Workflow/WFCrystal/SysUtility/utils/src/Utils.cpp
2026-01-16 17:18:41 +08:00

1032 lines
26 KiB
C++
Raw Blame History

#ifdef LLToString
#undef LLToString
#endif
#include <QCoreApplication>
#include <QDir>
#include "Utils.h"
// #include "error.h"
#include <limits.h>
#include <string>
using std::string;
#include <vector>
using std::vector;
#include <iostream>
#include <sstream>
using std::cout;
using std::cerr;
using std::endl;
// #include <dirent.h>
#ifdef LLToString
#undef LLToString
#endif
#ifdef WIN32
#include <Winsock.h>
// #pragma comment(lib, "WS2_32.lib");
#else
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <net/if.h>
#include <sys/ioctl.h>
#endif
using namespace pai::turtle;
using namespace pai::utils;
CUtils::CUtils()
{
}
CUtils::~CUtils()
{
}
void CUtils::SetHadoopClassPath()
{
char *szPaiHome = (char*)GetPaiHome().c_str();//getenv("PAI_HOME");
if (szPaiHome == NULL)
{
// throw pai::error::environment_error("PAI_HOME environment variable is not set.");
}
string paiHome(szPaiHome);
string paths[] =
{ "/java/common/","/hadoop/", "/hadoop/lib/" };
string classpath = "";
for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); ++i)
{
AppendClassPath(paiHome + paths[i], classpath);
}
classpath.erase(classpath.size() - 1);
//if (setenv("CLASSPATH", classpath.c_str(), 1) == -1)
std::string strPathName = "CLASSPATH";
if (GetTurtleAPI()->SetEnv(strPathName, classpath) == -1)
{
// throw pai::error::environment_error("Cannot set CLASSPATH environment variable.");
}
}
void CUtils::AppendClassPath(const std::string& path, string & classPath)
{
std::vector < std::string > files;
files.clear();
std::string filter = ".jar";
int ret = ListFiles(path, filter, files);
if (ret == 0)
{
// throw pai::error::filesystem_error("Cannot find any hadoop jar files. Please"
// "copy them to $PAI_HOME.");
}
std::vector<std::string>::iterator iter = files.begin();
while (iter != files.end())
{
classPath += path;
classPath += (*iter);
classPath += ":";
iter++;
}
}
// std::string CUtils::GetLocalIp()
// {
// #ifdef _WINDOWS
// WORD wVersion = MAKEWORD(2,2);
// WSAData wsaData;
// if (WSAStartup(wVersion, &wsaData) != 0)
// {
// throw std::exception ("Get local ip failed. Please make sure you have permission.");
// }
// char local[255] = {0};
// gethostname (local, sizeof(local));
// hostent * ph = gethostbyname(local);
// if (ph == NULL)
// {
// return "";
// };
// in_addr addr;
// memcpy (&addr, ph->h_addr_list[0], sizeof(in_addr));
// std::string ip (inet_ntoa (addr));
// WSACleanup ();
// return ip;
// #else
// int sock_get_ip;
// char ipaddr[50];
// struct sockaddr_in * sin;
// struct ifreq ifr_ip;
// if((sock_get_ip = socket(AF_INET,SOCK_STREAM,0)) == -1)
// {
// std::cout<<"socket create failed.."<<std::endl;
// return "";
// }
// memset(&ifr_ip,0,sizeof(ifr_ip));
// strncpy(ifr_ip.ifr_name, "eth0", sizeof(ifr_ip.ifr_name) - 1);
// if(ioctl(sock_get_ip,SIOCGIFADDR,&ifr_ip) < 0)
// {
// return "";
// }
// sin = (struct sockaddr_in *)&ifr_ip.ifr_addr;
// strcpy(ipaddr,inet_ntoa(sin->sin_addr));
// std::cout<<"local ip: "<< ipaddr <<std::endl;
// close(sock_get_ip);
// return ipaddr;
// #endif
// }
bool CUtils::ProcessExist(pid_t pID)
{
#ifdef WIN32
HANDLE ret;
ret = OpenProcess(PROCESS_ALL_ACCESS,false,pID);
#else
int ret = kill(pID,0);
#endif
if(ret == 0)
return true;
return false;
}
int CUtils::ListFiles(const std::string& path, const std::string& filter, std::vector<std::string>& file)
{
vector<string> filelist;
//struct dirent **namelist;
char const *b = path.c_str();
string listFsPath = "";
int total = 0;
int findResult = -1;
//int n = scandir(b, &namelist, 0, alphasort);
GetTurtleAPI()->ScanDir(path,filelist);
size_t n = filelist.size();
if (n < 0)
{
// throw pai::error::filesystem_error("Error scanning directory '" + std::string(b) + "'.");
}
else
{
while (--n)
{
listFsPath = filelist[n];
findResult = static_cast<int>(listFsPath.find(filter, 0));
if (findResult != -1)
{
file.push_back(listFsPath);
total++;
}
//free(filelist[n]);
}
//free(filelist);
}
return total;
}
#ifdef _IN_C
/**
* convert between 32 bit IBM and IEEE floating numbers
*
* @author Aaron (2010/10/28)
*
* @param from input vector
* @param to output vector, can be same as input vector
* @param n
* @param endian byte order =0 little endian (DEC, PC's),
* =1 other systems
*
*
* Notes:
* Up to 3 bits lost on IEEE -> IBM.
* Assumes sizeof(int) == 4
* IBM -> IEEE may overflow or underflow, taken care of by
* substituting large number or zero.
* Only integer shifting and masking are used.
*/
void ibm_to_float(int from[], int to[], int n, int endian)
{
register int fconv, fmant, i, t;
for (i = 0;i < n; ++i)
{
fconv = from[i];
/* if little endian, i.e. endian=0 do this */
if (endian == 0) fconv = (fconv << 24) | ((fconv >> 24) & 0xff) |
((fconv & 0xff00) << 8) | ((fconv & 0xff0000) >> 8);
if (fconv)
{
fmant = 0x00ffffff & fconv;
if (fmant == 0)
warn("mantissa is zero data may not be in IBM FLOAT Format !");
t = (int) ((0x7f000000 & fconv) >> 22) - 130;
while (!(fmant & 0x00800000))
{
--t; fmant <<= 1;
}
if (t > 254) fconv = (0x80000000 & fconv) | 0x7f7fffff;
else if (t <= 0) fconv = 0;
else fconv = (0x80000000 & fconv) | (t << 23)
| (0x007fffff & fmant);
}
to[i] = fconv;
}
return;
}
#endif
static int tt[256] =
{ 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 91, 46, 60, 40, 43, 33, 38, 32, 32, 32, 32,
32, 32, 32, 32, 32, 93, 36, 42, 41, 59, 94, 45, 47, 32, 32, 32, 32, 32, 32, 32, 32, 124, 44, 37, 95, 62, 63,
32, 32, 32, 32, 32, 32, 238, 160, 161, 96, 58, 35, 64, 39, 61, 34, 230, 97, 98, 99, 100, 101, 102, 103, 104,
105, 164, 165, 228, 163, 229, 168, 169, 106, 107, 108, 109, 110, 111, 112, 113, 114, 170, 171, 172, 173, 174,
175, 239, 126, 115, 116, 117, 118, 119, 120, 121, 122, 224, 225, 226, 227, 166, 162, 236, 235, 167, 232, 237,
233, 231, 234, 158, 128, 129, 150, 132, 133, 148, 131, 123, 65, 66, 67, 68, 69, 70, 71, 72, 73, 149, 136, 137,
138, 139, 140, 125, 74, 75, 76, 77, 78, 79, 80, 81, 82, 141, 142, 143, 159, 144, 145, 92, 32, 83, 84, 85, 86,
87, 88, 89, 90, 146, 147, 134, 130, 156, 155, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 135, 152, 157, 153, 151,
32 };
unsigned char CUtils::ebasc(unsigned char ascii)
{
unsigned char ebcd;
ebcd = static_cast<unsigned char>(tt[ascii]);
return ebcd;
}
void CUtils::ebasd(unsigned char* ascii, unsigned char* ebcd)
{
int n;
for (n = 0; ebcd[n]; n++)
{
ascii[n] = ebasc(ebcd[n]);
}
ascii[n] = 0;
}
char CUtils::asebc(unsigned char ascii)
{
char ebcd;
static int tts[256] =
{ 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* */
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* */
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* */
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* */
0x40, 0x4F, 0x7F, 0x7B, 0x5B, 0x6C, 0x50, 0x7D, /* !"#$%&' */
0x4D, 0x5D, 0x5C, 0x4E, 0x6B, 0x60, 0x4B, 0x61, /* ()*+,-./ */
0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, /* 01234567 */
0xF8, 0xF9, 0x7A, 0x5E, 0x4C, 0x7E, 0x6E, 0x6F, /* 89:;<=>? */
0x7C, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, /* @ABCDEFG */
0xC8, 0xC9, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, /* HIJKLMNO */
0xD7, 0xD8, 0xD9, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, /* PQRSTUVW */
0xE7, 0xE8, 0xE9, 0x4A, 0xE0, 0x5A, 0x5F, 0x6D, /* XYZ[\]^_ */
0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, /* `abcdefg */
0x88, 0x89, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, /* hijklmno */
0x97, 0x98, 0x99, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, /* pqrstuvw */
0xA7, 0xA8, 0xA9, 0xC0, 0x6A, 0xD0, 0xA1, 0x40, /* xyz{|}~ */
0xB9, 0xBA, 0xED, 0xBF, 0xBC, 0xBD, 0xEC, 0xFA, /* */
0xCB, 0xCC, 0xCD, 0xCE, 0xCF, 0xDA, 0xDB, 0xDC, /* */
0xDE, 0xDF, 0xEA, 0xEB, 0xBE, 0xCA, 0xBB, 0xFE, /* */
0xFB, 0xFD, 0x7d, 0xEF, 0xEE, 0xFC, 0xB8, 0xDD, /* */
0x77, 0x78, 0xAF, 0x8D, 0x8A, 0x8B, 0xAE, 0xB2, /* */
0x8F, 0x90, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F, /* */
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* */
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* */
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* */
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* */
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* */
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* */
0xAA, 0xAB, 0xAC, 0xAD, 0x8C, 0x8E, 0x80, 0xB6, /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
0xB3, 0xB5, 0xB7, 0xB1, 0xB0, 0xB4, 0x76, 0xA0, /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, /* */
0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* */
};
ebcd = static_cast<char>(tts[ascii]);
return ebcd;
}
void CUtils::asebd(char* ebcd, char* ascii)
{
int n;
for (n = 0; ascii[n]; n++)
{
ebcd[n] = asebc(static_cast<unsigned char>(ascii[n]));
}
ebcd[n] = 0;
}
void CUtils::SwapByte(unsigned char *buf, int len, int flag)
{
unsigned char c;
if (flag == 2)
{
len = len / 2;
for (int i = 0; i < len; i++)
{
c = buf[2 * i];
buf[2 * i] = buf[2 * i + 1];
buf[2 * i + 1] = c;
}
}
else if (flag == 4)
{
len = len / 4;
for (int i = 0; i < len; i++)
{
c = buf[4 * i];
buf[4 * i] = buf[4 * i + 3];
buf[4 * i + 3] = c;
c = buf[4 * i + 1];
buf[4 * i + 1] = buf[4 * i + 2];
buf[4 * i + 2] = c;
}
}
}
void CUtils::SwapBhdr(unsigned char *buf)
{
SwapByte(buf + 0, 12, 4);
SwapByte(buf + 12, 48, 2);
SwapByte(buf + 60, 340, 4);
}
void CUtils::SwapTraceHead(unsigned char *buf)
{
SwapByte(buf + 0, 28, 4);
SwapByte(buf + 28, 8, 2);
SwapByte(buf + 36, 32, 4);
SwapByte(buf + 68, 4, 2);
SwapByte(buf + 72, 16, 4);
SwapByte(buf + 88, 32, 2);
SwapByte(buf + 120, 120, 4);
}
void CUtils::SwapTxtHead(unsigned char *buf)
{
SwapByte(buf + 0, 12, 4);
SwapByte(buf + 12, 48, 2);
SwapByte(buf + 60, 340, 4);
}
void CUtils::ChrToIEEE(unsigned char *src, float *dst, int len)
{
for (int i = 0; i < len; i++)
dst[i] = static_cast<float>(src[i]);
}
void CUtils::SpiToIEEE(short *src, float *dst, int len)
{
for (int i = 0; i < len; i++)
dst[i] = static_cast<float>(src[i]);
}
void CUtils::BgnToIEEE(short *src, float *dst, int len)
{
int i, in4;
short int in2, in3, nmut;
for (i = 0; i < len; i++)
{
in2 = 15 - (src[i] & 0x000f);
in3 = static_cast<short int>(src[i] & 0xfff0);
in3 /= 16;
nmut = static_cast<short int>(1 << in2);
in4 = (int(in3)) * (int(nmut));
dst[i] = static_cast<float>(in4);
if (dst[i] < 0)
dst[i] = dst[i] - 1;
}
}
void CUtils::IntToIEEE(int *src, float *dst, int len)
{
for (int i = 0; i < len; i++)
dst[i] = static_cast<float>(src[i]);
}
void CUtils::IEEEToChr(float *src, char *dst, int len)
{
for (int i = 0; i < len; i++)
dst[i] = static_cast<char>(src[i]);
}
void CUtils::IEEEToSpi(float *src, short *dst, int len, short &scalar)
{
float maxNum = FLT_MIN;
for (int i = 0; i < len; i++)
{
if(maxNum < fabs(src[i]))
maxNum = fabs(src[i]);
}
if(SHRT_MAX < short(maxNum))
scalar = log(maxNum/SHRT_MAX)/log(2.0);
else
scalar = 0;
for (int i = 0; i < len; i++)
dst[i] = static_cast<short>(src[i]*pow(2.0, -(scalar)));
}
void CUtils::IEEEToInt(float *src, int *dst, int len)
{
for (int i = 0; i < len; i++)
dst[i] = static_cast<int>(src[i]);
}
void CUtils::IEEEToBgn(float *src, short *dst, int len)
{
int i, in4, sign;
short in2, nmut;
for (i = 0; i < len; i++)
{
nmut = 0;
in4 = static_cast<int>(src[i]);
if (in4 > 0)
while (in4 >= 2047)
{
nmut++;
in4 /= 2;
}
else
while (in4 <= -2048)
{
nmut++;
in4 /= 2;
}
nmut = short(15 - nmut);
sign = static_cast<int>(static_cast<unsigned int>(in4) & 0x80000000);
sign >>= 16;
in4 <<= 4;
in4 &= 0xfff0;
in2 = short(in4) | nmut;
in2 |= short(sign);
dst[i] = in2;
}
}
void CUtils::IbmIeee(float *src, float *dst, int n, int idirec)
{
int esrc, esun, mhsrc, emod;
int jj;
int m_m1[512][2];
float m_r1[512][2];
bool m_bfirst = true;
union
{
int l;
float f;
} temp;
int i;
if (m_bfirst)
{
m_bfirst = 0;
temp.l = -1;
for (i = 0; i < 512; i++)
{
esrc = (i & 255) >> 1;
mhsrc = i & 1;
esun = 4 * esrc - 130;
if (esun > 0 && esun <= 255)
{
m_m1[i][0] = (esun ^ (i & 255)) << 23;
if (mhsrc == 1)
{
m_r1[i][0] = 0.0;
}
else
{
temp.l = (esun | (i & 256)) << 23;
m_r1[i][0] = -temp.f;
}
}
else if (esun <= 0)
{
m_m1[i][0] = i << 23;
m_r1[i][0] = 0.0;
}
else
{
m_m1[i][0] = i << 23;
if (i < 256)
m_r1[i][0] = static_cast<float>(3.40282346638528860e+38);
else
m_r1[i][0] = static_cast<float>(-3.40282346638528860e+38);
}
if (i == 0)
{
m_m1[i][1] = 0;
m_r1[i][1] = 0.0;
}
else
{
esun = i & 255;
esrc = (esun + 133) >> 2;
emod = 4 * esrc - esun - 130;
if (emod == 0)
{
m_m1[i][1] = (esun | (2 * esrc + 1)) << 23;
m_r1[i][1] = 0.0;
}
else
{
m_m1[i][1] = (esun ^ (2 * esrc - emod)) << 23;
temp.l = ((i & 256) + 2 * esrc) << 23;
m_r1[i][1] = temp.f;
}
}
}
}
for (i = 0; i < n; i++)
{
temp.f = src[i];
jj = (temp.l >> 23) & 511;
temp.l = m_m1[jj][idirec] ^ temp.l;
dst[i] = temp.f + m_r1[jj][idirec];
}
}
void CUtils::IeeeIbm(char *from, char *to, int len, int type)
{
unsigned fconv, fsign, fexpn;
register unsigned fmant;
register int t;
register char *bp = reinterpret_cast<char*>(&fconv);
if (len <= 0)
return;
switch (type)
{
case 1: /* IEEE to IBM */
while (len--)
{
/* Load. Swap bytes if necessary for Vax */
bp[0] = from[0];
bp[1] = from[1];
bp[2] = from[2];
bp[3] = from[3];
from += 4;
fsign = unsigned(0x80000000) & fconv;
fexpn = unsigned(0x7f800000) & fconv;
fmant = (unsigned(0x007fffff) & fconv) | (unsigned(0x00800000));
if (!fexpn)
fconv = 0;
else
{
t = int(fexpn >> 23) - 126;
while (t & 0x3)
{
++t;
fmant >>= 1;
}
fexpn = unsigned((t >> 2) + 64) << 24;
fconv = fsign | fexpn | fmant;
}
*to++ = bp[0];
*to++ = bp[1];
*to++ = bp[2];
*to++ = bp[3];
}
break;
case 0: /* IBM to IEEE */
while (len--)
{
/* Load. Swap bytes if necessary */
bp[0] = from[0];
bp[1] = from[1];
bp[2] = from[2];
bp[3] = from[3];
from += 4;
fsign = unsigned(0x80000000) & fconv;
fexpn = unsigned(0x7f000000) & fconv;
fmant = unsigned(0x00ffffff) & fconv;
if (!fmant)
fconv = 0;
else
{
t = int(fexpn >> 22) - 256;
while (!(fmant & 0x00800000))
{
--t;
fmant <<= 1;
}
/* Check for valid IEEE exponent */
if (t > 127)
fconv = fsign | 0x7ff80000;
else if (t < -128)
fconv = 0;
else
{
fexpn = (unsigned(t + 126)) << 23;
fconv = fsign | fexpn | (0x007fffff & fmant);
}
}
*to++ = bp[0];
*to++ = bp[1];
*to++ = bp[2];
*to++ = bp[3];
}
break;
default:
break;
}
return;
}
void CUtils::IbmToIEEE(int *src, float *dst, int n)
{
IbmIeee(reinterpret_cast<float*>(src), dst, n, IBM_TO_IEEE);
}
void CUtils::IEEEToIbm(float *src, int *dst, int n)
{
IeeeIbm(reinterpret_cast<char*>(src), reinterpret_cast<char*>(dst), n, IEEE_TO_IBM);
}
std::string CUtils::ConcatWithChar(std::vector<long>& source, char sep)
{
std::string result;
std::vector<long>::iterator iter = source.begin();
while (iter != source.end())
{
result += CUtils::LLToString(*iter);
result += sep;
iter++;
}
return result;
}
std::string CUtils::ConcatWithChar(std::set<long>& source, char sep)
{
std::string result;
std::set<long>::iterator iter = source.begin();
while (iter != source.end())
{
result += CUtils::LLToString(*iter);
result += sep;
iter++;
}
return result;
}
std::string CUtils::ConcatWithChar(std::map<long, long>& source, char sep1, char sep2)
{
std::string result;
std::map<long, long>::iterator iter = source.begin();
while (iter != source.end())
{
result += CUtils::LLToString(iter->first);
result += sep1;
result += CUtils::LLToString(iter->second);
result += sep2;
iter++;
}
return result;
}
std::string CUtils::ConcatWithChar(std::map<int, std::string>& source, char sep1, char sep2)
{
std::string result;
std::map<int, std::string>::iterator iter = source.begin();
while (iter != source.end())
{
result += CUtils::IntToStr(iter->first);
result += sep1;
result += iter->second;
result += sep2;
iter++;
}
return result;
}
std::string CUtils::ConcatWithChar(std::map<long long, std::string>& source, char sep1, char sep2)
{
std::string result;
std::map<long long, std::string>::iterator iter = source.begin();
while (iter != source.end())
{
result += CUtils::LLToString(iter->first);
result += sep1;
result += iter->second;
result += sep2;
iter++;
}
return result;
}
std::string CUtils::DigitSizeToString(long long size)
{
std::stringstream stream;
stream.precision(1);
stream.setf(std::ios::fixed, std::ios::floatfield);
double dT = double(size) / (1024 * 1024) / (1024 * 1024);
if (dT >= 1.0) // 大于1T
{
stream << dT << " TB";
}
else
{
double d1G = 1024 * 1024 * 1024;
double dG = double(size) / d1G;
if (dG >= 1.0) // 大于1G
{
stream << dG << " GB";
}
else
{
double d1M = 1024 * 1024;
double dM = double(size) / d1M;
if (dM >= 1.0) // 大于1M
{
stream << dM << " MB";
}
else
{
double d1K = 1024;
double dK = double(size) / d1K;
if (dK >= 1.0) // 大于1K
{
stream << dK << " KB";
}
else // 小于1K
{
stream << size << " B";
}
}
}
}
return stream.str();
}
std::string CUtils::TimeToString(int64 seconds)
{
if (seconds < 0)
{
// throw pai::error::invalid_argument("Time cannot be negative.");
}
std::stringstream stream;
int64 secs = seconds;
if (secs > 0)
{
int64 tv = secs / 86400; // 1天 == 86400秒
if (tv != 0)
{
stream << tv << "d ";
secs %= 86400;
}
tv = secs / 3600; // 1小时 == 3600秒
if (tv != 0)
{
stream << tv << "h ";
secs %= 3600;
}
tv = secs / 60; // 1分钟 == 60秒
if (tv != 0)
{
stream << tv << "m ";
secs %= 60;
}
if(secs != 0)
{
stream << secs << "s";
}
}
else if (secs == 0)
{
stream << secs << "s";
}
return stream.str();
}
int64 CUtils::TimeStringToSeconds(const std::string &timeStr)
{
int64 seconds = 0;
// remove all the spaces
std::string time;
for (size_t i = 0; i < timeStr.size(); i++)
{
if (timeStr[i] != ' ')
{
time.push_back(timeStr[i]);
}
}
if (!time.empty())
{
// day
size_t pos = time.find_first_of("d");
if (pos != std::string::npos)
{
std::string str = time.substr(0, pos);
seconds += atoi(str.c_str()) * 86400;
if (pos+1 < time.size())
{
time = time.substr(pos+1);
}
}
// hour
pos = time.find_first_of("h");
if (pos != std::string::npos)
{
std::string str = time.substr(0, pos);
seconds += atoi(str.c_str()) * 3600;
if (pos+1 < time.size())
{
time = time.substr(pos+1);
}
}
// minute
pos = time.find_first_of("m");
if (pos != std::string::npos)
{
std::string str = time.substr(0, pos);
seconds += atoi(str.c_str()) * 60;
if (pos+1 < time.size())
{
time = time.substr(pos+1);
}
}
// second
pos = time.find_first_of("s");
if (pos != std::string::npos)
{
std::string str = time.substr(0, pos);
seconds += atoi(str.c_str());
if (pos+1 < time.size())
{
time = time.substr(pos+1);
}
}
}
return seconds;
}
int64 CUtils::getSystemTime(){
return static_cast<int64>(GetTurtleAPI()->GetSystemTime());
}
bool CUtils::getNanoTime(long long &time)
{
//TODO 跨平台
/*
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return int64(ts.tv_sec) * 1000000000LL + int64(ts.tv_nsec);
*/
return GetTurtleAPI()->GetNSTime(time);
}
std::string CUtils::getCurrentDate()
{
//TODO 跨平台
//timeval curTime;
//gettimeofday(&curTime, NULL);
int milli = int(GetTurtleAPI()->GetSystemTime()) / 1000;
time_t rawtime;
struct tm * timeinfo;
char buffer [80];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", timeinfo);
char currentTime[84] = "";
sprintf(currentTime, "%s:%d", buffer, milli);
return std::string(currentTime);
}
std::string CUtils::Replace_all(const std::string & str, const char old_value, const char new_value)
{
std::stringstream result;
for (size_t i = 0; i < str.size(); i++ )
{
if (str.at(i) == old_value)
{
result << new_value;
}
else
result << str.at(i);
}
return result.str();
}
std::string CUtils::Trim(std::string str)
{
//去掉前面的空格
size_t pos_value_front_space = str.find_first_not_of(" \t");
if (pos_value_front_space != std::string::npos)
{
str.erase(0, pos_value_front_space);
}
//去掉后面的空格
size_t pos_value_end_space = str.find_last_not_of(" \t");
if (pos_value_end_space != std::string::npos)
{
str.erase(pos_value_end_space + 1);
}
return str;
}
std::string CUtils::GetPaiHome()
{
wchar_t process_name[MAX_PATH] = {};
wchar_t process_name1[MAX_PATH] = {};
if(!GetModuleFileName(NULL,process_name,MAX_PATH))
{
return "";
}
std::wstring wstr=process_name;
std::wstring strReplace=L"\\";
std::wstring strDest=L"/";
while(wstr.find(strReplace)!=std::wstring::npos)
{
size_t pos = wstr.find(strReplace);
if (pos != std::wstring::npos)
{
wchar_t pBuf[1]={L'\0'};
wstr.replace(pos, strReplace.length(), pBuf, 0);
wstr.insert(pos, strDest);
}
}
int pos=wstr.find_last_of(L"/");
if(pos>-1) wstr[pos]=0;
int nLen = WideCharToMultiByte(CP_ACP, 0, process_name, (int)(wstr.size()), NULL, 0, NULL, NULL);
CHAR *pBuffer = new CHAR[nLen + 1];
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), (int)(wstr.size()), pBuffer, nLen, NULL, NULL);
pBuffer[nLen] = '\0';
std::string str;
str.append(pBuffer);
delete[] pBuffer;
return str;
}
std::string GetDBPath()
{
std::string tep;
tep=pai::utils::CUtils::GetPaiHome();
tep+="\\sqlitedb\\well.db";
return tep;
}
File_ptr::File_ptr(const char * n, const char * a):p(NULL)
{
p = std::fopen(n, a);
if (!p)
{
// throw pai::error::filesystem_error("Cannot open file '" +
// std::string(n) + "' with mode '" + std::string(a) + "'.");
}
}
File_ptr::File_ptr(FILE * pp):p(pp) { p = pp; }
File_ptr::~File_ptr() { if (p) { std::fclose(p); p = 0; } }
File_ptr::operator FILE*() { return p; }