Adbase
Adinf C++ base library V2
adbase::Utility

adbase 基础通用工具(使用方法见该模块的描述信息) More...

Classes

class  adbase::Buffer
 Buffer 可以存取多种类型的数据,保证二进制安全,并且内存可以自动分配 More...
 
class  adbase::Date
 日期工具类 More...
 
class  adbase::ReadSmallFile
 
class  adbase::AppendFile
 
class  adbase::Queue< T >
 
class  adbase::Sequence
 appid time seq mac 10 + 30 + 15 + 9 More...
 
class  adbase::Sha1
 实现 SHA1 算法 More...
 
class  adbase::Timestamp
 时间戳工具类 More...
 
class  adbase::TimeZone
 时区工具类 More...
 

Macros

#define SEQUENCE_BASE_TIME   1451577600
 

Functions

bool adbase::operator< (Date x, Date y)
 
bool adbase::operator> (Date x, Date y)
 
bool adbase::operator== (Date x, Date y)
 
uint64_t adbase::hostToNetwork64 (uint64_t host64)
 uint64_t 的整形数字由机器字节序转化为网络字节序 More...
 
uint32_t adbase::hostToNetwork32 (uint32_t host32)
 uint32_t 的整形数字由机器字节序转化为网络字节序 More...
 
uint16_t adbase::hostToNetwork16 (uint16_t host16)
 uint16_t 的整形数字由机器字节序转化为网络字节序 More...
 
uint64_t adbase::networkToHost64 (uint64_t net64)
 uint64_t 的整形数字由网络字节序转化为机器字节序 More...
 
uint32_t adbase::networkToHost32 (uint32_t net32)
 uint32_t 的整形数字由网络字节序转化为机器字节序 More...
 
uint16_t adbase::networkToHost16 (uint16_t net16)
 uint16_t 的整形数字由网络字节序转化为机器字节序 More...
 
template<typename S >
int adbase::readFile (std::string filename, int maxSize, S *context, int64_t *fileSize=nullptr, int64_t *modifyTime=nullptr, int64_t *createTime=nullptr)
 
void adbase::recursiveDir (std::string pathName, bool recursive, std::vector< std::string > excludes, std::vector< std::string > &pathInfo)
 递归遍历目录, 注意该函数禁止遍历根目录,并且目录中存在软链也忽略,只能遍历正常文件和目录 More...
 
bool adbase::mkdirRecursive (std::string pathName, int mode, bool recursive=true)
 递归创建目录 More...
 
pid_t adbase::pid ()
 获取进程ID More...
 
std::string adbase::pidString ()
 获取进程ID More...
 
std::string adbase::hostname ()
 获取主机名 More...
 
std::unordered_map< std::string, std::unordered_map< std::string, std::string > > adbase::ifconfig ()
 获取主机的 ip 列表 More...
 
std::string adbase::procname ()
 获取进程名 More...
 
std::string adbase::procname (const std::string &stat)
 获取进程名 More...
 
std::string adbase::procStatus ()
 获取进程状态(可视化版) More...
 
std::string adbase::procStat ()
 获取进程状态 More...
 
const std::unordered_map< std::string, std::string > adbase::procStats ()
 获取当前进程的状态 More...
 
int adbase::procFdNum ()
 获取当前进程打开的文件数 More...
 
std::string adbase::urlEncode (const std::string &str)
 http url encode More...
 
std::string adbase::urlDecode (const std::string &str)
 http url decode More...
 
std::string adbase::base64Encode (const char *data, size_t len)
 base64 encode More...
 
void adbase::base64Decode (char *data, int *dataLen, const std::string &str)
 base64 decode More...
 
unsigned char adbase::toHex (unsigned char x)
 char to hex More...
 
unsigned char adbase::fromHex (unsigned char x)
 hex to char More...
 
void adbase::hexStringToBytes (const std::string &str, char *buffer)
 16进制字符串转化 bits More...
 
std::string adbase::bytesToHexString (const char *buffer, size_t length)
 bits 转化为16进制可见字符 More...
 
std::string adbase::base62Encode (uint64_t id)
 
uint64_t adbase::base62Decode (const std::string &str)
 
std::string adbase::encodeSegment (std::string id)
 
std::string adbase::decodeSegment (const std::string &str)
 
std::string adbase::leftTrim (const std::string &str, const char *trimStr=nullptr)
 从字符串左边 trim More...
 
std::string adbase::rightTrim (const std::string &str, const char *trimStr=nullptr)
 从字符串右边 trim More...
 
std::string adbase::trim (const std::string &str, const char *trimStr=nullptr)
 从字符串 trim More...
 
const std::vector< std::string > adbase::explode (const std::string &s, const char &c, bool isTrim=false)
 拆分字符串 More...
 
