*/
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
*
#include <limits.h>
#include <h/mh.h>
+#include <h/utils.h>
#define STR(s) #s
#define SIZE(n) (sizeof STR(n)) /* Includes NUL. */
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));
}
#include "m_mktemp.h"
#include "makedir.h"
#include <fcntl.h>
+#include <limits.h>
extern char *mhdocdir;
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;
+}
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);
- context_replace (buf, mh_xstrdup(m_name(volume)));
+ context_replace (buf, mh_xstrdup(m_str(volume)));
}
context_replace (pfolder, folder); /* update current folder */