SDL  2.0
sys2utf8.c File Reference
#include "geniconv.h"
#include <stdlib.h>
+ Include dependency graph for sys2utf8.c:

Go to the source code of this file.

Functions

int StrUTF8 (int fToUTF8, char *pcDst, int cbDst, char *pcSrc, int cbSrc)
 
char * StrUTF8New (int fToUTF8, char *pcStr, int cbStr)
 
void StrUTF8Free (char *pszStr)
 

Function Documentation

◆ StrUTF8()

int StrUTF8 ( int  fToUTF8,
char *  pcDst,
int  cbDst,
char *  pcSrc,
int  cbSrc 
)

Definition at line 25 of file sys2utf8.c.

26 {
27  size_t rc;
28  char *pcDstStart = pcDst;
29  iconv_t cd;
30  char *pszToCP, *pszFromCP;
31  int fError = 0;
32 
33  if (cbDst < 4)
34  return -1;
35 
36  if (fToUTF8) {
37  pszToCP = "UTF-8";
38  pszFromCP = "";
39  } else {
40  pszToCP = "";
41  pszFromCP = "UTF-8";
42  }
43 
44  cd = iconv_open(pszToCP, pszFromCP);
45  if (cd == (iconv_t)-1)
46  return -1;
47 
48  while (cbSrc > 0) {
49  rc = iconv(cd, &pcSrc, (size_t *)&cbSrc, &pcDst, (size_t *)&cbDst);
50  if (rc == (size_t)-1) {
51  if (errno == EILSEQ) {
52  /* Try to skip invalid character */
53  pcSrc++;
54  cbSrc--;
55  continue;
56  }
57 
58  fError = 1;
59  break;
60  }
61  }
62 
63  iconv_close(cd);
64 
65  /* Write trailing ZERO (1 byte for UTF-8, 2 bytes for the system cp) */
66  if (fToUTF8) {
67  if (cbDst < 1) {
68  pcDst--;
69  fError = 1; /* The destination buffer overflow */
70  }
71  *pcDst = '\0';
72  } else {
73  if (cbDst < 2) {
74  pcDst -= (cbDst == 0)? 2 : 1;
75  fError = 1; /* The destination buffer overflow */
76  }
77  *((short *)pcDst) = '\0';
78  }
79 
80  return (fError) ? -1 : (pcDst - pcDstStart);
81 }
void * iconv_t
Definition: iconv.h:7
int iconv_close(iconv_t)
iconv_t iconv_open(const char *, const char *)
size_t iconv(iconv_t, char **, size_t *, char **, size_t *)

References iconv(), iconv_close(), and iconv_open().

Referenced by main(), and StrUTF8New().

◆ StrUTF8Free()

void StrUTF8Free ( char *  pszStr)

Definition at line 102 of file sys2utf8.c.

103 {
104  free(pszStr);
105 }
SDL_EventEntry * free
Definition: SDL_events.c:89

References free.

◆ StrUTF8New()

char* StrUTF8New ( int  fToUTF8,
char *  pcStr,
int  cbStr 
)

Definition at line 83 of file sys2utf8.c.

84 {
85  int cbNewStr = (((cbStr > 4)? cbStr : 4) + 1) * 2;
86  char *pszNewStr = (char *) malloc(cbNewStr);
87 
88  if (pszNewStr == NULL)
89  return NULL;
90 
91  cbNewStr = StrUTF8(fToUTF8, pszNewStr, cbNewStr, pcStr, cbStr);
92  if (cbNewStr != -1) {
93  pcStr = (char *) realloc(pszNewStr, cbNewStr + ((fToUTF8)? 1 : sizeof(short)));
94  if (pcStr)
95  return pcStr;
96  }
97 
98  free(pszNewStr);
99  return NULL;
100 }
#define malloc
Definition: SDL_qsort.c:46
#define NULL
Definition: begin_code.h:163
int StrUTF8(int fToUTF8, char *pcDst, int cbDst, char *pcSrc, int cbSrc)
Definition: sys2utf8.c:25

References free, malloc, NULL, and StrUTF8().

Referenced by main().