OSDN Git Service

ドキュメント類を自動拾いエディタの検索機能に対応。
[hengbandforosx/hengbandosx.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  * Convenient storage of the program name
10  */
11 cptr argv0 = NULL;
12
13
14 /*
15  * Determine if string "t" is equal to string "t"
16  */
17 bool streq(cptr a, cptr b)
18 {
19         return (!strcmp(a, b));
20 }
21
22
23 /*
24  * Determine if string "t" is a suffix of string "s"
25  */
26 bool suffix(cptr s, cptr t)
27 {
28         int tlen = strlen(t);
29         int slen = strlen(s);
30
31         /* Check for incompatible lengths */
32         if (tlen > slen) return (FALSE);
33
34         /* Compare "t" to the end of "s" */
35         return (!strcmp(s + slen - tlen, t));
36 }
37
38
39 /*
40  * Determine if string "t" is a prefix of string "s"
41  */
42 bool prefix(cptr s, cptr t)
43 {
44         /* Scan "t" */
45         while (*t)
46         {
47                 /* Compare content and length */
48                 if (*t++ != *s++) return (FALSE);
49         }
50
51         /* Matched, we have a prefix */
52         return (TRUE);
53 }
54
55
56
57 /*
58  * Redefinable "plog" action
59  */
60 void (*plog_aux)(cptr) = NULL;
61
62 /*
63  * Print (or log) a "warning" message (ala "perror()")
64  * Note the use of the (optional) "plog_aux" hook.
65  */
66 void plog(cptr str)
67 {
68         /* Use the "alternative" function if possible */
69         if (plog_aux) (*plog_aux)(str);
70
71         /* Just do a labeled fprintf to stderr */
72         else (void)(fprintf(stderr, "%s: %s\n", argv0 ? argv0 : "???", str));
73 }
74
75
76
77 /*
78  * Redefinable "quit" action
79  */
80 void (*quit_aux)(cptr) = NULL;
81
82 /*
83  * Exit (ala "exit()").  If 'str' is NULL, do "exit(0)".
84  * If 'str' begins with "+" or "-", do "exit(atoi(str))".
85  * Otherwise, plog() 'str' and exit with an error code of -1.
86  * But always use 'quit_aux', if set, before anything else.
87  */
88 void quit(cptr str)
89 {
90         /* Attempt to use the aux function */
91         if (quit_aux) (*quit_aux)(str);
92
93         /* Success */
94         if (!str) (void)(exit(0));
95
96         /* Extract a "special error code" */
97         if ((str[0] == '-') || (str[0] == '+')) (void)(exit(atoi(str)));
98
99         /* Send the string to plog() */
100         plog(str);
101
102         /* Failure */
103         (void)(exit(EXIT_FAILURE));
104 }
105
106
107
108 /*
109  * Redefinable "core" action
110  */
111 void (*core_aux)(cptr) = NULL;
112
113 /*
114  * Dump a core file, after printing a warning message
115  * As with "quit()", try to use the "core_aux()" hook first.
116  */
117 void core(cptr str)
118 {
119         char *crash = NULL;
120
121         /* Use the aux function */
122         if (core_aux) (*core_aux)(str);
123
124         /* Dump the warning string */
125         if (str) plog(str);
126
127         /* Attempt to Crash */
128         (*crash) = (*crash);
129
130         /* Be sure we exited */
131         quit("core() failed");
132 }
133
134
135
136