Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/tools/winedump/misc.c
4389 views
1
/*
2
* Misc functions
3
*
4
* Copyright 2000 Jon Griffiths
5
*
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.
10
*
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.
15
*
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
19
*/
20
21
#include "config.h"
22
23
#include "winedump.h"
24
25
26
/*******************************************************************
27
* str_substring
28
*
29
* Create a new substring from a string
30
*/
31
char *str_substring(const char *start, const char *end)
32
{
33
char *newstr;
34
35
assert (start && end && end > start);
36
37
newstr = xmalloc (end - start + 1);
38
memcpy (newstr, start, end - start);
39
newstr [end - start] = '\0';
40
41
return newstr;
42
}
43
44
45
/*******************************************************************
46
* str_replace
47
*
48
* Swap two strings in another string, in place
49
* Modified PD code from 'snippets'
50
*/
51
char *str_replace (char *str, const char *oldstr, const char *newstr)
52
{
53
int oldlen, newlen;
54
char *p, *q;
55
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;
63
}
64
65
66
/*******************************************************************
67
* str_match
68
*
69
* Locate one string in another, ignoring spaces
70
*/
71
const char *str_match (const char *str, const char *match, BOOL *found)
72
{
73
assert(str && match && found);
74
75
while (*str == ' ') str++;
76
if (!strncmp (str, match, strlen (match)))
77
{
78
*found = TRUE;
79
str += strlen (match);
80
while (*str == ' ') str++;
81
}
82
else
83
*found = FALSE;
84
return str;
85
}
86
87
88
/*******************************************************************
89
* str_find_set
90
*
91
* Locate the first occurrence of a set of characters in a string
92
*/
93
const char *str_find_set (const char *str, const char *findset)
94
{
95
assert(str && findset);
96
97
while (*str)
98
{
99
const char *p = findset;
100
while (*p)
101
if (*p++ == *str)
102
return str;
103
str++;
104
}
105
return NULL;
106
}
107
108
109
/*******************************************************************
110
* str_toupper
111
*
112
* Uppercase a string
113
*/
114
char *str_toupper (char *str)
115
{
116
char *save = str;
117
while (*str)
118
{
119
*str = toupper (*str);
120
str++;
121
}
122
return save;
123
}
124
125
126
/*******************************************************************
127
* open_file
128
*
129
* Open a file returning only on success
130
*/
131
FILE *open_file (const char *name, const char *ext, const char *mode)
132
{
133
char *fname;
134
FILE *fp;
135
136
fname = strmake( "%s%s%s", *mode == 'w' ? "./" : "", name, ext);
137
138
if (VERBOSE)
139
printf ("Open file %s\n", fname);
140
141
fp = fopen (fname, mode);
142
if (!fp)
143
fatal ("Can't open file");
144
return fp;
145
}
146
147
148
/*******************************************************************
149
* fatal
150
*
151
* Fatal error handling
152
*/
153
void fatal (const char *message)
154
{
155
if (errno)
156
perror (message);
157
else
158
puts (message);
159
exit(1);
160
}
161
162