comparison src/stdlib/SDL_iconv.c @ 1668:4da1ee79c9af SDL-1.3

more tweaking indent options
author Sam Lantinga <slouken@libsdl.org>
date Mon, 29 May 2006 04:04:35 +0000
parents 782fd950bd46
children
comparison
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
29 #ifdef HAVE_ICONV 29 #ifdef HAVE_ICONV
30 30
31 #include <errno.h> 31 #include <errno.h>
32 32
33 size_t 33 size_t
34 SDL_iconv (SDL_iconv_t cd, 34 SDL_iconv(SDL_iconv_t cd,
35 char **inbuf, size_t * inbytesleft, 35 char **inbuf, size_t * inbytesleft,
36 char **outbuf, size_t * outbytesleft) 36 char **outbuf, size_t * outbytesleft)
37 { 37 {
38 size_t retCode = iconv (cd, inbuf, inbytesleft, outbuf, outbytesleft); 38 size_t retCode = iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
39 if (retCode == (size_t) - 1) { 39 if (retCode == (size_t) - 1) {
40 switch (errno) { 40 switch (errno) {
41 case E2BIG: 41 case E2BIG:
42 return SDL_ICONV_E2BIG; 42 return SDL_ICONV_E2BIG;
43 case EILSEQ: 43 case EILSEQ:
119 "UCS-2", ENCODING_UCS2}, { 119 "UCS-2", ENCODING_UCS2}, {
120 "UCS4", ENCODING_UCS4}, { 120 "UCS4", ENCODING_UCS4}, {
121 "UCS-4", ENCODING_UCS4},}; 121 "UCS-4", ENCODING_UCS4},};
122 122
123 SDL_iconv_t 123 SDL_iconv_t
124 SDL_iconv_open (const char *tocode, const char *fromcode) 124 SDL_iconv_open(const char *tocode, const char *fromcode)
125 { 125 {
126 int src_fmt = ENCODING_UNKNOWN; 126 int src_fmt = ENCODING_UNKNOWN;
127 int dst_fmt = ENCODING_UNKNOWN; 127 int dst_fmt = ENCODING_UNKNOWN;
128 int i; 128 int i;
129 129
130 for (i = 0; i < SDL_arraysize (encodings); ++i) { 130 for (i = 0; i < SDL_arraysize(encodings); ++i) {
131 if (SDL_strcasecmp (fromcode, encodings[i].name) == 0) { 131 if (SDL_strcasecmp(fromcode, encodings[i].name) == 0) {
132 src_fmt = encodings[i].format; 132 src_fmt = encodings[i].format;
133 if (dst_fmt != ENCODING_UNKNOWN) { 133 if (dst_fmt != ENCODING_UNKNOWN) {
134 break; 134 break;
135 } 135 }
136 } 136 }
137 if (SDL_strcasecmp (tocode, encodings[i].name) == 0) { 137 if (SDL_strcasecmp(tocode, encodings[i].name) == 0) {
138 dst_fmt = encodings[i].format; 138 dst_fmt = encodings[i].format;
139 if (src_fmt != ENCODING_UNKNOWN) { 139 if (src_fmt != ENCODING_UNKNOWN) {
140 break; 140 break;
141 } 141 }
142 } 142 }
143 } 143 }
144 if (src_fmt != ENCODING_UNKNOWN && dst_fmt != ENCODING_UNKNOWN) { 144 if (src_fmt != ENCODING_UNKNOWN && dst_fmt != ENCODING_UNKNOWN) {
145 SDL_iconv_t cd = (SDL_iconv_t) SDL_malloc (sizeof (*cd)); 145 SDL_iconv_t cd = (SDL_iconv_t) SDL_malloc(sizeof(*cd));
146 if (cd) { 146 if (cd) {
147 cd->src_fmt = src_fmt; 147 cd->src_fmt = src_fmt;
148 cd->dst_fmt = dst_fmt; 148 cd->dst_fmt = dst_fmt;
149 return cd; 149 return cd;
150 } 150 }
151 } 151 }
152 return (SDL_iconv_t) - 1; 152 return (SDL_iconv_t) - 1;
153 } 153 }
154 154
155 size_t 155 size_t
156 SDL_iconv (SDL_iconv_t cd, 156 SDL_iconv(SDL_iconv_t cd,
157 char **inbuf, size_t * inbytesleft, 157 char **inbuf, size_t * inbytesleft,
158 char **outbuf, size_t * outbytesleft) 158 char **outbuf, size_t * outbytesleft)
159 { 159 {
160 /* For simplicity, we'll convert everything to and from UCS-4 */ 160 /* For simplicity, we'll convert everything to and from UCS-4 */
161 char *src, *dst; 161 char *src, *dst;
162 size_t srclen, dstlen; 162 size_t srclen, dstlen;
163 Uint32 ch; 163 Uint32 ch;
742 } 742 }
743 return total; 743 return total;
744 } 744 }
745 745
746 int 746 int
747 SDL_iconv_close (SDL_iconv_t cd) 747 SDL_iconv_close(SDL_iconv_t cd)
748 { 748 {
749 if (cd && cd != (SDL_iconv_t) - 1) { 749 if (cd && cd != (SDL_iconv_t) - 1) {
750 SDL_free (cd); 750 SDL_free(cd);
751 } 751 }
752 return 0; 752 return 0;
753 } 753 }
754 754
755 #endif /* !HAVE_ICONV */ 755 #endif /* !HAVE_ICONV */
756 756
757 char * 757 char *
758 SDL_iconv_string (const char *tocode, const char *fromcode, char *inbuf, 758 SDL_iconv_string(const char *tocode, const char *fromcode, char *inbuf,
759 size_t inbytesleft) 759 size_t inbytesleft)
760 { 760 {
761 SDL_iconv_t cd; 761 SDL_iconv_t cd;
762 char *string; 762 char *string;
763 size_t stringsize; 763 size_t stringsize;
764 char *outbuf; 764 char *outbuf;
765 size_t outbytesleft; 765 size_t outbytesleft;
766 size_t retCode = 0; 766 size_t retCode = 0;
767 767
768 cd = SDL_iconv_open (tocode, fromcode); 768 cd = SDL_iconv_open(tocode, fromcode);
769 if (cd == (SDL_iconv_t) - 1) { 769 if (cd == (SDL_iconv_t) - 1) {
770 return NULL; 770 return NULL;
771 } 771 }
772 772
773 stringsize = inbytesleft > 4 ? inbytesleft : 4; 773 stringsize = inbytesleft > 4 ? inbytesleft : 4;
774 string = SDL_malloc (stringsize); 774 string = SDL_malloc(stringsize);
775 if (!string) { 775 if (!string) {
776 SDL_iconv_close (cd); 776 SDL_iconv_close(cd);
777 return NULL; 777 return NULL;
778 } 778 }
779 outbuf = string; 779 outbuf = string;
780 outbytesleft = stringsize; 780 outbytesleft = stringsize;
781 SDL_memset (outbuf, 0, 4); 781 SDL_memset(outbuf, 0, 4);
782 782
783 while (inbytesleft > 0) { 783 while (inbytesleft > 0) {
784 retCode = 784 retCode = SDL_iconv(cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
785 SDL_iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
786 switch (retCode) { 785 switch (retCode) {
787 case SDL_ICONV_E2BIG: 786 case SDL_ICONV_E2BIG:
788 { 787 {
789 char *oldstring = string; 788 char *oldstring = string;
790 stringsize *= 2; 789 stringsize *= 2;
791 string = SDL_realloc (string, stringsize); 790 string = SDL_realloc(string, stringsize);
792 if (!string) { 791 if (!string) {
793 SDL_iconv_close (cd); 792 SDL_iconv_close(cd);
794 return NULL; 793 return NULL;
795 } 794 }
796 outbuf = string + (outbuf - oldstring); 795 outbuf = string + (outbuf - oldstring);
797 outbytesleft = stringsize - (outbuf - string); 796 outbytesleft = stringsize - (outbuf - string);
798 SDL_memset (outbuf, 0, 4); 797 SDL_memset(outbuf, 0, 4);
799 } 798 }
800 break; 799 break;
801 case SDL_ICONV_EILSEQ: 800 case SDL_ICONV_EILSEQ:
802 /* Try skipping some input data - not perfect, but... */ 801 /* Try skipping some input data - not perfect, but... */
803 ++inbuf; 802 ++inbuf;
808 /* We can't continue... */ 807 /* We can't continue... */
809 inbytesleft = 0; 808 inbytesleft = 0;
810 break; 809 break;
811 } 810 }
812 } 811 }
813 SDL_iconv_close (cd); 812 SDL_iconv_close(cd);
814 813
815 return string; 814 return string;
816 } 815 }
817 816
818 /* vi: set ts=4 sw=4 expandtab: */ 817 /* vi: set ts=4 sw=4 expandtab: */