tools: Convert source files to utf-8.
[wine] / tools / winedump / misc.c
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 #include "wine/port.h"
23
24 #include "winedump.h"
25
26
27 /*******************************************************************
28  *         str_create
29  *
30  * Create a single string from many substrings
31  */
32 char *str_create(size_t num_str, ...)
33 {
34   va_list args;
35   size_t len = 1, i = 0;
36   char *tmp, *t;
37
38   va_start (args, num_str);
39   for (i = 0; i < num_str; i++)
40     if ((t = va_arg(args, char *)))
41       len += strlen (t);
42   va_end (args);
43
44   if (!(tmp = (char *) malloc (len)))
45     fatal ("Out of memory");
46
47   tmp[0] = '\0';
48
49   va_start (args, num_str);
50   for (i = 0; i < num_str; i++)
51     if ((t = va_arg(args, char *)))
52       strcat (tmp, t);
53   va_end (args);
54   return tmp;
55 }
56
57
58 /*******************************************************************
59  *         str_create_num
60  *
61  * Create a single string from many substrings, terminating in a number
62  */
63 char *str_create_num(size_t num_str, int num, ...)
64 {
65   va_list args;
66   size_t len = 8, i = 0;
67   char *tmp, *t;
68
69   va_start (args, num);
70   for (i = 0; i < num_str; i++)
71     if ((t = va_arg(args, char *)))
72       len += strlen (t);
73   va_end (args);
74
75   if (!(tmp = (char *) malloc (len)))
76     fatal ("Out of memory");
77
78   tmp[0] = '\0';
79
80   va_start (args, num);
81   for (i = 0; i < num_str; i++)
82     if ((t = va_arg(args, char *)))
83       strcat (tmp, t);
84   va_end (args);
85   sprintf (tmp + len - 8, "%d", num);
86   return tmp;
87 }
88
89
90 /*******************************************************************
91  *         str_substring
92  *
93  * Create a new substring from a string
94  */
95 char *str_substring(const char *start, const char *end)
96 {
97   char *newstr;
98
99   assert (start && end && end > start);
100
101   if (!(newstr = (char *) malloc (end - start + 1)))
102     fatal ("Out of memory");
103
104   memcpy (newstr, start, end - start);
105   newstr [end - start] = '\0';
106
107   return newstr;
108 }
109
110
111 /*******************************************************************
112  *         str_replace
113  *
114  * Swap two strings in another string, in place
115  * Modified PD code from 'snippets'
116  */
117 char *str_replace (char *str, const char *oldstr, const char *newstr)
118 {
119   int oldlen, newlen;
120   char *p, *q;
121
122   if (!(p = strstr(str, oldstr)))
123     return p;
124   oldlen = strlen (oldstr);
125   newlen = strlen (newstr);
126   memmove (q = p + newlen, p + oldlen, strlen (p + oldlen) + 1);
127   memcpy (p, newstr, newlen);
128   return q;
129 }
130
131
132 /*******************************************************************
133  *         str_match
134  *
135  * Locate one string in another, ignoring spaces
136  */
137 const char *str_match (const char *str, const char *match, int *found)
138 {
139   assert(str && match && found);
140
141   while (*str == ' ') str++;
142   if (!strncmp (str, match, strlen (match)))
143   {
144     *found = 1;
145     str += strlen (match);
146     while (*str == ' ') str++;
147   }
148   else
149     *found = 0;
150   return str;
151 }
152
153
154 /*******************************************************************
155  *         str_find_set
156  *
157  * Locate the first occurrence of a set of characters in a string
158  */
159 const char *str_find_set (const char *str, const char *findset)
160 {
161   assert(str && findset);
162
163   while (*str)
164   {
165     const char *p = findset;
166     while (*p)
167       if (*p++ == *str)
168         return str;
169     str++;
170   }
171   return NULL;
172 }
173
174
175 /*******************************************************************
176  *         str_toupper
177  *
178  * Uppercase a string
179  */
180 char *str_toupper (char *str)
181 {
182   char *save = str;
183   while (*str)
184   {
185     *str = toupper (*str);
186     str++;
187   }
188   return save;
189 }
190
191
192 /*******************************************************************
193  *         open_file
194  *
195  * Open a file returning only on success
196  */
197 FILE *open_file (const char *name, const char *ext, const char *mode)
198 {
199   char  fname[128];
200   FILE *fp;
201
202   if (((unsigned)snprintf (fname, sizeof (fname), "%s%s%s",
203                  *mode == 'w' ? "./" : "", name, ext) > sizeof (fname)))
204     fatal ("File name too long");
205
206   if (VERBOSE)
207     printf ("Open file %s\n", fname);
208
209   fp = fopen (fname, mode);
210   if (!fp)
211     fatal ("Can't open file");
212   return fp;
213 }
214
215
216 /*******************************************************************
217  *         fatal
218  *
219  * Fatal error handling
220  */
221 void  fatal (const char *message)
222 {
223   if (errno)
224     perror (message);
225   else
226     puts (message);
227   exit(1);
228 }