/****************************************************************************
** File name    :  HTPublic.h
** Description  :  define common api
** Create date  :  2018.09.01
** Auther by    :  Liuyx
** Version info :  V1.0.01
** Copyright (C) 2002-2018 xi'an huatek, Inc Co., Ltd
** Update record:  
** DATE         AUTHER   DESC 
** -------------------------------------------------------------------------               
** 2018.09.01   Liuyx    first build
****************************************************************************/
#ifndef __HT_PUBLIC_H
#define __HT_PUBLIC_H

#ifdef _WIN32
#pragma comment(lib,"ws2_32.lib") 
#endif
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h> 
#include <ctype.h>
#include <time.h>        /* timespec{} for pselect() */
#include <errno.h>
#include <setjmp.h>
#include <signal.h>
#include <fcntl.h>       /* for nonblocking */
#include <stdarg.h>
#include <assert.h>
#include "HTType.h"

using namespace std;

#ifdef _WIN32
    #include <direct.h>
    #include <io.h>
    #include <process.h>
    #include <winsock2.h>
    #include <objbase.h>
    #include <guiddef.h>
#else
    #include <sys/io.h>
    #include <sys/socket.h>
    #include <linux/unistd.h>
    #include <sys/types.h>
    #include <sys/syscall.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <iconv.h>       /*字符集转换*/
    #include <netinet/in.h>  /* sockaddr_in{} and other Internet defns */
    #include <arpa/inet.h>   /* inet(3) functions */
    #include <netdb.h>
    #include <inttypes.h>
    #include <sys/time.h>
    #include <pthread.h>
    #include <strings.h>
    #include <dirent.h>
    #include <asm/unistd.h>
    #include <sys/wait.h>
    #include <uuid/uuid.h>
#endif

/*
#if defined(__linux__) && defined(__NR_gettid)
#define GETTID() syscall(__NR_gettid)
#else
#define GETTID() getpid()
#endif
*/
#ifdef _WIN32
#define GETTID() GetCurrentThreadId()    // 获取线程ID
#define GETPID() GetCurrentProcessId()   // 获取主进程ID
#else
#define GETTID() syscall(__NR_gettid)
#define GETPID() getpid()   // 获取主进程ID
#endif

