Repository navigation
Expand file tree
/
Copy pathstr.c
More file actions
243 lines (213 loc) · 5.48 KB
/
Copy pathstr.c
File metadata and controls
243 lines (213 loc) · 5.48 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Alertik: a tiny 'syslog' server & notification tool for Mikrotik routers.
* This is free and unencumbered software released into the public domain.
*/
/*
* String/append buffer implementation based on Aqua:
* https://gist.github.com/Theldus/09ed2205aa5ba15cdf4571b71cd1c8fc
*/
#include "str.h"
#include "log.h"
/* Malloc is only used if AB_USE_MALLOC is defined. */
#ifdef AB_USE_MALLOC
#if defined(AB_CALLOC) && defined(AB_REALLOC) && defined(AB_FREE)
# define AB_USE_STDLIB
#elif !defined(AB_CALLOC) && !defined(AB_REALLOC) && !defined(AB_FREE)
# define AB_USE_STDLIB
#else
#error "For custom memory allocators, you should define all three routines!"
#error "Please define: AB_CALLOC, AB_REALLOC and AB_FREE!"
#endif
#endif
#ifndef AB_CALLOC
#define AB_CALLOC(nmemb,sz) calloc((nmemb),(sz))
#define AB_REALLOC(p,newsz) realloc((p),(newsz))
#define AB_FREE(p) free((p))
#endif
#ifdef AB_USE_STDLIB
#include <stdlib.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
/* ========================================================================= */
/* BUFFER ROUTINES */
/* ========================================================================= */
#ifdef AB_USE_MALLOC
/**
* @brief Rounds up to the next power of two.
*
* @param target Target number to be rounded.
*
* @return Returns the next power of two.
*/
static size_t next_power(size_t target)
{
target--;
target |= target >> 1;
target |= target >> 2;
target |= target >> 4;
target |= target >> 8;
target |= target >> 16;
target++;
return (target);
}
#endif
/**
* @brief Checks if the new size fits in the append buffer, if not,
* reallocates the buffer size by @p incr bytes.
*
* If the macro AB_USE_MALLOC is not defined (default), this only
* checks if the new size fits the buffer.
*
* @param sh Aqua highlight context.
* @param incr Size (in bytes) to be incremented.
*
* @return Returns 0 if success, -1 otherwise.
*
* @note The new size is the next power of two, that is capable
* to hold the required buffer size.
*/
static int increase_buff(struct str_ab *sh, size_t incr)
{
#ifndef AB_USE_MALLOC
if (sh->pos + incr >= MAX_LINE) {
log_msg("(increase buffer) Unable to fit appended message!\n");
log_msg("(static storage) incr: %zu, buff_len: %zu, pos: %zu\n",
incr, sh->pos, sh->buff_len);
return (-1);
}
#else
char *new;
size_t new_size;
if (sh->pos + incr >= sh->buff_len)
{
new_size = next_power(sh->buff_len + incr);
new = AB_REALLOC(sh->buff, new_size);
if (new == NULL)
{
AB_FREE(sh->buff);
sh->buff = NULL;
log_msg("(increase buffer) Unable to fit appended message!\n");
log_msg("(realloc storage) incr: %zu, buff_len: %zu, pos: %zu\n",
incr, sh->pos, sh->buff_len);
return (-1);
}
sh->buff_len = new_size;
sh->buff = new;
}
#endif
return (0);
}
/**
* @brief Initializes the append buffer context.
*
* @param ab Append buffer structure.
*
* @return Returns 0 if success, -1 otherwise.
*/
int ab_init(struct str_ab *ab)
{
if (!ab)
return (-1);
memset(ab, 0, sizeof(*ab));
#ifndef AB_USE_MALLOC
ab->buff_len = MAX_LINE;
#else
ab->buff = AB_CALLOC(MAX_LINE, 1);
if (!ab->buff)
return (-1);
ab->buff_len = MAX_LINE;
#endif
return (0);
}
/**
* @brief Append a given char @p c into the buffer.
*
* @param sh Aqua highlight context.
* @param c Char to be appended.
*
* @return Returns 0 if success, -1 otherwise.
*/
int ab_append_chr(struct str_ab *sh, char c)
{
if (increase_buff(sh, 2) < 0)
return (-1);
sh->buff[sh->pos + 0] = c;
sh->buff[sh->pos + 1] = '\0';
sh->pos++;
return (0);
}
/**
* @brief Appends a given string pointed by @p s of size @p len
* into the current buffer.
*
* If @p len is 0, the string is assumed to be null-terminated
* and its length is obtained.
*
* @param ab Append buffer context.
* @param s String to be append into the buffer.
* @param len String size, if 0, it's length is obtained.
*
* @return Returns 0 if success, -1 otherwise.
*/
int ab_append_str(struct str_ab *ab, const char *s, size_t len)
{
if (!len)
len = strlen(s);
if (increase_buff(ab, len + 1) < 0)
return (-1);
memcpy(ab->buff + ab->pos, s, len);
ab->pos += len;
ab->buff[ab->pos] = '\0';
return (0);
}
/**
* @brief Appends a given formatted string pointed by @p fmt.
*
* @param ab Append buffer context.
* @param fmt Formatted string to be appended.
*
* @return Returns 0 if success, -1 otherwise.
*/
int ab_append_fmt(struct str_ab *ab, const char *fmt, ...)
{
int str_len, ab_len, orig_ab_pos;
char *buff_st;
va_list ap;
buff_st = ab->buff + ab->pos;
ab_len = ab->buff_len - ab->pos;
va_start(ap, fmt);
str_len = vsnprintf(buff_st, ab_len, fmt, ap);
if (str_len < 0) {
log_msg("Unable to fit appended message!\n");
return (-1);
}
va_end(ap);
/* If it fits, just happily returns. */
if (str_len + 1 <= ab_len) {
ab->pos += str_len;
return (0);
}
/* Otherwise, adjust current pos and try to increase buffer. */
else {
orig_ab_pos = ab->pos;
/* temporarily advance our buffer
* to trick our increase buffer. */
ab->pos = ab->buff_len;
if (increase_buff(ab, (str_len + 1) - ab_len))
return (-1);
ab->pos = orig_ab_pos;
}
buff_st = ab->buff + ab->pos;
ab_len = ab->buff_len - ab->pos;
va_start(ap, fmt);
str_len = vsnprintf(buff_st, ab_len, fmt, ap);
if (str_len < 0 || (str_len + 1) > ab_len) {
log_msg("Unable to fit appended message!\n");
return (-1);
}
va_end(ap);
ab->pos += str_len;
return (0);
}