Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
LDBL_PRINTF_PREFIX.cpp
Go to the documentation of this file.
1// checking for long double printf format prefix
2
3
25#include <stdio.h>
26
27#include "config.h"
28#include "float_defs.h" // for LDBL_XXX constants
29
30
31// avoid having to #include <string.h>
32int compare (const char *s1, const char *s2)
33{
34 for ( ; *s1 && *s1 == *s2; ++s1, ++s2);
35 return *s1 - *s2;
36}
37
38
39// checks to see if sprintf/sscanf works with the given format
40int chkfmt (const char *fmt)
41{
42 char outbuf [4096];
43 outbuf [0] = '\0';
44
45 long double vals [] = {
46 // avoid using edge values to prevent rounding errors
47 LDBL_MIN * 10, DBL_MIN * 10, FLT_MIN * 10, -2, -1, -0.1, -0.001,
48 0.001, 0.1, 1, FLT_MAX / 10, DBL_MAX / 10, LDBL_MAX / 10
49 };
50
51 for (unsigned i = 0; i != sizeof vals / sizeof *vals; ++i) {
52 long double ld = 0.0;
53 char inbuf [sizeof outbuf] = "";
54
55 // format into a char buffer
56 if (0 > sprintf (outbuf, fmt, vals [i]))
57 return -1;
58
59 // parse buffer
60 if (0 > sscanf (outbuf, fmt, &ld))
61 return -1;
62
63 // format parsed number into another buffer
64 if (0 > sprintf (inbuf, fmt, ld))
65 return -1;
66
67 // compare buffers, expect identical contents
68 if (compare (inbuf, outbuf))
69 return -1;
70 }
71 return 0;
72}
73
74
75// avoid having to #include <string.h>
76char* append (char *dst, const char *src)
77{
78 for (; *dst; ++dst);
79 for (; (*dst = *src); ++dst, ++src);
80 return dst;
81}
82
83int main ()
84{
85 const char* pfx[] = {
86 "L", // C89, C99
87 "l", // on systems where sizeof (long double) == sizeof (double)
88 "" // on systems where sizeof (long double) == sizeof (float)
89 };
90
91 for (unsigned i = 0; i != sizeof pfx / sizeof *pfx; ++i) {
92 char fmt[8] = "%";
93 append (append (fmt, pfx [i]), "e");
94
95 if (0 == chkfmt (fmt)) {
96 printf ("#define _RWSTD_LDBL_PRINTF_PREFIX \"%s\"\n", pfx [i]);
97 return 0;
98 }
99 }
100
101 return 1; // fail
102}
#define i
Definition 17.names.cpp:126
#define s1
Definition 17.names.cpp:242
#define s2
Definition 17.names.cpp:268
IMPORT_C SInt32 printf(const Char *,...)
char * append(char *dst, const char *src)
int chkfmt(const char *fmt)
int main()
#define LDBL_MIN
Definition _cfloat.h:67
#define FLT_MAX
Definition _cfloat.h:46
#define FLT_MIN
Definition _cfloat.h:47
#define DBL_MIN
Definition _cfloat.h:57
#define LDBL_MAX
Definition _cfloat.h:66
#define DBL_MAX
Definition _cfloat.h:56