MCPcopy Create free account
hub / github.com/AutoHotkey/AutoHotkey / sntprintfcat

Function sntprintfcat

source/util.cpp:486–512  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

484
485
486int sntprintfcat(LPTSTR aBuf, int aBufSize, LPCTSTR aFormat, ...)
487// aBufSize is an int so that any negative values passed in from caller are not lost.
488// aBuf will always be terminated here except when the amount of space left in the buffer is zero or less.
489// (in which case the caller should already have terminated it). If aBufSize is greater than zero but not
490// large enough to hold the entire result, as much of the result as possible is copied and the return value
491// is space_remaining - 1.
492// The caller must have ensured that aBuf and aFormat are non-NULL and that aBuf contains a valid string
493// (i.e. that it is null-terminated somewhere within the limits of aBufSize).
494// Returns the exact number of characters written, not including the zero terminator. A negative
495// number is never returned, even if aBufSize is <= zero (which means there isn't even enough space left
496// to write a zero terminator), under the assumption that the caller has already terminated the string
497// and thus prefers to have 0 rather than -1 returned in such cases.
498{
499 // The following should probably never be changed without a full suite of tests to ensure the
500 // change doesn't cause the finicky _vsnprintf() to break something.
501 size_t length = _tcslen(aBuf);
502 int space_remaining = (int)(aBufSize - length); // Must cast to int to avoid loss of negative values.
503 if (space_remaining < 1) // Can't even terminate it (no room) so just indicate that no characters were copied.
504 return 0;
505 aBuf += length; // aBuf is now the spot where the new text will be written.
506 va_list ap;
507 va_start(ap, aFormat);
508 // Must use vsnprintf() not snprintf() because of the way va_list is handled:
509 int result = _vsntprintf(aBuf, (size_t)space_remaining, aFormat, ap); // "returns the number of characters written, not including the terminating null character, or a negative value if an output error occurs"
510 aBuf[space_remaining - 1] = '\0'; // Confirmed through testing: Must terminate at this exact spot because _vsnprintf() doesn't always do it.
511 return result > -1 ? result : space_remaining - 1; // Never return a negative value. See comment under function definition, above.
512}
513
514
515

Callers 7

InvokeMethod · 0.85
InitMethod · 0.85
ReloadMethod · 0.85
PerformMethod · 0.85
ListKeyHistoryMethod · 0.85
BIF_DECLFunction · 0.85
GetHookStatusFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected