]>
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
);
16 /* Call realloc(3), exiting on NULL return. */
17 void *mh_xrealloc(void *ptr
, size_t size
);
19 /* Call calloc(3), exiting on NULL return. */
20 void *mh_xcalloc(size_t nelem
, size_t elsize
);
22 /* Duplicate a NUL-terminated string, exit on failure. */
23 char *mh_xstrdup(const char *src
);
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)))
35 char *add(const char *, char *);
36 char *addlist(char *, const char *);
37 int folder_exists(const char *);
38 void create_folder(char *, int, void (*)(int));
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 *);
69 char *rfind_str (const char [], size_t, const char *);
70 char *nmh_strcasestr (const char *, const char *);
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
);
77 bool has_suffix(const char *s
, const char *suffix
);
78 bool has_suffix_c(const char *s
, int c
);
79 void trim_suffix_c(char *s
, int c
);
80 void to_lower(char *s
);
81 void to_upper(char *s
);
84 * See if a string contains 8 bit characters (use isascii() for the test).
87 * start - Pointer to start of string to test.
88 * end - End of string to test (test will stop before reaching
89 * this point). If NULL, continue until reaching '\0'.
91 * This function always stops at '\0' regardless of the value of 'end'.
92 * Returns 1 if the string contains an 8-bit character, 0 if it does not.
94 int contains8bit(const char *start
, const char *end
);
97 * See if file has any 8-bit bytes.
100 * fd - file descriptor
101 * eightbit - address of result, will be set to 1 if the file contains
102 * any 8-bit bytes, 0 otherwise.
104 * Returns OK on success, NOTOK on read failure.
107 int scan_input (int fd
, int *eightbit
);
110 * program initialization
112 * argv0 - argv[0], presumably the program name
113 * read_context - 0: don't read context
114 * - 1: read context, check nmh version, and issue warning message
115 * if non-existent or old
116 * - 2: read context, don't check nmh version
118 int nmh_init(const char *argv0
, int read_context
);
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
);