Use std::array for console_buffer::line

This commit is contained in:
Kp 2022-09-24 17:47:52 +00:00
parent 23315ebec2
commit ae5e5c699a
3 changed files with 10 additions and 9 deletions

View file

@ -8,6 +8,7 @@
#pragma once
#include <array>
#include <cstddef>
#include <cstring>
#include <type_traits>
@ -36,8 +37,8 @@ constexpr std::integral_constant<std::size_t, 2048> CON_LINE_LENGTH{};
struct console_buffer
{
char line[CON_LINE_LENGTH];
int priority;
std::array<char, CON_LINE_LENGTH> line;
};
/* Define to 1 to capture the __FILE__, __LINE__ of callers to

View file

@ -29,7 +29,7 @@ public:
template <std::size_t N>
using scratch_buffer = std::false_type;
template <std::size_t N>
static std::span<char, N> insert_location_leader(char (&buffer)[N])
static std::span<char, N> insert_location_leader(std::array<char, N> &buffer)
{
return buffer;
}
@ -63,9 +63,9 @@ public:
* place further text.
*/
template <std::size_t N>
std::span<char> insert_location_leader(char (&buffer)[N]) const
std::span<char> insert_location_leader(std::array<char, N> &buffer) const
{
const auto written = std::snprintf(buffer, sizeof(buffer), "%s:%u: ", file, line);
const auto written = std::snprintf(buffer.data(), buffer.size(), "%s:%u: ", file, line);
return std::span(buffer).subspan(written);
}
/* Return a span describing the written area that the caller can read

View file

@ -85,7 +85,7 @@ static void con_add_buffer_line(const con_priority priority, const char *const b
console_buffer &c = con_buffer.back();
c.priority=priority;
size_t copy = std::min(len, CON_LINE_LENGTH - 1);
size_t copy = std::min(len, std::size(c.line) - 1);
c.line[copy] = 0;
memcpy(&c.line,buffer, copy);
}
@ -95,15 +95,15 @@ static void con_add_buffer_line(const con_priority priority, const char *const b
void (con_printf)(const con_priority_wrapper priority, const char *const fmt, ...)
{
va_list arglist;
char buffer[CON_LINE_LENGTH];
if (priority <= CGameArg.DbgVerbose)
{
va_start (arglist, fmt);
std::array<char, CON_LINE_LENGTH> buffer;
auto &&leader = priority.insert_location_leader(buffer);
const std::size_t len = std::max(vsnprintf(leader.data(), leader.size(), fmt, arglist), 0);
va_end (arglist);
con_force_puts(priority, buffer, len);
con_force_puts(priority, buffer.data(), len);
}
}
@ -318,9 +318,9 @@ static void con_draw(grs_canvas &canvas)
{
auto &b = con_buffer[CON_LINES_MAX - 1 - i];
gr_set_fontcolor(canvas, get_console_color_by_priority(b.priority), -1);
const auto &&[w, h] = gr_get_string_size(game_font, b.line);
const auto &&[w, h] = gr_get_string_size(game_font, b.line.data());
y -= h + fspacy1;
gr_string(canvas, game_font, fspacx1, y, b.line, w, h);
gr_string(canvas, game_font, fspacx1, y, b.line.data(), w, h);
i++;
if (y<=0 || CON_LINES_MAX-1-i <= 0 || i < 0)