Implement conversions between dates and strings.
[wine] / programs / winetest / util.c
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  *
20  */
21 #include <windows.h>
22
23 #include "winetest.h"
24
25 void fatal (const char* msg)
26 {
27     MessageBox (NULL, msg, "Fatal Error", MB_ICONERROR | MB_OK);
28     exit (1);
29 }
30
31 void warning (const char* msg)
32 {
33     MessageBox (NULL, msg, "Warning", MB_ICONWARNING | MB_OK);
34 }
35
36 void *xmalloc (size_t len)
37 {
38     void *p = malloc (len);
39
40     if (!p) fatal ("Out of memory.");
41     return p;
42 }
43
44 void *xrealloc (void *op, size_t len)
45 {
46     void *p = realloc (op, len);
47
48     if (!p) fatal ("Out of memory.");
49     return p;
50 }
51
52 void xprintf (const char *fmt, ...)
53 {
54     va_list ap;
55
56     va_start (ap, fmt);
57     if (vprintf (fmt, ap) < 0) fatal ("Can't write logs.");
58     va_end (ap);
59 }
60
61 char *vstrmake (size_t *lenp, const char *fmt, va_list ap)
62 {
63     size_t size = 1000;
64     char *p, *q;
65     int n;
66
67     p = malloc (size);
68     if (!p) return NULL;
69     while (1) {
70         n = vsnprintf (p, size, fmt, ap);
71         if (n < 0) size *= 2;   /* Windows */
72         else if ((unsigned)n >= size) size = n+1; /* glibc */
73         else break;
74         q = realloc (p, size);
75         if (!q) {
76           free (p);
77           return NULL;
78        }
79        p = q;
80     }
81     if (lenp) *lenp = n;
82     return p;
83 }
84
85 char *strmake (size_t *lenp, const char *fmt, ...)
86 {
87     va_list ap;
88     char *p;
89
90     va_start (ap, fmt);
91     p = vstrmake (lenp, fmt, ap);
92     if (!p) fatal ("Out of memory.");
93     va_end (ap);
94     return p;
95 }