]> diplodocus.org Git - nmh/blobdiff - sbr/utils.c
NEWS: Move "new features" that might bite a user to the top.
[nmh] / sbr / utils.c
index 8281b4fc44fc172fb0de52de84858966f6077441..90f36bd340ef8cfd9eab9aed71f71a0b68e2f6d1 100644 (file)
@@ -11,6 +11,7 @@
 #include "m_mktemp.h"
 #include "makedir.h"
 #include <fcntl.h>
+#include <limits.h>
 
 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
+        : "?";
+}