merge from gcc
[binutils.git] / binutils / winduni.c
blob65bc6085f33024c283a34b052404f7d2b4732064
1 /* winduni.c -- unicode support for the windres program.
2 Copyright 1997, 1998, 2000, 2001, 2003 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This file contains unicode support routines for the windres
23 program. Ideally, we would have generic unicode support which
24 would work on all systems. However, we don't. Instead, on a
25 Windows host, we are prepared to call some Windows routines. This
26 means that we will generate different output on Windows and Unix
27 hosts, but that seems better than not really supporting unicode at
28 all. */
30 #include "bfd.h"
31 #include "bucomm.h"
32 #include "winduni.h"
33 #include "safe-ctype.h"
35 #ifdef _WIN32
36 #include <windows.h>
37 #endif
39 /* Convert an ASCII string to a unicode string. We just copy it,
40 expanding chars to shorts, rather than doing something intelligent. */
42 void
43 unicode_from_ascii (int *length, unichar **unicode, const char *ascii)
45 int len;
46 const char *s;
47 unsigned short *w;
49 len = strlen (ascii);
51 if (length != NULL)
52 *length = len;
54 *unicode = ((unichar *) res_alloc ((len + 1) * sizeof (unichar)));
56 #ifdef _WIN32
57 /* FIXME: On Windows, we should be using MultiByteToWideChar to set
58 the length. */
59 MultiByteToWideChar (CP_ACP, 0, ascii, len + 1, *unicode, len + 1);
60 #else
61 for (s = ascii, w = *unicode; *s != '\0'; s++, w++)
62 *w = *s & 0xff;
63 *w = 0;
64 #endif
67 /* Print the unicode string UNICODE to the file E. LENGTH is the
68 number of characters to print, or -1 if we should print until the
69 end of the string. FIXME: On a Windows host, we should be calling
70 some Windows function, probably WideCharToMultiByte. */
72 void
73 unicode_print (FILE *e, const unichar *unicode, int length)
75 while (1)
77 unichar ch;
79 if (length == 0)
80 return;
81 if (length > 0)
82 --length;
84 ch = *unicode;
86 if (ch == 0 && length < 0)
87 return;
89 ++unicode;
91 if ((ch & 0x7f) == ch)
93 if (ch == '\\')
94 fputs ("\\", e);
95 else if (ISPRINT (ch))
96 putc (ch, e);
97 else
99 switch (ch)
101 case ESCAPE_A:
102 fputs ("\\a", e);
103 break;
105 case ESCAPE_B:
106 fputs ("\\b", e);
107 break;
109 case ESCAPE_F:
110 fputs ("\\f", e);
111 break;
113 case ESCAPE_N:
114 fputs ("\\n", e);
115 break;
117 case ESCAPE_R:
118 fputs ("\\r", e);
119 break;
121 case ESCAPE_T:
122 fputs ("\\t", e);
123 break;
125 case ESCAPE_V:
126 fputs ("\\v", e);
127 break;
129 default:
130 fprintf (e, "\\%03o", (unsigned int) ch);
131 break;
135 else if ((ch & 0xff) == ch)
136 fprintf (e, "\\%03o", (unsigned int) ch);
137 else
138 fprintf (e, "\\x%x", (unsigned int) ch);