]> diplodocus.org Git - nmh/commitdiff
Added m_str() and m_strn() functions to convert int to string.
authorDavid Levine <levinedl@acm.org>
Sat, 29 Jul 2017 14:02:28 +0000 (10:02 -0400)
committerDavid Levine <levinedl@acm.org>
Sat, 29 Jul 2017 14:02:28 +0000 (10:02 -0400)
Allows better fix to uip/forw.c than commit d711510305.

h/utils.h
sbr/m_name.c
sbr/utils.c
uip/forw.c

index 0c6c7347024f4d432ed12db363754b396e85aed0..944a3ccbc3319bbb56e3329e08326088ce6a978c 100644 (file)
--- a/h/utils.h
+++ b/h/utils.h
@@ -106,6 +106,19 @@ int contains8bit(const char *start, const char *end);
  */
 int scan_input (int fd, int *eightbit);
 
  */
 int scan_input (int fd, int *eightbit);
 
+/*
+ * Returns string representation of int, in static memory.
+ */
+char *m_str(int value);
+
+/*
+ * Returns string representation of an int, in static memory.  If width
+ * == 0, does not limit the width.  If width > 0 and value will not fit
+ * in field of that size, including any negative sign but excluding
+ * terminating null, then returns "?".  If width < 0, returns "?".
+ */
+char *m_strn(int value, unsigned int width);
+
 /*
  * program initialization
  *
 /*
  * program initialization
  *
index a4b49af8918d7c9eda60b368c9165819b483d4a1..9bc8c0b5e0c0e11e81399a89c1cf45f1dd4dacfa 100644 (file)
@@ -7,6 +7,7 @@
 
 #include <limits.h>
 #include <h/mh.h>
 
 #include <limits.h>
 #include <h/mh.h>
+#include <h/utils.h>
 
 #define STR(s) #s
 #define SIZE(n) (sizeof STR(n)) /* Includes NUL. */
 
 #define STR(s) #s
 #define SIZE(n) (sizeof STR(n)) /* Includes NUL. */
 char *
 m_name (int num)
 {
 char *
 m_name (int num)
 {
-    static char name[SIZE(INT_MAX)];
+    if (num <= 0) return "?";
 
 
-    if (num <= 0)
-       return "?";
-
-    snprintf(name, sizeof name, "%d", num);
-
-    return name;
+    return m_strn(num, SIZE(INT_MAX));
 }
 }
index 8281b4fc44fc172fb0de52de84858966f6077441..ef5116e82d905fde6589919a670c6a1a9f5b348f 100644 (file)
@@ -11,6 +11,7 @@
 #include "m_mktemp.h"
 #include "makedir.h"
 #include <fcntl.h>
 #include "m_mktemp.h"
 #include "makedir.h"
 #include <fcntl.h>
+#include <limits.h>
 
 extern char *mhdocdir;
 
 
 extern char *mhdocdir;
 
@@ -572,3 +573,43 @@ scan_input (int fd, int *eightbit) {
 
     return state == NOTOK  ?  NOTOK  :  OK;
 }
 
     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
+#define SIZE(n) (sizeof STR(n)) /* Includes NUL. */
+
+char *
+m_strn(int value, unsigned int width) {
+    /* +1 to allow negative sign. */
+    static char buffer[SIZE(INT_MAX) + 1];
+
+    if (width == 0) {
+        snprintf(buffer, sizeof buffer, "%d", value);
+    } else {
+        int max_val = 1;
+        unsigned int i;
+        for (i = 0; i < (value >= 0 ? width : width-1); ++i) {
+            max_val *= 10;
+        }
+
+        if (abs(value) <= max_val  &&  width > 0) {
+            snprintf(buffer, sizeof buffer, "%d", value);
+        } else {
+            snprintf(buffer, sizeof buffer, "%c", '?');
+        }
+    }
+
+    return buffer;
+}
index 331be16bb9b2e0ac5324057e54964d142876305e..cb0255418a2a7841182d8313ab55f4bd9f64d50a 100644 (file)
@@ -454,9 +454,9 @@ try_it_again:
 
        if (digest) {
            snprintf (buf, sizeof(buf), IFORMAT, digest);
 
        if (digest) {
            snprintf (buf, sizeof(buf), IFORMAT, digest);
-           context_replace (buf, mh_xstrdup(m_name(issue)));
+           context_replace (buf, mh_xstrdup(m_str(issue)));
            snprintf (buf, sizeof(buf), VFORMAT, digest);
            snprintf (buf, sizeof(buf), VFORMAT, digest);
-           context_replace (buf, mh_xstrdup(m_name(volume)));
+           context_replace (buf, mh_xstrdup(m_str(volume)));
        }
 
        context_replace (pfolder, folder);      /* update current folder   */
        }
 
        context_replace (pfolder, folder);      /* update current folder   */