-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.hpp
More file actions
94 lines (77 loc) · 1.89 KB
/
utils.hpp
File metadata and controls
94 lines (77 loc) · 1.89 KB
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
#ifndef UTILS_HPP
#define UTILS_HPP
#include <cstdio>
#include <cstdlib>
//#include <cstring>
#include <string>
using namespace std;
#define HEADER_LEN 4
/* Class defines the response structure */
class response_
{
public:
enum { header_length = HEADER_LEN };
enum { max_body_length = 1024 };
response_(string value)
: body_length_(0)
{
encode_message(value);
}
const char* data() const
{
return data_;
}
char* data()
{
return data_;
}
int length() const
{
return header_length + body_length_;
}
const char* body() const
{
return data_ + header_length;
}
char* body()
{
return data_ + header_length;
}
int body_length() const
{
return body_length_;
}
void set_body_length(int new_length)
{
body_length_ = new_length;
if (body_length_ > max_body_length)
body_length_ = max_body_length;
}
void encode_message(string value)
{
set_body_length(value.size());
int len = 0;
if (body_length_ > 0) {
len = int (log10(body_length_)) + 1;
}
// Prepare header
memcpy (data_, "0000", header_length - len);
if (value.size() > 0) {
memcpy(data_ + (header_length - len),
to_string(body_length_).c_str(), len);
// copy body
memcpy(data_ + header_length, value.c_str(), value.size());
}
// // Another way to write header length
// using namespace std; // For sprintf and memcpy.
// char header[header_length + 1] = "";
// sprintf(header, "%4d", body_length_);
// memcpy(data_, header, header_length);
}
private:
/* First "header_length" byte contatin size of the message and remaining bytes is body */
char data_[header_length + max_body_length];
int body_length_;
};
typedef response_ response_t;
#endif // UTILS_HPP