#ifdef __cplusplus
extern "C" {
#endif  /* __cplusplus */

void vSetPid();
int  iGetPid();

// 时间函数
void          vGetHostTime (char *pszTime) ;
time_t        vGetHostTimeFmt (char *pszTime) ;
time_t        vGetHostTimeFmtBeforBay(char *pszTime, int beforbay);
void          vTranHostTimeFmt (time_t t, char *pszTime);
time_t        strTime2int(char *pszTime);
time_t        strTimeFmt2int(char *pszTime);
void          getCurrentBefor360Hour(char *pstime, char *petime);
time_t        vGeOneDayTimeFmt(char *pSTime, char *pETime);

time_t        getUTCLongTime (void) ;
void          getUTCFmtTime (char *pszTime) ;
void          UTC2FmtTime (time_t utc, char *pszTime) ;
void          UTC2LocalTime (time_t utc, char *pszTime) ;
void          diffTimes(unsigned int c, char *pszTime);
unsigned int  uiTime (void) ;
int           getCurrTimeDiff(unsigned char *bcd_t) ;

time_t        local2Utc(unsigned char *pTime, int iLen);
time_t        local2UtcTime(time_t localTime);

int getuuid(char *str, int inlen);

void vGetHostTimeOnMillsecond(char *pszTime);       // 获取时间,YYYYMMDDHH24MISS:MLSS
time_t vGetHostTimeFmtOnMillsecond(char *pszTime);  // 获取时间,YYYY-MM-DD HH24:MI:SS+MLSS

unsigned int  getWarnID(unsigned char *pTime, int iLen, unsigned int id);


// 目录操作函数
char* getBaseName(char* path, char *basename);
char* getDirName(char* path, char *dirname);
int   iDirOrFileExist(const char *pszName);
int   iBuildDirent(const char *pName);
bool  bMoveFile(char *filename, char *pBakPath);
int   iSplitNumberFiled(char *pstr, char node, int no);
int   iSplitStringFiled(char *pstr, char node, char no, char *pFiled);
int   get_file_size(const char *path);
int   getFileCreateTime(const char *path, char *pCreateTime);

bool  bSplitFilenameByFullPath(char *pFullPath, char *pPrantPath, char *pFilename, char *pExt);
bool  bSplitFilenameByPath(char *pFullPath, char *pPrantPath, char *pFilename);
int   iGetFileBeforString(char *pinstr, int pstrlen, char *poutstr);

// 将Linux文件路径转为Windows的路径格式
void  Unix2WindowPath(char *pszPath);
void  Window2UnixPath(char *pszPath);
void  getGnssData(unsigned char *p_date);
void  getGnssTime(unsigned char *p_time);
void  setPostionDate(unsigned char *pDate, unsigned char *pTime);
void  setPostionTime(unsigned char *pDate, unsigned char *pTime);

int   iTrimVersionHeadChar(char *strver);
void  LRTrim(unsigned char *str);
int   RigthTrim(unsigned char *str);
int   LeftTrim(unsigned char *str);
void  MiddleTrim(unsigned char *str);
bool  isStringDigit(char *pstr);
char  *str2lower(char *s);
char  *str2upper(char *s);

int   stringcasecmp(const char *ps1, const char *ps2);
int   stringncasecmp(const char *ps1, const char *ps2, int length);

int   bits_check(unsigned char *map, int field_no);

void  getDateFromTime(char* p_time,char* p_date);
void  getTimeFromTime(char* p_time,char* p_times);

void  bcd_to_asc(unsigned char *pusFrom, unsigned int uiFromLen,unsigned char *pusTo);
void  asc_to_bcd(unsigned char *pusFrom, unsigned int uiFromLen, unsigned char *pusTo);

unsigned int htoni(unsigned int us);
unsigned int ntohi(unsigned int us);
float n2hf(float value);

double getRandByTime();

#ifdef ntohll
utint64  ntohll(utint64 arg64) ;
#endif
#ifdef htonll
utint64  htonll(utint64 arg64) ;
#endif
unsigned int int64To32(utint64  u64);
void LocalTime2Bcd(char *pLocalTime, char *pBcd);

int  getStringLen(unsigned char *pstr, int length);
int  getHexString(unsigned char *str, int length, unsigned char *pstr);

/*************************************************************************
Function     : 32位整数转二进制字符串
Param in     :
       num   : source uint
Param out    :
      pBin   : object strings.(min length = 32)
Return Code  : none
*************************************************************************/
void uint32_binstr(unsigned int num, char* pBin);

/*************************************************************************
Function     : 64位整数转二进制字符串
Param in     :
       num   : source uint
Param out    :
      pBin   : object strings.(min length = 64)
Return Code  : none
*************************************************************************/
void uint64_binstr(utint64 num, char* pBin);

/*************************************************************************
Function     : 二进制字符串转32位整数
Param in     :
       pBin  : source bin string(eg. 1010101001111), need '\0' end.
Param out    :
             : none
Return Code  : uint32 interger
*************************************************************************/
unsigned int binstr_uint32(char *pBin);

/*************************************************************************
Function     : 二进制字符串转64位整数
Param in     :
       pBin  : source bin string(eg. 1010101001111), need '\0' end.
Param out    :
             : none
Return Code  : utint64 interger
*************************************************************************/
utint64 binstr_uint64(char *pBin);

/*************************************************************************
Function     : 字符串反转
Param in     :
       str   : source string(eg. 1010101001111), need '\0' end.
       len   : source length
Param out    :
             : str
Return Code  : str, null is faild
*************************************************************************/
char* strSwap(char* szT,int len);

//从低位到高位,取n的第m位,m=1...32
int getBit(int n, int m);

//从低位到高位.将n的第m位置1, m=1...32
int setBitToOne(int n, int m);

//从低位到高位, 将n的第m位置0,m=1...32
int setBitToZero(int n, int m);

#ifdef  __cplusplus
}
#endif
#endif // end __HT_PUBLIC_H

