-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathutil.h
98 lines (85 loc) · 2.63 KB
/
util.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
/** @file
*/
#ifndef UTIL_H
#define UTIL_H
extern "C" {
#include <stdint.h>
}
#include <new>
#ifndef ntohll
#define ntohll_macro(x) ( (((uint64_t) ((unsigned char *) (&x))[7]) << 0*8) | \
(((uint64_t) ((unsigned char *) (&x))[6]) << 1*8) | \
(((uint64_t) ((unsigned char *) (&x))[5]) << 2*8) | \
(((uint64_t) ((unsigned char *) (&x))[4]) << 3*8) | \
(((uint64_t) ((unsigned char *) (&x))[3]) << 4*8) | \
(((uint64_t) ((unsigned char *) (&x))[2]) << 5*8) | \
(((uint64_t) ((unsigned char *) (&x))[1]) << 6*8) | \
(((uint64_t) ((unsigned char *) (&x))[0]) << 7*8) )
#endif
/**
* @brief Convert a 64bit number from network byte order to host byte order.
*/
inline uint64_t ntohll(uint64_t arg) {
return ntohll_macro(arg);
}
/**
* General purpose variable size array of bytes.
*/
class growing_buffer {
public:
/**
* @brief Construct empty buffer. Memory is allocated after first append.
*/
growing_buffer();
/**
* @brief copy constructor
*/
growing_buffer(const growing_buffer& other) throw(std::bad_alloc);
~growing_buffer();
/**
* @brief Append data to buffer.
* @param size Number of bytes to add.
* @param data Array of @a size bytes to be append to buffer
* @return Pointer to the first byte of newly appended data.
*/
char *append(size_t size, const void *data) throw(std::bad_alloc);
/**
* @brief Append newly allocated bytes to the buffer. Value of these bytes is undefined.
* @param size Number of bytes to add.
* @return Pointer to the first byte of newly appended data.
*/
char *append_blank(size_t size) throw(std::bad_alloc);
/**
* @brief Change buffer size to zero.
*/
void empty();
/**
* @brief Allocate buffer at once.
* @param new_size New size of the buffer. Must be greater than the old size.
*/
void allocate(size_t new_size) throw(std::bad_alloc);
/**
* @brief Get pointer to buffer data.
* @param index Position in the buffer. Must be less than buffer size.
* @return Pointer to the requested byte.
*/
char *access(size_t index);
/**
* @brief Get current size of the buffer.
* @return Current size of the buffer.
*/
size_t get_size();
private:
static const size_t default_size = 128;
size_t allocated;
size_t size;
char *data;
};
/**
* @brief Create directory with all its parents if needed.
* @param path Path to the directory to be created.
* @param mode Access mode for the directory.
* @return true if the directory was created.
*/
bool mkdir_parents(const char *path, mode_t mode);
#endif