2020-12-30 02:22:58 -04:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
expanding string for easy construction of text buffers
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-02-28 21:19:08 -04:00
|
|
|
#include <AP_Common/AP_Common.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2020-12-30 02:22:58 -04:00
|
|
|
|
|
|
|
class ExpandingString {
|
|
|
|
public:
|
2024-01-27 12:36:35 -04:00
|
|
|
ExpandingString() : buf(0), buflen(0), used(0), allocation_failed(false), external_buffer(false) {}
|
|
|
|
ExpandingString(char* s, uint32_t total_len);
|
2020-12-30 02:22:58 -04:00
|
|
|
|
|
|
|
const char *get_string(void) const {
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
uint32_t get_length(void) const {
|
|
|
|
return used;
|
|
|
|
}
|
2021-01-16 00:59:55 -04:00
|
|
|
char *get_writeable_string(void) const {
|
|
|
|
return buf;
|
|
|
|
}
|
2020-12-30 02:22:58 -04:00
|
|
|
|
|
|
|
// print into the string
|
|
|
|
void printf(const char *format, ...) FMT_PRINTF(2,3);
|
|
|
|
|
2021-04-05 03:53:29 -03:00
|
|
|
// append data to the string. s can be null for zero fill
|
|
|
|
bool append(const char *s, uint32_t len);
|
2020-12-30 02:22:58 -04:00
|
|
|
|
2021-11-09 06:42:22 -04:00
|
|
|
// set address to custom external buffer
|
|
|
|
void set_buffer(char *s, uint32_t total_len, uint32_t used_len);
|
2021-09-14 17:29:31 -03:00
|
|
|
// zero out the string
|
|
|
|
void reset() { used = 0; }
|
2021-11-09 06:42:22 -04:00
|
|
|
|
2020-12-30 02:22:58 -04:00
|
|
|
// destructor
|
|
|
|
~ExpandingString();
|
|
|
|
|
|
|
|
bool has_failed_allocation() const {
|
|
|
|
return allocation_failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
char *buf;
|
|
|
|
uint32_t buflen;
|
|
|
|
uint32_t used;
|
|
|
|
bool allocation_failed;
|
2021-11-09 06:42:22 -04:00
|
|
|
bool external_buffer;
|
2020-12-30 02:22:58 -04:00
|
|
|
|
|
|
|
// try to expand the buffer
|
|
|
|
bool expand(uint32_t min_needed) WARN_IF_UNUSED;
|
|
|
|
};
|