]> diplodocus.org Git - nmh/blob - h/utils.h
datetime.c: Replace some int with bool.
[nmh] / h / utils.h
1 /* utils.h -- utility prototypes
2 */
3
4 /* PLURALS gives a pointer to the string "s" when n isn't 1, and to the
5 * empty string "" when it is. Suitable for obtaining the plural `s'
6 * used for English nouns. It treats -1 as plural, as does GNU gettext.
7 * Having output vary for plurals is annoying for those writing parsers;
8 * better to phrase the output such that no test is needed, e.g.
9 * "messages found: 42". */
10 extern const char plurals[];
11 #define PLURALS(n) (plurals + ((n) == 1))
12
13 /* Call malloc(3), exiting on NULL return. */
14 void *mh_xmalloc(size_t size) MALLOC ALLOC_SIZE(1);
15
16 /* Call realloc(3), exiting on NULL return. */
17 void *mh_xrealloc(void *ptr, size_t size) ALLOC_SIZE(2);
18
19 /* Call calloc(3), exiting on NULL return. */
20 void *mh_xcalloc(size_t nelem, size_t elsize) MALLOC ALLOC_SIZE(1, 2);
21
22 /* Duplicate a NUL-terminated string, exit on failure. */
23 char *mh_xstrdup(const char *src) MALLOC;
24
25 /* Call free(3), if ptr isn't NULL. */
26 void mh_xfree(void *ptr);
27
28 /* Set p to point to newly allocated, uninitialised, memory. */
29 #define NEW(p) ((p) = mh_xmalloc(sizeof *(p)))
30
31 /* Set p to point to newly allocated, zeroed, memory. */
32 #define NEW0(p) ((p) = mh_xcalloc(1, sizeof *(p)))
33
34 /* Zero the bytes to which p points. */
35 #define ZERO(p) memset((p), 0, sizeof *(p))
36
37 char *pwd(void);
38 char *add(const char *, char *) MALLOC;
39 char *addlist(char *, const char *) MALLOC;
40 int folder_exists(const char *);
41 void create_folder(char *, int, void (*)(int));
42 int num_digits(int) PURE;
43
44 /*
45 * A vector of char array, used to hold a list of string message numbers
46 * or command arguments.
47 */
48
49 struct msgs_array {
50 int max, size;
51 char **msgs;
52 };
53
54 /*
55 * Same as msgs_array, but for a vector of ints
56 */
57
58 struct msgnum_array {
59 int max, size;
60 int *msgnums;
61 };
62
63 /*
64 * Add a argument to the given msgs_array or msgnum_array structure; extend
65 * the array size if necessary
66 */
67
68 void app_msgarg(struct msgs_array *, char *);
69 void app_msgnum(struct msgnum_array *, int);
70
71 char *find_str (const char [], size_t, const char *) PURE;
72 char *rfind_str (const char [], size_t, const char *) PURE;
73 char *nmh_strcasestr (const char *, const char *) PURE;
74
75 void trunccpy(char *dst, const char *src, size_t size);
76 /* A convenience for the common case of dst being an array. */
77 #define TRUNCCPY(dst, src) trunccpy(dst, src, sizeof (dst))
78
79 bool has_prefix(const char *s, const char *prefix) PURE;
80 bool has_suffix(const char *s, const char *suffix) PURE;
81 bool has_suffix_c(const char *s, int c) PURE;
82 void trim_suffix_c(char *s, int c);
83 void to_lower(char *s);
84 void to_upper(char *s);
85
86 bool contains8bit(const char *start, const char *end);
87
88 /*
89 * See if file has any 8-bit bytes.
90 * Arguments include:
91 *
92 * fd - file descriptor
93 * eightbit - address of result, will be set to 1 if the file contains
94 * any 8-bit bytes, 0 otherwise.
95 *
96 * Returns OK on success, NOTOK on read failure.
97 *
98 */
99 int scan_input (int fd, int *eightbit);
100
101 /*
102 * Returns string representation of int, in static memory.
103 */
104 char *m_str(int value);
105
106 /*
107 * Returns string representation of an int, in static memory. If width
108 * == 0, does not limit the width. If width > 0 and value will not fit
109 * in field of that size, including any negative sign but excluding
110 * terminating null, then returns "?".
111 */
112 char *m_strn(int value, unsigned int width);
113
114 /*
115 * program initialization
116 *
117 * argv0 - argv[0], presumably the program name
118 * read_context - 0: don't read context
119 * - 1: read context, check nmh version, and issue warning message
120 * if non-existent or old
121 * - 2: read context, don't check nmh version
122 */
123 int nmh_init(const char *argv0, int read_context);
124
125 /*
126 * Compares prior version of nmh with current version. Returns 1
127 * if they compare the be the same, 0 if not.
128 *
129 * older - 0 for difference comparison, 1 for only if older
130 */
131 int nmh_version_changed (int older);