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