From a981ebc6f801f417c18770a77c63b3fd740707a9 Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Fri, 3 Dec 2021 18:35:50 +0300
Subject: [PATCH] replaced all libc calls with SDL_ counterparts.
---
SDL_rtf.c | 19 ++++-------
SDL_rtfreadr.c | 22 ++++--------
rtfactn.c | 30 +++++++----------
rtfreadr.c | 91 ++++++++++++++++++++++++--------------------------
showrtf.c | 44 +++++++++++-------------
5 files changed, 88 insertions(+), 118 deletions(-)
diff --git a/SDL_rtf.c b/SDL_rtf.c
index 07423ae..5a028ec 100644
--- a/SDL_rtf.c
+++ b/SDL_rtf.c
@@ -19,11 +19,6 @@
3. This notice may not be removed or altered from any source distribution.
*/
-/* $Id$ */
-
-#include <stdlib.h>
-#include <string.h>
-
#include "SDL.h"
#include "SDL_rtf.h"
@@ -54,20 +49,20 @@ RTF_Context *RTF_CreateContext(SDL_Renderer *renderer, RTF_FontEngine *fontEngin
return(NULL);
}
- ctx = (RTF_Context *)malloc(sizeof(*ctx));
+ ctx = (RTF_Context *)SDL_malloc(sizeof(*ctx));
if ( ctx == NULL ) {
RTF_SetError("Out of memory");
return(NULL);
}
- memset(ctx, 0, sizeof(*ctx));
+ SDL_memset(ctx, 0, sizeof(*ctx));
ctx->renderer = renderer;
- ctx->fontEngine = malloc(sizeof *fontEngine);
+ ctx->fontEngine = SDL_malloc(sizeof *fontEngine);
if ( ctx->fontEngine == NULL ) {
RTF_SetError("Out of memory");
- free(ctx);
+ SDL_free(ctx);
return(NULL);
}
- memcpy(ctx->fontEngine, fontEngine, sizeof(*fontEngine));
+ SDL_memcpy(ctx->fontEngine, fontEngine, sizeof(*fontEngine));
return(ctx);
}
@@ -197,8 +192,8 @@ void RTF_FreeContext(RTF_Context *ctx)
{
/* Free it all! */
ecClearContext(ctx);
- free(ctx->fontEngine);
- free(ctx);
+ SDL_free(ctx->fontEngine);
+ SDL_free(ctx);
}
/* vi: set ts=4 sw=4 expandtab: */
diff --git a/SDL_rtfreadr.c b/SDL_rtfreadr.c
index 139c1cd..6171d21 100644
--- a/SDL_rtfreadr.c
+++ b/SDL_rtfreadr.c
@@ -19,13 +19,6 @@
3. This notice may not be removed or altered from any source distribution.
*/
-/* $Id$ */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
-
#include "SDL_rtf.h"
#include "SDL_rtfreadr.h"
@@ -72,7 +65,7 @@ void RTF_FreeSurface(void *surface)
*/
void *RTF_CreateColor(int r, int g, int b)
{
- SDL_Color *color = (SDL_Color *) malloc(sizeof(*color));
+ SDL_Color *color = (SDL_Color *) SDL_malloc(sizeof(*color));
if (!color)
return NULL;
color->r = r;
@@ -87,8 +80,7 @@ void *RTF_CreateColor(int r, int g, int b)
*/
void RTF_FreeColor(void *color)
{
- if (color)
- free(color);
+ SDL_free(color);
}
/*
@@ -176,7 +168,7 @@ static RTF_Surface *CreateSurface(RTF_Context *ctx,
RTF_TextBlock *textBlock, int offset, int numChars)
{
SDL_Renderer *renderer = (SDL_Renderer *)ctx->renderer;
- RTF_Surface *surface = (RTF_Surface *) malloc(sizeof(*surface));
+ RTF_Surface *surface = (RTF_Surface *) SDL_malloc(sizeof(*surface));
SDL_Color color;
if (surface)
@@ -191,12 +183,12 @@ static RTF_Surface *CreateSurface(RTF_Context *ctx,
if (textBlock->color)
color = *(SDL_Color *) textBlock->color;
else
- memset(&color, 0, sizeof(color));
+ SDL_memset(&color, 0, sizeof(color));
surface->surface = ((RTF_FontEngine *) ctx->fontEngine)->RenderText(textBlock->font, renderer, text, color);
*end = ch;
if (!surface->surface)
{
- free(surface);
+ SDL_free(surface);
return (NULL);
}
surface->x = 0;
@@ -228,7 +220,7 @@ static int TextWithinWidth(RTF_TextBlock *textBlock, int offset,
for (wrapIndex = offset + num - 1; wrapIndex > offset;
--wrapIndex)
{
- if (isspace(
+ if (SDL_isspace(
textBlock->text[textBlock->byteOffsets[wrapIndex]]))
break;
}
@@ -248,7 +240,7 @@ static int ReflowLine(RTF_Context *ctx, RTF_Line *line, int width)
RTF_Surface *surface = line->startSurface;
line->startSurface = surface->next;
RTF_FreeSurface(surface->surface);
- free(surface);
+ SDL_free(surface);
}
if (line->start)
{
diff --git a/rtfactn.c b/rtfactn.c
index c21eaa0..3f63516 100644
--- a/rtfactn.c
+++ b/rtfactn.c
@@ -3,11 +3,8 @@
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnrtfspec/html/rtfspec.asp
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stddef.h>
-#include <ctype.h>
+#include "SDL.h"
+
#include "rtftype.h"
#include "rtfdecl.h"
@@ -235,13 +232,13 @@ int ecParseSpecialProperty(RTF_Context *ctx, IPROP iprop, int val)
ctx->values[2] = val;
return ecOK;
case ipropPard:
- memset(&ctx->pap, 0, sizeof(ctx->pap));
+ SDL_memset(&ctx->pap, 0, sizeof(ctx->pap));
return ecOK;
case ipropPlain:
- memset(&ctx->chp, 0, sizeof(ctx->chp));
+ SDL_memset(&ctx->chp, 0, sizeof(ctx->chp));
return ecOK;
case ipropSectd:
- memset(&ctx->sep, 0, sizeof(ctx->sep));
+ SDL_memset(&ctx->sep, 0, sizeof(ctx->sep));
return ecOK;
default:
return ecBadTable;
@@ -269,7 +266,7 @@ int ecTranslateKeyword(RTF_Context *ctx, char *szKeyword, int param,
/* search for szKeyword in rgsymRtf */
for (isym = 0; isym < isymMax; isym++)
- if (strcmp(szKeyword, rgsymRtf[isym].szKeyword) == 0)
+ if (SDL_strcmp(szKeyword, rgsymRtf[isym].szKeyword) == 0)
break;
if (isym == isymMax) /* control word not found */
{
@@ -360,24 +357,21 @@ int ecEndGroupAction(RTF_Context *ctx, RDS rds)
switch (ctx->rds)
{
case rdsTitle:
- if (ctx->title)
- free(ctx->title);
+ SDL_free(ctx->title);
ctx->data[ctx->datapos] = '\0';
- ctx->title = *ctx->data ? strdup(ctx->data) : NULL;
+ ctx->title = *ctx->data ? SDL_strdup(ctx->data) : NULL;
ctx->datapos = 0;
break;
case rdsSubject:
- if (ctx->subject)
- free(ctx->subject);
+ SDL_free(ctx->subject);
ctx->data[ctx->datapos] = '\0';
- ctx->subject = *ctx->data ? strdup(ctx->data) : NULL;
+ ctx->subject = *ctx->data ? SDL_strdup(ctx->data) : NULL;
ctx->datapos = 0;
break;
case rdsAuthor:
- if (ctx->author)
- free(ctx->author);
+ SDL_free(ctx->author);
ctx->data[ctx->datapos] = '\0';
- ctx->author = *ctx->data ? strdup(ctx->data) : NULL;
+ ctx->author = *ctx->data ? SDL_strdup(ctx->data) : NULL;
ctx->datapos = 0;
break;
default:
diff --git a/rtfreadr.c b/rtfreadr.c
index 952c77f..ba7f82c 100644
--- a/rtfreadr.c
+++ b/rtfreadr.c
@@ -3,10 +3,8 @@
* http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnrtfspec/html/rtfspec.asp
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <ctype.h>
+#include "SDL.h"
+
#include "rtftype.h"
#include "rtfdecl.h"
@@ -20,13 +18,13 @@ static void FreeTextBlock(RTF_TextBlock *text);
int ecAddFontEntry(RTF_Context *ctx, int number, const char *name,
int family, int charset)
{
- RTF_FontEntry *entry = (RTF_FontEntry *) malloc(sizeof(*entry));
+ RTF_FontEntry *entry = (RTF_FontEntry *) SDL_malloc(sizeof(*entry));
if (!entry)
return ecStackOverflow;
entry->number = number;
- entry->name = strdup(name);
+ entry->name = SDL_strdup(name);
entry->family = (RTF_FontFamily) family;
entry->charset = charset;
entry->fonts = NULL;
@@ -91,7 +89,7 @@ void *ecLookupFont(RTF_Context *ctx)
}
/* Create a new font entry */
- font = (RTF_Font *) malloc(sizeof(*font));
+ font = (RTF_Font *) SDL_malloc(sizeof(*font));
if (!font)
return NULL;
@@ -99,7 +97,7 @@ void *ecLookupFont(RTF_Context *ctx)
entry->family, entry->charset, size, style);
if (!font->font)
{
- free(font);
+ SDL_free(font);
return NULL;
}
font->size = size;
@@ -119,16 +117,16 @@ int ecClearFonts(RTF_Context *ctx)
RTF_FontEntry *entry = ctx->fontTable;
ctx->fontTable = entry->next;
- free(entry->name);
+ SDL_free(entry->name);
while (entry->fonts)
{
RTF_Font *font = entry->fonts;
entry->fonts = font->next;
RTF_FreeFont(ctx->fontEngine, font->font);
- free(font);
+ SDL_free(font);
}
- free(entry);
+ SDL_free(entry);
}
return ecOK;
}
@@ -139,7 +137,7 @@ int ecClearFonts(RTF_Context *ctx)
int ecAddColorEntry(RTF_Context *ctx, int r, int g, int b)
{
RTF_ColorEntry *ptr;
- RTF_ColorEntry *entry = (RTF_ColorEntry *) malloc(sizeof(*entry));
+ RTF_ColorEntry *entry = (RTF_ColorEntry *) SDL_malloc(sizeof(*entry));
if (!entry)
return ecStackOverflow;
@@ -186,7 +184,7 @@ int ecClearColors(RTF_Context *ctx)
{
e2 = e->next;
RTF_FreeColor(e->color);
- free(e);
+ SDL_free(e);
}
return ecOK;
}
@@ -204,7 +202,7 @@ int ecAddLine(RTF_Context *ctx)
if (!font)
return ecFontNotFound;
- line = (RTF_Line *) malloc(sizeof(*line));
+ line = (RTF_Line *) SDL_malloc(sizeof(*line));
if (!line)
return ecStackOverflow;
@@ -275,17 +273,17 @@ int ecAddText(RTF_Context *ctx, const char *text)
}
line = ctx->last;
- textBlock = (RTF_TextBlock *) malloc(sizeof(*textBlock));
+ textBlock = (RTF_TextBlock *) SDL_malloc(sizeof(*textBlock));
if (!textBlock)
return ecStackOverflow;
textBlock->font = font;
textBlock->color = ecLookupColor(ctx);
- numChars = strlen(text) + 1;
+ numChars = SDL_strlen(text) + 1;
textBlock->tabs = line->tabs;
- textBlock->text = strdup(text);
- textBlock->byteOffsets = (int *) malloc(numChars * sizeof(int));
- textBlock->pixelOffsets = (int *) malloc(numChars * sizeof(int));
+ textBlock->text = SDL_strdup(text);
+ textBlock->byteOffsets = (int *) SDL_malloc(numChars * sizeof(int));
+ textBlock->pixelOffsets = (int *) SDL_malloc(numChars * sizeof(int));
if (!textBlock->text || !textBlock->byteOffsets ||
!textBlock->pixelOffsets)
{
@@ -334,36 +332,36 @@ int ecClearContext(RTF_Context *ctx)
{
if (ctx->data)
{
- free(ctx->data);
+ SDL_free(ctx->data);
ctx->data = NULL;
ctx->datapos = 0;
ctx->datamax = 0;
}
- memset(ctx->values, 0, sizeof(ctx->values));
+ SDL_memset(ctx->values, 0, sizeof(ctx->values));
ecClearFonts(ctx);
ecClearColors(ctx);
if (ctx->title)
{
- free(ctx->title);
+ SDL_free(ctx->title);
ctx->title = NULL;
}
if (ctx->subject)
{
- free(ctx->subject);
+ SDL_free(ctx->subject);
ctx->subject = NULL;
}
if (ctx->author)
{
- free(ctx->author);
+ SDL_free(ctx->author);
ctx->author = NULL;
}
- memset(&ctx->chp, 0, sizeof(ctx->chp));
- memset(&ctx->pap, 0, sizeof(ctx->pap));
- memset(&ctx->sep, 0, sizeof(ctx->sep));
- memset(&ctx->dop, 0, sizeof(ctx->dop));
+ SDL_memset(&ctx->chp, 0, sizeof(ctx->chp));
+ SDL_memset(&ctx->pap, 0, sizeof(ctx->pap));
+ SDL_memset(&ctx->sep, 0, sizeof(ctx->sep));
+ SDL_memset(&ctx->dop, 0, sizeof(ctx->dop));
ecClearLines(ctx);
@@ -460,11 +458,11 @@ int ecRtfParse(RTF_Context *ctx)
if (ctx->ris != risHex)
return ecAssertion;
b = b << 4;
- if (isdigit(ch))
+ if (SDL_isdigit(ch))
b += (char) ch - '0';
else
{
- if (islower(ch))
+ if (SDL_islower(ch))
{
if (ch < 'a' || ch > 'f')
return ecInvalidHex;
@@ -505,7 +503,7 @@ int ecRtfParse(RTF_Context *ctx)
*/
int ecPushRtfState(RTF_Context *ctx)
{
- SAVE *psaveNew = malloc(sizeof(SAVE));
+ SAVE *psaveNew = SDL_malloc(sizeof(SAVE));
if (!psaveNew)
return ecStackOverflow;
@@ -554,7 +552,7 @@ int ecPopRtfState(RTF_Context *ctx)
psaveOld = ctx->psave;
ctx->psave = ctx->psave->pNext;
ctx->cGroup--;
- free(psaveOld);
+ SDL_free(psaveOld);
return ecOK;
}
@@ -579,13 +577,13 @@ int ecParseRtfKeyword(RTF_Context *ctx)
szParameter[0] = '\0';
if (ecRtfGetChar(ctx, &ch) != ecOK)
return ecEndOfFile;
- if (!isalpha(ch)) /* a control symbol; no delimiter. */
+ if (!SDL_isalpha(ch)) /* a control symbol; no delimiter. */
{
szKeyword[0] = (char) ch;
szKeyword[1] = '\0';
return ecTranslateKeyword(ctx, szKeyword, 0, fParam);
}
- for (pch = szKeyword; isalpha(ch);)
+ for (pch = szKeyword; SDL_isalpha(ch);)
{
*pch++ = (char) ch;
if (ecRtfGetChar(ctx, &ch) != ecOK)
@@ -598,20 +596,20 @@ int ecParseRtfKeyword(RTF_Context *ctx)
if (ecRtfGetChar(ctx, &ch) != ecOK)
return ecEndOfFile;
}
- if (isdigit(ch))
+ if (SDL_isdigit(ch))
{
fParam = fTrue; /* a digit after the control means we have a parameter */
- for (pch = szParameter; isdigit(ch);)
+ for (pch = szParameter; SDL_isdigit(ch);)
{
*pch++ = (char) ch;
if (ecRtfGetChar(ctx, &ch) != ecOK)
return ecEndOfFile;
}
*pch = '\0';
- param = atoi(szParameter);
+ param = SDL_atoi(szParameter);
if (fNeg)
param = -param;
- ctx->lParam = atol(szParameter);
+ ctx->lParam = SDL_strtol(szParameter, NULL, 10);
if (fNeg)
ctx->lParam = -ctx->lParam;
}
@@ -694,7 +692,7 @@ int ecPrintChar(RTF_Context *ctx, int ch)
if (ctx->datapos >= (ctx->datamax - 4))
{
ctx->datamax += 256; /* 256 byte chunk size */
- ctx->data = (char *) realloc(ctx->data, ctx->datamax);
+ ctx->data = (char *) SDL_realloc(ctx->data, ctx->datamax);
if (!ctx->data)
{
return ecStackOverflow;
@@ -798,7 +796,7 @@ static void FreeLine(RTF_Line *line)
RTF_Surface *surface = line->startSurface;
line->startSurface = surface->next;
RTF_FreeSurface(surface->surface);
- free(surface);
+ SDL_free(surface);
}
while (line->start)
{
@@ -807,18 +805,15 @@ static void FreeLine(RTF_Line *line)
line->start = text->next;
FreeTextBlock(text);
}
- free(line);
+ SDL_free(line);
}
static void FreeTextBlock(RTF_TextBlock *text)
{
- if (text->text)
- free(text->text);
- if (text->byteOffsets)
- free(text->byteOffsets);
- if (text->pixelOffsets)
- free(text->pixelOffsets);
- free(text);
+ SDL_free(text->text);
+ SDL_free(text->byteOffsets);
+ SDL_free(text->pixelOffsets);
+ SDL_free(text);
}
/* vi: set ts=4 sw=4 expandtab: */
diff --git a/showrtf.c b/showrtf.c
index 9ef4df7..3041a4a 100644
--- a/showrtf.c
+++ b/showrtf.c
@@ -19,14 +19,8 @@
3. This notice may not be removed or altered from any source distribution.
*/
-/* $Id$ */
-
/* A simple program to test the RTF rendering of the SDL_rtf library */
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-
#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_rtf.h"
@@ -163,21 +157,20 @@ static void SDLCALL FreeFont(void *_font)
static void LoadRTF(RTF_Context *ctx, const char *file)
{
if ( RTF_Load(ctx, file) < 0 ) {
- fprintf(stderr, "Couldn't load %s: %s\n", file, RTF_GetError());
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, RTF_GetError());
return;
}
}
static void PrintUsage(const char *argv0)
{
- printf("Usage: %s -fdefault font.ttf [-froman font.ttf] [-fswiss font.ttf] [-fmodern font.ttf] [-fscript font.ttf] [-fdecor font.ttf] [-ftech font.ttf] file.rtf\n", argv0);
+ SDL_Log("Usage: %s -fdefault font.ttf [-froman font.ttf] [-fswiss font.ttf] [-fmodern font.ttf] [-fscript font.ttf] [-fdecor font.ttf] [-ftech font.ttf] file.rtf\n", argv0);
}
-static void cleanup(int exitcode)
+static void cleanup(void)
{
TTF_Quit();
SDL_Quit();
- exit(exitcode);
}
int main(int argc, char *argv[])
@@ -195,19 +188,19 @@ int main(int argc, char *argv[])
/* Parse command line arguments */
for ( i = 1; i < argc; ++i ) {
- if ( strcmp(argv[i], "-fdefault") == 0 ) {
+ if ( SDL_strcmp(argv[i], "-fdefault") == 0 ) {
FontList[FontFamilyToIndex(RTF_FontDefault)] = argv[++i];
- } else if ( strcmp(argv[i], "-froman") == 0 ) {
+ } else if ( SDL_strcmp(argv[i], "-froman") == 0 ) {
FontList[FontFamilyToIndex(RTF_FontRoman)] = argv[++i];
- } else if ( strcmp(argv[i], "-fswiss") == 0 ) {
+ } else if ( SDL_strcmp(argv[i], "-fswiss") == 0 ) {
FontList[FontFamilyToIndex(RTF_FontSwiss)] = argv[++i];
- } else if ( strcmp(argv[i], "-fmodern") == 0 ) {
+ } else if ( SDL_strcmp(argv[i], "-fmodern") == 0 ) {
FontList[FontFamilyToIndex(RTF_FontModern)] = argv[++i];
- } else if ( strcmp(argv[i], "-fscript") == 0 ) {
+ } else if ( SDL_strcmp(argv[i], "-fscript") == 0 ) {
FontList[FontFamilyToIndex(RTF_FontScript)] = argv[++i];
- } else if ( strcmp(argv[i], "-fdecor") == 0 ) {
+ } else if ( SDL_strcmp(argv[i], "-fdecor") == 0 ) {
FontList[FontFamilyToIndex(RTF_FontDecor)] = argv[++i];
- } else if ( strcmp(argv[i], "-ftech") == 0 ) {
+ } else if ( SDL_strcmp(argv[i], "-ftech") == 0 ) {
FontList[FontFamilyToIndex(RTF_FontTech)] = argv[++i];
} else {
break;
@@ -222,14 +215,15 @@ int main(int argc, char *argv[])
/* Initialize the TTF library */
if ( TTF_Init() < 0 ) {
- fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError());
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize TTF: %s\n",SDL_GetError());
SDL_Quit();
return(3);
}
if (SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE, &window, &renderer) < 0) {
- fprintf(stderr, "SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError());
- cleanup(4);
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError());
+ cleanup();
+ return(4);
}
/* Create and load the RTF document */
@@ -241,8 +235,9 @@ int main(int argc, char *argv[])
fontEngine.FreeFont = FreeFont;
ctx = RTF_CreateContext(renderer, &fontEngine);
if ( ctx == NULL ) {
- fprintf(stderr, "Couldn't create RTF context: %s\n", RTF_GetError());
- cleanup(5);
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create RTF context: %s\n", RTF_GetError());
+ cleanup();
+ return(5);
}
LoadRTF(ctx, argv[i]);
SDL_SetWindowTitle(window, RTF_GetTitle(ctx));
@@ -257,7 +252,7 @@ int main(int argc, char *argv[])
while (SDL_PollEvent(&event)) {
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED) {
float ratio = (float)offset / height;
- printf("Resetting window\n");
+ SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Resetting window\n");
SDL_GetWindowSize(window, &w, &h);
SDL_RenderSetViewport(renderer, NULL);
height = RTF_GetHeight(ctx, w);
@@ -330,9 +325,8 @@ int main(int argc, char *argv[])
/* Clean up and exit */
RTF_FreeContext(ctx);
- cleanup(0);
+ cleanup();
- /* Not reached, but fixes compiler warnings */
return 0;
}