Added LGPL standard comment, and copyright notices where necessary.
[wine] / server / unicode.c
1 /*
2  * Unicode routines for use inside the server
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <ctype.h>
22 #include <stdio.h>
23
24 #include "unicode.h"
25
26 /* dump a Unicode string with proper escaping */
27 int dump_strW( const WCHAR *str, size_t len, FILE *f, char escape[2] )
28 {
29     static const char escapes[32] = ".......abtnvfr.............e....";
30     char buffer[256];
31     char *pos = buffer;
32     int count = 0;
33
34     for (; len; str++, len--)
35     {
36         if (pos > buffer + sizeof(buffer) - 8)
37         {
38             fwrite( buffer, pos - buffer, 1, f );
39             count += pos - buffer;
40             pos = buffer;
41         }
42         if (*str > 127)  /* hex escape */
43         {
44             if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
45                 pos += sprintf( pos, "\\x%04x", *str );
46             else
47                 pos += sprintf( pos, "\\x%x", *str );
48             continue;
49         }
50         if (*str < 32)  /* octal or C escape */
51         {
52             if (!*str && len == 1) continue;  /* do not output terminating NULL */
53             if (escapes[*str] != '.')
54                 pos += sprintf( pos, "\\%c", escapes[*str] );
55             else if (len > 1 && str[1] >= '0' && str[1] <= '7')
56                 pos += sprintf( pos, "\\%03o", *str );
57             else
58                 pos += sprintf( pos, "\\%o", *str );
59             continue;
60         }
61         if (*str == '\\' || *str == escape[0] || *str == escape[1]) *pos++ = '\\';
62         *pos++ = *str;
63     }
64     fwrite( buffer, pos - buffer, 1, f );
65     count += pos - buffer;
66     return count;
67 }