]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - binutils/winduni.c
This commit was generated by cvs2svn to track changes on a CVS vendor
[thirdparty/binutils-gdb.git] / binutils / winduni.c
1 /* winduni.c -- unicode support for the windres program.
2 Copyright 1997, 1998, 2000, 2001 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
4
5 This file is part of GNU Binutils.
6
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.
11
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.
16
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. */
21
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. */
29
30 #include "bfd.h"
31 #include "bucomm.h"
32 #include "winduni.h"
33 #include "safe-ctype.h"
34
35 #ifdef _WIN32
36 #include <windows.h>
37 #endif
38
39 /* Convert an ASCII string to a unicode string. We just copy it,
40 expanding chars to shorts, rather than doing something intelligent. */
41
42 void
43 unicode_from_ascii (length, unicode, ascii)
44 int *length;
45 unichar **unicode;
46 const char *ascii;
47 {
48 int len;
49 const char *s;
50 unsigned short *w;
51
52 len = strlen (ascii);
53
54 if (length != NULL)
55 *length = len;
56
57 *unicode = ((unichar *) res_alloc ((len + 1) * sizeof (unichar)));
58
59 #ifdef _WIN32
60 /* FIXME: On Windows, we should be using MultiByteToWideChar to set
61 the length. */
62 MultiByteToWideChar (CP_ACP, 0, ascii, len + 1, *unicode, len + 1);
63 #else
64 for (s = ascii, w = *unicode; *s != '\0'; s++, w++)
65 *w = *s & 0xff;
66 *w = 0;
67 #endif
68 }
69
70 /* Print the unicode string UNICODE to the file E. LENGTH is the
71 number of characters to print, or -1 if we should print until the
72 end of the string. FIXME: On a Windows host, we should be calling
73 some Windows function, probably WideCharToMultiByte. */
74
75 void
76 unicode_print (e, unicode, length)
77 FILE *e;
78 const unichar *unicode;
79 int length;
80 {
81 while (1)
82 {
83 unichar ch;
84
85 if (length == 0)
86 return;
87 if (length > 0)
88 --length;
89
90 ch = *unicode;
91
92 if (ch == 0 && length < 0)
93 return;
94
95 ++unicode;
96
97 if ((ch & 0x7f) == ch)
98 {
99 if (ch == '\\')
100 fputs ("\\", e);
101 else if (ISPRINT (ch))
102 putc (ch, e);
103 else
104 {
105 switch (ch)
106 {
107 case ESCAPE_A:
108 fputs ("\\a", e);
109 break;
110
111 case ESCAPE_B:
112 fputs ("\\b", e);
113 break;
114
115 case ESCAPE_F:
116 fputs ("\\f", e);
117 break;
118
119 case ESCAPE_N:
120 fputs ("\\n", e);
121 break;
122
123 case ESCAPE_R:
124 fputs ("\\r", e);
125 break;
126
127 case ESCAPE_T:
128 fputs ("\\t", e);
129 break;
130
131 case ESCAPE_V:
132 fputs ("\\v", e);
133 break;
134
135 default:
136 fprintf (e, "\\%03o", (unsigned int) ch);
137 break;
138 }
139 }
140 }
141 else if ((ch & 0xff) == ch)
142 fprintf (e, "\\%03o", (unsigned int) ch);
143 else
144 fprintf (e, "\\x%x", (unsigned int) ch);
145 }
146 }