You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
6.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "util.h"
void split(std::string s, const char* delim, std::vector<std::string>* ret)
{
size_t last = 0;
size_t index = s.find(delim, last);
int size=strlen(delim);
while (index != std::string::npos) {
ret->push_back(s.substr(last, index - last));
last = index + size;
index = s.find(delim, last);
}
if (index - last > 0) {
ret->push_back(s.substr(last, index - last));
}
}
bool isTypeOf(tag_t objtag, const char * type_name) {
tag_t type = NULLTAG;
TCTYPE_ask_object_type(objtag, &type);
tag_t item_type = NULLTAG;
TCTYPE_find_type(type_name, "", &item_type);
bool is_type = false;
if (item_type != NULLTAG) {
logical isok = FALSE;
TCTYPE_is_type_of(type, item_type, &isok);
if (isok) {
is_type = true;
}
}
return is_type;
}
void vecToArray(vector<string> &vec,char ***arr)
{
(*arr)=(char **)MEM_alloc(vec.size()*sizeof(char *));
for(auto i=0;i<vec.size();i++)
{
(*arr)[i]=(char *)MEM_alloc((vec[i].size()+1)*sizeof(char));
strcpy((*arr)[i],vec[i].c_str());
}
}
const char* newGUID()
{
static char buf[64] = { 0 };
GUID guid;
if (S_OK == ::CoCreateGuid(&guid))
{
_snprintf(buf, sizeof(buf)
, "%08X-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X"
, guid.Data1
, guid.Data2
, guid.Data3
, guid.Data4[0], guid.Data4[1]
, guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5]
, guid.Data4[6], guid.Data4[7]
);
}
return (const char*)buf;
}
string getTime2()
{
stringstream ss;
time_t t = time(0);
tm* local = localtime(&t);
ss << local->tm_year + 1900 << '-';
if (local->tm_mon < 9)
ss << "0";
ss << local->tm_mon + 1 << '-';
if (local->tm_mday < 9)
ss << "0";
ss << local->tm_mday << ' ';
if (local->tm_hour < 10)
ss << "0";
if (local->tm_min < 10)
ss << "0";
ss << local->tm_min;
if(local->tm_sec<10)
ss<<"0";
ss<<local->tm_sec;
return ss.str();
}
string getTime()
{
stringstream ss;
time_t t = time(0);
tm* local = localtime(&t);
ss << local->tm_year + 1900 << '-';
if (local->tm_mon < 9)
ss << "0";
ss << local->tm_mon + 1 << '-';
if (local->tm_mday < 9)
ss << "0";
ss << local->tm_mday << ' ';
if (local->tm_hour < 10)
ss << "0";
ss << local->tm_hour << ':';
if (local->tm_min < 10)
ss << "0";
ss << local->tm_min<<':';
if(local->tm_sec<10)
ss<<"0";
ss<<local->tm_sec;
return ss.str();
}
char* G2U(const char* gb2312)
{
int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
if (wstr) delete[] wstr;
return str;
}
char* U2G(const char* utf8)
{
int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len + 1];
memset(wstr, 0, len + 1);
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len + 1];
memset(str, 0, len + 1);
WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
if (wstr) delete[] wstr;
return str;
}
string WStringToString(const wstring& ws)
{
string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "chs");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = 2 * ws.size() + 1;
char *_Dest = new char[_Dsize];
memset(_Dest,0,_Dsize);
wcstombs(_Dest,_Source,_Dsize);
string result = _Dest;
delete []_Dest;
setlocale(LC_ALL, curLocale.c_str());
result = G2U(result.c_str());
return result;
}
wstring StringToWString(const string s)
{
setlocale(LC_ALL, "chs");
const char* _Source = s.c_str();
size_t _Dsize = s.size() + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest,_Source,_Dsize);
wstring result = _Dest;
delete []_Dest;
setlocale(LC_ALL, "C");
return result;
}
std::string string_To_UTF8(const std::string str)
{
int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
wchar_t * pwBuf = new wchar_t[nwLen + 1];//һ<><D2BB>Ҫ<EFBFBD><D2AA>1<EFBFBD><31><EFBFBD><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>β<EFBFBD><CEB2>
ZeroMemory(pwBuf, nwLen * 2 + 2);
::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char * pBuf = new char[nLen + 1];
ZeroMemory(pBuf, nLen + 1);
::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string retStr(pBuf);
delete []pwBuf;
delete []pBuf;
pwBuf = NULL;
pBuf = NULL;
return retStr;
}
double stringToNum( string str)
{
istringstream iss(str);
double num;
iss >> num;
return num;
}
void trim(std::string &s)
{
if (s.empty())
{
return;
}
s.erase(0,s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1);
}
void getLatest(tag_t item,tag_t *rev)
{
int cnt,rel_cnt;
tag_t *tags,*rels;
ITEM_list_all_revs(item,&cnt,&tags);
for(auto i=cnt-1;i>=0;i--)
{
AOM_ask_value_tags(tags[i],"release_status_list",&rel_cnt,&rels);
if(rel_cnt)
{
*rev=tags[i];
return;
}
}
*rev=NULLTAG;
}
string get_excel_data(libxl::Book *book, libxl::Sheet *sheet, int row, int col)
{
string result;
double temp;
try
{
libxl::CellType cell_type = sheet->cellType(row, col);
switch (cell_type)
{
case libxl::CELLTYPE_STRING:
result = WStringToString(sheet->readStr(row, col));
break;
case libxl::CELLTYPE_BLANK:
result = "";
break;
case libxl::CELLTYPE_EMPTY:
result = "";
break;
case libxl::CELLTYPE_NUMBER:
temp = sheet->readNum(row, col);
if (sheet->isDate(row, col))
{
int year = 0, month = 0, day = 0, hour = 0, min = 0, second = 0;
book->dateUnpack(temp, &year, &month, &day, &hour, &min, &second);
date_t d;
d.year = year; d.month = month-1; d.day = day; d.hour = hour; d.minute = min; d.second = second;
char *date_str, *date_format;
DATE_default_date_format(&date_format);
DATE_date_to_string(d, date_format, &date_str);
result = date_str;
}
else
{
result = temp == (int)temp ? to_string((int)temp) : to_string(temp);
}
break;
case libxl::CELLTYPE_BOOLEAN:
result = to_string(sheet->readBool(row, col));
break;
case libxl::CELLTYPE_ERROR:
result = "";
break;
}
}
catch (exception e)
{
// LERROR<<"col num"<<k<<"error:"<<e.what();
}
return result;
}
void log(const char* format, ...) {
va_list arg;
char tmp[8192];
//get the message
memset(tmp, 0, sizeof(tmp));
va_start(arg, format);
vsprintf(tmp, format, arg);
va_end(arg);
//----------print to command window for trace--------//
printf("%s\n", tmp);
//print message to log file
info(tmp);
}
void log2(string msg) {
printf("%s\n", msg.c_str());
info(msg.c_str());
}