logplus/Workflow/WFCrystal/SysUtility/utils/include/Utils.h

394 lines
10 KiB
C
Raw Normal View History

2026-01-16 17:18:41 +08:00
/**
* @file Utils.h
*/
#ifndef PAI_FRAME_SYSUTILITY_UTILS_H
#define PAI_FRAME_SYSUTILITY_UTILS_H
#ifdef LLToString
#undef LLToString
#endif
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include <sstream>
#include <map>
#include <set>
#include <string.h>
#include <stdarg.h>
//#include<sys/time.h>
#include <cfloat>
#include <cmath>
#include "Turtle.h"
#ifdef LLToString
#undef LLToString
#endif
std::string PAI_UTILS_EXPORT GetDBPath();
typedef long long int64;
using namespace pai::turtle;
namespace pai {
namespace utils {
static const int CONST_FORMAT_BUFFER_SIZE = 4096;
static const float F_EPSILON = static_cast<float>(1e-6);
static const double D_EPSILON = 1e-6;
class PAI_UTILS_EXPORT CUtils
{
public:
/**
* @brief
*/
CUtils();
/**
* @brief
*/
~CUtils();
/**
* ,"YYYY-MM-DD HH:MM:SS.MMM"
*/
static std::string getCurrentDate();
/**
* @brief Get C Format String
* @param[in] fmt format string
* @param[in] args
* @return formated string
*/
static std::string GetFormatStr(const std::string& fmt,...){
char buf[CONST_FORMAT_BUFFER_SIZE];
std::stringstream ss;
va_list ap;
va_start(ap, fmt);
vsprintf(buf, fmt.c_str(), ap);
va_end(ap);
ss << buf << std::endl;
return ss.str();
}
/**
* @brief Get environment path of "PAI_HOME"
* @return the environment path of "PAI_HOME"
*/
static std::string GetPaiHome();
/**
* @brief string类型变量转换为int类型变量
* @param[in] s string类型变量
* @return int型变量
*
*/
static int StrToInt(const std::string& s)
{
return std::atoi(s.c_str());
}
/**
* @brief int类型变量转换为string类型变量
* @param[in] value int类型变量
* @return string型变量
*
*/
static std::string IntToStr(long long value)
{
char buff[64];
std::sprintf(buff, "%lld", value);
return std::string(buff);
}
/**
* @brief string类型变量转换为long long类型变量
* @param[in] s string类型变量
* @return long long型变量
*
*/
static long long StrToLL(const std::string& s)
{
return GetTurtleAPI()->StringToLong(s);
}
/**
* @brief long long类型变量转换为string类型变量
* @param[in] value long long类型变量
* @return string型变量
*
*/
static std::string LLToString(long long value)
{
char buff[64];
std::sprintf(buff, "%lld", value);
return std::string(buff);
}
/**
* @brief string类型变量转换为float类型变量
* @param[in] s string类型变量
* @return float型变量
*
*/
static float StrToFloat(const std::string& s)
{
return static_cast<float>(std::atof(s.c_str()));
}
/**
* @brief string类型变量转换为float类型变量
* @param[in] s string类型变量
* @return double型变量
*
*/
static double StrToDouble(const std::string& s)
{
return std::atof(s.c_str());
}
/**
* @brief float类型变量转换为string类型变量
* @param[in] value float类型变量
* @return string型变量
*
*/
static std::string FloatToStr(double value)
{
char buff[128];
std::sprintf(buff, "%f", value);
std::string ret = std::string(buff);
size_t size = ret.size();
std::string s = ret;
while(ret[size-1] == '0')
{
s.erase(size-1,1);
size--;
}
if (ret[size-1] == '.')
{
s.erase(size-1,1);
size--;
}
return s;
}
/**
* @brief float类型变量转换为string类型变量
* @param[in] value float类型变量
* @return string型变量
*
*/
static std::string FloatToStr(double value,int digits)
{
char buff[128];
std::stringstream ss;
ss << "%0." << digits << "lf";
std::sprintf(buff, ss.str().c_str(), value);
return buff;
}
/**
* @brief float类型是否相等
* @param[in] value float类型变量
* @return true false
*/
static bool CompareEqual(float value1,float value2)
{
if(fabs(value1-value2) < F_EPSILON)
return true;
else
return false;
}
/**
* @brief double类型是否相等
* @param[in] value double类型变量
* @return true false
*/
static bool CompareEqual(double value1,double value2)
{
if(fabs(value1-value2) < D_EPSILON)
return true;
else
return false;
}
/**
* @brief classpath环境变量
*
* @return 0
* -1
*/
static void SetHadoopClassPath();
/**
* @brief
*
* 1=1KB 1024=1MB
*
* @param[in] size
*/
static std::string DigitSizeToString(long long size);
/**
* @brief IP
*
* @param[out] IP地址
*/
// static std::string GetLocalIp();
/**
* @brief ID是否存在
* @param[in] pID ID
* @param[out] true false
*/
static bool ProcessExist(pid_t pID);
/**
* @brief 便
* @param[in] seconds
* @return 便
*/
static std::string TimeToString(int64 seconds);
/**
* @brief
* @param[in] timeStr
* @return
*/
static int64 TimeStringToSeconds(const std::string &timeStr);
/**
* ()
*/
static int64 getSystemTime();
/**
* @brief ()
* @param[out] time
* @return cpu频率支持纳秒精度 true ,false.
*/
static bool getNanoTime(long long &time);
//----------------SeisUtil-------------------------
//----------------SegyUtil--------------------------
enum EIBM2IEEE
{
IBM_TO_IEEE = 0, IEEE_TO_IBM = 1
};
static unsigned char ebasc(unsigned char ascii);
static void ebasd(unsigned char* ascii, unsigned char* ebcd);
static void asebd(char* ebcd, char* ascii);
static char asebc(unsigned char ascii);
static void SwapBhdr(unsigned char *buf);
static void SwapTraceHead(unsigned char *buf);
static void SwapTxtHead(unsigned char *buf);
static void ChrToIEEE(unsigned char *src, float *dst, int len);
static void SpiToIEEE(short *src, float *dst, int len);
static void BgnToIEEE(short *src, float *dst, int len);
static void IntToIEEE(int *src, float *dst, int len);
static void IEEEToChr(float *src, char *dst, int len);
static void IEEEToSpi(float *src, short *dst, int len, short &scalar);
static void IEEEToInt(float *src, int *dst, int len);
static void IEEEToBgn(float *src, short *dst, int len);
static void IbmToIEEE(int *src, float *dst, int n);
static void IEEEToIbm(float *src, int *dst, int n);
static void SwapByte(unsigned char *buf, int len, int flag);
/**
* @brief
* @return
*/
static std::string ConcatWithChar(std::vector<long>& source, char sep);
static std::string ConcatWithChar(std::set<long>& source, char sep);
static std::string ConcatWithChar(std::map<long, long>& source, char sep1, char sep2);
static std::string ConcatWithChar(std::map<int, std::string>& source, char sep1, char sep2);
static std::string ConcatWithChar(std::map<long long, std::string>& source, char sep1, char sep2);
/**
* @brief
*/
static std::string Replace_all(const std::string & str, const char old_value, const char new_value);
/**
* @brief
* @param[in] str
*
* @return
*/
static std::string Trim(std::string str);
private:
static void AppendClassPath(const std::string& path, std::string & classPath);
/**
* @return Number of matching items.
*/
static int ListFiles(const std::string& path, const std::string& filter, std::vector<std::string>& file);
//-----------------SegyUtil------------------
static void IbmIeee(float *src, float *dst, int n, int idirec);
static void IeeeIbm(char *from, char *to,int len, int type);
};
/** @brief Handle class that supports RAII to wrap FILE pointers.
* Can be used exactly as FILE pointers (or as if it were FILE pointer itself)
* but users don't need to care about closing File_ptr as it will be closed
* automatically when object will go out of it's scope. Also implicit
* convertions from/to FILE pointers are provided.
*/
class PAI_UTILS_EXPORT File_ptr {
public:
/** @brief constructor that opens file
* @param[in] n file name to open a file
* @param[in] a file opening mode
*/
File_ptr(const char * n, const char * a);
File_ptr(FILE * pp);
~File_ptr() ;
operator FILE*() ;
private:
File_ptr(File_ptr& fp);
File_ptr& operator=(File_ptr & fp);
FILE * p;
};
template <class C>
void SplitByChar(const std::string& s, char sep, C& result, bool allowEmpty = true)
{
size_t len = s.size();
std::string line;
for (size_t i = 0; i < len; i++)
{
char c = s[i];
if (c == sep)
{
if (!allowEmpty)
{
if (!line.empty())
result.push_back(line);
} else
{
result.push_back(line);
}
line.clear();
} else
{
line.push_back(c);
}
}
if (!line.empty())
{
result.push_back(line);
}
}
}
}
#endif