configure: Fix check for cms.
[wine/multimedia.git] / tools / winedump / misc.c
blob98cd3098934cfeda223483b15d855861c1aa9abe
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include "winedump.h"
27 /*******************************************************************
28 * str_create
30 * Create a single string from many substrings
32 char *str_create(size_t num_str, ...)
34 va_list args;
35 size_t len = 1, i = 0;
36 char *tmp, *t;
38 va_start (args, num_str);
39 for (i = 0; i < num_str; i++)
40 if ((t = va_arg(args, char *)))
41 len += strlen (t);
42 va_end (args);
44 if (!(tmp = malloc (len)))
45 fatal ("Out of memory");
47 tmp[0] = '\0';
49 va_start (args, num_str);
50 for (i = 0; i < num_str; i++)
51 if ((t = va_arg(args, char *)))
52 strcat (tmp, t);
53 va_end (args);
54 return tmp;
58 /*******************************************************************
59 * str_create_num
61 * Create a single string from many substrings, terminating in a number
63 char *str_create_num(size_t num_str, int num, ...)
65 va_list args;
66 size_t len = 8, i = 0;
67 char *tmp, *t;
69 va_start (args, num);
70 for (i = 0; i < num_str; i++)
71 if ((t = va_arg(args, char *)))
72 len += strlen (t);
73 va_end (args);
75 if (!(tmp = malloc (len)))
76 fatal ("Out of memory");
78 tmp[0] = '\0';
80 va_start (args, num);
81 for (i = 0; i < num_str; i++)
82 if ((t = va_arg(args, char *)))
83 strcat (tmp, t);
84 va_end (args);
85 sprintf (tmp + len - 8, "%d", num);
86 return tmp;
90 /*******************************************************************
91 * str_substring
93 * Create a new substring from a string
95 char *str_substring(const char *start, const char *end)
97 char *newstr;
99 assert (start && end && end > start);
101 if (!(newstr = malloc (end - start + 1)))
102 fatal ("Out of memory");
104 memcpy (newstr, start, end - start);
105 newstr [end - start] = '\0';
107 return newstr;
111 /*******************************************************************
112 * str_replace
114 * Swap two strings in another string, in place
115 * Modified PD code from 'snippets'
117 char *str_replace (char *str, const char *oldstr, const char *newstr)
119 int oldlen, newlen;
120 char *p, *q;
122 if (!(p = strstr(str, oldstr)))
123 return p;
124 oldlen = strlen (oldstr);
125 newlen = strlen (newstr);
126 memmove (q = p + newlen, p + oldlen, strlen (p + oldlen) + 1);
127 memcpy (p, newstr, newlen);
128 return q;
132 /*******************************************************************
133 * str_match
135 * Locate one string in another, ignoring spaces
137 const char *str_match (const char *str, const char *match, BOOL *found)
139 assert(str && match && found);
141 while (*str == ' ') str++;
142 if (!strncmp (str, match, strlen (match)))
144 *found = TRUE;
145 str += strlen (match);
146 while (*str == ' ') str++;
148 else
149 *found = FALSE;
150 return str;
154 /*******************************************************************
155 * str_find_set
157 * Locate the first occurrence of a set of characters in a string
159 const char *str_find_set (const char *str, const char *findset)
161 assert(str && findset);
163 while (*str)
165 const char *p = findset;
166 while (*p)
167 if (*p++ == *str)
168 return str;
169 str++;
171 return NULL;
175 /*******************************************************************
176 * str_toupper
178 * Uppercase a string
180 char *str_toupper (char *str)
182 char *save = str;
183 while (*str)
185 *str = toupper (*str);
186 str++;
188 return save;
192 /*******************************************************************
193 * open_file
195 * Open a file returning only on success
197 FILE *open_file (const char *name, const char *ext, const char *mode)
199 char fname[128];
200 FILE *fp;
202 if (((unsigned)snprintf (fname, sizeof (fname), "%s%s%s",
203 *mode == 'w' ? "./" : "", name, ext) > sizeof (fname)))
204 fatal ("File name too long");
206 if (VERBOSE)
207 printf ("Open file %s\n", fname);
209 fp = fopen (fname, mode);
210 if (!fp)
211 fatal ("Can't open file");
212 return fp;
216 /*******************************************************************
217 * fatal
219 * Fatal error handling
221 void fatal (const char *message)
223 if (errno)
224 perror (message);
225 else
226 puts (message);
227 exit(1);