Make the testing framework thread safe.
[wine/gsoc_dplay.git] / tools / winedump / misc.c
blobaaac6e370b444964a436cb1dbb0d4dcc9710e899
1 /*
2 * Misc functions
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "winedump.h"
23 /*******************************************************************
24 * str_create
26 * Create a single string from many substrings
28 char *str_create(size_t num_str, ...)
30 va_list args;
31 size_t len = 1, i = 0;
32 char *tmp, *t;
34 va_start (args, num_str);
35 for (i = 0; i < num_str; i++)
36 if ((t = va_arg(args, char *)))
37 len += strlen (t);
38 va_end (args);
40 if (!(tmp = (char *) malloc (len)))
41 fatal ("Out of memory");
43 tmp[0] = '\0';
45 va_start (args, num_str);
46 for (i = 0; i < num_str; i++)
47 if ((t = va_arg(args, char *)))
48 strcat (tmp, t);
49 va_end (args);
50 return tmp;
54 /*******************************************************************
55 * str_create_num
57 * Create a single string from many substrings, terminating in a number
59 char *str_create_num(size_t num_str, int num, ...)
61 va_list args;
62 size_t len = 8, i = 0;
63 char *tmp, *t;
65 va_start (args, num);
66 for (i = 0; i < num_str; i++)
67 if ((t = va_arg(args, char *)))
68 len += strlen (t);
69 va_end (args);
71 if (!(tmp = (char *) malloc (len)))
72 fatal ("Out of memory");
74 tmp[0] = '\0';
76 va_start (args, num);
77 for (i = 0; i < num_str; i++)
78 if ((t = va_arg(args, char *)))
79 strcat (tmp, t);
80 va_end (args);
81 sprintf (tmp + len - 8, "%d", num);
82 return tmp;
86 /*******************************************************************
87 * str_substring
89 * Create a new substring from a string
91 char *str_substring(const char *start, const char *end)
93 char *newstr;
95 assert (start && end && end > start);
97 if (!(newstr = (char *) malloc (end - start + 1)))
98 fatal ("Out of memory");
100 memcpy (newstr, start, end - start);
101 newstr [end - start] = '\0';
103 return newstr;
107 /*******************************************************************
108 * str_replace
110 * Swap two strings in another string, in place
111 * Modified PD code from 'snippets'
113 char *str_replace (char *str, const char *oldstr, const char *newstr)
115 int oldlen, newlen;
116 char *p, *q;
118 if (!(p = strstr(str, oldstr)))
119 return p;
120 oldlen = strlen (oldstr);
121 newlen = strlen (newstr);
122 memmove (q = p + newlen, p + oldlen, strlen (p + oldlen) + 1);
123 memcpy (p, newstr, newlen);
124 return q;
128 /*******************************************************************
129 * str_match
131 * Locate one string in another, ignoring spaces
133 const char *str_match (const char *str, const char *match, int *found)
135 assert(str && match && found);
137 while (*str == ' ') str++;
138 if (!strncmp (str, match, strlen (match)))
140 *found = 1;
141 str += strlen (match);
142 while (*str == ' ') str++;
144 else
145 *found = 0;
146 return str;
150 /*******************************************************************
151 * str_find_set
153 * Locate the first occurrence of a set of characters in a string
155 const char *str_find_set (const char *str, const char *findset)
157 assert(str && findset);
159 while (*str)
161 const char *p = findset;
162 while (*p)
163 if (*p++ == *str)
164 return str;
165 str++;
167 return NULL;
171 /*******************************************************************
172 * str_toupper
174 * Uppercase a string
176 char *str_toupper (char *str)
178 char *save = str;
179 while (*str)
181 *str = toupper (*str);
182 str++;
184 return save;
188 /*******************************************************************
189 * open_file
191 * Open a file returning only on success
193 FILE *open_file (const char *name, const char *ext, const char *mode)
195 char fname[128];
196 FILE *fp;
198 if (((unsigned)snprintf (fname, sizeof (fname), "%s%s%s",
199 *mode == 'w' ? "./" : "", name, ext) > sizeof (fname)))
200 fatal ("File name too long");
202 if (VERBOSE)
203 printf ("Open file %s\n", fname);
205 fp = fopen (fname, mode);
206 if (!fp)
207 fatal ("Cant open file");
208 return fp;
212 /*******************************************************************
213 * fatal
215 * Fatal error handling
217 void fatal (const char *message)
219 if (errno)
220 perror (message);
221 else
222 puts (message);
223 do_usage ();