DEADSOFTWARE

Patched for Linux
[mp3cc.git] / MPC.3.5.LINUX / preverifier / convert_md.c
1 /*
2 * @(#)convert_md.c 1.2 00/11/22
3 *
4 * Copyright 1995-1998 by Sun Microsystems, Inc.,
5 * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
6 * All rights reserved.
7 *
8 * This software is the confidential and proprietary information
9 * of Sun Microsystems, Inc. ("Confidential Information"). You
10 * shall not disclose such Confidential Information and shall use
11 * it only in accordance with the terms of the license agreement
12 * you entered into with Sun.
13 * Use is subject to license terms.
14 */
16 #include <stdio.h>
17 //#include <errno.h>
18 #include <string.h>
20 #ifdef UNIX
21 #include <langinfo.h>
22 #include <iconv.h>
23 #include <locale.h>
24 #include <stdlib.h>
26 #define UTF8 "UTF-8"
28 static iconv_t
29 open_iconv(const char* to, const char* from) {
30 iconv_t ic = iconv_open(to, from);
31 if (ic == (iconv_t)-1) {
32 // if (errno == EINVAL) {
33 //if (0)
34 /* There is a bug in some versions of Solaris in which
35 * nl_langinfo() returns a string beginning with ISO, but you
36 * have to remove the ISO before calling open_iconv.
37 */
38 // if (strncmp(from, "ISO", 3) == 0) {
39 // /* Try again, but with the ISO in front of the "from" */
40 // return open_iconv(to, from + 3);
41 // } else if (strncmp(to, "ISO", 3) == 0) {
42 // /* Try again, but with the ISO in front of the "to" */
43 // return open_iconv(to + 3, from);
44 // } else {
45 // fprintf(stderr, "%s to %s not supported on this platform\n",
46 // from, to);
47 // }
48 //} else {
49 // perror("iconv_open error: ");
50 //}
51 exit(1);
52 }
53 return ic;
54 }
56 static char*
57 get_langinfo_codeset()
58 {
59 static char *name = NULL;
61 if (name == NULL) {
62 name = nl_langinfo(CODESET);
63 if (name == NULL || strlen(name) == 0) {
64 name = "ISO8859-1";
65 }
66 }
67 return name;
68 }
70 int native2utf8(const char* from, char* to, int buflen) {
71 int ret;
72 size_t ileft, oleft;
73 char *langinfo_codeset = get_langinfo_codeset();
74 iconv_t ic;
75 if (strncmp(langinfo_codeset, UTF8, 5) == 0) {
76 /* don't invoke 'iconv' functions to do the
77 * conversion if it's already in UTF-8 encoding
78 */
79 memcpy(to, from, buflen);
80 return 0;
81 }
83 ic = open_iconv(UTF8, get_langinfo_codeset());
84 memset(to, 0, buflen);
85 ileft = strlen(from);
86 oleft = buflen;
88 ret = iconv(ic, &from, &ileft, &to, &oleft);
89 if (ret == (size_t)-1) {
90 fprintf(stderr, "native2utf8:Failed to convert (err=%d)\n", ret);
91 exit(1);
92 }
93 iconv_close(ic);
95 return buflen-oleft;
96 }
98 int utf2native(const char* from, char* to, int buflen) {
99 int ret;
100 size_t ileft, oleft;
102 char *langinfo_codeset = get_langinfo_codeset();
103 iconv_t ic;
105 if (strncmp(langinfo_codeset, UTF8, 5) == 0) {
106 /* Don't do the conversion if it's
107 * already in UTF-8 encoding
108 * Copy over the 'from' to 'to'.
109 */
110 memcpy(to, from, buflen);
111 return 0;
114 ic = open_iconv(get_langinfo_codeset(), UTF8);
115 memset(to, 0, buflen);
116 ileft = strlen(from);
117 oleft = buflen;
119 ret = iconv(ic, &from, &ileft, &to, &oleft);
120 if (ret == (size_t)-1) {
121 fprintf(stderr, "utf2native:Failed to convert (err=%d)\n", ret);
122 exit(1);
124 iconv_close(ic);
126 return buflen-oleft;
130 #endif
131 #ifdef WIN32
133 #include <WINDOWS.H>
135 #include "oobj.h"
136 #include "utf.h"
138 int native2utf8(const char* from, char* to, int buflen) {
139 int len;
140 unsigned short unicode[BUFSIZ];
141 len = MultiByteToWideChar(CP_ACP, 0, from, -1, &unicode[0], BUFSIZ);
142 unicode2utf(&unicode[0], len-1, to, buflen);
143 return utfstrlen(to);
145 int utf2native(const char* from, char* to, int buflen) {
146 int len, len2;
147 unsigned short unicode[BUFSIZ];
148 utf2unicode((char*)from, &unicode[0], BUFSIZ, &len);
149 len2 = WideCharToMultiByte(CP_ACP, 0, &unicode[0], len, to,
150 buflen, NULL, NULL);
151 to[len2]=0;
152 return len2;
154 #endif