/* winduni.c -- unicode support for the windres program.
- Copyright 1997, 1998 Free Software Foundation, Inc.
+ Copyright 1997, 1998, 2000, 2001, 2003, 2007
+ Free Software Foundation, Inc.
Written by Ian Lance Taylor, Cygnus Support.
This file is part of GNU Binutils.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
- 02111-1307, USA. */
+ Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
+ 02110-1301, USA. */
/* This file contains unicode support routines for the windres
program. Ideally, we would have generic unicode support which
hosts, but that seems better than not really supporting unicode at
all. */
+#include "sysdep.h"
#include "bfd.h"
-#include "bucomm.h"
#include "winduni.h"
-
-#include <ctype.h>
+#include "safe-ctype.h"
#ifdef _WIN32
#include <windows.h>
expanding chars to shorts, rather than doing something intelligent. */
void
-unicode_from_ascii (length, unicode, ascii)
- int *length;
- unichar **unicode;
- const char *ascii;
+unicode_from_ascii (int *length, unichar **unicode, const char *ascii)
{
int len;
+#ifndef _WIN32
const char *s;
unsigned short *w;
len = strlen (ascii);
-
- if (length != NULL)
- *length = len;
-
*unicode = ((unichar *) res_alloc ((len + 1) * sizeof (unichar)));
-
-#ifdef _WIN32
- /* FIXME: On Windows, we should be using MultiByteToWideChar to set
- the length. */
- MultiByteToWideChar (CP_ACP, 0, ascii, len + 1, *unicode, len + 1);
-#else
for (s = ascii, w = *unicode; *s != '\0'; s++, w++)
*w = *s & 0xff;
*w = 0;
+#else
+ /* We use MultiByteToWideChar rather than strlen to get the unicode
+ string length to allow multibyte "ascii" chars. The value returned
+ by this function includes the trailing '\0'. */
+ len = MultiByteToWideChar (CP_ACP, 0, ascii, -1, NULL, 0);
+ if (len)
+ {
+ *unicode = ((unichar *) res_alloc (len * sizeof (unichar)));
+ MultiByteToWideChar (CP_ACP, 0, ascii, -1, *unicode, len);
+ }
+ /* Discount the trailing '/0'. If MultiByteToWideChar failed,
+ this will set *length to -1. */
+ len--;
#endif
+
+ if (length != NULL)
+ *length = len;
}
/* Print the unicode string UNICODE to the file E. LENGTH is the
some Windows function, probably WideCharToMultiByte. */
void
-unicode_print (e, unicode, length)
- FILE *e;
- const unichar *unicode;
- int length;
+unicode_print (FILE *e, const unichar *unicode, int length)
{
while (1)
{
{
if (ch == '\\')
fputs ("\\", e);
- else if (isprint (ch))
+ else if (ISPRINT (ch))
putc (ch, e);
else
{