mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-01-24 05:40:39 +00:00
Use SDL_bool instead an int return code in the SDL API
Most SDL functions used to indicate success or failure using an int return code. These functions have been changed to return SDL_bool. Here is a coccinelle patch to change code that previously compared the return value to 0 and changes it to a boolean test: @ bool_return_type @ identifier func =~ "^(SDL_AddEventWatch|SDL_AddHintCallback|SDL_AddSurfaceAlternateImage|SDL_AddVulkanRenderSemaphores|SDL_BindAudioStream|SDL_BindAudioStreams|SDL_BlitSurface|SDL_BlitSurface9Grid|SDL_BlitSurfaceScaled|SDL_BlitSurfaceTiled|SDL_BlitSurfaceTiledWithScale|SDL_BlitSurfaceUnchecked|SDL_BlitSurfaceUncheckedScaled|SDL_CaptureMouse|SDL_ClearAudioStream|SDL_ClearClipboardData|SDL_ClearComposition|SDL_ClearError|SDL_ClearProperty|SDL_ClearSurface|SDL_CloseIO|SDL_CloseStorage|SDL_ConvertAudioSamples|SDL_ConvertEventToRenderCoordinates|SDL_ConvertPixels|SDL_ConvertPixelsAndColorspace|SDL_CopyFile|SDL_CopyProperties|SDL_CopyStorageFile|SDL_CreateDirectory|SDL_CreateStorageDirectory|SDL_CreateWindowAndRenderer|SDL_DateTimeToTime|SDL_DestroyWindowSurface|SDL_DetachVirtualJoystick|SDL_DisableScreenSaver|SDL_EnableScreenSaver|SDL_EnumerateDirectory|SDL_EnumerateProperties|SDL_EnumerateStorageDirectory|SDL_FillSurfaceRect|SDL_FillSurfaceRects|SDL_FlashWindow|SDL_FlipSurface|SDL_FlushAudioStream|SDL_FlushRenderer|SDL_GL_DestroyContext|SDL_GL_GetAttribute|SDL_GL_GetSwapInterval|SDL_GL_LoadLibrary|SDL_GL_MakeCurrent|SDL_GL_SetAttribute|SDL_GL_SetSwapInterval|SDL_GL_SwapWindow|SDL_GetAudioDeviceFormat|SDL_GetAudioStreamFormat|SDL_GetCameraFormat|SDL_GetClosestFullscreenDisplayMode|SDL_GetCurrentRenderOutputSize|SDL_GetCurrentTime|SDL_GetDXGIOutputInfo|SDL_GetDateTimeLocalePreferences|SDL_GetDisplayBounds|SDL_GetDisplayUsableBounds|SDL_GetGDKDefaultUser|SDL_GetGDKTaskQueue|SDL_GetGamepadSensorData|SDL_GetGamepadTouchpadFinger|SDL_GetHapticEffectStatus|SDL_GetJoystickBall|SDL_GetMasksForPixelFormat|SDL_GetPathInfo|SDL_GetRectUnion|SDL_GetRectUnionFloat|SDL_GetRenderClipRect|SDL_GetRenderColorScale|SDL_GetRenderDrawBlendMode|SDL_GetRenderDrawColor|SDL_GetRenderDrawColorFloat|SDL_GetRenderLogicalPresentation|SDL_GetRenderLogicalPresentationRect|SDL_GetRenderOutputSize|SDL_GetRenderSafeArea|SDL_GetRenderScale|SDL_GetRenderVSync|SDL_GetRenderViewport|SDL_GetSensorData|SDL_GetStorageFileSize|SDL_GetStoragePathInfo|SDL_GetSurfaceAlphaMod|SDL_GetSurfaceBlendMode|SDL_GetSurfaceClipRect|SDL_GetSurfaceColorKey|SDL_GetSurfaceColorMod|SDL_GetTextInputArea|SDL_GetTextureAlphaMod|SDL_GetTextureAlphaModFloat|SDL_GetTextureBlendMode|SDL_GetTextureColorMod|SDL_GetTextureColorModFloat|SDL_GetTextureScaleMode|SDL_GetTextureSize|SDL_GetWindowAspectRatio|SDL_GetWindowBordersSize|SDL_GetWindowMaximumSize|SDL_GetWindowMinimumSize|SDL_GetWindowPosition|SDL_GetWindowRelativeMouseMode|SDL_GetWindowSafeArea|SDL_GetWindowSize|SDL_GetWindowSizeInPixels|SDL_GetWindowSurfaceVSync|SDL_HideCursor|SDL_HideWindow|SDL_Init|SDL_InitHapticRumble|SDL_InitSubSystem|SDL_LoadWAV|SDL_LoadWAV_IO|SDL_LockAudioStream|SDL_LockProperties|SDL_LockSurface|SDL_LockTexture|SDL_LockTextureToSurface|SDL_MaximizeWindow|SDL_MinimizeWindow|SDL_MixAudio|SDL_OpenURL|SDL_OutOfMemory|SDL_PauseAudioDevice|SDL_PauseAudioStreamDevice|SDL_PauseHaptic|SDL_PlayHapticRumble|SDL_PremultiplyAlpha|SDL_PremultiplySurfaceAlpha|SDL_PushEvent|SDL_PutAudioStreamData|SDL_RaiseWindow|SDL_ReadStorageFile|SDL_ReadSurfacePixel|SDL_ReadSurfacePixelFloat|SDL_RegisterApp|SDL_ReloadGamepadMappings|SDL_RemovePath|SDL_RemoveStoragePath|SDL_RemoveTimer|SDL_RenamePath|SDL_RenameStoragePath|SDL_RenderClear|SDL_RenderCoordinatesFromWindow|SDL_RenderCoordinatesToWindow|SDL_RenderFillRect|SDL_RenderFillRects|SDL_RenderGeometry|SDL_RenderGeometryRaw|SDL_RenderLine|SDL_RenderLines|SDL_RenderPoint|SDL_RenderPoints|SDL_RenderPresent|SDL_RenderRect|SDL_RenderRects|SDL_RenderTexture|SDL_RenderTexture9Grid|SDL_RenderTextureRotated|SDL_RenderTextureTiled|SDL_RequestAndroidPermission|SDL_RestoreWindow|SDL_ResumeAudioDevice|SDL_ResumeAudioStreamDevice|SDL_ResumeHaptic|SDL_RumbleGamepad|SDL_RumbleGamepadTriggers|SDL_RumbleJoystick|SDL_RumbleJoystickTriggers|SDL_RunHapticEffect|SDL_SaveBMP|SDL_SaveBMP_IO|SDL_SendAndroidMessage|SDL_SendGamepadEffect|SDL_SendJoystickEffect|SDL_SendJoystickVirtualSensorData|SDL_SetAppMetadata|SDL_SetAppMetadataProperty|SDL_SetAudioDeviceGain|SDL_SetAudioPostmixCallback|SDL_SetAudioStreamFormat|SDL_SetAudioStreamFrequencyRatio|SDL_SetAudioStreamGain|SDL_SetAudioStreamGetCallback|SDL_SetAudioStreamInputChannelMap|SDL_SetAudioStreamOutputChannelMap|SDL_SetAudioStreamPutCallback|SDL_SetBooleanProperty|SDL_SetClipboardData|SDL_SetClipboardText|SDL_SetCursor|SDL_SetFloatProperty|SDL_SetGamepadLED|SDL_SetGamepadMapping|SDL_SetGamepadPlayerIndex|SDL_SetGamepadSensorEnabled|SDL_SetHapticAutocenter|SDL_SetHapticGain|SDL_SetJoystickLED|SDL_SetJoystickPlayerIndex|SDL_SetJoystickVirtualAxis|SDL_SetJoystickVirtualBall|SDL_SetJoystickVirtualButton|SDL_SetJoystickVirtualHat|SDL_SetJoystickVirtualTouchpad|SDL_SetLinuxThreadPriority|SDL_SetLinuxThreadPriorityAndPolicy|SDL_SetLogPriorityPrefix|SDL_SetMemoryFunctions|SDL_SetNumberProperty|SDL_SetPaletteColors|SDL_SetPointerProperty|SDL_SetPointerPropertyWithCleanup|SDL_SetPrimarySelectionText|SDL_SetRenderClipRect|SDL_SetRenderColorScale|SDL_SetRenderDrawBlendMode|SDL_SetRenderDrawColor|SDL_SetRenderDrawColorFloat|SDL_SetRenderLogicalPresentation|SDL_SetRenderScale|SDL_SetRenderTarget|SDL_SetRenderVSync|SDL_SetRenderViewport|SDL_SetScancodeName|SDL_SetStringProperty|SDL_SetSurfaceAlphaMod|SDL_SetSurfaceBlendMode|SDL_SetSurfaceColorKey|SDL_SetSurfaceColorMod|SDL_SetSurfaceColorspace|SDL_SetSurfacePalette|SDL_SetSurfaceRLE|SDL_SetTLS|SDL_SetTextInputArea|SDL_SetTextureAlphaMod|SDL_SetTextureAlphaModFloat|SDL_SetTextureBlendMode|SDL_SetTextureColorMod|SDL_SetTextureColorModFloat|SDL_SetTextureScaleMode|SDL_SetThreadPriority|SDL_SetWindowAlwaysOnTop|SDL_SetWindowAspectRatio|SDL_SetWindowBordered|SDL_SetWindowFocusable|SDL_SetWindowFullscreen|SDL_SetWindowFullscreenMode|SDL_SetWindowHitTest|SDL_SetWindowIcon|SDL_SetWindowKeyboardGrab|SDL_SetWindowMaximumSize|SDL_SetWindowMinimumSize|SDL_SetWindowModalFor|SDL_SetWindowMouseGrab|SDL_SetWindowMouseRect|SDL_SetWindowOpacity|SDL_SetWindowPosition|SDL_SetWindowRelativeMouseMode|SDL_SetWindowResizable|SDL_SetWindowShape|SDL_SetWindowSize|SDL_SetWindowSurfaceVSync|SDL_SetWindowTitle|SDL_SetiOSAnimationCallback|SDL_ShowAndroidToast|SDL_ShowCursor|SDL_ShowMessageBox|SDL_ShowSimpleMessageBox|SDL_ShowWindow|SDL_ShowWindowSystemMenu|SDL_StartTextInput|SDL_StartTextInputWithProperties|SDL_StopHapticEffect|SDL_StopHapticEffects|SDL_StopHapticRumble|SDL_StopTextInput|SDL_SyncWindow|SDL_TimeToDateTime|SDL_TryLockMutex|SDL_TryLockRWLockForReading|SDL_TryLockRWLockForWriting|SDL_TryWaitSemaphore|SDL_UnlockAudioStream|SDL_UpdateHapticEffect|SDL_UpdateNVTexture|SDL_UpdateTexture|SDL_UpdateWindowSurface|SDL_UpdateWindowSurfaceRects|SDL_UpdateYUVTexture|SDL_Vulkan_CreateSurface|SDL_Vulkan_LoadLibrary|SDL_WaitConditionTimeout|SDL_WaitSemaphoreTimeout|SDL_WarpMouseGlobal|SDL_WriteStorageFile|SDL_WriteSurfacePixel|SDL_WriteSurfacePixelFloat)$"; @@ ( func( ... ) - == 0 | - func( + !func( ... ) - < 0 | - func( + !func( ... ) - != 0 | - func( + !func( ... ) - == -1 )
This commit is contained in:
@@ -245,7 +245,7 @@ const char *SDL_getenv(const char *name)
|
||||
{
|
||||
DWORD length, maxlen = 0;
|
||||
char *string = NULL;
|
||||
const char *retval = NULL;
|
||||
const char *result = NULL;
|
||||
|
||||
// Input validation
|
||||
if (!name || *name == '\0') {
|
||||
@@ -274,10 +274,10 @@ const char *SDL_getenv(const char *name)
|
||||
}
|
||||
}
|
||||
if (string) {
|
||||
retval = SDL_GetPersistentString(string);
|
||||
result = SDL_GetPersistentString(string);
|
||||
SDL_free(string);
|
||||
}
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
#else
|
||||
const char *SDL_getenv(const char *name)
|
||||
|
||||
@@ -5257,10 +5257,10 @@ void SDL_GetMemoryFunctions(SDL_malloc_func *malloc_func,
|
||||
}
|
||||
}
|
||||
|
||||
int SDL_SetMemoryFunctions(SDL_malloc_func malloc_func,
|
||||
SDL_calloc_func calloc_func,
|
||||
SDL_realloc_func realloc_func,
|
||||
SDL_free_func free_func)
|
||||
SDL_bool SDL_SetMemoryFunctions(SDL_malloc_func malloc_func,
|
||||
SDL_calloc_func calloc_func,
|
||||
SDL_realloc_func realloc_func,
|
||||
SDL_free_func free_func)
|
||||
{
|
||||
if (!malloc_func) {
|
||||
return SDL_InvalidParamError("malloc_func");
|
||||
@@ -5279,7 +5279,7 @@ int SDL_SetMemoryFunctions(SDL_malloc_func malloc_func,
|
||||
s_mem.calloc_func = calloc_func;
|
||||
s_mem.realloc_func = realloc_func;
|
||||
s_mem.free_func = free_func;
|
||||
return 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
int SDL_GetNumAllocations(void)
|
||||
|
||||
@@ -532,7 +532,7 @@ int SDL_isblank(int x) { return ((x) == ' ') || ((x) == '\t'); }
|
||||
void *SDL_aligned_alloc(size_t alignment, size_t size)
|
||||
{
|
||||
size_t padding;
|
||||
Uint8 *retval = NULL;
|
||||
Uint8 *result = NULL;
|
||||
|
||||
if (alignment < sizeof(void*)) {
|
||||
alignment = sizeof(void*);
|
||||
@@ -545,16 +545,16 @@ void *SDL_aligned_alloc(size_t alignment, size_t size)
|
||||
void *original = SDL_malloc(size);
|
||||
if (original) {
|
||||
// Make sure we have enough space to store the original pointer
|
||||
retval = (Uint8 *)original + sizeof(original);
|
||||
result = (Uint8 *)original + sizeof(original);
|
||||
|
||||
// Align the pointer we're going to return
|
||||
retval += alignment - (((size_t)retval) % alignment);
|
||||
result += alignment - (((size_t)result) % alignment);
|
||||
|
||||
// Store the original pointer right before the returned value
|
||||
SDL_memcpy(retval - sizeof(original), &original, sizeof(original));
|
||||
SDL_memcpy(result - sizeof(original), &original, sizeof(original));
|
||||
}
|
||||
}
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
|
||||
void SDL_aligned_free(void *mem)
|
||||
|
||||
@@ -213,10 +213,10 @@ static Uint32 StepUTF8(const char **_str, const size_t slen)
|
||||
} else if (((octet & 0xE0) == 0xC0) && (slen >= 2)) { // 110xxxxx 10xxxxxx: two byte codepoint.
|
||||
const Uint8 str1 = str[1];
|
||||
if ((str1 & 0xC0) == 0x80) { // If trailing bytes aren't 10xxxxxx, sequence is bogus.
|
||||
const Uint32 retval = ((octet & 0x1F) << 6) | (str1 & 0x3F);
|
||||
if (retval >= 0x0080) { // rfc3629 says you can't use overlong sequences for smaller values.
|
||||
const Uint32 result = ((octet & 0x1F) << 6) | (str1 & 0x3F);
|
||||
if (result >= 0x0080) { // rfc3629 says you can't use overlong sequences for smaller values.
|
||||
*_str += 2;
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
} else if (((octet & 0xF0) == 0xE0) && (slen >= 3)) { // 1110xxxx 10xxxxxx 10xxxxxx: three byte codepoint.
|
||||
@@ -225,11 +225,11 @@ static Uint32 StepUTF8(const char **_str, const size_t slen)
|
||||
if (((str1 & 0xC0) == 0x80) && ((str2 & 0xC0) == 0x80)) { // If trailing bytes aren't 10xxxxxx, sequence is bogus.
|
||||
const Uint32 octet2 = ((Uint32) (str1 & 0x3F)) << 6;
|
||||
const Uint32 octet3 = ((Uint32) (str2 & 0x3F));
|
||||
const Uint32 retval = ((octet & 0x0F) << 12) | octet2 | octet3;
|
||||
if (retval >= 0x800) { // rfc3629 says you can't use overlong sequences for smaller values.
|
||||
if ((retval < 0xD800) || (retval > 0xDFFF)) { // UTF-16 surrogate values are illegal in UTF-8.
|
||||
const Uint32 result = ((octet & 0x0F) << 12) | octet2 | octet3;
|
||||
if (result >= 0x800) { // rfc3629 says you can't use overlong sequences for smaller values.
|
||||
if ((result < 0xD800) || (result > 0xDFFF)) { // UTF-16 surrogate values are illegal in UTF-8.
|
||||
*_str += 3;
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -241,10 +241,10 @@ static Uint32 StepUTF8(const char **_str, const size_t slen)
|
||||
const Uint32 octet2 = ((Uint32) (str1 & 0x1F)) << 12;
|
||||
const Uint32 octet3 = ((Uint32) (str2 & 0x3F)) << 6;
|
||||
const Uint32 octet4 = ((Uint32) (str3 & 0x3F));
|
||||
const Uint32 retval = ((octet & 0x07) << 18) | octet2 | octet3 | octet4;
|
||||
if (retval >= 0x10000) { // rfc3629 says you can't use overlong sequences for smaller values.
|
||||
const Uint32 result = ((octet & 0x07) << 18) | octet2 | octet3 | octet4;
|
||||
if (result >= 0x10000) { // rfc3629 says you can't use overlong sequences for smaller values.
|
||||
*_str += 4;
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -260,9 +260,9 @@ Uint32 SDL_StepUTF8(const char **pstr, size_t *pslen)
|
||||
return StepUTF8(pstr, 4); // 4 == max codepoint size.
|
||||
}
|
||||
const char *origstr = *pstr;
|
||||
const Uint32 retval = StepUTF8(pstr, *pslen);
|
||||
const Uint32 result = StepUTF8(pstr, *pslen);
|
||||
*pslen -= (size_t) (*pstr - origstr);
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
|
||||
#if (SDL_SIZEOF_WCHAR_T == 2)
|
||||
@@ -891,20 +891,20 @@ size_t SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size
|
||||
|
||||
size_t SDL_utf8strlen(const char *str)
|
||||
{
|
||||
size_t retval = 0;
|
||||
size_t result = 0;
|
||||
while (SDL_StepUTF8(&str, NULL)) {
|
||||
retval++;
|
||||
result++;
|
||||
}
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t SDL_utf8strnlen(const char *str, size_t bytes)
|
||||
{
|
||||
size_t retval = 0;
|
||||
size_t result = 0;
|
||||
while (SDL_StepUTF8(&str, &bytes)) {
|
||||
retval++;
|
||||
result++;
|
||||
}
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen)
|
||||
@@ -1405,7 +1405,7 @@ static bool CharacterMatchesSet(char c, const char *set, size_t set_len)
|
||||
// NOLINTNEXTLINE(readability-non-const-parameter)
|
||||
int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_list ap)
|
||||
{
|
||||
int retval = 0;
|
||||
int result = 0;
|
||||
|
||||
if (!text || !*text) {
|
||||
return -1;
|
||||
@@ -1462,7 +1462,7 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
|
||||
while (count--) {
|
||||
*valuep++ = *text++;
|
||||
}
|
||||
++retval;
|
||||
++result;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -1521,7 +1521,7 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
|
||||
if (advance && !suppress) {
|
||||
Sint64 *valuep = va_arg(ap, Sint64 *);
|
||||
*valuep = value;
|
||||
++retval;
|
||||
++result;
|
||||
}
|
||||
} else if (inttype == DO_SIZE_T) {
|
||||
Sint64 value = 0;
|
||||
@@ -1530,7 +1530,7 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
|
||||
if (advance && !suppress) {
|
||||
size_t *valuep = va_arg(ap, size_t *);
|
||||
*valuep = (size_t)value;
|
||||
++retval;
|
||||
++result;
|
||||
}
|
||||
} else {
|
||||
long value = 0;
|
||||
@@ -1558,7 +1558,7 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
|
||||
// Handled above
|
||||
break;
|
||||
}
|
||||
++retval;
|
||||
++result;
|
||||
}
|
||||
}
|
||||
done = true;
|
||||
@@ -1582,7 +1582,7 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
|
||||
if (advance && !suppress) {
|
||||
Uint64 *valuep = va_arg(ap, Uint64 *);
|
||||
*valuep = value;
|
||||
++retval;
|
||||
++result;
|
||||
}
|
||||
} else if (inttype == DO_SIZE_T) {
|
||||
Uint64 value = 0;
|
||||
@@ -1591,7 +1591,7 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
|
||||
if (advance && !suppress) {
|
||||
size_t *valuep = va_arg(ap, size_t *);
|
||||
*valuep = (size_t)value;
|
||||
++retval;
|
||||
++result;
|
||||
}
|
||||
} else {
|
||||
unsigned long value = 0;
|
||||
@@ -1619,7 +1619,7 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
|
||||
// Handled above
|
||||
break;
|
||||
}
|
||||
++retval;
|
||||
++result;
|
||||
}
|
||||
}
|
||||
done = true;
|
||||
@@ -1632,7 +1632,7 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
|
||||
if (advance && !suppress) {
|
||||
void **valuep = va_arg(ap, void **);
|
||||
*valuep = (void *)value;
|
||||
++retval;
|
||||
++result;
|
||||
}
|
||||
}
|
||||
done = true;
|
||||
@@ -1645,7 +1645,7 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
|
||||
if (advance && !suppress) {
|
||||
float *valuep = va_arg(ap, float *);
|
||||
*valuep = (float)value;
|
||||
++retval;
|
||||
++result;
|
||||
}
|
||||
}
|
||||
done = true;
|
||||
@@ -1671,7 +1671,7 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
|
||||
}
|
||||
}
|
||||
*valuep = '\0';
|
||||
++retval;
|
||||
++result;
|
||||
}
|
||||
done = true;
|
||||
break;
|
||||
@@ -1706,7 +1706,7 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
|
||||
}
|
||||
*valuep = '\0';
|
||||
if (had_match) {
|
||||
++retval;
|
||||
++result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1730,50 +1730,50 @@ int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_li
|
||||
break;
|
||||
}
|
||||
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
#endif // HAVE_VSSCANF
|
||||
|
||||
int SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int retval;
|
||||
int result;
|
||||
|
||||
va_start(ap, fmt);
|
||||
retval = SDL_vsnprintf(text, maxlen, fmt, ap);
|
||||
result = SDL_vsnprintf(text, maxlen, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
|
||||
int SDL_swprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int retval;
|
||||
int result;
|
||||
|
||||
va_start(ap, fmt);
|
||||
retval = SDL_vswprintf(text, maxlen, fmt, ap);
|
||||
result = SDL_vswprintf(text, maxlen, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
|
||||
#if defined(HAVE_LIBC) && defined(__WATCOMC__)
|
||||
// _vsnprintf() doesn't ensure nul termination
|
||||
int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
|
||||
{
|
||||
int retval;
|
||||
int result;
|
||||
if (!fmt) {
|
||||
fmt = "";
|
||||
}
|
||||
retval = _vsnprintf(text, maxlen, fmt, ap);
|
||||
result = _vsnprintf(text, maxlen, fmt, ap);
|
||||
if (maxlen > 0) {
|
||||
text[maxlen - 1] = '\0';
|
||||
}
|
||||
if (retval < 0) {
|
||||
retval = (int)maxlen;
|
||||
if (result < 0) {
|
||||
result = (int)maxlen;
|
||||
}
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
#elif defined(HAVE_VSNPRINTF)
|
||||
int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
|
||||
@@ -2305,7 +2305,7 @@ int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FO
|
||||
int SDL_vswprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, const wchar_t *fmt, va_list ap)
|
||||
{
|
||||
char *text_utf8 = NULL, *fmt_utf8 = NULL;
|
||||
int retval;
|
||||
int result;
|
||||
|
||||
if (fmt) {
|
||||
fmt_utf8 = SDL_iconv_string("UTF-8", "WCHAR_T", (const char *)fmt, (SDL_wcslen(fmt) + 1) * sizeof(wchar_t));
|
||||
@@ -2324,42 +2324,42 @@ int SDL_vswprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, const wcha
|
||||
return -1;
|
||||
}
|
||||
|
||||
retval = SDL_vsnprintf(text_utf8, maxlen * 4, fmt_utf8, ap);
|
||||
result = SDL_vsnprintf(text_utf8, maxlen * 4, fmt_utf8, ap);
|
||||
|
||||
if (retval >= 0) {
|
||||
if (result >= 0) {
|
||||
wchar_t *text_wchar = (wchar_t *)SDL_iconv_string("WCHAR_T", "UTF-8", text_utf8, SDL_strlen(text_utf8) + 1);
|
||||
if (text_wchar) {
|
||||
if (text) {
|
||||
SDL_wcslcpy(text, text_wchar, maxlen);
|
||||
}
|
||||
retval = (int)SDL_wcslen(text_wchar);
|
||||
result = (int)SDL_wcslen(text_wchar);
|
||||
SDL_free(text_wchar);
|
||||
} else {
|
||||
retval = -1;
|
||||
result = -1;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_free(text_utf8);
|
||||
SDL_free(fmt_utf8);
|
||||
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
|
||||
int SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int retval;
|
||||
int result;
|
||||
|
||||
va_start(ap, fmt);
|
||||
retval = SDL_vasprintf(strp, fmt, ap);
|
||||
result = SDL_vasprintf(strp, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
|
||||
int SDL_vasprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
|
||||
{
|
||||
int retval;
|
||||
int result;
|
||||
int size = 100; // Guess we need no more than 100 bytes
|
||||
char *p, *np;
|
||||
va_list aq;
|
||||
@@ -2374,23 +2374,23 @@ int SDL_vasprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list
|
||||
while (1) {
|
||||
// Try to print in the allocated space
|
||||
va_copy(aq, ap);
|
||||
retval = SDL_vsnprintf(p, size, fmt, aq);
|
||||
result = SDL_vsnprintf(p, size, fmt, aq);
|
||||
va_end(aq);
|
||||
|
||||
// Check error code
|
||||
if (retval < 0) {
|
||||
if (result < 0) {
|
||||
SDL_free(p);
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
|
||||
// If that worked, return the string
|
||||
if (retval < size) {
|
||||
if (result < size) {
|
||||
*strp = p;
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
|
||||
// Else try again with more space
|
||||
size = retval + 1; // Precisely what is needed
|
||||
size = result + 1; // Precisely what is needed
|
||||
|
||||
np = (char *)SDL_realloc(p, size);
|
||||
if (!np) {
|
||||
|
||||
Reference in New Issue
Block a user