Make unicode.o into a separate ELF library so that we can use it from
[wine] / server / unicode.c
1 /*
2  * Unicode routines for use inside the server
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  */
6
7 #include <ctype.h>
8 #include <stdio.h>
9
10 #include "unicode.h"
11
12 /* dump a Unicode string with proper escaping */
13 int dump_strW( const WCHAR *str, size_t len, FILE *f, char escape[2] )
14 {
15     static const char escapes[32] = ".......abtnvfr.............e....";
16     char buffer[256];
17     char *pos = buffer;
18     int count = 0;
19
20     for (; len; str++, len--)
21     {
22         if (pos > buffer + sizeof(buffer) - 8)
23         {
24             fwrite( buffer, pos - buffer, 1, f );
25             count += pos - buffer;
26             pos = buffer;
27         }
28         if (*str > 127)  /* hex escape */
29         {
30             if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
31                 pos += sprintf( pos, "\\x%04x", *str );
32             else
33                 pos += sprintf( pos, "\\x%x", *str );
34             continue;
35         }
36         if (*str < 32)  /* octal or C escape */
37         {
38             if (!*str && len == 1) continue;  /* do not output terminating NULL */
39             if (escapes[*str] != '.')
40                 pos += sprintf( pos, "\\%c", escapes[*str] );
41             else if (len > 1 && str[1] >= '0' && str[1] <= '7')
42                 pos += sprintf( pos, "\\%03o", *str );
43             else
44                 pos += sprintf( pos, "\\%o", *str );
45             continue;
46         }
47         if (*str == '\\' || *str == escape[0] || *str == escape[1]) *pos++ = '\\';
48         *pos++ = *str;
49     }
50     fwrite( buffer, pos - buffer, 1, f );
51     count += pos - buffer;
52     return count;
53 }