forked from chunyi1994/cppevent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
135 lines (116 loc) · 2.53 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef UTILS_H
#define UTILS_H
#include <string>
#include <iostream>
#include <cctype>
#include <ctime>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sstream>
namespace cppevent{
static bool trim(std::string& str)
{
if (str.empty())
{
return false;
}
str.erase(0, str.find_first_not_of(" "));
str.erase( str.find_last_not_of(" ") + 1);
return true;
}
static bool beginWith(const std::string& src, const std::string& dest)
{
if(dest.length() > src.length())
{
return false;
}
if(src.substr(0, dest.length()) == dest){
return true;
}
return false;
}
static long getTimeOfDaySec(){
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec;
}
static long getTimeOfDayUSec(){
struct timeval tv;
//struct timezone tz;
gettimeofday(&tv, NULL);
return tv.tv_usec;
// printf("tv_sec:%ld\n",tv.tv_sec);
// printf("tv_usec:%ld\n",tv.tv_usec);
}
static std::string getTime(){
std::time_t t;
std::time ( &t );
std::string tStr = std::ctime(&t);
//去掉回车
tStr.erase(tStr.size() - 1, 1);
return tStr;
}
static void log(const std::string & msg){
std::cout<<getTime()<<" : "<<msg<<std::endl;
}
static int string2int(const std::string& s){
if(s.length() == 0){
return 0;
}
std::istringstream is(s);
int ret = 0;
is>>ret;
return ret;
}
static std::string int2string(int i){
std::stringstream is;
is<<i;
return is.str();
}
static int getHostByName(const std::string& name, std::string& ip){
struct hostent *hptr;
char **pptr;
char str[32];
if( (hptr = gethostbyname(name.c_str()) ) == NULL )
{
return -1; /* 如果调用gethostbyname发生错误,返回1 */
}
pptr=hptr->h_addr_list;
inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str));
ip = str;
return 0;
}
static void parseUrl(const std::string& url, std::string& host, std::string& path)
{
size_t pos1 = 0;
if(beginWith(url, "http://"))
{
pos1 = 7;
}
else if(beginWith(url, "https://"))
{
pos1 = 8;
}
size_t pos2 = url.find("/", pos1);
if(pos2 == std::string::npos)
{
pos2 = url.length();
}
host = url.substr(pos1, pos2 - pos1);
//path
if(pos2 == url.length())
{
path = "/";
}
else
{
path = url.substr(pos2, url.length() - pos2);
}
}
}
#endif // UTILS_H