SDL_rtf: Update api to use SDL_bool instead of an int return code

From 0077d43a6e71004be3c8f91271fdb1cfbfd092b3 Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Wed, 28 Aug 2024 11:50:32 +0300
Subject: [PATCH] Update api to use SDL_bool instead of an int return code

also update against the api changes in SDL_ttf.
---
 cmake/test/main.c          |  4 +-
 examples/showrtf.c         | 84 +++++++++++++++++++++-----------------
 include/SDL3_rtf/SDL_rtf.h | 19 ++++-----
 src/SDL_rtf.c              | 81 +++++++++++++++---------------------
 src/rtfreadr.c             |  4 +-
 src/rtftype.h              |  8 ++--
 6 files changed, 96 insertions(+), 104 deletions(-)

diff --git a/cmake/test/main.c b/cmake/test/main.c
index 202026a..5761def 100644
--- a/cmake/test/main.c
+++ b/cmake/test/main.c
@@ -13,8 +13,8 @@ int main(int argc, char *argv[])
         SDL_Log("SDL_Init: could not initialize SDL: %s", SDL_GetError());
         return 1;
     }
-    if (TTF_Init() == -1) {
-        SDL_Log("TTF_Init: %s", TTF_GetError());
+    if (!TTF_Init()) {
+        SDL_Log("TTF_Init: %s", SDL_GetError());
     }
     version = RTF_Version();
     SDL_Log("SDL_rtf linked version: %u.%u.%u",
diff --git a/examples/showrtf.c b/examples/showrtf.c
index e4c7edb..aef60fb 100644
--- a/examples/showrtf.c
+++ b/examples/showrtf.c
@@ -33,7 +33,7 @@ static const char *FontList[8];
 /* Note, this is only one way of looking up fonts */
 static int FontFamilyToIndex(RTF_FontFamily family)
 {
-    switch(family) {
+    switch (family) {
         case RTF_FontDefault:
             return 0;
         case RTF_FontRoman:
@@ -61,16 +61,16 @@ static Uint32 UTF8_to_UNICODE(const char *utf8, int *advance)
     Uint32 ch;
 
     ch = ((const unsigned char *)utf8)[i];
-    if ( ch >= 0xF0 ) {
+    if (ch >= 0xF0) {
         ch  =  (Uint16)((utf8[i]&0x07) << 18);
         ch |=  (Uint16)(utf8[++i]&0x3F) << 12;
         ch |=  (Uint16)(utf8[++i]&0x3F) << 6;
         ch |=  (Uint16)(utf8[++i]&0x3F);
-    } else if ( ch >= 0xE0 ) {
+    } else if (ch >= 0xE0) {
         ch  =  (Uint16)(utf8[i]&0x3F) << 12;
         ch |=  (Uint16)(utf8[++i]&0x3F) << 6;
         ch |=  (Uint16)(utf8[++i]&0x3F);
-    } else if ( ch >= 0xC0 ) {
+    } else if (ch >= 0xC0) {
         ch  =  (Uint16)(utf8[i]&0x3F) << 6;
         ch |=  (Uint16)(utf8[++i]&0x3F);
     }
@@ -87,18 +87,22 @@ static void * SDLCALL CreateFont(const char *name, RTF_FontFamily family, int ch
     (void)charset;
 
     index = FontFamilyToIndex(family);
-    if (!FontList[index])
+    if (!FontList[index]) {
         index = 0;
+    }
 
     font = TTF_OpenFont(FontList[index], size);
     if (font) {
         int TTF_style = TTF_STYLE_NORMAL;
-        if ( style & RTF_FontBold )
+        if (style & RTF_FontBold) {
             TTF_style |= TTF_STYLE_BOLD;
-        if ( style & RTF_FontItalic )
+        }
+        if (style & RTF_FontItalic) {
             TTF_style |= TTF_STYLE_ITALIC;
-        if ( style & RTF_FontUnderline )
+        }
+        if (style & RTF_FontUnderline) {
             TTF_style |= TTF_STYLE_UNDERLINE;
+        }
         TTF_SetFontStyle(font, TTF_style);
     }
 
@@ -121,7 +125,7 @@ static int SDLCALL GetCharacterOffsets(void *_font, const char *text, int *byteO
     int pixels = 0;
     int advance;
     Uint16 ch;
-    while ( *text && i < maxOffsets ) {
+    while (*text && i < maxOffsets) {
         byteOffsets[i] = bytes;
         pixelOffsets[i] = pixels;
         ++i;
@@ -132,7 +136,7 @@ static int SDLCALL GetCharacterOffsets(void *_font, const char *text, int *byteO
         TTF_GlyphMetrics(font, ch, NULL, NULL, NULL, NULL, &advance);
         pixels += advance;
     }
-    if ( i < maxOffsets ) {
+    if (i < maxOffsets) {
         byteOffsets[i] = bytes;
         pixelOffsets[i] = pixels;
     }
@@ -159,8 +163,8 @@ static void SDLCALL FreeFont(void *_font)
 
 static void LoadRTF(RTF_Context *ctx, const char *file)
 {
-    if ( RTF_Load(ctx, file) < 0 ) {
-        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, RTF_GetError());
+    if (!RTF_Load(ctx, file)) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, SDL_GetError());
         return;
     }
 }
@@ -190,43 +194,43 @@ int main(int argc, char *argv[])
     const Uint8 *keystate;
 
     /* Parse command line arguments */
-    for ( i = 1; i < argc; ++i ) {
-        if ( SDL_strcmp(argv[i], "-fdefault") == 0 ) {
+    for (i = 1; i < argc; ++i) {
+        if (SDL_strcmp(argv[i], "-fdefault") == 0) {
             FontList[FontFamilyToIndex(RTF_FontDefault)] = argv[++i];
-        } else if ( SDL_strcmp(argv[i], "-froman") == 0 ) {
+        } else if (SDL_strcmp(argv[i], "-froman") == 0) {
             FontList[FontFamilyToIndex(RTF_FontRoman)] = argv[++i];
-        } else if ( SDL_strcmp(argv[i], "-fswiss") == 0 ) {
+        } else if (SDL_strcmp(argv[i], "-fswiss") == 0) {
             FontList[FontFamilyToIndex(RTF_FontSwiss)] = argv[++i];
-        } else if ( SDL_strcmp(argv[i], "-fmodern") == 0 ) {
+        } else if (SDL_strcmp(argv[i], "-fmodern") == 0) {
             FontList[FontFamilyToIndex(RTF_FontModern)] = argv[++i];
-        } else if ( SDL_strcmp(argv[i], "-fscript") == 0 ) {
+        } else if (SDL_strcmp(argv[i], "-fscript") == 0) {
             FontList[FontFamilyToIndex(RTF_FontScript)] = argv[++i];
-        } else if ( SDL_strcmp(argv[i], "-fdecor") == 0 ) {
+        } else if (SDL_strcmp(argv[i], "-fdecor") == 0) {
             FontList[FontFamilyToIndex(RTF_FontDecor)] = argv[++i];
-        } else if ( SDL_strcmp(argv[i], "-ftech") == 0 ) {
+        } else if (SDL_strcmp(argv[i], "-ftech") == 0) {
             FontList[FontFamilyToIndex(RTF_FontTech)] = argv[++i];
         } else {
             break;
         }
     }
     start = i;
-    stop = (argc-1);
-    if ( !FontList[0] || (start > stop) ) {
+    stop = (argc - 1);
+    if (!FontList[0] || (start > stop)) {
         PrintUsage(argv[0]);
-        return(1);
+        return 1;
     }
 
     /* Initialize the TTF library */
-    if ( TTF_Init() < 0 ) {
-        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize TTF: %s\n",SDL_GetError());
+    if (!TTF_Init()) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize TTF: %s\n", SDL_GetError());
         SDL_Quit();
-        return(3);
+        return 3;
     }
 
     if (!SDL_CreateWindowAndRenderer("showrtf demo", SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError());
         cleanup();
-        return(4);
+        return 4;
     }
 
     /* Create and load the RTF document */
@@ -237,10 +241,10 @@ int main(int argc, char *argv[])
     fontEngine.RenderText = RenderText;
     fontEngine.FreeFont = FreeFont;
     ctx = RTF_CreateContext(renderer, &fontEngine);
-    if ( ctx == NULL ) {
-        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create RTF context: %s\n", RTF_GetError());
+    if (!ctx) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create RTF context: %s\n", SDL_GetError());
         cleanup();
-        return(5);
+        return 5;
     }
     LoadRTF(ctx, argv[i]);
     SDL_SetWindowTitle(window, RTF_GetTitle(ctx));
@@ -267,7 +271,7 @@ int main(int argc, char *argv[])
                         done = 1;
                         break;
                     case SDLK_LEFT:
-                        if ( i > start ) {
+                        if (i > start) {
                             --i;
                             LoadRTF(ctx, argv[i]);
                             offset = 0;
@@ -275,7 +279,7 @@ int main(int argc, char *argv[])
                         }
                         break;
                     case SDLK_RIGHT:
-                        if ( i < stop ) {
+                        if (i < stop) {
                             ++i;
                             LoadRTF(ctx, argv[i]);
                             offset = 0;
@@ -290,14 +294,16 @@ int main(int argc, char *argv[])
                         break;
                     case SDLK_PAGEUP:
                         offset -= h;
-                        if ( offset < 0 )
+                        if (offset < 0) {
                             offset = 0;
+                        }
                         break;
                     case SDLK_PAGEDOWN:
                     case SDLK_SPACE:
                         offset += h;
-                        if ( offset > (height - h) )
+                        if (offset > (height - h)) {
                             offset = (height - h);
+                        }
                         break;
                     default:
                         break;
@@ -308,15 +314,17 @@ int main(int argc, char *argv[])
             }
         }
         keystate = SDL_GetKeyboardState(NULL);
-        if ( keystate[SDL_SCANCODE_UP] ) {
+        if (keystate[SDL_SCANCODE_UP]) {
             offset -= 1;
-            if ( offset < 0 )
+            if (offset < 0) {
                 offset = 0;
+            }
         }
-        if ( keystate[SDL_SCANCODE_DOWN] ) {
+        if (keystate[SDL_SCANCODE_DOWN]) {
             offset += 1;
-            if ( offset > (height - h) )
+            if (offset > (height - h)) {
                 offset = (height - h);
+            }
         }
 
         SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
diff --git a/include/SDL3_rtf/SDL_rtf.h b/include/SDL3_rtf/SDL_rtf.h
index 6fcb6e1..e512c22 100644
--- a/include/SDL3_rtf/SDL_rtf.h
+++ b/include/SDL3_rtf/SDL_rtf.h
@@ -147,18 +147,19 @@ extern SDL_DECLSPEC RTF_Context * SDLCALL RTF_CreateContext(SDL_Renderer *render
  *
  * \param ctx the RTF context to update.
  * \param file the file path to load RTF data from.
- * \returns 0 on success, -1 on failure.
+ * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
+ *          for more information.
  *
  * \since This function is available since SDL_rtf 3.0.0.
  */
-extern SDL_DECLSPEC int SDLCALL RTF_Load(RTF_Context *ctx, const char *file);
+extern SDL_DECLSPEC SDL_bool SDLCALL RTF_Load(RTF_Context *ctx, const char *file);
 
 /**
  * Set the text of an RTF context, with data loaded from an SDL_IOStream.
  *
  * This can be called multiple times to change the text displayed.
  *
- * If `closeio` is non-zero, this function will close `src`, whether this
+ * If `closeio` is SDL_TRUE, this function will close `src`, whether this
  * function succeeded or not.
  *
  * On failure, call RTF_GetError() to get a human-readable text message
@@ -166,12 +167,14 @@ extern SDL_DECLSPEC int SDLCALL RTF_Load(RTF_Context *ctx, const char *file);
  *
  * \param ctx the RTF context to update.
  * \param src the SDL_IOStream to load RTF data from.
- * \param closeio non-zero to close/free `src`, zero to leave open.
- * \returns 0 on success, -1 on failure.
+ * \param closeio SDL_TRUE to close `src` when the font is closed, SDL_FALSE
+ *                to leave it open.
+ * \returns SDL_TRUE on success or SDL_FALSE on failure; call SDL_GetError()
+ *          for more information.
  *
  * \since This function is available since SDL_rtf 3.0.0.
  */
-extern SDL_DECLSPEC int SDLCALL RTF_Load_IO(RTF_Context *ctx, SDL_IOStream *src, int closeio);
+extern SDL_DECLSPEC SDL_bool SDLCALL RTF_Load_IO(RTF_Context *ctx, SDL_IOStream *src, SDL_bool closeio);
 
 /**
  * Get the title of an RTF document.
@@ -261,10 +264,6 @@ extern SDL_DECLSPEC void SDLCALL RTF_Render(RTF_Context *ctx, SDL_Rect *rect, in
  */
 extern SDL_DECLSPEC void SDLCALL RTF_FreeContext(RTF_Context *ctx);
 
-/* We'll use SDL for reporting errors */
-#define RTF_SetError    SDL_SetError
-#define RTF_GetError    SDL_GetError
-
 /* Ends C function definitions when using C++ */
 #ifdef __cplusplus
 }
diff --git a/src/SDL_rtf.c b/src/SDL_rtf.c
index 5d1575d..2fb8d83 100644
--- a/src/SDL_rtf.c
+++ b/src/SDL_rtf.c
@@ -26,14 +26,12 @@
 #include "rtfdecl.h"
 #include "SDL_rtfreadr.h"
 
-
 /* rcg06192001 get linked library's version. */
 int RTF_Version(void)
 {
     return SDL_RTF_VERSION;
 }
 
-
 /* Create an RTF display context, with the given font engine.
  * Once a context is created, it can be used to load and display
  * text in Microsoft RTF format.
@@ -42,35 +40,35 @@ RTF_Context *RTF_CreateContext(SDL_Renderer *renderer, RTF_FontEngine *fontEngin
 {
     RTF_Context *ctx;
 
-    if ( fontEngine->version != RTF_FONT_ENGINE_VERSION ) {
-            RTF_SetError("Unknown font engine version");
-            return(NULL);
+    if (fontEngine->version != RTF_FONT_ENGINE_VERSION) {
+            SDL_SetError("Unknown font engine version");
+            return NULL;
     }
 
     ctx = (RTF_Context *)SDL_malloc(sizeof(*ctx));
-    if ( ctx == NULL ) {
-            RTF_SetError("Out of memory");
-            return(NULL);
+    if (!ctx) {
+            SDL_SetError("Out of memory");
+            return NULL;
     }
     SDL_memset(ctx, 0, sizeof(*ctx));
     ctx->renderer = renderer;
-    ctx->fontEngine = SDL_malloc(sizeof *fontEngine);
-    if ( ctx->fontEngine == NULL ) {
-        RTF_SetError("Out of memory");
+    ctx->fontEngine = (RTF_FontEngine *)SDL_malloc(sizeof *fontEngine);
+    if (!ctx->fontEngine) {
+        SDL_SetError("Out of memory");
         SDL_free(ctx);
-        return(NULL);
+        return NULL;
     }
     SDL_memcpy(ctx->fontEngine, fontEngine, sizeof(*fontEngine));
-    return(ctx);
+    return ctx;
 }
 
 /* Set the text of an RTF context.
- * This function returns 0 if it succeeds or -1 if it fails.
- * Use RTF_GetError() to get a text message corresponding to the error.
+ * This function returns SDL_TRUE if it succeeds or SDL_FALSE if it fails.
+ * Use SDL_GetError() to get a text message corresponding to the error.
  */
-int RTF_Load_IO(RTF_Context *ctx, SDL_IOStream *src, int closeio)
+SDL_bool RTF_Load_IO(RTF_Context *ctx, SDL_IOStream *src, SDL_bool closeio)
 {
-    int retval;
+    SDL_bool retval;
 
     ecClearContext(ctx);
 
@@ -83,63 +81,54 @@ int RTF_Load_IO(RTF_Context *ctx, SDL_IOStream *src, int closeio)
     ctx->nextch = -1;
 
     /* Parse the RTF text and clean up */
-    switch(ecRtfParse(ctx)) {
+    switch (ecRtfParse(ctx)) {
         case ecOK:
-            retval = 0;
+            retval = SDL_TRUE;
             break;
         case ecStackUnderflow:
-            RTF_SetError("Unmatched '}'");
-            retval = -1;
+            retval = SDL_SetError("Unmatched '}'");
             break;
         case ecStackOverflow:
-            RTF_SetError("Too many '{' -- memory exhausted");
-            retval = -1;
+            retval = SDL_SetError("Too many '{' -- memory exhausted");
             break;
         case ecUnmatchedBrace:
-            RTF_SetError("RTF ended during an open group");
-            retval = -1;
+            retval = SDL_SetError("RTF ended during an open group");
             break;
         case ecInvalidHex:
-            RTF_SetError("Invalid hex character found in data");
-            retval = -1;
+            retval = SDL_SetError("Invalid hex character found in data");
             break;
         case ecBadTable:
-            RTF_SetError("RTF table (sym or prop) invalid");
-            retval = -1;
+            retval = SDL_SetError("RTF table (sym or prop) invalid");
             break;
         case ecAssertion:
-            RTF_SetError("Assertion failure");
-            retval = -1;
+            retval = SDL_SetError("Assertion failure");
             break;
         case ecEndOfFile:
-            RTF_SetError("End of file reached while reading RTF");
-            retval = -1;
+            retval = SDL_SetError("End of file reached while reading RTF");
             break;
         case ecFontNotFound:
-            RTF_SetError("Couldn't find font for text");
-            retval = -1;
+            retval = SDL_SetError("Couldn't find font for text");
             break;
         default:
-            RTF_SetError("Unknown error");
-            retval = -1;
+            retval = SDL_SetError("Unknown error");
             break;
     }
-    while ( ctx->psave ) {
+    while (ctx->psave) {
         ecPopRtfState(ctx);
     }
     ctx->stream = NULL;
 
-    if ( closeio ) {
+    if (closeio) {
         SDL_CloseIO(src);
     }
-    return(retval);
+    return retval;
 }
-int RTF_Load(RTF_Context *ctx, const char *file)
+
+SDL_bool RTF_Load(RTF_Context *ctx, const char *file)
 {
     SDL_IOStream *src = SDL_IOFromFile(file, "rb");
-    if ( src == NULL ) {
-        /*RTF_SetError(SDL_GetError());*/
-        return -1;
+    if (!src) {
+        return SDL_FALSE;
     }
     return RTF_Load_IO(ctx, src, 1);
 }
@@ -180,7 +169,7 @@ void RTF_Render(RTF_Context *ctx, SDL_Rect *rect, int yOffset)
 {
     SDL_Renderer *renderer = (SDL_Renderer *)ctx->renderer;
     SDL_Rect fullRect;
-    if ( !rect ) {
+    if (!rect) {
         SDL_GetRenderViewport(renderer, &fullRect);
         fullRect.x = 0;
         fullRect.y = 0;
@@ -197,5 +186,3 @@ void RTF_FreeContext(RTF_Context *ctx)
     SDL_free(ctx->fontEngine);
     SDL_free(ctx);
 }
-
-/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/rtfreadr.c b/src/rtfreadr.c
index 7db5bf8..6ce84cf 100644
--- a/src/rtfreadr.c
+++ b/src/rtfreadr.c
@@ -566,8 +566,8 @@ int ecPopRtfState(RTF_Context *ctx)
 int ecParseRtfKeyword(RTF_Context *ctx)
 {
     int ch;
-    char fParam = fFalse;
-    char fNeg = fFalse;
+    bool fParam = fFalse;
+    bool fNeg = fFalse;
     int param = 0;
     char *pch;
     char szKeyword[30];
diff --git a/src/rtftype.h b/src/rtftype.h
index 52fc74d..094dbe9 100644
--- a/src/rtftype.h
+++ b/src/rtftype.h
@@ -6,12 +6,10 @@
 #ifndef _RTFTYPE_H
 #define _RTFTYPE_H
 
-#if !defined(__cplusplus) && !defined(bool)
-typedef char bool;
-#endif
+#include <stdbool.h>
 
-#define fTrue 1
-#define fFalse 0
+#define fTrue true
+#define fFalse false
 
 typedef struct char_prop
 {