const std::string adbase::replace (const std::string &search, const std::string &replace, const std::string &subject, int &count)
 替换字符串 More...
 
bool adbase::operator< (Timestamp lhs, Timestamp rhs)
 比较两个 Timestamp 大小 More...
 
bool adbase::operator== (Timestamp lhs, Timestamp rhs)
 判断两个 Timestamp 是否相等 More...
 
double adbase::timeDifference (Timestamp high, Timestamp low)
 求两个 Timestamp 的差值 More...
 
Timestamp adbase::addTime (Timestamp timestamp, double seconds)
 

Detailed Description

adbase 基础通用工具(使用方法见该模块的描述信息)

使用方法
Note
请勿直接引用对应头文件,util 模块统一引用<adbase/Utility.hpp> 文件
Example
.....
const string input = "dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
adbase::Sha1 checksum;
checksum.update(input);
char hash[20] = {0};
checksum.final(hash);
.....

Macro Definition Documentation

#define SEQUENCE_BASE_TIME   1451577600

Definition at line 16 of file Sequence.hpp.

Referenced by adbase::Sequence::getSeqId().

Function Documentation

Timestamp adbase::addTime ( Timestamp  timestamp,
double  seconds 
)
inline
uint64_t adbase::base62Decode ( const std::string &  str)

Definition at line 271 of file Serialize.cpp.

References DECODEBLOCKSIZE, adbase::decodeSegment(), and ENCODEBLOCKSIZE.

std::string adbase::base62Encode ( uint64_t  id)

Definition at line 232 of file Serialize.cpp.

References DECODEBLOCKSIZE, ENCODEBLOCKSIZE, and adbase::encodeSegment().

void adbase::base64Decode ( char *  data,
int *  dataLen,
const std::string &  str 
)

base64 decode

Parameters
datadecode result
dataLendecode data length
strdecode string
Returns
void
Exceptions
none
Example
std::string base64 = "test base64";
std::string base64encode = adbase::base64Encode(base64.c_str(), base64.size());
std::cout << base64encode << std::endl;
char base64decode[base64encode.size()];
memset(base64decode, 0, base64encode.size());
int length = 0;
adbase::base64Decode(base64decode, &length, base64encode);
std::cout << base64decode << std::endl;

Definition at line 155 of file Serialize.cpp.

std::string adbase::base64Encode ( const char *  data,
size_t  len 
)

base64 encode

Parameters
dataencode data
lenencode data length
Returns
encode result
Exceptions
none
Example
std::string base64 = "test base64";
std::string base64encode = adbase::base64Encode(base64.c_str(), base64.size());
std::cout << base64encode << std::endl;

Definition at line 111 of file Serialize.cpp.

std::string adbase::bytesToHexString ( const char *  buffer,
size_t  length 
)

bits 转化为16进制可见字符

Parameters
buffer要转化的 bits
length要转化的 bits 的长度
Returns
std::string 转化结果
Example
std::string str = "1d23456adbcdef";
char buffer[str.size() / 2];
cout << adbase::bytesToHexString(buffer, str.size() / 2);

Definition at line 47 of file Serialize.cpp.

std::string adbase::decodeSegment ( const std::string &  str)

Definition at line 256 of file Serialize.cpp.

Referenced by adbase::base62Decode().

std::string adbase::encodeSegment ( std::string  id)

Definition at line 208 of file Serialize.cpp.

Referenced by adbase::base62Encode().

const std::vector< std::string > adbase::explode ( const std::string &  s,
const char &  c,
bool  isTrim = false 
)

拆分字符串

Parameters
s要拆分的字符串
c分隔符
isTrim是否将拆分的字符串做 trim 操作
Returns
拆分后的字符串集合
Exceptions
none
Example
std::string a = "a1 a2 a3";
std::vector<std::string> b = adbase::explode(a);

Definition at line 41 of file String.cpp.

References adbase::trim().

Referenced by adbase::mc::ClientHandler::get(), adbase::metrics::Metrics::getMetricName(), adbase::IniParse::load(), adbase::mc::Ascii::processData(), and adbase::procStats().

unsigned char adbase::fromHex ( unsigned char  x)

hex to char

Definition at line 16 of file Serialize.cpp.

Referenced by adbase::hexStringToBytes(), and adbase::urlDecode().

void adbase::hexStringToBytes ( const std::string &  str,
char *  buffer 
)

16进制字符串转化 bits

Parameters
str需要转化的 16 进制字符串
buffer转化结果
Returns
void
Note
buffer 内存分配的长度可以根据 str 长度的 1/2 来分配
Example
std::string str = "1d23456adbcdef";
char buffer[str.size() / 2];
cout << adbase::bytesToHexString(buffer, str.size() / 2);

Definition at line 35 of file Serialize.cpp.

References adbase::fromHex().

