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
26 /*******************************************************************
29 * Create a new substring from a string
31 char *str_substring(const char *start
, const char *end
)
35 assert (start
&& end
&& end
> start
);
37 newstr
= xmalloc (end
- start
+ 1);
38 memcpy (newstr
, start
, end
- start
);
39 newstr
[end
- start
] = '\0';
45 /*******************************************************************
48 * Swap two strings in another string, in place
49 * Modified PD code from 'snippets'
51 char *str_replace (char *str
, const char *oldstr
, const char *newstr
)
56 if (!(p
= strstr(str
, oldstr
)))
58 oldlen
= strlen (oldstr
);
59 newlen
= strlen (newstr
);
60 memmove (q
= p
+ newlen
, p
+ oldlen
, strlen (p
+ oldlen
) + 1);
61 memcpy (p
, newstr
, newlen
);
66 /*******************************************************************
69 * Locate one string in another, ignoring spaces
71 const char *str_match (const char *str
, const char *match
, BOOL
*found
)
73 assert(str
&& match
&& found
);
75 while (*str
== ' ') str
++;
76 if (!strncmp (str
, match
, strlen (match
)))
79 str
+= strlen (match
);
80 while (*str
== ' ') str
++;
88 /*******************************************************************
91 * Locate the first occurrence of a set of characters in a string
93 const char *str_find_set (const char *str
, const char *findset
)
95 assert(str
&& findset
);
99 const char *p
= findset
;
109 /*******************************************************************
114 char *str_toupper (char *str
)
119 *str
= toupper (*str
);
126 /*******************************************************************
129 * Open a file returning only on success
131 FILE *open_file (const char *name
, const char *ext
, const char *mode
)
136 fname
= strmake( "%s%s%s", *mode
== 'w' ? "./" : "", name
, ext
);
139 printf ("Open file %s\n", fname
);
141 fp
= fopen (fname
, mode
);
143 fatal ("Can't open file");
148 /*******************************************************************
151 * Fatal error handling
153 void fatal (const char *message
)