util.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Utility functions.
  3. *
  4. * Copyright 2003 Dimitrie O. Paun
  5. * Copyright 2003 Ferenc Wagner
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include <stdarg.h>
  22. #include <windef.h>
  23. #include <winbase.h>
  24. #include "winetest.h"
  25. void *xalloc (size_t len)
  26. {
  27. void *p = malloc( len );
  28. if (!p) report (R_FATAL, "Out of memory.");
  29. return p;
  30. }
  31. void *xrealloc (void *op, size_t len)
  32. {
  33. void *p = realloc(op, len);
  34. if (len && !p) report (R_FATAL, "Out of memory.");
  35. return p;
  36. }
  37. char *xstrdup( const char *str )
  38. {
  39. char* res = strdup( str );
  40. if (!res) report (R_FATAL, "Out of memory.");
  41. return res;
  42. }
  43. static char *vstrfmtmake( const char *fmt, va_list ap ) __WINE_PRINTF_ATTR(1,0);
  44. static char *vstrfmtmake( const char *fmt, va_list ap )
  45. {
  46. size_t size = 1000;
  47. char *p;
  48. int n;
  49. if (!fmt) fmt = "";
  50. p = xalloc(size);
  51. while (1) {
  52. n = vsnprintf (p, size, fmt, ap);
  53. if (n < 0) size *= 2; /* Windows */
  54. else if ((unsigned)n >= size) size = n+1; /* glibc */
  55. else break;
  56. p = xrealloc(p, size);
  57. }
  58. return p;
  59. }
  60. char *vstrmake(va_list ap)
  61. {
  62. const char *fmt;
  63. fmt = va_arg (ap, const char*);
  64. return vstrfmtmake (fmt, ap);
  65. }
  66. char *strmake( const char *fmt, ... )
  67. {
  68. va_list ap;
  69. char *p;
  70. va_start( ap, fmt );
  71. p = vstrfmtmake( fmt, ap );
  72. va_end( ap );
  73. return p;
  74. }
  75. void output( HANDLE file, const char *fmt, ... )
  76. {
  77. va_list ap;
  78. DWORD written;
  79. char *buffer, *head;
  80. va_start (ap, fmt);
  81. buffer = vstrfmtmake (fmt, ap);
  82. head = buffer;
  83. va_end (ap);
  84. while (head[0]) {
  85. if (!WriteFile( file, head, strlen(head), &written, NULL ))
  86. report (R_FATAL, "Can't write logs: %u", GetLastError());
  87. head += written;
  88. }
  89. free(buffer);
  90. }
  91. int
  92. goodtagchar (char c)
  93. {
  94. return (('a'<=c && c<='z') ||
  95. ('A'<=c && c<='Z') ||
  96. ('0'<=c && c<='9') ||
  97. c=='-' || c=='.');
  98. }
  99. const char *
  100. findbadtagchar (const char *tag)
  101. {
  102. while (*tag)
  103. if (goodtagchar (*tag)) tag++;
  104. else return tag;
  105. return NULL;
  106. }