/*1* Misc functions2*3* Copyright 2000 Jon Griffiths4*5* This library is free software; you can redistribute it and/or6* modify it under the terms of the GNU Lesser General Public7* License as published by the Free Software Foundation; either8* version 2.1 of the License, or (at your option) any later version.9*10* This library is distributed in the hope that it will be useful,11* but WITHOUT ANY WARRANTY; without even the implied warranty of12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU13* Lesser General Public License for more details.14*15* You should have received a copy of the GNU Lesser General Public16* License along with this library; if not, write to the Free Software17* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA18*/1920#include "config.h"2122#include "winedump.h"232425/*******************************************************************26* str_substring27*28* Create a new substring from a string29*/30char *str_substring(const char *start, const char *end)31{32char *newstr;3334assert (start && end && end > start);3536newstr = xmalloc (end - start + 1);37memcpy (newstr, start, end - start);38newstr [end - start] = '\0';3940return newstr;41}424344/*******************************************************************45* str_replace46*47* Swap two strings in another string, in place48* Modified PD code from 'snippets'49*/50char *str_replace (char *str, const char *oldstr, const char *newstr)51{52int oldlen, newlen;53char *p, *q;5455if (!(p = strstr(str, oldstr)))56return p;57oldlen = strlen (oldstr);58newlen = strlen (newstr);59memmove (q = p + newlen, p + oldlen, strlen (p + oldlen) + 1);60memcpy (p, newstr, newlen);61return q;62}636465/*******************************************************************66* str_match67*68* Locate one string in another, ignoring spaces69*/70const char *str_match (const char *str, const char *match, BOOL *found)71{72assert(str && match && found);7374while (*str == ' ') str++;75if (!strncmp (str, match, strlen (match)))76{77*found = TRUE;78str += strlen (match);79while (*str == ' ') str++;80}81else82*found = FALSE;83return str;84}858687/*******************************************************************88* str_find_set89*90* Locate the first occurrence of a set of characters in a string91*/92const char *str_find_set (const char *str, const char *findset)93{94assert(str && findset);9596while (*str)97{98const char *p = findset;99while (*p)100if (*p++ == *str)101return str;102str++;103}104return NULL;105}106107108/*******************************************************************109* str_toupper110*111* Uppercase a string112*/113char *str_toupper (char *str)114{115char *save = str;116while (*str)117{118*str = toupper (*str);119str++;120}121return save;122}123124125/*******************************************************************126* open_file127*128* Open a file returning only on success129*/130FILE *open_file (const char *name, const char *ext, const char *mode)131{132char *fname;133FILE *fp;134135fname = strmake( "%s%s%s", *mode == 'w' ? "./" : "", name, ext);136137if (VERBOSE)138printf ("Open file %s\n", fname);139140fp = fopen (fname, mode);141if (!fp)142fatal ("Can't open file");143return fp;144}145146147/*******************************************************************148* fatal149*150* Fatal error handling151*/152void fatal (const char *message)153{154if (errno)155perror (message);156else157puts (message);158exit(1);159}160161162