std::string adbase::hostname ( )

获取主机名

Definition at line 39 of file ProcessInfo.cpp.

Referenced by adbase::InetAddress::portNetEndian(), and adbase::LogFile::rollFile().

uint16_t adbase::hostToNetwork16 ( uint16_t  host16)
inline

uint16_t 的整形数字由机器字节序转化为网络字节序

Parameters
host16需要转化的数据
Returns
转化为网络字节序的数据
Exceptions
none
Example
uint16_t a = 23;
uint16_t b = adbase::hostToNetwork16(a);

Definition at line 108 of file Endian.hpp.

Referenced by adbase::Buffer::appendInt16(), adbase::sockets::fromIpPort(), adbase::mc::ClientHandler::get(), adbase::InetAddress::InetAddress(), adbase::Buffer::prependInt16(), adbase::head::Binary::processData(), and adbase::mc::Binary::processData().

uint32_t adbase::hostToNetwork32 ( uint32_t  host32)
inline

uint32_t 的整形数字由机器字节序转化为网络字节序

Parameters
host32需要转化的数据
Returns
转化为网络字节序的数据
Exceptions
none
Example
uint32_t a = 23;
uint32_t b = adbase::hostToNetwork32(a);

Definition at line 91 of file Endian.hpp.

Referenced by adbase::Buffer::appendInt32(), adbase::mc::ClientHandler::get(), adbase::InetAddress::InetAddress(), adbase::Buffer::prependInt32(), adbase::head::Binary::processData(), and adbase::mc::Binary::processData().

uint64_t adbase::hostToNetwork64 ( uint64_t  host64)
inline

uint64_t 的整形数字由机器字节序转化为网络字节序

Parameters
host64需要转化的数据
Returns
转化为网络字节序的数据
Exceptions
none
Example
uint64_t a = 23;
uint64_t b = adbase::hostToNetwork64(a);

Definition at line 74 of file Endian.hpp.

Referenced by adbase::Buffer::appendInt64(), adbase::Buffer::prependInt64(), and adbase::mc::Binary::processData().

std::unordered_map< std::string, std::unordered_map< std::string, std::string > > adbase::ifconfig ( )

获取主机的 ip 列表

Definition at line 52 of file ProcessInfo.cpp.

References adbase::sockets::close().

std::string adbase::leftTrim ( const std::string &  str,
const char *  trimStr 
)

从字符串左边 trim

Definition at line 20 of file String.cpp.

Referenced by adbase::trim(), and adbase::IniParse::write().

bool adbase::mkdirRecursive ( std::string  pathName,
int  mode,
bool  recursive 
)

递归创建目录

Definition at line 228 of file FileSystem.cpp.

Referenced by adbase::AppendFile::writtenBytes().

uint16_t adbase::networkToHost16 ( uint16_t  net16)
inline

uint16_t 的整形数字由网络字节序转化为机器字节序

Parameters
net16需要转化的数据
Returns
转化为机器字节序的数据
Exceptions
none
Example
uint16_t a = 23;
uint16_t b = adbase::networkToHost16(a);

Definition at line 159 of file Endian.hpp.

Referenced by adbase::mc::ClientHandler::get(), adbase::Buffer::peekInt16(), adbase::mc::Binary::processData(), adbase::sockets::toIpPort(), and adbase::InetAddress::toPort().

uint32_t adbase::networkToHost32 ( uint32_t  net32)
inline

uint32_t 的整形数字由网络字节序转化为机器字节序

Parameters
net32需要转化的数据
Returns
转化为机器字节序的数据
Exceptions
none
Example
uint32_t a = 23;
uint32_t b = adbase::networkToHost32(a);

Definition at line 142 of file Endian.hpp.

Referenced by adbase::mc::ClientHandler::get(), adbase::Buffer::peekInt32(), adbase::head::Binary::processData(), adbase::mc::Binary::processData(), and adbase::detail::File::readInt32().

uint64_t adbase::networkToHost64 ( uint64_t  net64)
inline

uint64_t 的整形数字由网络字节序转化为机器字节序

Parameters
net64需要转化的数据
Returns
转化为机器字节序的数据
Exceptions
none
Example
uint64_t a = 23;
uint64_t b = adbase::networkToHost64(a);

Definition at line 125 of file Endian.hpp.

Referenced by adbase::Buffer::peekInt64(), and adbase::mc::Binary::processData().

bool adbase::operator< ( Date  x,
Date  y 
)
inline

Definition at line 86 of file Date.hpp.

bool adbase::operator< ( Timestamp  lhs,
Timestamp  rhs 
)
inline

比较两个 Timestamp 大小

Returns
bool

Definition at line 170 of file Timestamp.hpp.

References adbase::Timestamp::microSecondsSinceEpoch().

