Skip to content

Commit 3aff98e

Browse files
committed
preliminary cleanup for split_str_once refactor
String processing enhancement prerequisites for the upcoming `split_str_once` PR. 1. Add overloads of some string processing functions with a length parameter, to allow processing of substrings of C-strings 2. Add overloads of gr_printf that work with string lengths 3. Add constructors to `HUD_message_data` and `line_node` 4. Rename "size" to "len" in `vm_strndup` for clarity 5. Fill in some string attribute annotations 6. Make the rest of the HUD render functions `const` 7. Removed unneeded circular #includes in code/globalincs/memory/
1 parent 5fc84d5 commit 3aff98e

13 files changed

Lines changed: 135 additions & 101 deletions

File tree

code/globalincs/memory/memory.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#include <cstddef>
55
#include <cstdlib>
66

7-
#include "globalincs/pstypes.h"
8-
97
namespace memory
108
{
119
struct quiet_alloc_t { quiet_alloc_t(){} };

code/globalincs/memory/utils.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22

33
#include <cstring>
44

5-
#include "globalincs/pstypes.h"
6-
7-
inline char *vm_strndup(const char *ptr, size_t size)
5+
inline char *vm_strndup(const char *ptr, size_t len)
86
{
9-
char *dst = (char *) vm_malloc(size + 1);
7+
char *dst = static_cast<char *>(vm_malloc(len + 1));
108

119
if (!dst)
12-
return NULL;
10+
return nullptr;
1311

14-
std::strncpy(dst, ptr, size);
12+
std::strncpy(dst, ptr, len);
1513
// make sure it has a NULL terminiator
16-
dst[size] = '\0';
14+
dst[len] = '\0';
1715

1816
return dst;
1917
}

code/globalincs/toolchain/clang.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#if defined(__clang__)
2222

2323
#define SCP_FORMAT_STRING
24+
// from gcc: Since non-static C++ methods have an implicit this argument, the arguments of such methods should be counted from two, not one, when giving values for string-index and first-to-check.
2425
#define SCP_FORMAT_STRING_ARGS(x,y) __attribute__((format(printf, x, y)))
2526

2627
#define __UNUSED __attribute__((__unused__))

code/globalincs/toolchain/gcc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#if defined(__GNUC__) && !defined(__clang__)
2323

2424
#define SCP_FORMAT_STRING
25+
// from gcc: Since non-static C++ methods have an implicit this argument, the arguments of such methods should be counted from two, not one, when giving values for string-index and first-to-check.
2526
#define SCP_FORMAT_STRING_ARGS(x,y) __attribute__((format(printf, x, y)))
2627

2728
#define __UNUSED __attribute__((__unused__))

code/globalincs/toolchain/mingw.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <stdio.h>
2424

2525
#define SCP_FORMAT_STRING
26+
// from gcc: Since non-static C++ methods have an implicit this argument, the arguments of such methods should be counted from two, not one, when giving values for string-index and first-to-check.
2627
#define SCP_FORMAT_STRING_ARGS(x,y) __attribute__((format(__MINGW_PRINTF_FORMAT, x, y)))
2728

2829
#define __UNUSED __attribute__((__unused__))

code/graphics/2d.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,13 +1043,17 @@ bool gr_resize_screen_posf(float *x, float *y, float *w = NULL, float *h = NULL,
10431043
// Does formatted printing. This calls gr_string after formatting,
10441044
// so if you don't need to format the string, then call gr_string
10451045
// directly.
1046-
extern void gr_printf( int x, int y, const char * format, SCP_FORMAT_STRING ... ) SCP_FORMAT_STRING_ARGS(3, 4);
1046+
extern void gr_printf(int x, int y, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(3, 4);
1047+
extern void gr_printf(int x, int y, size_t len, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(4, 5);
10471048
// same as gr_printf but positions text correctly in menus
1048-
extern void gr_printf_menu( int x, int y, const char * format, SCP_FORMAT_STRING ... ) SCP_FORMAT_STRING_ARGS(3, 4);
1049+
extern void gr_printf_menu(int x, int y, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(3, 4);
1050+
extern void gr_printf_menu(int x, int y, size_t len, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(4, 5);
10491051
// same as gr_printf_menu but accounts for menu zooming
1050-
extern void gr_printf_menu_zoomed( int x, int y, const char * format, SCP_FORMAT_STRING ... ) SCP_FORMAT_STRING_ARGS(3, 4);
1052+
extern void gr_printf_menu_zoomed(int x, int y, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(3, 4);
1053+
extern void gr_printf_menu_zoomed(int x, int y, size_t len, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(4, 5);
10511054
// same as gr_printf but doesn't resize for non-standard resolutions
1052-
extern void gr_printf_no_resize( int x, int y, const char * format, SCP_FORMAT_STRING ... ) SCP_FORMAT_STRING_ARGS(3, 4);
1055+
extern void gr_printf_no_resize(int x, int y, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(3, 4);
1056+
extern void gr_printf_no_resize(int x, int y, size_t len, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(4, 5);
10531057

10541058
// Returns the size of the string in pixels in w and h
10551059
extern void gr_get_string_size( int *w, int *h, const char * text, float scaleMultiplier = 1.0f, size_t len = std::string::npos);

code/graphics/software/font.cpp

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -801,60 +801,81 @@ void gr_string_win(int x, int y, char *s)
801801

802802
#endif // ifdef _WIN32
803803

804-
char grx_printf_text[2048];
804+
static char grx_printf_text[2048];
805805

806-
void gr_printf(int x, int y, const char * format, ...)
806+
void gr_printf_args(int resize_mode, int x, int y, size_t len, SCP_FORMAT_STRING const char *format, va_list args)
807807
{
808-
va_list args;
808+
if (!FontManager::isReady())
809+
return;
809810

810-
if (!FontManager::isReady()) return;
811+
len = std::min(len, sizeof(grx_printf_text) - 1);
811812

812-
va_start(args, format);
813-
vsnprintf(grx_printf_text, sizeof(grx_printf_text), format, args);
814-
va_end(args);
815-
grx_printf_text[sizeof(grx_printf_text) - 1] = '\0';
813+
vsnprintf(grx_printf_text, len+1, format, args);
814+
grx_printf_text[len] = '\0';
816815

817-
gr_string(x, y, grx_printf_text);
816+
gr_string(x, y, grx_printf_text, resize_mode, 1.0f, len);
818817
}
819818

820-
void gr_printf_menu(int x, int y, const char * format, ...)
819+
void gr_printf(int x, int y, SCP_FORMAT_STRING const char *format, ...)
821820
{
822821
va_list args;
823-
824-
if (!FontManager::isReady()) return;
825-
826822
va_start(args, format);
827-
vsnprintf(grx_printf_text, sizeof(grx_printf_text), format, args);
823+
gr_printf_args(GR_RESIZE_FULL, x, y, std::string::npos, format, args);
828824
va_end(args);
829-
grx_printf_text[sizeof(grx_printf_text) - 1] = '\0';
830-
831-
gr_string(x, y, grx_printf_text, GR_RESIZE_MENU);
832825
}
833826

834-
void gr_printf_menu_zoomed(int x, int y, const char * format, ...)
827+
void gr_printf(int x, int y, size_t len, SCP_FORMAT_STRING const char *format, ...)
835828
{
836829
va_list args;
830+
va_start(args, format);
831+
gr_printf_args(GR_RESIZE_FULL, x, y, len, format, args);
832+
va_end(args);
833+
}
837834

838-
if (!FontManager::isReady()) return;
839-
835+
void gr_printf_menu(int x, int y, SCP_FORMAT_STRING const char *format, ...)
836+
{
837+
va_list args;
840838
va_start(args, format);
841-
vsnprintf(grx_printf_text, sizeof(grx_printf_text), format, args);
839+
gr_printf_args(GR_RESIZE_MENU, x, y, std::string::npos, format, args);
842840
va_end(args);
843-
grx_printf_text[sizeof(grx_printf_text) - 1] = '\0';
841+
}
844842

845-
gr_string(x, y, grx_printf_text, GR_RESIZE_MENU_ZOOMED);
843+
void gr_printf_menu(int x, int y, size_t len, SCP_FORMAT_STRING const char *format, ...)
844+
{
845+
va_list args;
846+
va_start(args, format);
847+
gr_printf_args(GR_RESIZE_MENU, x, y, len, format, args);
848+
va_end(args);
846849
}
847850

848-
void gr_printf_no_resize(int x, int y, const char * format, ...)
851+
void gr_printf_menu_zoomed(int x, int y, SCP_FORMAT_STRING const char *format, ...)
849852
{
850853
va_list args;
854+
va_start(args, format);
855+
gr_printf_args(GR_RESIZE_MENU_ZOOMED, x, y, std::string::npos, format, args);
856+
va_end(args);
857+
}
851858

852-
if (!FontManager::isReady()) return;
859+
void gr_printf_menu_zoomed(int x, int y, size_t len, SCP_FORMAT_STRING const char *format, ...)
860+
{
861+
va_list args;
862+
va_start(args, format);
863+
gr_printf_args(GR_RESIZE_MENU_ZOOMED, x, y, len, format, args);
864+
va_end(args);
865+
}
853866

867+
void gr_printf_no_resize(int x, int y, SCP_FORMAT_STRING const char *format, ...)
868+
{
869+
va_list args;
854870
va_start(args, format);
855-
vsnprintf(grx_printf_text, sizeof(grx_printf_text), format, args);
871+
gr_printf_args(GR_RESIZE_NONE, x, y, std::string::npos, format, args);
856872
va_end(args);
857-
grx_printf_text[sizeof(grx_printf_text) - 1] = '\0';
873+
}
858874

859-
gr_string(x, y, grx_printf_text, GR_RESIZE_NONE);
875+
void gr_printf_no_resize(int x, int y, size_t len, SCP_FORMAT_STRING const char *format, ...)
876+
{
877+
va_list args;
878+
va_start(args, format);
879+
gr_printf_args(GR_RESIZE_NONE, x, y, len, format, args);
880+
va_end(args);
860881
}

code/hud/hud.cpp

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,12 @@ void HudGauge::render(float /*frametime*/, bool config)
896896
}
897897
}
898898

899-
void HudGauge::renderString(int x, int y, const char *str, float scale, bool config)
899+
void HudGauge::renderString(int x, int y, const char *str, float scale, bool config) const
900+
{
901+
renderString(x, y, str, std::string::npos, scale, config);
902+
}
903+
904+
void HudGauge::renderString(int x, int y, const char *str, size_t len, float scale, bool config) const
900905
{
901906
int nx = 0, ny = 0;
902907
int resize = GR_RESIZE_FULL;
@@ -923,15 +928,20 @@ void HudGauge::renderString(int x, int y, const char *str, float scale, bool con
923928
if (HUD_shadows) {
924929
color cur = gr_screen.current_color;
925930
gr_set_color_fast(&Color_black);
926-
gr_string(x + nx + 1, y + ny + 1, str, resize, scale);
931+
gr_string(x + nx + 1, y + ny + 1, str, resize, scale, len);
927932
gr_set_color_fast(&cur);
928933
}
929934

930-
gr_string(x + nx, y + ny, str, resize, scale);
935+
gr_string(x + nx, y + ny, str, resize, scale, len);
931936
gr_reset_screen_scale();
932937
}
933938

934-
void HudGauge::renderString(int x, int y, int gauge_id, const char *str, float scale, bool config)
939+
void HudGauge::renderString(int x, int y, int gauge_id, const char *str, float scale, bool config) const
940+
{
941+
renderString(x, y, gauge_id, str, std::string::npos, scale, config);
942+
}
943+
944+
void HudGauge::renderString(int x, int y, int gauge_id, const char *str, size_t len, float scale, bool config) const
935945
{
936946
int nx = 0, ny = 0;
937947
int resize = GR_RESIZE_FULL;
@@ -960,32 +970,37 @@ void HudGauge::renderString(int x, int y, int gauge_id, const char *str, float s
960970
if (HUD_shadows) {
961971
color cur = gr_screen.current_color;
962972
gr_set_color_fast(&Color_black);
963-
emp_hud_string(x + nx + 1, y + ny + 1, gauge_id, str, resize, scale);
973+
emp_hud_string(x + nx + 1, y + ny + 1, gauge_id, str, len, resize, scale);
964974
gr_set_color_fast(&cur);
965975
}
966-
emp_hud_string(x + nx, y + ny, gauge_id, str, resize, scale);
976+
emp_hud_string(x + nx, y + ny, gauge_id, str, len, resize, scale);
967977
} else {
968978
if (HUD_shadows) {
969979
color cur = gr_screen.current_color;
970980
gr_set_color_fast(&Color_black);
971-
gr_string(x + nx + 1, y + ny + 1, str, resize, scale);
981+
gr_string(x + nx + 1, y + ny + 1, str, resize, scale, len);
972982
gr_set_color_fast(&cur);
973983
}
974-
gr_string(x + nx, y + ny, str, resize, scale);
984+
gr_string(x + nx, y + ny, str, resize, scale, len);
975985
}
976986

977987
gr_reset_screen_scale();
978988
}
979989

980-
void HudGauge::renderStringAlignCenter(int x, int y, int area_width, const char *s, float scale, bool config)
990+
void HudGauge::renderStringAlignCenter(int x, int y, int area_width, const char *s, float scale, bool config) const
991+
{
992+
renderStringAlignCenter(x, y, area_width, s, std::string::npos, scale, config);
993+
}
994+
995+
void HudGauge::renderStringAlignCenter(int x, int y, int area_width, const char *s, size_t len, float scale, bool config) const
981996
{
982997
int w, h;
983998

984-
gr_get_string_size(&w, &h, s, scale);
985-
renderString(x + ((area_width - w) / 2), y, s, scale, config);
999+
gr_get_string_size(&w, &h, s, scale, len);
1000+
renderString(x + ((area_width - w) / 2), y, s, len, scale, config);
9861001
}
9871002

988-
void HudGauge::renderPrintf(int x, int y, float scale, bool config, const char* format, ...)
1003+
void HudGauge::renderPrintf(int x, int y, float scale, bool config, SCP_FORMAT_STRING const char* format, ...) const
9891004
{
9901005
SCP_string tmp;
9911006
va_list args;
@@ -998,7 +1013,7 @@ void HudGauge::renderPrintf(int x, int y, float scale, bool config, const char*
9981013
renderString(x, y, tmp.c_str(), scale, config);
9991014
}
10001015

1001-
void HudGauge::renderPrintfWithGauge(int x, int y, int gauge_id, float scale, bool config, const char* format, ...)
1016+
void HudGauge::renderPrintfWithGauge(int x, int y, int gauge_id, float scale, bool config, SCP_FORMAT_STRING const char* format, ...) const
10021017
{
10031018
SCP_string tmp;
10041019
va_list args;

code/hud/hud.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,14 @@ class HudGauge
366366
void renderBitmap(int frame, int x, int y, float scale = 1.0f, bool config = false) const;
367367
void renderBitmapColor(int frame, int x, int y, float scale = 1.0f, bool config = false) const;
368368
void renderBitmapEx(int frame, int x, int y, int w, int h, int sx, int sy, float scale = 1.0f, bool config = false) const;
369-
void renderString(int x, int y, const char *str, float scale = 1.0f, bool config = false);
370-
void renderString(int x, int y, int gauge_id, const char *str, float scale = 1.0f, bool config = false);
371-
void renderStringAlignCenter(int x, int y, int area_width, const char *s, float scale = 1.0f, bool config = false);
372-
void renderPrintf(int x, int y, float scale, bool config, SCP_FORMAT_STRING const char* format, ...) SCP_FORMAT_STRING_ARGS(6, 7);
373-
void renderPrintfWithGauge(int x, int y, int gauge_id, float scale, bool config, SCP_FORMAT_STRING const char* format, ...) SCP_FORMAT_STRING_ARGS(7, 8);
369+
void renderString(int x, int y, const char *str, float scale = 1.0f, bool config = false) const;
370+
void renderString(int x, int y, const char *str, size_t len, float scale = 1.0f, bool config = false) const;
371+
void renderString(int x, int y, int gauge_id, const char *str, float scale = 1.0f, bool config = false) const;
372+
void renderString(int x, int y, int gauge_id, const char *str, size_t len, float scale = 1.0f, bool config = false) const;
373+
void renderStringAlignCenter(int x, int y, int area_width, const char *s, float scale = 1.0f, bool config = false) const;
374+
void renderStringAlignCenter(int x, int y, int area_width, const char *s, size_t len, float scale = 1.0f, bool config = false) const;
375+
void renderPrintf(int x, int y, float scale, bool config, SCP_FORMAT_STRING const char* format, ...) const SCP_FORMAT_STRING_ARGS(6, 7);
376+
void renderPrintfWithGauge(int x, int y, int gauge_id, float scale, bool config, SCP_FORMAT_STRING const char* format, ...) const SCP_FORMAT_STRING_ARGS(7, 8);
374377
void renderLine(int x1, int y1, int x2, int y2, bool config = false) const;
375378
void renderGradientLine(int x1, int y1, int x2, int y2, bool config = false) const;
376379
void renderRect(int x, int y, int w, int h, bool config = false) const;

code/hud/hudmessage.cpp

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,9 @@ void HudGaugeMessages::processMessageBuffer()
331331

332332
void HudGaugeMessages::addPending(const char *text, int source, int x)
333333
{
334-
Assert(text != NULL);
334+
Assert(text != nullptr);
335335

336-
HUD_message_data new_message;
337-
338-
new_message.text = text;
339-
new_message.source = source;
340-
new_message.x = x;
341-
342-
pending_messages.push(new_message);
336+
pending_messages.emplace(text, source, x);
343337
}
344338

345339
void HudGaugeMessages::scrollMessages()
@@ -484,7 +478,7 @@ void HudGaugeMessages::render(float /*frametime*/, bool config)
484478
}
485479

486480
// Similar to HUD printf, but shows only one message at a time, at a fixed location.
487-
void HUD_fixed_printf(float duration, color col, const char *format, ...)
481+
void HUD_fixed_printf(float duration, color col, SCP_FORMAT_STRING const char *format, ...)
488482
{
489483
va_list args;
490484

@@ -525,8 +519,7 @@ int HUD_source_get_team(int source)
525519
return source - HUD_SOURCE_TEAM_OFFSET;
526520
}
527521

528-
529-
void HUD_printf(const char *format, ...)
522+
void HUD_printf(SCP_FORMAT_STRING const char *format, ...)
530523
{
531524
va_list args;
532525
SCP_string tmp;
@@ -551,7 +544,7 @@ void HUD_printf(const char *format, ...)
551544
// message on the HUD. Text is split into multiple lines if width exceeds msg display area
552545
// width. 'source' is used to indicate who send the message, and is used to color code text.
553546
//
554-
void HUD_sourced_printf(int source, const char *format, ...)
547+
void HUD_sourced_printf(int source, SCP_FORMAT_STRING const char *format, ...)
555548
{
556549
va_list args;
557550
SCP_string tmp;
@@ -579,13 +572,7 @@ void hud_sourced_print(int source, const SCP_string &msg)
579572
// add message to the scrollback log first
580573
hud_add_msg_to_scrollback(msg.c_str(), source, Missiontime);
581574

582-
HUD_message_data new_msg;
583-
584-
new_msg.text = msg;
585-
new_msg.source = source;
586-
new_msg.x = 0;
587-
588-
HUD_msg_buffer.push_back(new_msg);
575+
HUD_msg_buffer.emplace_back(msg, source, 0);
589576

590577
// Invoke the scripting hook
591578
if (OnHudMessageReceivedHook->isActive()) {
@@ -604,13 +591,7 @@ void hud_sourced_print(int source, const char *msg)
604591
// add message to the scrollback log first
605592
hud_add_msg_to_scrollback(msg, source, Missiontime);
606593

607-
HUD_message_data new_msg;
608-
609-
new_msg.text = SCP_string(msg);
610-
new_msg.source = source;
611-
new_msg.x = 0;
612-
613-
HUD_msg_buffer.push_back(new_msg);
594+
HUD_msg_buffer.emplace_back(msg, source, 0);
614595

615596
// Invoke the scripting hook
616597
if (OnHudMessageReceivedHook->isActive()) {
@@ -663,10 +644,7 @@ void hud_add_msg_to_scrollback(const char *text, int source, int t)
663644
}
664645

665646
// create the new node for the vector
666-
line_node newLine = {t, The_mission.HUD_timer_padding, source, 0, 1, w, ""};
667-
newLine.text = text;
668-
669-
Msg_scrollback_vec.push_back(newLine);
647+
Msg_scrollback_vec.emplace_back(t, The_mission.HUD_timer_padding, source, 0, 1, w, text);
670648
}
671649

672650
// how many lines to skip

0 commit comments

Comments
 (0)