2003-12-04 03:01:39 +01:00
|
|
|
/*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2003-12-04 03:01:39 +01:00
|
|
|
*/
|
2004-06-16 00:45:15 +02:00
|
|
|
|
2008-07-10 17:45:22 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <windef.h>
|
|
|
|
#include <winbase.h>
|
2003-12-04 03:01:39 +01:00
|
|
|
|
|
|
|
#include "winetest.h"
|
|
|
|
|
2008-07-10 17:45:22 +02:00
|
|
|
HANDLE logfile = 0;
|
|
|
|
|
2009-05-17 13:28:23 +02:00
|
|
|
void *heap_alloc (size_t len)
|
2003-12-04 03:01:39 +01:00
|
|
|
{
|
|
|
|
void *p = malloc (len);
|
|
|
|
|
2004-01-15 02:48:05 +01:00
|
|
|
if (!p) report (R_FATAL, "Out of memory.");
|
2003-12-04 03:01:39 +01:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2009-05-17 13:28:23 +02:00
|
|
|
void *heap_realloc (void *op, size_t len)
|
2003-12-04 03:01:39 +01:00
|
|
|
{
|
|
|
|
void *p = realloc (op, len);
|
|
|
|
|
2005-03-18 15:09:55 +01:00
|
|
|
if (len && !p) report (R_FATAL, "Out of memory.");
|
2003-12-04 03:01:39 +01:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2009-05-17 13:28:23 +02:00
|
|
|
char *heap_strdup( const char *str )
|
2006-09-11 14:23:07 +02:00
|
|
|
{
|
|
|
|
char *res = strdup( str );
|
|
|
|
if (!res) report (R_FATAL, "Out of memory.");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2009-05-17 13:28:23 +02:00
|
|
|
void heap_free (void *op)
|
|
|
|
{
|
|
|
|
free (op);
|
|
|
|
}
|
|
|
|
|
2005-06-16 12:45:25 +02:00
|
|
|
static char *vstrfmtmake (size_t *lenp, const char *fmt, va_list ap)
|
2003-12-04 03:01:39 +01:00
|
|
|
{
|
|
|
|
size_t size = 1000;
|
|
|
|
char *p, *q;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
p = malloc (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 = realloc (p, size);
|
|
|
|
if (!q) {
|
2009-05-17 13:28:23 +02:00
|
|
|
heap_free (p);
|
2003-12-04 03:01:39 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p = q;
|
|
|
|
}
|
|
|
|
if (lenp) *lenp = n;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2004-03-30 06:32:46 +02:00
|
|
|
char *vstrmake (size_t *lenp, va_list ap)
|
|
|
|
{
|
|
|
|
const char *fmt;
|
|
|
|
|
|
|
|
fmt = va_arg (ap, const char*);
|
|
|
|
return vstrfmtmake (lenp, fmt, ap);
|
|
|
|
}
|
|
|
|
|
2004-01-15 02:48:05 +01:00
|
|
|
char *strmake (size_t *lenp, ...)
|
2003-12-04 03:01:39 +01:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *p;
|
|
|
|
|
2004-01-15 02:48:05 +01:00
|
|
|
va_start (ap, lenp);
|
|
|
|
p = vstrmake (lenp, ap);
|
|
|
|
if (!p) report (R_FATAL, "Out of memory.");
|
2003-12-04 03:01:39 +01:00
|
|
|
va_end (ap);
|
|
|
|
return p;
|
|
|
|
}
|
2004-02-19 05:12:42 +01:00
|
|
|
|
2004-03-30 06:32:46 +02:00
|
|
|
void xprintf (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
size_t size;
|
2008-07-10 17:45:22 +02:00
|
|
|
DWORD written;
|
2004-03-30 06:32:46 +02:00
|
|
|
char *buffer, *head;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
buffer = vstrfmtmake (&size, fmt, ap);
|
|
|
|
head = buffer;
|
|
|
|
va_end (ap);
|
2008-07-10 17:45:22 +02:00
|
|
|
while (size) {
|
|
|
|
if (!WriteFile( logfile, head, size, &written, NULL ))
|
|
|
|
report (R_FATAL, "Can't write logs: %u", GetLastError());
|
2004-03-30 06:32:46 +02:00
|
|
|
head += written;
|
|
|
|
size -= written;
|
|
|
|
}
|
2009-05-17 13:28:23 +02:00
|
|
|
heap_free (buffer);
|
2004-03-30 06:32:46 +02:00
|
|
|
}
|
|
|
|
|
2005-04-18 11:54:24 +02:00
|
|
|
int
|
|
|
|
goodtagchar (char c)
|
|
|
|
{
|
|
|
|
return (('a'<=c && c<='z') ||
|
|
|
|
('A'<=c && c<='Z') ||
|
|
|
|
('0'<=c && c<='9') ||
|
|
|
|
c=='-' || c=='.');
|
|
|
|
}
|
|
|
|
|
2004-04-23 01:43:54 +02:00
|
|
|
const char *
|
2005-04-18 11:54:24 +02:00
|
|
|
findbadtagchar (const char *tag)
|
2004-02-19 05:12:42 +01:00
|
|
|
{
|
|
|
|
while (*tag)
|
2005-04-18 11:54:24 +02:00
|
|
|
if (goodtagchar (*tag)) tag++;
|
2004-02-19 05:12:42 +01:00
|
|
|
else return tag;
|
|
|
|
return NULL;
|
|
|
|
}
|