X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/0bb0a48241ac6130afe4cf8eb5616f2f33ee1a84..97f756c32bfb86b4949ec26857ea1b1acd74d801:/sbr/utils.c diff --git a/sbr/utils.c b/sbr/utils.c index 8281b4fc..90f36bd3 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -11,6 +11,7 @@ #include "m_mktemp.h" #include "makedir.h" #include +#include extern char *mhdocdir; @@ -572,3 +573,32 @@ scan_input (int fd, int *eightbit) { return state == NOTOK ? NOTOK : OK; } + + +/* + * Convert an int to a char string. + */ +char * +m_str(int value) { + return m_strn(value, 0); +} + + +/* + * Convert an int to a char string, of limited width if > 0. + */ +#define STR(s) #s +/* SIZE(n) includes NUL. n must just be digits, not an equation. */ +#define SIZE(n) (sizeof STR(n)) + +char * +m_strn(int value, unsigned int width) { + /* Need to include space for negative sign. But don't use INT_MIN + because it could be a macro that would fool SIZE(n). */ + static char buffer[SIZE(-INT_MAX)]; + const int num_chars = snprintf(buffer, sizeof buffer, "%d", value); + + return num_chars > 0 && (width == 0 || (unsigned int) num_chars <= width) + ? buffer + : "?"; +}