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.

304 lines
6.3 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"
int checkIsTypeOrSubtype(tag_t objtag, 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);
int is_type = 0;
if (item_type != NULLTAG) {
logical isok = FALSE;
TCTYPE_is_type_of(type, item_type, &isok);
if (isok) {
is_type = 1;
}
}
return is_type;
}
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());
}
}
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();
}
string WStringToString(const wstring& ws)
{
if(ws.empty()|| ws.length()==0)
{
LINFO<<"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>";
return "";
}
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());
return result;
}
wstring StringToWString(const string s)
{
printf("%s\r\n","StringToWString start");
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");
printf("StringToWString start end\r\n");
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;
}
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;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 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;
}
//<2F><><EFBFBD><EFBFBD>itemID<49><44>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD>
void queryRevByItemID(string item_id, tag_t *rev)
{
tag_t latest = NULLTAG;
tag_t query;
QRY_find2("chint_query_item", &query);
if (query == NULL){
printf("û<EFBFBD>иò<EFBFBD>ѯ\r\n");
return ;
}
char **entries, **vals;
entries = (char **)MEM_alloc(1 * sizeof(char *));
vals = (char **)MEM_alloc(1 * sizeof(char *));
char *a = "ID";
entries[0] = (char *)MEM_alloc((int)strlen(a) * sizeof(char));
strcpy(entries[0], a);
vals[0] = (char *)MEM_alloc((int)strlen(item_id.c_str()) * sizeof(char));
strcpy(vals[0], item_id.c_str());
int count;
tag_t *item_revs;
//<2F><><EFBFBD>ò<EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD>ó<EFBFBD><C3B3>Ұ汾
SAFECALL(QRY_execute(query, 1, entries, vals, &count, &item_revs));
if (count>0)
{
*rev = item_revs[0];
//char *gname;
//tag_t vendor;
//SAFECALL(ITEM_ask_item_of_rev(item_revs[0], &vendor));
}else
{
(*rev) = NULLTAG;
}
}