unicode.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include "config.h"
  21. #include "wine/port.h"
  22. #include <ctype.h>
  23. #include <stdio.h>
  24. #include "unicode.h"
  25. /* dump a Unicode string with proper escaping */
  26. int dump_strW( const WCHAR *str, size_t len, FILE *f, const char escape[2] )
  27. {
  28. static const char escapes[32] = ".......abtnvfr.............e....";
  29. char buffer[256];
  30. char *pos = buffer;
  31. int count = 0;
  32. for (; len; str++, len--)
  33. {
  34. if (pos > buffer + sizeof(buffer) - 8)
  35. {
  36. fwrite( buffer, pos - buffer, 1, f );
  37. count += pos - buffer;
  38. pos = buffer;
  39. }
  40. if (*str > 127) /* hex escape */
  41. {
  42. if (len > 1 && str[1] < 128 && isxdigit((char)str[1]))
  43. pos += sprintf( pos, "\\x%04x", *str );
  44. else
  45. pos += sprintf( pos, "\\x%x", *str );
  46. continue;
  47. }
  48. if (*str < 32) /* octal or C escape */
  49. {
  50. if (!*str && len == 1) continue; /* do not output terminating NULL */
  51. if (escapes[*str] != '.')
  52. pos += sprintf( pos, "\\%c", escapes[*str] );
  53. else if (len > 1 && str[1] >= '0' && str[1] <= '7')
  54. pos += sprintf( pos, "\\%03o", *str );
  55. else
  56. pos += sprintf( pos, "\\%o", *str );
  57. continue;
  58. }
  59. if (*str == '\\' || *str == escape[0] || *str == escape[1]) *pos++ = '\\';
  60. *pos++ = *str;
  61. }
  62. fwrite( buffer, pos - buffer, 1, f );
  63. count += pos - buffer;
  64. return count;
  65. }