Gather up another utility.
[u/mdw/catacomb] / mptext.h
index 93cbf50..ddd6328 100644 (file)
--- a/mptext.h
+++ b/mptext.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mptext.h,v 1.3 1999/12/22 15:56:30 mdw Exp $
+ * $Id: mptext.h,v 1.7 2004/04/08 01:36:15 mdw Exp $
  *
  * Textual representation of multiprecision numbers
  *
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: mptext.h,v $
- * Revision 1.3  1999/12/22 15:56:30  mdw
- * Make the buffer passed to `put' op constant.
- *
- * Revision 1.2  1999/12/10 23:29:48  mdw
- * Change header file guard names.
- *
- * Revision 1.1  1999/11/17 18:02:16  mdw
- * New multiprecision integer arithmetic suite.
- *
- */
-
 #ifndef CATACOMB_MPTEXT_H
 #define CATACOMB_MPTEXT_H
 
@@ -100,6 +86,20 @@ extern mp *mp_read(mp */*m*/, int /*radix*/,
 extern int mp_write(mp */*m*/, int /*radix*/,
                    const mptext_ops */*ops*/, void */*p*/);
 
+/* --- @mptext_len@ --- *
+ *
+ * Arguments:  @mp *x@ = number to work on
+ *             @int r@ = radix the number will be expressed in
+ *
+ * Returns:    The number of digits needed to represent the number in the
+ *             given base.  This will not include space for a leading sign
+ *             (use @MP_ISNEG@ to check that, or just add one on for luck);
+ *             neither will it add space for a terminating null.  In general
+ *             the answer will be an overestimate.
+ */
+
+extern size_t mptext_len(mp */*x*/, int /*r*/);
+
 /*----- File I/O ----------------------------------------------------------*/
 
 #include <stdio.h>
@@ -116,6 +116,26 @@ extern const mptext_ops mptext_fileops;
 extern mp *mp_readfile(mp */*m*/, FILE */*fp*/, int /*radix*/);
 extern int mp_writefile(mp */*m*/, FILE */*fp*/, int /*radix*/);
 
+#define MP_DOFPRINTFR(fp, args, m, r) do {                             \
+  fprintf args;                                                                \
+  if (m)                                                               \
+    mp_writefile(m, fp, r);                                            \
+  else                                                                 \
+    fputs("<null>", fp);                                               \
+  fputc('\n', fp);                                                     \
+} while (0)
+
+#define MP_DOFPRINTR(fp, name, m, r)                                   \
+  MP_DOFPRINTFR(fp, (fp, "%s = ", name), m, r)
+
+#define MP_PRINT(name, m) MP_DOFPRINTR(stdout, name, m, 10)
+#define MP_EPRINT(name, m) MP_DOFPRINTR(stderr, name, m, 10)
+#define MP_PRINTX(name, m) MP_DOFPRINTR(stdout, name, m, 16)
+#define MP_EPRINTX(name, m) MP_DOFPRINTR(stderr, name, m, 16)
+
+#define MP_FPRINTF(fp, args, m) MP_DOFPRINTFR(fp, args, m, 10)
+#define MP_FPRINTFX(fp, args, m) MP_DOFPRINTFR(fp, args, m, 16)
+
 /*----- String I/O --------------------------------------------------------*/
 
 /* --- Context format --- */