bool adbase::operator== ( Date  x,
Date  y 
)
inline

Definition at line 94 of file Date.hpp.

bool adbase::operator== ( Timestamp  lhs,
Timestamp  rhs 
)
inline

判断两个 Timestamp 是否相等

Returns
bool

Definition at line 180 of file Timestamp.hpp.

References adbase::Timestamp::microSecondsSinceEpoch().

bool adbase::operator> ( Date  x,
Date  y 
)
inline

Definition at line 90 of file Date.hpp.

pid_t adbase::pid ( )

获取进程ID

Definition at line 23 of file ProcessInfo.cpp.

Referenced by adbase::pidString(), and adbase::LogFile::rollFile().

std::string adbase::pidString ( )

获取进程ID

Definition at line 30 of file ProcessInfo.cpp.

References adbase::pid().

int adbase::procFdNum ( )

获取当前进程打开的文件数

Definition at line 183 of file ProcessInfo.cpp.

References adbase::recursiveDir().

Referenced by adbase::procStats().

std::string adbase::procname ( )

获取进程名

Definition at line 100 of file ProcessInfo.cpp.

References adbase::procStat().

std::string adbase::procname ( const std::string &  stat)

获取进程名

Definition at line 107 of file ProcessInfo.cpp.

std::string adbase::procStat ( )

获取进程状态

Definition at line 129 of file ProcessInfo.cpp.

References adbase::readFile().

Referenced by adbase::procname(), and adbase::procStats().

const std::unordered_map< std::string, std::string > adbase::procStats ( )

获取当前进程的状态

Definition at line 138 of file ProcessInfo.cpp.

References ADBASE_SOVERSION, ADBASE_VERSION, adbase::explode(), adbase::procFdNum(), and adbase::procStat().

std::string adbase::procStatus ( )

获取进程状态(可视化版)

Definition at line 120 of file ProcessInfo.cpp.

References adbase::readFile().

template<typename S >
int adbase::readFile ( std::string  filename,
int  maxSize,
S *  context,
int64_t *  fileSize = nullptr,
int64_t *  modifyTime = nullptr,
int64_t *  createTime = nullptr 
)

Definition at line 51 of file FileSystem.hpp.

References adbase::ReadSmallFile::readToString().

void adbase::recursiveDir ( std::string  pathName,
bool  recursive,
std::vector< std::string >  excludes,
std::vector< std::string > &  pathInfo 
)

递归遍历目录, 注意该函数禁止遍历根目录,并且目录中存在软链也忽略,只能遍历正常文件和目录

Definition at line 171 of file FileSystem.cpp.

Referenced by adbase::procFdNum(), and adbase::AppendFile::writtenBytes().

const std::string adbase::replace ( const std::string &  search,
const std::string &  replace,
const std::string &  subject,
int &  count 
)

替换字符串

Parameters
search要替换搜索的字符串
replace要替换的子字符串
subject替换文本
count替换的次数
Returns
替换后的字符串
Exceptions
none
Example
std::string a = "a1 a2 a3";
int count;
std::string b = adbase::replace("a1", "a2", a, count);

Definition at line 68 of file String.cpp.

Referenced by adbase::http::Server::setLocationFallback().

std::string adbase::rightTrim ( const std::string &  str,
const char *  trimStr 
)

从字符串右边 trim

Definition at line 6 of file String.cpp.

Referenced by adbase::trim().

double adbase::timeDifference ( Timestamp  high,
Timestamp  low 
)
inline

求两个 Timestamp 的差值

Returns
bool

Definition at line 190 of file Timestamp.hpp.

References adbase::Timestamp::kMicroSecondsPerSecond, and adbase::Timestamp::microSecondsSinceEpoch().

unsigned char adbase::toHex ( unsigned char  x)

char to hex

Definition at line 8 of file Serialize.cpp.

Referenced by adbase::urlEncode().

std::string adbase::trim ( const std::string &  str,
const char *  trimStr 
)
std::string adbase::urlDecode ( const std::string &  str)

http url decode

Parameters
strdecode url
Returns
decode result
Exceptions
none
Example
std::string url = "http://www.weibo.com?test=1&test??????";
std::string encode = adbase::urlEncode(url);
std::cout << encode << std::endl;
std::cout << adbase::urlDecode(encode) << std::endl;

Definition at line 89 of file Serialize.cpp.

References adbase::fromHex().

std::string adbase::urlEncode ( const std::string &  str)

http url encode

Parameters
strencode url
Returns
encode result
Exceptions
none
Example
std::string url = "http://www.weibo.com?test=1&test??????";
std::string encode = adbase::urlEncode(url);
std::cout << encode << std::endl;
std::cout << adbase::urlDecode(encode) << std::endl;

Definition at line 63 of file Serialize.cpp.

References adbase::toHex().