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
22 #include "wine/port.h"
27 /*******************************************************************
30 * Create a single string from many substrings
32 char *str_create(size_t num_str
, ...)
35 size_t len
= 1, i
= 0;
38 va_start (args
, num_str
);
39 for (i
= 0; i
< num_str
; i
++)
40 if ((t
= va_arg(args
, char *)))
44 if (!(tmp
= (char *) malloc (len
)))
45 fatal ("Out of memory");
49 va_start (args
, num_str
);
50 for (i
= 0; i
< num_str
; i
++)
51 if ((t
= va_arg(args
, char *)))
58 /*******************************************************************
61 * Create a single string from many substrings, terminating in a number
63 char *str_create_num(size_t num_str
, int num
, ...)
66 size_t len
= 8, i
= 0;
70 for (i
= 0; i
< num_str
; i
++)
71 if ((t
= va_arg(args
, char *)))
75 if (!(tmp
= (char *) malloc (len
)))
76 fatal ("Out of memory");
81 for (i
= 0; i
< num_str
; i
++)
82 if ((t
= va_arg(args
, char *)))
85 sprintf (tmp
+ len
- 8, "%d", num
);
90 /*******************************************************************
93 * Create a new substring from a string
95 char *str_substring(const char *start
, const char *end
)
99 assert (start
&& end
&& end
> start
);
101 if (!(newstr
= (char *) malloc (end
- start
+ 1)))
102 fatal ("Out of memory");
104 memcpy (newstr
, start
, end
- start
);
105 newstr
[end
- start
] = '\0';
111 /*******************************************************************
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
)
122 if (!(p
= strstr(str
, oldstr
)))
124 oldlen
= strlen (oldstr
);
125 newlen
= strlen (newstr
);
126 memmove (q
= p
+ newlen
, p
+ oldlen
, strlen (p
+ oldlen
) + 1);
127 memcpy (p
, newstr
, newlen
);
132 /*******************************************************************
135 * Locate one string in another, ignoring spaces
137 const char *str_match (const char *str
, const char *match
, int *found
)
139 assert(str
&& match
&& found
);
141 while (*str
== ' ') str
++;
142 if (!strncmp (str
, match
, strlen (match
)))
145 str
+= strlen (match
);
146 while (*str
== ' ') str
++;
154 /*******************************************************************
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
);
165 const char *p
= findset
;
175 /*******************************************************************
180 char *str_toupper (char *str
)
185 *str
= toupper (*str
);
192 /*******************************************************************
195 * Open a file returning only on success
197 FILE *open_file (const char *name
, const char *ext
, const char *mode
)
202 if (((unsigned)snprintf (fname
, sizeof (fname
), "%s%s%s",
203 *mode
== 'w' ? "./" : "", name
, ext
) > sizeof (fname
)))
204 fatal ("File name too long");
207 printf ("Open file %s\n", fname
);
209 fp
= fopen (fname
, mode
);
211 fatal ("Can't open file");
216 /*******************************************************************
219 * Fatal error handling
221 void fatal (const char *message
)