blob: fd875dbb2a844a3618fb0a096e6dc4723af20fb7 [file] [log] [blame]
Austin Schuhbb1338c2024-06-15 19:31:16 -07001/* operator<< -- mpf formatted output to an ostream.
2
3Copyright 2001, 2002 Free Software Foundation, Inc.
4
5This file is part of the GNU MP Library.
6
7The GNU MP Library is free software; you can redistribute it and/or modify
8it under the terms of either:
9
10 * the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13
14or
15
16 * the GNU General Public License as published by the Free Software
17 Foundation; either version 2 of the License, or (at your option) any
18 later version.
19
20or both in parallel, as here.
21
22The GNU MP Library is distributed in the hope that it will be useful, but
23WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25for more details.
26
27You should have received copies of the GNU General Public License and the
28GNU Lesser General Public License along with the GNU MP Library. If not,
29see https://www.gnu.org/licenses/. */
30
31#include <clocale>
32#include <iostream>
33#include <stdarg.h> /* for va_list and hence doprnt_funs_t */
34#include <string.h>
35
36#include "gmp-impl.h"
37
38using namespace std;
39
40
41/* The gmp_asprintf support routines never give an error, so
42 __gmp_doprnt_mpf shouldn't fail and it's return can just be checked with
43 an ASSERT. */
44
45ostream&
46operator<< (ostream &o, mpf_srcptr f)
47{
48 struct doprnt_params_t param;
49 struct gmp_asprintf_t d;
50 char *result;
51 int ret;
52
53 __gmp_doprnt_params_from_ios (&param, o);
54
55#if HAVE_STD__LOCALE
56 char point[2];
57 point[0] = use_facet< numpunct<char> >(o.getloc()).decimal_point();
58 point[1] = '\0';
59#else
60 const char *point = GMP_DECIMAL_POINT;
61#endif
62
63 GMP_ASPRINTF_T_INIT (d, &result);
64 ret = __gmp_doprnt_mpf (&__gmp_asprintf_funs_noformat, &d, &param, point, f);
65 ASSERT (ret != -1);
66 __gmp_asprintf_final (&d);
67
68 gmp_allocated_string t (result);
69 return o.write (t.str, t.len);
70}