server: Do not set connectionless sockets into the CONNECTING or CONNECTED state.
[wine.git] / tools / winedump / misc.c
blob62a5962547add30437cf2b39d6d63b59b7a51c47
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"
23 #include "winedump.h"
26 /*******************************************************************
27 * str_substring
29 * Create a new substring from a string
31 char *str_substring(const char *start, const char *end)
33 char *newstr;
35 assert (start && end && end > start);
37 newstr = xmalloc (end - start + 1);
38 memcpy (newstr, start, end - start);
39 newstr [end - start] = '\0';
41 return newstr;
45 /*******************************************************************
46 * str_replace
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)
53 int oldlen, newlen;
54 char *p, *q;
56 if (!(p = strstr(str, oldstr)))
57 return p;
58 oldlen = strlen (oldstr);
59 newlen = strlen (newstr);
60 memmove (q = p + newlen, p + oldlen, strlen (p + oldlen) + 1);
61 memcpy (p, newstr, newlen);
62 return q;
66 /*******************************************************************
67 * str_match
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)))
78 *found = TRUE;
79 str += strlen (match);
80 while (*str == ' ') str++;
82 else
83 *found = FALSE;
84 return str;
88 /*******************************************************************
89 * str_find_set
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);
97 while (*str)
99 const char *p = findset;
100 while (*p)
101 if (*p++ == *str)
102 return str;
103 str++;
105 return NULL;
109 /*******************************************************************
110 * str_toupper
112 * Uppercase a string
114 char *str_toupper (char *str)
116 char *save = str;
117 while (*str)
119 *str = toupper (*str);
120 str++;
122 return save;
126 /*******************************************************************
127 * open_file
129 * Open a file returning only on success
131 FILE *open_file (const char *name, const char *ext, const char *mode)
133 char *fname;
134 FILE *fp;
136 fname = strmake( "%s%s%s", *mode == 'w' ? "./" : "", name, ext);
138 if (VERBOSE)
139 printf ("Open file %s\n", fname);
141 fp = fopen (fname, mode);
142 if (!fp)
143 fatal ("Can't open file");
144 return fp;
148 /*******************************************************************
149 * fatal
151 * Fatal error handling
153 void fatal (const char *message)
155 if (errno)
156 perror (message);
157 else
158 puts (message);
159 exit(1);