util.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. HANDLE logfile = 0;
  26. void *heap_alloc (size_t len)
  27. {
  28. void *p = HeapAlloc(GetProcessHeap(), 0, len);
  29. if (!p) report (R_FATAL, "Out of memory.");
  30. return p;
  31. }
  32. void *heap_realloc (void *op, size_t len)
  33. {
  34. void *p = HeapReAlloc(GetProcessHeap(), 0, op, len);
  35. if (len && !p) report (R_FATAL, "Out of memory.");
  36. return p;
  37. }
  38. char *heap_strdup( const char *str )
  39. {
  40. int len = strlen(str) + 1;
  41. char* res = HeapAlloc(GetProcessHeap(), 0, len);
  42. if (!res) report (R_FATAL, "Out of memory.");
  43. memcpy(res, str, len);
  44. return res;
  45. }
  46. void heap_free (void *op)
  47. {
  48. HeapFree(GetProcessHeap(), 0, op);
  49. }
  50. static char *vstrfmtmake (size_t *lenp, const char *fmt, va_list ap)
  51. {
  52. size_t size = 1000;
  53. char *p, *q;
  54. int n;
  55. p = HeapAlloc(GetProcessHeap(), 0, size);
  56. if (!p) return NULL;
  57. while (1) {
  58. n = vsnprintf (p, size, fmt, ap);
  59. if (n < 0) size *= 2; /* Windows */
  60. else if ((unsigned)n >= size) size = n+1; /* glibc */
  61. else break;
  62. q = HeapReAlloc(GetProcessHeap(), 0, p, size);
  63. if (!q) {
  64. heap_free (p);
  65. return NULL;
  66. }
  67. p = q;
  68. }
  69. if (lenp) *lenp = n;
  70. return p;
  71. }
  72. char *vstrmake (size_t *lenp, va_list ap)
  73. {
  74. const char *fmt;
  75. fmt = va_arg (ap, const char*);
  76. return vstrfmtmake (lenp, fmt, ap);
  77. }
  78. char * WINAPIV strmake (size_t *lenp, ...)
  79. {
  80. va_list ap;
  81. char *p;
  82. va_start (ap, lenp);
  83. p = vstrmake (lenp, ap);
  84. if (!p) report (R_FATAL, "Out of memory.");
  85. va_end (ap);
  86. return p;
  87. }
  88. void WINAPIV xprintf (const char *fmt, ...)
  89. {
  90. va_list ap;
  91. size_t size;
  92. DWORD written;
  93. char *buffer, *head;
  94. va_start (ap, fmt);
  95. buffer = vstrfmtmake (&size, fmt, ap);
  96. head = buffer;
  97. va_end (ap);
  98. while (size) {
  99. if (!WriteFile( logfile, head, size, &written, NULL ))
  100. report (R_FATAL, "Can't write logs: %u", GetLastError());
  101. head += written;
  102. size -= written;
  103. }
  104. heap_free (buffer);
  105. }
  106. int
  107. goodtagchar (char c)
  108. {
  109. return (('a'<=c && c<='z') ||
  110. ('A'<=c && c<='Z') ||
  111. ('0'<=c && c<='9') ||
  112. c=='-' || c=='.');
  113. }
  114. const char *
  115. findbadtagchar (const char *tag)
  116. {
  117. while (*tag)
  118. if (goodtagchar (*tag)) tag++;
  119. else return tag;
  120. return NULL;
  121. }