OSDN Git Service

Leon氏の勧めに従って、Vanillaのコードと同様に各ソースファイルの頭の
[hengband/hengband.git] / src / z-util.c
1 /* File: z-util.c */
2
3 /*
4  * Copyright (c) 1997 Ben Harrison
5  *
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.
9  */
10
11 /* Purpose: Low level utilities -BEN- */
12
13 #include "z-util.h"
14
15
16 /*
17  * Convenient storage of the program name
18  */
19 cptr argv0 = NULL;
20
21
22 /*
23  * Determine if string "t" is equal to string "t"
24  */
25 bool streq(cptr a, cptr b)
26 {
27         return (!strcmp(a, b));
28 }
29
30
31 /*
32  * Determine if string "t" is a suffix of string "s"
33  */
34 bool suffix(cptr s, cptr t)
35 {
36         int tlen = strlen(t);
37         int slen = strlen(s);
38
39         /* Check for incompatible lengths */
40         if (tlen > slen) return (FALSE);
41
42         /* Compare "t" to the end of "s" */
43         return (!strcmp(s + slen - tlen, t));
44 }
45
46
47 /*
48  * Determine if string "t" is a prefix of string "s"
49  */
50 bool prefix(cptr s, cptr t)
51 {
52         /* Scan "t" */
53         while (*t)
54         {
55                 /* Compare content and length */
56                 if (*t++ != *s++) return (FALSE);
57         }
58
59         /* Matched, we have a prefix */
60         return (TRUE);
61 }
62
63
64
65 /*
66  * Redefinable "plog" action
67  */
68 void (*plog_aux)(cptr) = NULL;
69
70 /*
71  * Print (or log) a "warning" message (ala "perror()")
72  * Note the use of the (optional) "plog_aux" hook.
73  */
74 void plog(cptr str)
75 {
76         /* Use the "alternative" function if possible */
77         if (plog_aux) (*plog_aux)(str);
78
79         /* Just do a labeled fprintf to stderr */
80         else (void)(fprintf(stderr, "%s: %s\n", argv0 ? argv0 : "???", str));
81 }
82
83
84
85 /*
86  * Redefinable "quit" action
87  */
88 void (*quit_aux)(cptr) = NULL;
89
90 /*
91  * Exit (ala "exit()").  If 'str' is NULL, do "exit(0)".
92  * If 'str' begins with "+" or "-", do "exit(atoi(str))".
93  * Otherwise, plog() 'str' and exit with an error code of -1.
94  * But always use 'quit_aux', if set, before anything else.
95  */
96 void quit(cptr str)
97 {
98         /* Attempt to use the aux function */
99         if (quit_aux) (*quit_aux)(str);
100
101         /* Success */
102         if (!str) (void)(exit(0));
103
104         /* Extract a "special error code" */
105         if ((str[0] == '-') || (str[0] == '+')) (void)(exit(atoi(str)));
106
107         /* Send the string to plog() */
108         plog(str);
109
110         /* Failure */
111         (void)(exit(EXIT_FAILURE));
112 }
113
114
115
116 /*
117  * Redefinable "core" action
118  */
119 void (*core_aux)(cptr) = NULL;
120
121 /*
122  * Dump a core file, after printing a warning message
123  * As with "quit()", try to use the "core_aux()" hook first.
124  */
125 void core(cptr str)
126 {
127         char *crash = NULL;
128
129         /* Use the aux function */
130         if (core_aux) (*core_aux)(str);
131
132         /* Dump the warning string */
133         if (str) plog(str);
134
135         /* Attempt to Crash */
136         (*crash) = (*crash);
137
138         /* Be sure we exited */
139         quit("core() failed");
140 }
141
142
143
144