2015-08-11 03:28:43 -03:00
|
|
|
#include "AP_HAL.h"
|
2013-09-21 01:25:42 -03:00
|
|
|
#include "Util.h"
|
|
|
|
#include "utility/print_vprintf.h"
|
|
|
|
|
|
|
|
/* Helper class implements AP_HAL::Print so we can use utility/vprintf */
|
|
|
|
class BufferPrinter : public AP_HAL::Print {
|
|
|
|
public:
|
|
|
|
BufferPrinter(char* str, size_t size) : _offs(0), _str(str), _size(size) {}
|
|
|
|
size_t write(uint8_t c) {
|
|
|
|
if (_offs < _size) {
|
|
|
|
_str[_offs] = c;
|
|
|
|
_offs++;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2013-10-02 04:36:01 -03:00
|
|
|
size_t write(const uint8_t *buffer, size_t size) {
|
|
|
|
size_t n = 0;
|
|
|
|
while (size--) {
|
|
|
|
n += write(*buffer++);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2013-09-21 01:25:42 -03:00
|
|
|
size_t _offs;
|
|
|
|
char* const _str;
|
|
|
|
const size_t _size;
|
|
|
|
};
|
|
|
|
|
|
|
|
int AP_HAL::Util::snprintf(char* str, size_t size, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
2013-09-21 02:45:05 -03:00
|
|
|
int res = vsnprintf(str, size, format, ap);
|
2013-09-21 01:25:42 -03:00
|
|
|
va_end(ap);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AP_HAL::Util::vsnprintf(char* str, size_t size, const char *format, va_list ap)
|
|
|
|
{
|
|
|
|
BufferPrinter buf(str, size);
|
|
|
|
print_vprintf(&buf, 0, format, ap);
|
|
|
|
// null terminate if possible
|
2013-09-21 02:45:05 -03:00
|
|
|
int ret = buf._offs;
|
2013-09-21 01:25:42 -03:00
|
|
|
buf.write(0);
|
2013-09-21 02:45:05 -03:00
|
|
|
return ret;
|
2013-09-21 01:25:42 -03:00
|
|
|
}
|