Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Unicode routines for use inside the server |
| 3 | * |
| 4 | * Copyright (C) 1999 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 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 |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Alexandre Julliard | 5769d1d | 2002-04-26 19:05:15 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | #include "wine/port.h" |
| 23 | |
Alexandre Julliard | 7e495e1 | 2000-07-25 21:01:59 +0000 | [diff] [blame] | 24 | #include <ctype.h> |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | |
| 27 | #include "unicode.h" |
| 28 | |
| 29 | /* dump a Unicode string with proper escaping */ |
| 30 | int dump_strW( const WCHAR *str, size_t len, FILE *f, char escape[2] ) |
| 31 | { |
| 32 | static const char escapes[32] = ".......abtnvfr.............e...."; |
| 33 | char buffer[256]; |
| 34 | char *pos = buffer; |
| 35 | int count = 0; |
| 36 | |
| 37 | for (; len; str++, len--) |
| 38 | { |
| 39 | if (pos > buffer + sizeof(buffer) - 8) |
| 40 | { |
| 41 | fwrite( buffer, pos - buffer, 1, f ); |
| 42 | count += pos - buffer; |
| 43 | pos = buffer; |
| 44 | } |
| 45 | if (*str > 127) /* hex escape */ |
| 46 | { |
| 47 | if (len > 1 && str[1] < 128 && isxdigit((char)str[1])) |
| 48 | pos += sprintf( pos, "\\x%04x", *str ); |
| 49 | else |
| 50 | pos += sprintf( pos, "\\x%x", *str ); |
| 51 | continue; |
| 52 | } |
| 53 | if (*str < 32) /* octal or C escape */ |
| 54 | { |
| 55 | if (!*str && len == 1) continue; /* do not output terminating NULL */ |
| 56 | if (escapes[*str] != '.') |
| 57 | pos += sprintf( pos, "\\%c", escapes[*str] ); |
| 58 | else if (len > 1 && str[1] >= '0' && str[1] <= '7') |
| 59 | pos += sprintf( pos, "\\%03o", *str ); |
| 60 | else |
| 61 | pos += sprintf( pos, "\\%o", *str ); |
| 62 | continue; |
| 63 | } |
| 64 | if (*str == '\\' || *str == escape[0] || *str == escape[1]) *pos++ = '\\'; |
| 65 | *pos++ = *str; |
| 66 | } |
| 67 | fwrite( buffer, pos - buffer, 1, f ); |
| 68 | count += pos - buffer; |
| 69 | return count; |
| 70 | } |