source: trunk/tools/wrc/utils.c@ 882

Last change on this file since 882 was 882, checked in by sandervl, 26 years ago

Created Wine port of wrc (using EMX/GCC)

File size: 4.1 KB
Line 
1/*
2 * Utility routines
3 *
4 * Copyright 1998 Bertho A. Stultiens
5 *
6 */
7
8#include "config.h"
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <stdarg.h>
13#include <string.h>
14#include <assert.h>
15#include <ctype.h>
16
17#include "wrc.h"
18#include "utils.h"
19#include "parser.h"
20
21#define WANT_NEAR_INDICATION
22
23
24#ifdef WANT_NEAR_INDICATION
25void make_print(char *str)
26{
27 while(*str)
28 {
29 if(!isprint(*str))
30 *str = ' ';
31 str++;
32 }
33}
34#endif
35
36int yyerror(const char *s, ...)
37{
38 va_list ap;
39 va_start(ap, s);
40 fprintf(stderr, "Error %s: %d, %d: ", input_name ? input_name : "stdin", line_number, char_number);
41 vfprintf(stderr, s, ap);
42#ifdef WANT_NEAR_INDICATION
43 {
44 char *cpy = xstrdup(yytext);
45 make_print(cpy);
46 fprintf(stderr, " near '%s'\n", cpy);
47 free(cpy);
48 }
49#else
50 fprintf(stderr, "\n");
51#endif
52 va_end(ap);
53 exit(1);
54 return 1;
55}
56
57int yywarning(const char *s, ...)
58{
59 va_list ap;
60 va_start(ap, s);
61 fprintf(stderr, "Warning %s: %d, %d: ", input_name ? input_name : "stdin", line_number, char_number);
62 vfprintf(stderr, s, ap);
63#ifdef WANT_NEAR_INDICATION
64 {
65 char *cpy = xstrdup(yytext);
66 make_print(cpy);
67 fprintf(stderr, " near '%s'\n", cpy);
68 free(cpy);
69 }
70#else
71 fprintf(stderr, "\n");
72#endif
73 va_end(ap);
74 return 0;
75}
76
77void internal_error(const char *file, int line, const char *s, ...)
78{
79 va_list ap;
80 va_start(ap, s);
81 fprintf(stderr, "Internal error (please report) %s %d: ", file, line);
82 vfprintf(stderr, s, ap);
83 fprintf(stderr, "\n");
84 va_end(ap);
85 exit(3);
86}
87
88void error(const char *s, ...)
89{
90 va_list ap;
91 va_start(ap, s);
92 fprintf(stderr, "Error: ");
93 vfprintf(stderr, s, ap);
94 fprintf(stderr, "\n");
95 va_end(ap);
96 exit(2);
97}
98
99void warning(const char *s, ...)
100{
101 va_list ap;
102 va_start(ap, s);
103 fprintf(stderr, "Warning: ");
104 vfprintf(stderr, s, ap);
105 fprintf(stderr, "\n");
106 va_end(ap);
107}
108
109void chat(const char *s, ...)
110{
111 if(debuglevel & DEBUGLEVEL_CHAT)
112 {
113 va_list ap;
114 va_start(ap, s);
115 fprintf(stderr, "FYI: ");
116 vfprintf(stderr, s, ap);
117 fprintf(stderr, "\n");
118 va_end(ap);
119 }
120}
121
122char *dup_basename(const char *name, const char *ext)
123{
124 int namelen;
125 int extlen = strlen(ext);
126 char *base;
127 char *slash;
128
129 if(!name)
130 name = "wrc.tab";
131
132 slash = strrchr(name, '/');
133 if (slash)
134 name = slash + 1;
135
136 namelen = strlen(name);
137
138 /* +4 for later extension and +1 for '\0' */
139 base = (char *)xmalloc(namelen +4 +1);
140 strcpy(base, name);
141 if(!strcasecmp(name + namelen-extlen, ext))
142 {
143 base[namelen - extlen] = '\0';
144 }
145 return base;
146}
147
148void *xmalloc(size_t size)
149{
150 void *res;
151
152 assert(size > 0);
153 assert(size < 102400);
154 res = malloc(size);
155 if(res == NULL)
156 {
157 error("Virtual memory exhausted.\n");
158 }
159 memset(res, 0, size);
160 return res;
161}
162
163
164void *xrealloc(void *p, size_t size)
165{
166 void *res;
167
168 assert(size > 0);
169 assert(size < 102400);
170 res = realloc(p, size);
171 if(res == NULL)
172 {
173 error("Virtual memory exhausted.\n");
174 }
175 return res;
176}
177
178char *xstrdup(const char *str)
179{
180 char *s = (char *)xmalloc(strlen(str)+1);
181 return strcpy(s, str);
182}
183
184int string_compare(const string_t *s1, const string_t *s2)
185{
186 if(s1->type == str_char && s2->type == str_char)
187 {
188 return strcasecmp(s1->str.cstr, s2->str.cstr);
189 }
190 else
191 {
192 internal_error(__FILE__, __LINE__, "Cannot yet compare unicode strings");
193 }
194 return 0;
195}
196
197int wstrlen(const short *s)
198{
199 int cnt = 0;
200 while(*s++)
201 cnt++;
202 return cnt;
203}
204
205short *wstrcpy(short *dst, const short *src)
206{
207 short *d = dst;
208 while(*src)
209 *d++ = *src++;
210 return dst;
211}
212
213int wstricmp(const short *s1, const short *s2)
214{
215 char *cs1 = dupwstr2cstr(s1);
216 char *cs2 = dupwstr2cstr(s2);
217 int retval = strcasecmp(cs1, cs2);
218 free(cs1);
219 free(cs2);
220 warning("Comparing unicode strings without case -> converting to ascii");
221 return retval;;
222}
223
224short *dupcstr2wstr(const char *str)
225{
226 int len = strlen(str) + 1;
227 short *ws = (short *)xmalloc(len*2);
228 short *wptr;
229
230 wptr = ws;
231 /* FIXME: codepage translation */
232 while(*str)
233 *wptr++ = (short)(*str++ & 0xff);
234 *wptr = 0;
235 return ws;
236}
237
238char *dupwstr2cstr(const short *str)
239{
240 int len = wstrlen(str) + 1;
241 char *cs = (char *)xmalloc(len);
242 char *cptr;
243
244 cptr = cs;
245 /* FIXME: codepage translation */
246 while(*str)
247 *cptr++ = (char)*str++;
248 *cptr = 0;
249 return cs;
250}
251
Note: See TracBrowser for help on using the repository browser.