Back to index

cell-binutils  2.17cvs20070401
winduni.c
Go to the documentation of this file.
00001 /* winduni.c -- unicode support for the windres program.
00002    Copyright 1997, 1998, 2000, 2001, 2003 Free Software Foundation, Inc.
00003    Written by Ian Lance Taylor, Cygnus Support.
00004 
00005    This file is part of GNU Binutils.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
00020    02110-1301, USA.  */
00021 
00022 /* This file contains unicode support routines for the windres
00023    program.  Ideally, we would have generic unicode support which
00024    would work on all systems.  However, we don't.  Instead, on a
00025    Windows host, we are prepared to call some Windows routines.  This
00026    means that we will generate different output on Windows and Unix
00027    hosts, but that seems better than not really supporting unicode at
00028    all.  */
00029 
00030 #include "bfd.h"
00031 #include "bucomm.h"
00032 #include "winduni.h"
00033 #include "safe-ctype.h"
00034 
00035 #ifdef _WIN32
00036 #include <windows.h>
00037 #endif
00038 
00039 /* Convert an ASCII string to a unicode string.  We just copy it,
00040    expanding chars to shorts, rather than doing something intelligent.  */
00041 
00042 void
00043 unicode_from_ascii (int *length, unichar **unicode, const char *ascii)
00044 {
00045   int len;
00046 #ifndef _WIN32
00047   const char *s;
00048   unsigned short *w;
00049 
00050   len = strlen (ascii);
00051   *unicode = ((unichar *) res_alloc ((len + 1) * sizeof (unichar)));
00052   for (s = ascii, w = *unicode; *s != '\0'; s++, w++)
00053     *w = *s & 0xff;
00054   *w = 0;
00055 #else
00056   /* We use  MultiByteToWideChar rather than strlen to get the unicode
00057      string length to allow multibyte "ascii" chars. The value returned
00058      by this function includes the trailing '\0'.  */
00059   len = MultiByteToWideChar (CP_ACP, 0, ascii, -1, NULL, 0);
00060   if (len)
00061     {
00062       *unicode = ((unichar *) res_alloc (len * sizeof (unichar)));
00063       MultiByteToWideChar (CP_ACP, 0, ascii, -1, *unicode, len);
00064     }
00065   /* Discount the trailing '/0'.  If MultiByteToWideChar failed,
00066      this will set *length to -1.  */
00067   len--;
00068 #endif
00069 
00070   if (length != NULL)
00071     *length = len;
00072 }
00073 
00074 /* Print the unicode string UNICODE to the file E.  LENGTH is the
00075    number of characters to print, or -1 if we should print until the
00076    end of the string.  FIXME: On a Windows host, we should be calling
00077    some Windows function, probably WideCharToMultiByte.  */
00078 
00079 void
00080 unicode_print (FILE *e, const unichar *unicode, int length)
00081 {
00082   while (1)
00083     {
00084       unichar ch;
00085 
00086       if (length == 0)
00087        return;
00088       if (length > 0)
00089        --length;
00090 
00091       ch = *unicode;
00092 
00093       if (ch == 0 && length < 0)
00094        return;
00095 
00096       ++unicode;
00097 
00098       if ((ch & 0x7f) == ch)
00099        {
00100          if (ch == '\\')
00101            fputs ("\\", e);
00102          else if (ISPRINT (ch))
00103            putc (ch, e);
00104          else
00105            {
00106              switch (ch)
00107               {
00108               case ESCAPE_A:
00109                 fputs ("\\a", e);
00110                 break;
00111 
00112               case ESCAPE_B:
00113                 fputs ("\\b", e);
00114                 break;
00115 
00116               case ESCAPE_F:
00117                 fputs ("\\f", e);
00118                 break;
00119 
00120               case ESCAPE_N:
00121                 fputs ("\\n", e);
00122                 break;
00123 
00124               case ESCAPE_R:
00125                 fputs ("\\r", e);
00126                 break;
00127 
00128               case ESCAPE_T:
00129                 fputs ("\\t", e);
00130                 break;
00131 
00132               case ESCAPE_V:
00133                 fputs ("\\v", e);
00134                 break;
00135 
00136               default:
00137                 fprintf (e, "\\%03o", (unsigned int) ch);
00138                 break;
00139               }
00140            }
00141        }
00142       else if ((ch & 0xff) == ch)
00143        fprintf (e, "\\%03o", (unsigned int) ch);
00144       else
00145        fprintf (e, "\\x%x", (unsigned int) ch);
00146     }
00147 }