Debian Codegraph @

texk/musixtnt/musixtnt-src/utils.c

raw
1 /* Copyright (C) 2014-15 R. D. Tennent School of Computing,
2 * Queen's University, [email protected]
3 *
4 * This program is free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General
6 * Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will
11 * be useful, but WITHOUT ANY WARRANTY; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General
17 * Public License along with this program; if not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street,
19 * Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 /* utils.c - utilities for fixmsxpart.c and msxlint.c
23 */
24
25 # include "utils.h"
26
27 void
28 warning (const char msg[]) /* output warning message msg to stderr */
29 {
30 fprintf (stderr, "Warning: %s\n", msg);
31 return;
32 }
33
34 void
35 error (const char msg[]) /* abort with stderr message msg */
36 {
37 fprintf (stderr, "Error: %s\n", msg);
38 exit (EXIT_FAILURE);
39 }
40
41 bool
42 prefix (const char *cs, const char *ct)
43 /*
44 is string cs[] a prefix of ct[]?
45 */
46 {
47 const char *pcs = cs;
48 const char *pct = ct;
49 while (*pcs != '\0')
50 {
51 if (*pcs != *pct) return false;
52 pcs++; pct++;
53 }
54 return true;
55 }
56
57 bool
58 suffix (const char *cs, const char *ct)
59 /* is string cs[] a suffix of ct[]? */
60 {
61 const char *pcs = cs;
62 const char *pct = ct + strlen (ct) - strlen (cs);
63 while (*pcs != '\0')
64 {
65 if (*pct != *pcs) return false;
66 pcs++; pct++;
67 }
68 return true;
69 }
70
71 size_t
72 append (char *dst, char **offset, const char *src, size_t n)
73 /* Copies src to *offset and updates *offset accordingly (if possible).
74 * Assumes *offset is dst if offset == NULL.
75 * Execution aborts if **offset != '\0'. The src string must be null-terminated.
76 * Returns (original offset - dst) + strlen(src); if >= n, the string was truncated.
77 */
78 {
79 const char *s = src;
80 char *d;
81 char *off_orig = ( offset != NULL ? *offset : dst );
82 d = off_orig;
83 if (*d != '\0')
84 {
85 char msg[LINE_LEN + 24] = {'\0'};
86 char *msg_n = msg;
87 append (msg, &msg_n, "\nNull character expected at offset specified for string ", sizeof (msg));
88 append (msg, &msg_n, src, sizeof (msg));
89 error (msg);
90 }
91 n = n - 1 - (off_orig - dst); /* number of available slots (leaving room for \0) */
92 while (n > 0 && *s != '\0')
93 {
94 *d = *s; /* copy non-null character */
95 d++; s++; n--;
96 }
97 *d = '\0'; /* null-terminate */
98 if (offset != NULL)
99 *offset = d; /* update *offset (if possible) */
100 while (*s != '\0') /* traverse rest of src */
101 s++;
102 return (off_orig - dst) + (s - src); /* length of the untruncated string */
103 }