| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- /**
- * @content:基本类型定义
- * @time:2016-8-23
- * @author:Mr_zhu
- * @version: V1.0
- * @describe:
- * 1#2016-8-23#V1.0#首次生成
- */
-
- #ifndef TYPE_H
- #define TYPE_H
-
- #define LOG_TAG "can_dexn"
-
- #include <elog.h>
-
- #include <errno.h>
- #include <math.h>
- #include <sys/select.h>
- #include <sys/time.h>
- #include <cstdio>
- #include <ctime>
- #include <iostream>
- #include <iterator>
- #include <string>
- #include <vector>
-
- typedef long HRESULT;
- typedef unsigned long ULONG;
-
- typedef char BYTE;
- typedef char* PBYTE;
-
- class IUnknown;
-
- #ifndef NULL
- #define NULL 0
- #endif
-
- typedef struct _GUID
- {
- unsigned int Data1;
- unsigned short Data2;
- unsigned short Data3;
- unsigned char Data4[8];
-
- std::string toString()
- {
- char t[256];
- snprintf(t, 256, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", Data1, Data2, Data3, Data4[0], Data4[1], Data4[2], Data4[3], Data4[4], Data4[5],
- Data4[6], Data4[7]);
- return t;
- }
-
- } GUID;
-
- typedef GUID IID;
-
- typedef std::string ProgID;
-
- #define interface struct
- #define CLSID IID //----组件标识符
- #define OBJID IID //----对象标识符
-
- #define E_NOINTERFACE 0x80004002L
- #define S_OK 0
- #define S_FALSE 1
-
- #define SUCCEEDED(x) ((x)==S_OK)
-
- #define READ_O 0x00000001
- #define READ_WAIT 0x00000002
- #define READ_TIMEOUT 0x00000004
- #define WRITE_O 0x00000008
- #define WRITE_WAIT 0x00000010
- #define WRITE_TIMEOUT 0x00000020
- #define WRITE_FAIL 0x00000040
-
- #define HEX 16
-
- typedef IUnknown* (*CreateFuncPTR)();
-
- inline BYTE HByte(unsigned int x)
- {
- return (((x) >> 8) & 0x00ff);
- }
- inline BYTE LByte(unsigned int x)
- {
- return (((x)) & 0x00ff);
- }
-
- inline BYTE HHByte(unsigned int x)
- {
- return (((x) >> 24) & 0x000000ff);
- }
- inline BYTE HLByte(unsigned int x)
- {
- return (((x) >> 16) & 0x000000ff);
- }
- inline BYTE LHByte(unsigned int x)
- {
- return (((x) >> 8) & 0x000000ff);
- }
- inline BYTE LLByte(unsigned int x)
- {
- return (((x)) & 0x000000ff);
- }
-
- inline BYTE HHByte(long int x)
- {
- return (((x) >> 24) & 0x000000ff);
- }
- inline BYTE HLByte(long int x)
- {
- return (((x) >> 16) & 0x000000ff);
- }
- inline BYTE LHByte(long int x)
- {
- return (((x) >> 8) & 0x000000ff);
- }
- inline BYTE LLByte(long int x)
- {
- return (((x)) & 0x000000ff);
- }
-
- inline BYTE HHByte(int x)
- {
- return (((x) >> 24) & 0x000000ff);
- }
- inline BYTE HLByte(int x)
- {
- return (((x) >> 16) & 0x000000ff);
- }
- inline BYTE LHByte(int x)
- {
- return (((x) >> 8) & 0x000000ff);
- }
- inline BYTE LLByte(int x)
- {
- return (((x)) & 0x000000ff);
- }
-
- /**
- * @content:输出调试信息至stdout
- * @time:2016-8-26
- * @author:Mr_zhu
- * @param: const ProgID(组件中文标识符),const char*(待输出信息)
- * @return: void
- * @decribe
- * 1#2016-8-26#V1.0#首次生成
- */
- inline void trace(const ProgID pid, const char* msg)
- {
- struct timeval tv;
- struct timezone tz;
-
- struct tm *p;
-
- gettimeofday(&tv, &tz);
- // printf("tv_sec:%ld\n", tv.tv_sec);
- // printf("tv_usec:%ld\n", tv.tv_usec);
- // printf("tz_minuteswest:%d\n", tz.tz_minuteswest);
- // printf("tz_dsttime:%d\n", tz.tz_dsttime);
-
- p = localtime(&tv.tv_sec);
- char t[255];
- snprintf(t, 255, "time_now:%04d-%02d-%02d %02d-%02d-%02d,%ld", 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, tv.tv_usec);
- // std::cout << __FILE__ << "," << __LINE__ << "[" << t << "], "<< pid << ":" << msg
- // << std::endl;
- std::cout << "[" << t << "], " << pid << ":" << msg << std::endl;
- }
-
- /**
- * @content:将String转为Hex
- * @time:2016-10-1
- * @author:Mr_zhu
- * @param: std::string(待转字符串)
- * @return: unsigned int(返回值)
- * @decribe
- * 1#2016-10-1#V1.0#首次生成
- */
- inline unsigned int hexstringtoInt(std::string str)
- {
- unsigned int result = 0;
- for (unsigned int i = 0; i < str.length(); i++)
- {
- if ((str.at(i) >= '0') && (str.at(i) <= '9'))
- {
- result += ((str.at(i) - 48) * pow(16, str.length() - 1 - i));
- }
- else if ((str.at(i) >= 'a') && (str.at(i) <= 'f'))
- {
- result += ((str.at(i) - 97 + 10) * pow(16, str.length() - 1 - i));
- }
- }
- return result;
- }
-
- /**
- * @content:将String转为Hex
- * @time:2016-10-1
- * @author:Mr_zhu
- * @param: std::string(待转字符串)
- * @return: unsigned short(返回值)
- * @decribe
- * 1#2016-10-1#V1.0#首次生成
- */
- inline unsigned short hexstringtoShort(std::string str)
- {
- unsigned short result = 0;
- for (unsigned int i = 0; i < str.length(); i++)
- {
- if ((str.at(i) >= '0') && (str.at(i) <= '9'))
- {
- result += (str.at(i) - 48) * pow(16, str.length() - 1 - i);
- }
- if ((str.at(i) >= 'a') && (str.at(i) <= 'f'))
- {
- result += (str.at(i) - 97 + 10) * pow(16, str.length() - 1 - i);
- }
- }
- return result;
- }
-
- /**
- * @content:将String转为Hex
- * @time:2016-10-1
- * @author:Mr_zhu
- * @param: std::string(待转字符串)
- * @return: unsigned char(返回值)
- * @decribe
- * 1#2016-10-1#V1.0#首次生成
- */
- inline unsigned char hexstringtoChar(std::string str)
- {
- unsigned char result = 0;
- for (unsigned int i = 0; i < str.length(); i++)
- {
- if ((str.at(i) >= '0') && (str.at(i) <= '9'))
- {
- result += (str.at(i) - 48) * pow(16, str.length() - 1 - i);
- }
- if ((str.at(i) >= 'a') && (str.at(i) <= 'f'))
- {
- result += (str.at(i) - 97 + 10) * pow(16, str.length() - 1 - i);
- }
- }
- return result;
- }
-
- /**
- * @content:将String转为GUID
- * @time:2016-10-1
- * @author:Mr_zhu
- * @param: std::string(待转字符串)
- * @return: GUID(返回的GUID)
- * @decribe
- * 1#2016-10-1#V1.0#首次生成
- */
- inline GUID stringtoGUID(std::string str)
- {
- GUID t_guid =
- { 0, 0, 0,
- { 0, 0, 0, 0, 0, 0, 0, 0 } };
-
- if (!str.empty())
- {
- std::string t_str = str;
- std::vector<std::string> t_vstr;
- // int index = t_str.find('{');
- // int index1 = t_str.find('}');
- int index = 0;
- int index1 = 0;
- if ((index != -1) && (index1 != -1))
- {
- t_str = t_str.substr(0);
- int index = t_str.find('-');
-
- while (index != -1)
- {
- t_vstr.push_back(t_str.substr(0, index));
- t_str = t_str.substr(index + 1);
- index = t_str.find('-');
- if (index == -1)
- {
- break;
- }
- }
- t_vstr.push_back(t_str);
- }
- if (t_vstr.size() == 5)
- {
- t_guid.Data1 = hexstringtoInt(t_vstr.at(0));
- t_guid.Data2 = hexstringtoShort(t_vstr.at(1));
- t_guid.Data3 = hexstringtoShort(t_vstr.at(2));
- t_guid.Data4[0] = hexstringtoChar(t_vstr.at(3).substr(0, 2));
- t_guid.Data4[1] = hexstringtoChar(t_vstr.at(3).substr(2));
- std::string sstr = t_vstr.at(4);
- t_guid.Data4[2] = hexstringtoChar(sstr.substr(0, 2));
- t_guid.Data4[3] = hexstringtoChar(sstr.substr(2, 2));
- t_guid.Data4[4] = hexstringtoChar(sstr.substr(4, 2));
- t_guid.Data4[5] = hexstringtoChar(sstr.substr(6, 2));
- t_guid.Data4[6] = hexstringtoChar(sstr.substr(8, 2));
- t_guid.Data4[7] = hexstringtoChar(sstr.substr(10, 2));
- }
- }
- return t_guid;
- }
-
- /**
- * @content: 比较两个标识符是否相等
- * @time:2016-8-26
- * @author:Mr_zhu
- * @param: const IID&(待比较标识符1),const IID2&(待比较标识符2)
- * @return: void
- * @decribe
- * 1#2016-8-26#V1.0#首次生成
- */
- inline bool operator==(const IID& guid1, const IID& guid2)
- {
- if (guid1.Data1 != guid2.Data1)
- return false;
- if (guid1.Data2 != guid2.Data2)
- return false;
- if (guid1.Data3 != guid2.Data3)
- return false;
- for (int i = 0; i < 8; i++)
- {
- if (guid1.Data4[i] != guid2.Data4[i])
- return false;
- }
- return true;
- }
-
- /**
- * @content: 比较标识符1是否小于标识符2
- * @time:2016-8-26
- * @author:Mr_zhu
- * @param: const IID&(待比较标识符1),const IID2&(待比较标识符2)
- * @return: void
- * @decribe
- * 1#2016-8-26#V1.0#首次生成
- */
- inline bool operator<(const IID& guid1, const IID& guid2)
- {
- if (guid1.Data1 >= guid2.Data1)
- return false;
- if (guid1.Data2 >= guid2.Data2)
- return false;
- if (guid1.Data3 >= guid2.Data3)
- return false;
- for (int i = 0; i < 8; i++)
- {
- if (guid1.Data4[i] >= guid2.Data4[i])
- return false;
- }
- return true;
- }
-
- inline bool isGUIDEqual(const IID& guid1, const IID& guid2)
- {
- return ((guid1.Data1 == guid2.Data1) && (guid1.Data2 == guid2.Data2) && (guid1.Data3 == guid2.Data3) && (guid1.Data4[0] == guid2.Data4[0])
- && (guid1.Data4[1] == guid2.Data4[1]) && (guid1.Data4[2] == guid2.Data4[2]) && (guid1.Data4[3] == guid2.Data4[3])
- && (guid1.Data4[4] == guid2.Data4[4]) && (guid1.Data4[5] == guid2.Data4[5]) && (guid1.Data4[6] == guid2.Data4[6])
- && (guid1.Data4[7] == guid2.Data4[7]));
- }
-
- inline void secondsSleep(unsigned int seconds)
- {
- struct timeval tv;
- tv.tv_sec = seconds;
- tv.tv_usec = 0;
- int err;
- do
- {
- err = select(0, NULL, NULL, NULL, &tv);
- } while (err < 0 && errno == EINTR);
- }
-
- inline void milisecondsSleep(unsigned int mSeconds)
- {
- struct timeval tv;
- tv.tv_sec = mSeconds / 1000;
- tv.tv_usec = (mSeconds % 1000) * 1000;
- int err;
- do
- {
- err = select(0, NULL, NULL, NULL, &tv);
- } while (err < 0 && errno == EINTR);
- }
-
- inline void microssecondsSleep(unsigned int uSeconds)
- {
- struct timeval tv;
- tv.tv_sec = uSeconds / 1000000;
- tv.tv_usec = uSeconds % 1000000;
- int err;
- do
- {
- err = select(0, NULL, NULL, NULL, &tv);
- } while (err < 0 && errno == EINTR);
- }
-
- /**
- * DEBUG等级
- */
- typedef enum
- {
- DEBUG = 0, INFO, WARN, ERROR, FATAL
- } LEVEL;
-
- #endif
|