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

230 lines
7.3 KiB
C

/****************************************************************************
** 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 <iostream>
#include <string>
#include <stdio.h>
#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 <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
#define DEF_PID_FILE "../run/pid_file.pid" // 程序主进程ID文件
#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);
void vFormatTimes(char *pszInTimeStr, char *pszOutFormatTime);
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);
// 目录操作函数
int iDirOrFileExist(const char *pszName);
int iBuildDirent(const char *pName);
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);
// 将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);
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);
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);
#ifdef __cplusplus
}
#endif
int replaceAll(std::string& input, const std::string& search, const std::string& replace);
#endif // end __HT_PUBLIC_H