]>
diplodocus.org Git - nmh/blob - h/utils.h
1 /* utils.h -- utility prototypes
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))
13 /* Call malloc(3), exiting on NULL return. */
14 void *mh_xmalloc(size_t size
) MALLOC
ALLOC_SIZE(1);
16 /* Call realloc(3), exiting on NULL return. */
17 void *mh_xrealloc(void *ptr
, size_t size
) ALLOC_SIZE(2);
19 /* Call calloc(3), exiting on NULL return. */
20 void *mh_xcalloc(size_t nelem
, size_t elsize
) MALLOC
ALLOC_SIZE(1, 2);
22 /* Duplicate a NUL-terminated string, exit on failure. */
23 char *mh_xstrdup(const char *src
) MALLOC
;
25 /* Call free(3), if ptr isn't NULL. */
26 void mh_xfree(void *ptr
);
28 /* Set p to point to newly allocated, uninitialised, memory. */
29 #define NEW(p) ((p) = mh_xmalloc(sizeof *(p)))
31 /* Set p to point to newly allocated, zeroed, memory. */
32 #define NEW0(p) ((p) = mh_xcalloc(1, sizeof *(p)))
34 /* Zero the bytes to which p points. */
35 #define ZERO(p) memset((p), 0, sizeof *(p))
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
;
45 * A vector of char array, used to hold a list of string message numbers
46 * or command arguments.
55 * Same as msgs_array, but for a vector of ints
64 * Add a argument to the given msgs_array or msgnum_array structure; extend
65 * the array size if necessary
68 void app_msgarg(struct msgs_array
*, char *);
69 void app_msgnum(struct msgnum_array
*, int);
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
;
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))
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
);
86 bool contains8bit(const char *start
, const char *end
);
89 * See if file has any 8-bit bytes.
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.
96 * Returns OK on success, NOTOK on read failure.
99 int scan_input (int fd
, int *eightbit
);
102 * Returns string representation of int, in static memory.
104 char *m_str(int value
);
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 "?".
112 char *m_strn(int value
, unsigned int width
);
115 * program initialization
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
123 int nmh_init(const char *argv0
, int read_context
);
126 * Compares prior version of nmh with current version. Returns 1
127 * if they compare the be the same, 0 if not.
129 * older - 0 for difference comparison, 1 for only if older
131 int nmh_version_changed (int older
);