os2, geniconv: replaced many uses of libc calls with SDL_ counterparts.

FIXME: figure out a way to handle errno checks properly.
This commit is contained in:
Ozkan Sezer 2021-10-18 14:11:20 +03:00
parent 0a0f685473
commit c4bac66b74
4 changed files with 57 additions and 28 deletions

View file

@ -11,7 +11,7 @@ LIBFILE = geniconv.lib
all: $(LIBFILE) test.exe .symbolic all: $(LIBFILE) test.exe .symbolic
CFLAGS = -I$(%WATCOM)/h/os2 -I$(%WATCOM)/h -I. -bt=os2 -q -d0 -w2 CFLAGS = -I$(%WATCOM)/h/os2 -I$(%WATCOM)/h -I. -bt=os2 -q -d0 -w2 -DGENICONV_STANDALONE=1
SRCS = geniconv.c os2cp.c os2iconv.c SRCS = geniconv.c os2cp.c os2iconv.c
SRCS+= sys2utf8.c SRCS+= sys2utf8.c

View file

@ -22,11 +22,21 @@
#define INCL_DOSNLS #define INCL_DOSNLS
#define INCL_DOSERRORS #define INCL_DOSERRORS
#include <os2.h> #include <os2.h>
#include <string.h>
#include <ctype.h>
#include "os2cp.h" #include "os2cp.h"
#ifndef GENICONV_STANDALONE
#include "../../../SDL_internal.h"
#else
#include <string.h>
#include <ctype.h>
#define SDL_isspace isspace
#define SDL_strchr strchr
#define SDL_memcpy memcpy
#define SDL_strupr strupr
#define SDL_strcmp strcmp
#endif
typedef struct _CP2NAME { typedef struct _CP2NAME {
ULONG ulCode; ULONG ulCode;
PSZ pszName; PSZ pszName;
@ -354,28 +364,28 @@ unsigned long os2cpFromName(char *cp)
return (DosQueryCp(sizeof(aulCP), aulCP, &cCP) != NO_ERROR)? 0 : aulCP[0]; return (DosQueryCp(sizeof(aulCP), aulCP, &cCP) != NO_ERROR)? 0 : aulCP[0];
} }
while (isspace(*cp)) while (SDL_isspace(*cp))
cp++; cp++;
pcEnd = strchr(cp, ' '); pcEnd = SDL_strchr(cp, ' ');
if (pcEnd == NULL) if (pcEnd == NULL)
pcEnd = strchr(cp, '\0'); pcEnd = SDL_strchr(cp, '\0');
ulNext = pcEnd - cp; ulNext = pcEnd - cp;
if (ulNext >= sizeof(acBuf)) if (ulNext >= sizeof(acBuf))
return 0; return 0;
memcpy(acBuf, cp, ulNext); SDL_memcpy(acBuf, cp, ulNext);
acBuf[ulNext] = '\0'; acBuf[ulNext] = '\0';
strupr(acBuf); SDL_strupr(acBuf);
lCmp = strcmp(aName2CP[0].pszName, acBuf); lCmp = SDL_strcmp(aName2CP[0].pszName, acBuf);
if (lCmp > 0) if (lCmp > 0)
return 0; return 0;
else if (lCmp == 0) else if (lCmp == 0)
return aName2CP[0].ulCode; return aName2CP[0].ulCode;
lCmp = strcmp(aName2CP[ulHi].pszName, acBuf); lCmp = SDL_strcmp(aName2CP[ulHi].pszName, acBuf);
if (lCmp < 0) if (lCmp < 0)
return 0; return 0;
else if (lCmp == 0) else if (lCmp == 0)
@ -384,7 +394,7 @@ unsigned long os2cpFromName(char *cp)
while ((ulHi - ulLo) > 1) { while ((ulHi - ulLo) > 1) {
ulNext = (ulLo + ulHi) / 2; ulNext = (ulLo + ulHi) / 2;
lCmp = strcmp(aName2CP[ulNext].pszName, acBuf); lCmp = SDL_strcmp(aName2CP[ulNext].pszName, acBuf);
if (lCmp < 0) if (lCmp < 0)
ulLo = ulNext; ulLo = ulNext;
else if (lCmp > 0) else if (lCmp > 0)

View file

@ -31,19 +31,30 @@
#define _ULS_CALLCONV_ #define _ULS_CALLCONV_
#define CALLCONV _System #define CALLCONV _System
#include <uconv.h> #include <uconv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef ICONV_THREAD_SAFE #ifdef ICONV_THREAD_SAFE
#define INCL_DOSSEMAPHORES #define INCL_DOSSEMAPHORES
#define INCL_DOSERRORS #define INCL_DOSERRORS
#include <os2.h> #include <os2.h>
#endif #endif
#include "os2cp.h" #include "os2cp.h"
#ifndef GENICONV_STANDALONE
#include "../../../SDL_internal.h"
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !defined(min) #if !defined(min)
#define min(a, b) (((a) < (b)) ? (a) : (b)) #define min(a, b) (((a) < (b)) ? (a) : (b))
#endif #endif
#define SDL_min min
#define SDL_strcasecmp stricmp
#define SDL_snprintf _snprintf
#define SDL_malloc malloc
#define SDL_free free
#define SDL_memcpy memcpy
#endif
#define MAX_CP_NAME_LEN 64 #define MAX_CP_NAME_LEN 64
@ -82,7 +93,7 @@ static int uconv_open(const char *code, UconvObject *uobj)
{ {
int rc; int rc;
if (!stricmp(code, "UTF-16")) { if (!SDL_strcasecmp(code, "UTF-16")) {
*uobj = NULL; *uobj = NULL;
return ULS_SUCCESS; return ULS_SUCCESS;
} }
@ -92,7 +103,7 @@ static int uconv_open(const char *code, UconvObject *uobj)
unsigned long cp = os2cpFromName((char *)code); unsigned long cp = os2cpFromName((char *)code);
char cp_name[16]; char cp_name[16];
if (cp != 0 && _snprintf(cp_name, sizeof(cp_name), "IBM-%u", cp) > 0) if (cp != 0 && SDL_snprintf(cp_name, sizeof(cp_name), "IBM-%u", cp) > 0)
rc = _createUconvObj(cp_name, uobj); rc = _createUconvObj(cp_name, uobj);
} }
@ -113,7 +124,7 @@ extern iconv_t _System os2_iconv_open(const char* tocode, const char* fromcode)
if (fromcode == NULL) if (fromcode == NULL)
fromcode = ""; fromcode = "";
if (stricmp(tocode, fromcode) != 0) { if (SDL_strcasecmp(tocode, fromcode) != 0) {
rc = uconv_open(fromcode, &uo_fromcode); rc = uconv_open(fromcode, &uo_fromcode);
if (rc != ULS_SUCCESS) { if (rc != ULS_SUCCESS) {
errno = EINVAL; errno = EINVAL;
@ -131,7 +142,7 @@ extern iconv_t _System os2_iconv_open(const char* tocode, const char* fromcode)
uo_fromcode = NULL; uo_fromcode = NULL;
} }
iuobj = (iuconv_obj *) malloc(sizeof(iuconv_obj)); iuobj = (iuconv_obj *) SDL_malloc(sizeof(iuconv_obj));
iuobj->uo_tocode = uo_tocode; iuobj->uo_tocode = uo_tocode;
iuobj->uo_fromcode = uo_fromcode; iuobj->uo_fromcode = uo_fromcode;
iuobj->buf_len = 0; iuobj->buf_len = 0;
@ -158,8 +169,8 @@ extern size_t _System os2_iconv(iconv_t cd, char* * inbuf,
size_t ret = (size_t)(-1); size_t ret = (size_t)(-1);
if (uo_tocode == NULL && uo_fromcode == NULL) { if (uo_tocode == NULL && uo_fromcode == NULL) {
uc_buf_len = min(*inbytesleft, *outbytesleft); uc_buf_len = SDL_min(*inbytesleft, *outbytesleft);
memcpy(*outbuf, *inbuf, uc_buf_len); SDL_memcpy(*outbuf, *inbuf, uc_buf_len);
*inbytesleft -= uc_buf_len; *inbytesleft -= uc_buf_len;
*outbytesleft -= uc_buf_len; *outbytesleft -= uc_buf_len;
outbuf += uc_buf_len; outbuf += uc_buf_len;
@ -174,9 +185,9 @@ extern size_t _System os2_iconv(iconv_t cd, char* * inbuf,
if (uo_tocode && uo_fromcode && if (uo_tocode && uo_fromcode &&
(((iuconv_obj *)cd)->buf_len >> 1) < *inbytesleft) { (((iuconv_obj *)cd)->buf_len >> 1) < *inbytesleft) {
if (((iuconv_obj *)cd)->buf != NULL) if (((iuconv_obj *)cd)->buf != NULL)
free(((iuconv_obj *)cd)->buf); SDL_free(((iuconv_obj *)cd)->buf);
((iuconv_obj *)cd)->buf_len = *inbytesleft << 1; ((iuconv_obj *)cd)->buf_len = *inbytesleft << 1;
((iuconv_obj *)cd)->buf = (UniChar *)malloc(((iuconv_obj *)cd)->buf_len); ((iuconv_obj *)cd)->buf = (UniChar *)SDL_malloc(((iuconv_obj *)cd)->buf_len);
} }
if (uo_fromcode) { if (uo_fromcode) {
@ -260,9 +271,9 @@ int _System os2_iconv_close(iconv_t cd)
UniFreeUconvObject(((iuconv_obj *)cd)->uo_fromcode); UniFreeUconvObject(((iuconv_obj *)cd)->uo_fromcode);
if (((iuconv_obj *)cd)->buf != NULL) if (((iuconv_obj *)cd)->buf != NULL)
free(((iuconv_obj *)cd)->buf); SDL_free(((iuconv_obj *)cd)->buf);
free(cd); SDL_free(cd);
return 0; return 0;
} }

View file

@ -20,7 +20,15 @@
*/ */
#include "geniconv.h" #include "geniconv.h"
#ifndef GENICONV_STANDALONE
#include "../../../SDL_internal.h"
#else
#include <stdlib.h> #include <stdlib.h>
#define SDL_malloc malloc
#define SDL_realloc realloc
#define SDL_free free
#endif
int StrUTF8(int fToUTF8, char *pcDst, int cbDst, char *pcSrc, int cbSrc) int StrUTF8(int fToUTF8, char *pcDst, int cbDst, char *pcSrc, int cbSrc)
{ {
@ -83,25 +91,25 @@ int StrUTF8(int fToUTF8, char *pcDst, int cbDst, char *pcSrc, int cbSrc)
char *StrUTF8New(int fToUTF8, char *pcStr, int cbStr) char *StrUTF8New(int fToUTF8, char *pcStr, int cbStr)
{ {
int cbNewStr = (((cbStr > 4)? cbStr : 4) + 1) * 2; int cbNewStr = (((cbStr > 4)? cbStr : 4) + 1) * 2;
char *pszNewStr = (char *) malloc(cbNewStr); char *pszNewStr = (char *) SDL_malloc(cbNewStr);
if (pszNewStr == NULL) if (pszNewStr == NULL)
return NULL; return NULL;
cbNewStr = StrUTF8(fToUTF8, pszNewStr, cbNewStr, pcStr, cbStr); cbNewStr = StrUTF8(fToUTF8, pszNewStr, cbNewStr, pcStr, cbStr);
if (cbNewStr != -1) { if (cbNewStr != -1) {
pcStr = (char *) realloc(pszNewStr, cbNewStr + ((fToUTF8)? 1 : sizeof(short))); pcStr = (char *) SDL_realloc(pszNewStr, cbNewStr + ((fToUTF8)? 1 : sizeof(short)));
if (pcStr) if (pcStr)
return pcStr; return pcStr;
} }
free(pszNewStr); SDL_free(pszNewStr);
return NULL; return NULL;
} }
void StrUTF8Free(char *pszStr) void StrUTF8Free(char *pszStr)
{ {
free(pszStr); SDL_free(pszStr);
} }
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */