remove .la files
[platform/upstream/gmp.git] / printf / doprnti.c
1 /* __gmp_doprnt_integer -- integer style formatted output.
2
3    THE FUNCTIONS IN THIS FILE ARE FOR INTERNAL USE ONLY.  THEY'RE ALMOST
4    CERTAIN TO BE SUBJECT TO INCOMPATIBLE CHANGES OR DISAPPEAR COMPLETELY IN
5    FUTURE GNU MP RELEASES.
6
7 Copyright 2001 Free Software Foundation, Inc.
8
9 This file is part of the GNU MP Library.
10
11 The GNU MP Library is free software; you can redistribute it and/or modify
12 it under the terms of either:
13
14   * the GNU Lesser General Public License as published by the Free
15     Software Foundation; either version 3 of the License, or (at your
16     option) any later version.
17
18 or
19
20   * the GNU General Public License as published by the Free Software
21     Foundation; either version 2 of the License, or (at your option) any
22     later version.
23
24 or both in parallel, as here.
25
26 The GNU MP Library is distributed in the hope that it will be useful, but
27 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
28 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
29 for more details.
30
31 You should have received copies of the GNU General Public License and the
32 GNU Lesser General Public License along with the GNU MP Library.  If not,
33 see https://www.gnu.org/licenses/.  */
34
35 #include <stdarg.h>    /* for va_list and hence doprnt_funs_t */
36 #include <string.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39
40 #include "gmp.h"
41 #include "gmp-impl.h"
42
43
44 int
45 __gmp_doprnt_integer (const struct doprnt_funs_t *funs,
46                       void *data,
47                       const struct doprnt_params_t *p,
48                       const char *s)
49 {
50   int         retval = 0;
51   int         slen, justlen, showbaselen, sign, signlen, slashlen, zeros;
52   int         justify, den_showbaselen;
53   const char  *slash, *showbase;
54
55   /* '+' or ' ' if wanted, and don't already have '-' */
56   sign = p->sign;
57   if (s[0] == '-')
58     {
59       sign = s[0];
60       s++;
61     }
62   signlen = (sign != '\0');
63
64   /* if the precision was explicitly 0, print nothing for a 0 value */
65   if (*s == '0' && p->prec == 0)
66     s++;
67
68   slen = strlen (s);
69   slash = strchr (s, '/');
70
71   showbase = NULL;
72   showbaselen = 0;
73
74   if (p->showbase != DOPRNT_SHOWBASE_NO)
75     {
76       switch (p->base) {
77       case 16:  showbase = "0x"; showbaselen = 2; break;
78       case -16: showbase = "0X"; showbaselen = 2; break;
79       case 8:   showbase = "0";  showbaselen = 1; break;
80       }
81     }
82
83   den_showbaselen = showbaselen;
84   if (slash == NULL
85       || (p->showbase == DOPRNT_SHOWBASE_NONZERO && slash[1] == '0'))
86     den_showbaselen = 0;
87
88   if (p->showbase == DOPRNT_SHOWBASE_NONZERO && s[0] == '0')
89     showbaselen = 0;
90
91   /* the influence of p->prec on mpq is currently undefined */
92   zeros = MAX (0, p->prec - slen);
93
94   /* space left over after actual output length */
95   justlen = p->width
96     - (strlen(s) + signlen + showbaselen + den_showbaselen + zeros);
97
98   justify = p->justify;
99   if (justlen <= 0) /* no justifying if exceed width */
100     justify = DOPRNT_JUSTIFY_NONE;
101
102   if (justify == DOPRNT_JUSTIFY_RIGHT)             /* pad right */
103     DOPRNT_REPS (p->fill, justlen);
104
105   DOPRNT_REPS_MAYBE (sign, signlen);               /* sign */
106
107   DOPRNT_MEMORY_MAYBE (showbase, showbaselen);     /* base */
108
109   DOPRNT_REPS_MAYBE ('0', zeros);                  /* zeros */
110
111   if (justify == DOPRNT_JUSTIFY_INTERNAL)          /* pad internal */
112     DOPRNT_REPS (p->fill, justlen);
113
114   /* if there's a showbase on the denominator, then print the numerator
115      separately so it can be inserted */
116   if (den_showbaselen != 0)
117     {
118       ASSERT (slash != NULL);
119       slashlen = slash+1 - s;
120       DOPRNT_MEMORY (s, slashlen);                 /* numerator and slash */
121       slen -= slashlen;
122       s += slashlen;
123       DOPRNT_MEMORY (showbase, den_showbaselen);
124     }
125
126   DOPRNT_MEMORY (s, slen);                         /* number, or denominator */
127
128   if (justify == DOPRNT_JUSTIFY_LEFT)              /* pad left */
129     DOPRNT_REPS (p->fill, justlen);
130
131  done:
132   return retval;
133
134  error:
135   retval = -1;
136   goto done;
137 }