230 lines
5.7 KiB
C
230 lines
5.7 KiB
C
#ifndef _MY_BASE_H_
|
|
#define _MY_BASE_H_
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
#include <semaphore.h>
|
|
#include <pthread.h>
|
|
#include <sys/stat.h>
|
|
// #include <regex.h>
|
|
|
|
#define COLOR_RESET "\033[0m"
|
|
#define COLOR_RED "\033[31m"
|
|
#define COLOR_GREEN "\033[32m"
|
|
#define COLOR_YELLOW "\033[33m"
|
|
#define COLOR_BLUE "\033[34m"
|
|
#define COLOR_MAGENTA "\033[35m"
|
|
#define COLOR_CYAN "\033[36m"
|
|
#define COLOR_WHITE "\033[37m"
|
|
|
|
|
|
#if defined(__linux__)
|
|
#define __SHORT_FILE__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
|
|
#else
|
|
#define __SHORT_FILE__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
|
|
#endif
|
|
|
|
#define MY_LOG(color, level, fmt, ...) \
|
|
do{\
|
|
printf("%s[%s] [%s, %u] " fmt COLOR_RESET "\n", \
|
|
color, level, __SHORT_FILE__, __LINE__, ##__VA_ARGS__); \
|
|
}while (0)
|
|
|
|
|
|
#define MY_LOG_I(fmt, ...) MY_LOG(COLOR_WHITE, "INFO", fmt, ##__VA_ARGS__)
|
|
#define MY_LOG_E(fmt, ...) MY_LOG(COLOR_RED, "ERROR", fmt, ##__VA_ARGS__)
|
|
|
|
#ifndef LOCAL
|
|
#define LOCAL static
|
|
#endif
|
|
|
|
#define SHORT_STR_LEN (64)
|
|
typedef char S_STR[SHORT_STR_LEN];
|
|
|
|
#define LONG_STR_LEN (128)
|
|
typedef char L_STR[LONG_STR_LEN];
|
|
|
|
|
|
|
|
|
|
#ifndef MY_GET_SHORT_BIGEND
|
|
#define MY_GET_SHORT_BIGEND(p) ((uint16_t)((((uint8_t *)(p))[0] << 8) | (((uint8_t *)(p))[1])))
|
|
#endif
|
|
|
|
#ifndef MY_SET_SHORT_BIGEND
|
|
#define MY_SET_SHORT_BIGEND(m, n) ((m)[0] = (uint8_t)((n) >> 8), (m)[1] = (uint8_t)(n))
|
|
#endif
|
|
|
|
#ifndef MY_GET_SHORT_LITTLEEND
|
|
#define MY_GET_SHORT_LITTLEEND(p) ((uint16_t)((((uint8_t *)(p))[1] << 8) | (((uint8_t *)(p))[0])))
|
|
#endif
|
|
|
|
#ifndef MY_SET_SHORT_LITTLEEND
|
|
#define MY_SET_SHORT_LITTLEEND(m, n) ((m)[0] = (uint8_t)(n), (m)[1] = (uint8_t)((n) >> 8))
|
|
#endif
|
|
|
|
|
|
#ifndef MY_GET_INT_BIGEND
|
|
#define MY_GET_INT_BIGEND(p) ((int32_t)((((uint8_t *)(p))[0] << 24) | (((uint8_t *)(p))[1] << 16) | (((uint8_t *)(p))[2] << 8) | (((uint8_t *)(p))[3])))
|
|
#endif
|
|
|
|
#ifndef MY_GET_INT_LITTLEEND
|
|
#define MY_GET_INT_LITTLEEND(p) ((int32_t)((((uint8_t *)(p))[3] << 24) | (((uint8_t *)(p))[2] << 16) | (((uint8_t *)(p))[1] << 8) | (((uint8_t *)(p))[0])))
|
|
#endif
|
|
|
|
#ifndef MY_SET_INT_BIGEND
|
|
#define MY_SET_INT_BIGEND(m, n) ((m)[0] = (uint8_t)((n) >> 24), (m)[1] = (uint8_t)((n) >> 16), (m)[2] = (uint8_t)((n) >> 8), (m)[3] = (uint8_t)(n))
|
|
#endif
|
|
|
|
#ifndef MY_SET_INT_LITTLEEND
|
|
#define MY_SET_INT_LITTLEEND(m, n) ((m)[3] = (uint8_t)((n) >> 24), (m)[2] = (uint8_t)((n) >> 16), (m)[1] = (uint8_t)((n) >> 8), (m)[0] = (uint8_t)(n))
|
|
#endif
|
|
|
|
#ifndef MY_GET_LONG_LITTLEEND
|
|
#define MY_GET_LONG_LITTLEEND(p) ((uint64_t)(((uint8_t *)(p))[7]) << 56) | \
|
|
((uint64_t)(((uint8_t *)(p))[6]) << 48) | \
|
|
((uint64_t)(((uint8_t *)(p))[5]) << 40) | \
|
|
((uint64_t)(((uint8_t *)(p))[4]) << 32) | \
|
|
((uint64_t)(((uint8_t *)(p))[3]) << 24) | \
|
|
((uint64_t)(((uint8_t *)(p))[2]) << 16) | \
|
|
((uint64_t)(((uint8_t *)(p))[1]) << 8) | \
|
|
((uint64_t)(((uint8_t *)(p))[0]))
|
|
#endif
|
|
|
|
#ifndef MY_GET_LONG_BIGEND
|
|
#define MY_GET_LONG_BIGEND(p) ((uint64_t)(((uint8_t *)(p))[0]) << 56) | \
|
|
((uint64_t)(((uint8_t *)(p))[1]) << 48) | \
|
|
((uint64_t)(((uint8_t *)(p))[2]) << 40) | \
|
|
((uint64_t)(((uint8_t *)(p))[3]) << 32) | \
|
|
((uint64_t)(((uint8_t *)(p))[4]) << 24) | \
|
|
((uint64_t)(((uint8_t *)(p))[5]) << 16) | \
|
|
((uint64_t)(((uint8_t *)(p))[6]) << 8) | \
|
|
((uint64_t)(((uint8_t *)(p))[7]))
|
|
#endif
|
|
|
|
#ifndef MY_SET_LONG_BIGEND
|
|
#define MY_SET_LONG_BIGEND(m, n) ((m)[0] = (uint8_t)((n) >> 56), (m)[1] = (uint8_t)((n) >> 48), (m)[2] = (uint8_t)((n) >> 40), (m)[3] = (uint8_t)((n) >> 32),\
|
|
(m)[4] = (uint8_t)((n) >> 24), (m)[5] = (uint8_t)((n) >> 16), (m)[6] = (uint8_t)((n) >> 8), (m)[7] = (uint8_t)(n))
|
|
#endif
|
|
|
|
#ifndef MY_SET_LONG_LITTLEEND
|
|
#define MY_SET_LONG_LITTLEEND(m, n) ((m)[7] = (uint8_t)((n) >> 56), (m)[6] = (uint8_t)((n) >> 48), (m)[5] = (uint8_t)((n) >> 40), (m)[4] = (uint8_t)((n) >> 32),\
|
|
(m)[3] = (uint8_t)((n) >> 24), (m)[2] = (uint8_t)((n) >> 16), (m)[1] = (uint8_t)((n) >> 8), (m)[0] = (uint8_t)(n))
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef MY_GET_DATA_WITH_TYPE
|
|
#define MY_GET_DATA_WITH_TYPE(p, type) (*(type *)(p))
|
|
#endif
|
|
|
|
|
|
#ifndef MY_SET_DATA_WITH_TYPE
|
|
#define MY_SET_DATA_WITH_TYPE(p, q, type) (*(type *)(p) = *(type *)(q))
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 数据类型定义
|
|
#define DATA_TYPE_B 1
|
|
#define DATA_TYPE_S8 43
|
|
#define DATA_TYPE_U8 32
|
|
#define DATA_TYPE_S16 33
|
|
#define DATA_TYPE_U16 45
|
|
#define DATA_TYPE_S32 2
|
|
#define DATA_TYPE_U32 35
|
|
#define DATA_TYPE_L64 36
|
|
#define DATA_TYPE_UL64 37
|
|
#define DATA_TYPE_F32 38
|
|
#define DATA_TYPE_D64 39
|
|
#define DATA_TYPE_IP 100
|
|
#define DATA_TYPE_MAC 101
|
|
#define DATA_TYPE_C8 102
|
|
#define DATA_TYPE_C32 103
|
|
#define DATA_TYPE_C64 104
|
|
#define DATA_TYPE_C128 105
|
|
#define DATA_TYPE_C1 106
|
|
#define DATA_TYPE_STR 107
|
|
|
|
|
|
// typedef union
|
|
// {
|
|
// bool b;
|
|
// int8_t s8;
|
|
// uint8_t u8;
|
|
// int16_t s16;
|
|
// uint16_t u16;
|
|
// int32_t s32;
|
|
// uint32_t u32;
|
|
// int64_t s64;
|
|
// uint64_t u64;
|
|
// float f;
|
|
// double d;
|
|
// uint8_t ip[16];
|
|
// uint8_t mac[18];
|
|
// char c;
|
|
// char c8[8];
|
|
// char c32[32];
|
|
// char c64[64];
|
|
// // char c128[128];
|
|
// uint8_t buf[64];
|
|
// }un_data;
|
|
|
|
|
|
#endif |