OSDN Git Service

thangorodorimにあったパッチいろいろ。サブウィンドウの更新の不備の修正、
[hengband/hengband.git] / src / z-util.c
1 /* File: z-util.c */
2
3 /* Purpose: Low level utilities -BEN- */
4
5 #include "z-util.h"
6
7
8
9 /*
10  * Global variables for temporary use
11  */
12 char char_tmp = 0;
13 byte byte_tmp = 0;
14 sint sint_tmp = 0;
15 uint uint_tmp = 0;
16 long long_tmp = 0;
17 huge huge_tmp = 0;
18 errr errr_tmp = 0;
19
20
21 /*
22  * Global pointers for temporary use
23  */
24 cptr cptr_tmp = NULL;
25 vptr vptr_tmp = NULL;
26
27
28
29
30 /*
31  * Constant bool meaning true
32  */
33 bool bool_true = 1;
34
35 /*
36  * Constant bool meaning false
37  */
38 bool bool_false = 0;
39
40
41 /*
42  * Global NULL cptr
43  */
44 cptr cptr_null = NULL;
45
46
47 /*
48  * Global NULL vptr
49  */
50 vptr vptr_null = NULL;
51
52
53
54 /*
55  * Global SELF vptr
56  */
57 vptr vptr_self = (vptr)(&vptr_self);
58
59
60
61 /*
62  * Convenient storage of the program name
63  */
64 cptr argv0 = NULL;
65
66
67
68 /*
69  * A routine that does nothing
70  */
71 void func_nothing(void)
72 {
73         /* Do nothing */
74 }
75
76
77 /*
78  * A routine that always returns "success"
79  */
80 errr func_success(void)
81 {
82         return (0);
83 }
84
85
86 /*
87  * A routine that always returns a simple "problem code"
88  */
89 errr func_problem(void)
90 {
91         return (1);
92 }
93
94
95 /*
96  * A routine that always returns a simple "failure code"
97  */
98 errr func_failure(void)
99 {
100         return (-1);
101 }
102
103
104
105 /*
106  * A routine that always returns "true"
107  */
108 bool func_true(void)
109 {
110         return (1);
111 }
112
113
114 /*
115  * A routine that always returns "false"
116  */
117 bool func_false(void)
118 {
119         return (0);
120 }
121
122
123
124
125 /*
126  * Determine if string "t" is equal to string "t"
127  */
128 bool streq(cptr a, cptr b)
129 {
130         return (!strcmp(a, b));
131 }
132
133
134 /*
135  * Determine if string "t" is a suffix of string "s"
136  */
137 bool suffix(cptr s, cptr t)
138 {
139         int tlen = strlen(t);
140         int slen = strlen(s);
141
142         /* Check for incompatible lengths */
143         if (tlen > slen) return (FALSE);
144
145         /* Compare "t" to the end of "s" */
146         return (!strcmp(s + slen - tlen, t));
147 }
148
149
150 /*
151  * Determine if string "t" is a prefix of string "s"
152  */
153 bool prefix(cptr s, cptr t)
154 {
155         /* Scan "t" */
156         while (*t)
157         {
158                 /* Compare content and length */
159                 if (*t++ != *s++) return (FALSE);
160         }
161
162         /* Matched, we have a prefix */
163         return (TRUE);
164 }
165
166
167
168 /*
169  * Redefinable "plog" action
170  */
171 void (*plog_aux)(cptr) = NULL;
172
173 /*
174  * Print (or log) a "warning" message (ala "perror()")
175  * Note the use of the (optional) "plog_aux" hook.
176  */
177 void plog(cptr str)
178 {
179         /* Use the "alternative" function if possible */
180         if (plog_aux) (*plog_aux)(str);
181
182         /* Just do a labeled fprintf to stderr */
183         else (void)(fprintf(stderr, "%s: %s\n", argv0 ? argv0 : "???", str));
184 }
185
186
187
188 /*
189  * Redefinable "quit" action
190  */
191 void (*quit_aux)(cptr) = NULL;
192
193 /*
194  * Exit (ala "exit()").  If 'str' is NULL, do "exit(0)".
195  * If 'str' begins with "+" or "-", do "exit(atoi(str))".
196  * Otherwise, plog() 'str' and exit with an error code of -1.
197  * But always use 'quit_aux', if set, before anything else.
198  */
199 void quit(cptr str)
200 {
201         /* Attempt to use the aux function */
202         if (quit_aux) (*quit_aux)(str);
203
204         /* Success */
205         if (!str) (void)(exit(0));
206
207         /* Extract a "special error code" */
208         if ((str[0] == '-') || (str[0] == '+')) (void)(exit(atoi(str)));
209
210         /* Send the string to plog() */
211         plog(str);
212
213         /* Failure */
214         (void)(exit(EXIT_FAILURE));
215 }
216
217
218
219 /*
220  * Redefinable "core" action
221  */
222 void (*core_aux)(cptr) = NULL;
223
224 /*
225  * Dump a core file, after printing a warning message
226  * As with "quit()", try to use the "core_aux()" hook first.
227  */
228 void core(cptr str)
229 {
230         char *crash = NULL;
231
232         /* Use the aux function */
233         if (core_aux) (*core_aux)(str);
234
235         /* Dump the warning string */
236         if (str) plog(str);
237
238         /* Attempt to Crash */
239         (*crash) = (*crash);
240
241         /* Be sure we exited */
242         quit("core() failed");
243 }
244
245
246
247