]>
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 /* Set p to point to newly allocated, uninitialised, memory. */
26 #define NEW(p) ((p) = mh_xmalloc(sizeof *(p)))
28 /* Set p to point to newly allocated, zeroed, memory. */
29 #define NEW0(p) ((p) = mh_xcalloc(1, sizeof *(p)))
31 /* Zero the bytes to which p points. */
32 #define ZERO(p) memset((p), 0, sizeof *(p))
35 char *add(const char *, char *) MALLOC
;
36 char *addlist(char *, const char *) MALLOC
;
37 int folder_exists(const char *);
38 void create_folder(char *, int, void (*)(int));
39 int num_digits(int) PURE
;
42 * A vector of char array, used to hold a list of string message numbers
43 * or command arguments.
52 * Same as msgs_array, but for a vector of ints
61 * Add a argument to the given msgs_array or msgnum_array structure; extend
62 * the array size if necessary
65 void app_msgarg(struct msgs_array
*, char *);
66 void app_msgnum(struct msgnum_array
*, int);
68 char *find_str (const char [], size_t, const char *) PURE
;
69 char *rfind_str (const char [], size_t, const char *) PURE
;
70 char *nmh_strcasestr (const char *, const char *) PURE
;
72 void trunccpy(char *dst
, const char *src
, size_t size
);
73 /* A convenience for the common case of dst being an array. */
74 #define TRUNCCPY(dst, src) trunccpy(dst, src, sizeof (dst))
76 bool has_prefix(const char *s
, const char *prefix
) PURE
;
77 bool has_suffix(const char *s
, const char *suffix
) PURE
;
78 bool has_suffix_c(const char *s
, int c
) PURE
;
79 void trim_suffix_c(char *s
, int c
);
80 void to_lower(char *s
);
81 void to_upper(char *s
);
83 bool contains8bit(const char *start
, const char *end
);
86 * See if file has any 8-bit bytes.
89 * fd - file descriptor
90 * eightbit - address of result, will be set to 1 if the file contains
91 * any 8-bit bytes, 0 otherwise.
93 * Returns OK on success, NOTOK on read failure.
96 int scan_input (int fd
, int *eightbit
);
99 * Returns string representation of int, in static memory.
101 char *m_str(int value
);
104 * Returns string representation of an int, in static memory. If width
105 * == 0, does not limit the width. If width > 0 and value will not fit
106 * in field of that size, including any negative sign but excluding
107 * terminating null, then returns "?".
109 char *m_strn(int value
, unsigned int width
);
112 * program initialization
114 * argv0 - argv[0], presumably the program name
115 * read_context - whether to read the context
116 * check_version - if read_context, whether to check the version, and issue warning message if non-existent or old
118 int nmh_init(const char *argv0
, bool read_context
, bool check_version
);
121 * Compares prior version of nmh with current version. Returns 1
122 * if they compare the be the same, 0 if not.
124 * older - 0 for difference comparison, 1 for only if older
126 int nmh_version_changed (int older
);