]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/winduni.c
9def8dff54de4ed7b0947343bd6cb27c2c1526ca
1 /* winduni.c -- unicode support for the windres program.
2 Copyright 1997, 1998, 2000, 2001, 2003, 2007
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
5 Rewritten by Kai Tietz, Onevision.
7 This file is part of GNU Binutils.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
24 /* This file contains unicode support routines for the windres
25 program. Ideally, we would have generic unicode support which
26 would work on all systems. However, we don't. Instead, on a
27 Windows host, we are prepared to call some Windows routines. This
28 means that we will generate different output on Windows and Unix
29 hosts, but that seems better than not really supporting unicode at
36 #include "safe-ctype.h"
43 static int unichar_isascii (const unichar
*, rc_uint_type
);
45 /* Convert an ASCII string to a unicode string. We just copy it,
46 expanding chars to shorts, rather than doing something intelligent. */
49 unicode_from_ascii (rc_uint_type
*length
, unichar
**unicode
, const char *ascii
)
57 *unicode
= ((unichar
*) res_alloc ((len
+ 1) * sizeof (unichar
)));
58 for (s
= ascii
, w
= *unicode
; *s
!= '\0'; s
++, w
++)
62 /* We use MultiByteToWideChar rather than strlen to get the unicode
63 string length to allow multibyte "ascii" chars. The value returned
64 by this function includes the trailing '\0'. */
65 len
= (rc_uint_type
) MultiByteToWideChar (CP_ACP
, 0, ascii
, -1, NULL
, 0);
68 *unicode
= ((unichar
*) res_alloc (len
* sizeof (unichar
)));
69 MultiByteToWideChar (CP_ACP
, 0, ascii
, -1, *unicode
, (int) len
);
71 /* Discount the trailing '/0'. If MultiByteToWideChar failed,
72 this will set *length to -1. */
80 /* Convert an unicode string to an ASCII string. We just copy it,
81 shrink shorts to chars, rather than doing something intelligent.
82 Shorts with not within the char range are replaced by '_'. */
85 ascii_from_unicode (rc_uint_type
*length
, const unichar
*unicode
, char **ascii
)
90 const unsigned short *w
;
93 while (unicode
[len
] != 0)
95 *ascii
= ((char *) res_alloc (len
+ 1));
96 for (s
= *ascii
, w
= unicode
; *w
!= '\0'; w
++, s
++)
105 WINBOOL used_def
= FALSE
;
106 /* We use MultiByteToWideChar rather than strlen to get the unicode
107 string length to allow multibyte "ascii" chars. The value returned
108 by this function includes the trailing '\0'. */
109 len
= (rc_uint_type
) WideCharToMultiByte (CP_ACP
, WC_DEFAULTCHAR
, unicode
, -1, NULL
,
113 *ascii
= (char *) res_alloc (len
* sizeof (char));
114 WideCharToMultiByte (CP_ACP
, WC_DEFAULTCHAR
, unicode
, -1, *ascii
, (int) len
,
117 /* Discount the trailing '/0'. If MultiByteToWideChar failed,
118 this will set *length to -1. */
126 /* Print the unicode string UNICODE to the file E. LENGTH is the
127 number of characters to print, or -1 if we should print until the
128 end of the string. FIXME: On a Windows host, we should be calling
129 some Windows function, probably WideCharToMultiByte. */
132 unicode_print (FILE *e
, const unichar
*unicode
, rc_uint_type length
)
140 if ((bfd_signed_vma
) length
> 0)
145 if (ch
== 0 && (bfd_signed_vma
) length
< 0)
150 if ((ch
& 0x7f) == ch
)
156 else if (ISPRINT (ch
))
191 fprintf (e
, "\\%03o", (unsigned int) ch
);
196 else if ((ch
& 0xff) == ch
)
197 fprintf (e
, "\\%03o", (unsigned int) ch
);
199 fprintf (e
, "\\x%x", (unsigned int) ch
);
203 /* Print a unicode string to a file. */
205 ascii_print (FILE *e
, const char *s
, rc_uint_type length
)
213 if ((bfd_signed_vma
) length
> 0)
218 if (ch
== 0 && (bfd_signed_vma
) length
< 0)
223 if ((ch
& 0x7f) == ch
)
229 else if (ISPRINT (ch
))
264 fprintf (e
, "\\%03o", (unsigned int) ch
);
270 fprintf (e
, "\\%03o", (unsigned int) ch
& 0xff);
275 unichar_len (const unichar
*unicode
)
279 while (unicode
[r
] != 0)
287 unichar_dup (const unichar
*unicode
)
294 for (len
= 0; unicode
[len
] != 0; ++len
)
297 r
= ((unichar
*) res_alloc (len
* sizeof (unichar
)));
298 memcpy (r
, unicode
, len
* sizeof (unichar
));
303 unichar_dup_uppercase (const unichar
*u
)
305 unichar
*r
= unichar_dup (u
);
311 for (i
= 0; r
[i
] != 0; ++i
)
313 if (r
[i
] >= 'a' && r
[i
] <= 'z')
320 unichar_isascii (const unichar
*u
, rc_uint_type len
)
323 if ((bfd_signed_vma
) len
< 0)
326 len
= (rc_uint_type
) unichar_len (u
);
331 for (i
= 0; i
< len
; i
++)
332 if ((u
[i
] & 0xff80) != 0)
338 unicode_print_quoted (FILE *e
, const unichar
*u
, rc_uint_type len
)
340 if (! unichar_isascii (u
, len
))
343 unicode_print (e
, u
, len
);