CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
Ardupilot

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: Ardupilot/ardupilot
Path: blob/master/libraries/AP_Common/ExpandingString.h
Views: 1798
1
/*
2
This program is free software: you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation, either version 3 of the License, or
5
(at your option) any later version.
6
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
11
12
You should have received a copy of the GNU General Public License
13
along with this program. If not, see <http://www.gnu.org/licenses/>.
14
*/
15
/*
16
expanding string for easy construction of text buffers
17
*/
18
19
#pragma once
20
21
#include <AP_Common/AP_Common.h>
22
23
#include <stdint.h>
24
25
class ExpandingString {
26
public:
27
ExpandingString() : buf(0), buflen(0), used(0), allocation_failed(false), external_buffer(false) {}
28
ExpandingString(char* s, uint32_t total_len);
29
30
const char *get_string(void) const {
31
return buf;
32
}
33
uint32_t get_length(void) const {
34
return used;
35
}
36
char *get_writeable_string(void) const {
37
return buf;
38
}
39
40
// print into the string
41
void printf(const char *format, ...) FMT_PRINTF(2,3);
42
43
// append data to the string. s can be null for zero fill
44
bool append(const char *s, uint32_t len);
45
46
// set address to custom external buffer
47
void set_buffer(char *s, uint32_t total_len, uint32_t used_len);
48
// zero out the string
49
void reset() { used = 0; }
50
51
// destructor
52
~ExpandingString();
53
54
bool has_failed_allocation() const {
55
return allocation_failed;
56
}
57
58
private:
59
char *buf;
60
uint32_t buflen;
61
uint32_t used;
62
bool allocation_failed;
63
bool external_buffer;
64
65
// try to expand the buffer
66
bool expand(uint32_t min_needed) WARN_IF_UNUSED;
67
};
68
69