Sweden-Number/programs/winetest/util.c

96 lines
2.1 KiB
C

/*
* 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
*
*/
#include <windows.h>
#include "winetest.h"
void fatal (const char* msg)
{
MessageBox (NULL, msg, "Fatal Error", MB_ICONERROR | MB_OK);
exit (1);
}
void warning (const char* msg)
{
MessageBox (NULL, msg, "Warning", MB_ICONWARNING | MB_OK);
}
void *xmalloc (size_t len)
{
void *p = malloc (len);
if (!p) fatal ("Out of memory.");
return p;
}
void *xrealloc (void *op, size_t len)
{
void *p = realloc (op, len);
if (!p) fatal ("Out of memory.");
return p;
}
void xprintf (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
if (vprintf (fmt, ap) < 0) fatal ("Can't write logs.");
va_end (ap);
}
char *vstrmake (size_t *lenp, const char *fmt, va_list ap)
{
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) {
free (p);
return NULL;
}
p = q;
}
if (lenp) *lenp = n;
return p;
}
char *strmake (size_t *lenp, const char *fmt, ...)
{
va_list ap;
char *p;
va_start (ap, fmt);
p = vstrmake (lenp, fmt, ap);
if (!p) fatal ("Out of memory.");
va_end (ap);
return p;
}