/** * @file Utils.h */ #ifndef PAI_FRAME_SYSUTILITY_UTILS_H #define PAI_FRAME_SYSUTILITY_UTILS_H #ifdef LLToString #undef LLToString #endif #include #include #include #include #include #include #include #include #include //#include #include #include #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(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(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& source, char sep); static std::string ConcatWithChar(std::set& source, char sep); static std::string ConcatWithChar(std::map& source, char sep1, char sep2); static std::string ConcatWithChar(std::map& source, char sep1, char sep2); static std::string ConcatWithChar(std::map& 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& 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 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