Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
LLONG_PRINTF_PREFIX.cpp
Go to the documentation of this file.
1// checking for long long printf format prefix
2
3
25#include <stdio.h>
26
27
28#if !defined (_MSC_VER)
29# define long_long long long
30#else
31# define long_long __int64
32#endif // _MSC_VER
33
34// computes the largest positive long long that's a power of 2
36{
37 long_long ll = 1;
38 while (ll * 2 > ll)
39 ll *= 2;
40 return ll;
41}
42
43
44// checks to see if sprintf/sscanf works with the given format
45int chkfmt (const char *fmt)
46{
47 char buf [80] = "";
48
49 long_long vals [] = {
50 0, // placeholder
51 -2, -1, 0, 1,
52 0 // placeholder
53 };
54
55 vals [0] = -get_big_num ();
56 vals [sizeof vals / sizeof *vals - 1] = get_big_num ();
57
58 for (unsigned i = 0; i != sizeof vals / sizeof *vals; ++i) {
59 long_long ll = 0;
60
61 if (0 > sprintf (buf, fmt, vals [i]))
62 return -1;
63 if (0 > sscanf (buf, fmt, &ll))
64 return -1;
65 if (ll != vals [i])
66 return -1;
67 }
68 return 0;
69}
70
71
72// avoid having to #include <string>
73char* append (char *dst, const char *src)
74{
75 for (; *dst; ++dst);
76 for (; (*dst = *src); ++dst, ++src);
77 return dst;
78}
79
80int main ()
81{
82 const char* pfx[] = {
83 "ll", // 7.19.6.2, p11 of C99, HP-UX 11, SunOS 5.7, ...
84 "L", // Linux
85 "LL",
86 "q", // BSD 4.4
87 "l",
88 "I64" // Win32/64
89 };
90
91 for (unsigned i = 0; i != sizeof pfx / sizeof *pfx; ++i) {
92 char fmt[8] = "%";
93 append (append (fmt, pfx [i]), "d");
94
95 if (0 == chkfmt (fmt)) {
96 printf ("#define _RWSTD_LLONG_PRINTF_PREFIX \"%s\"\n", pfx [i]);
97 return 0;
98 }
99 }
100
101 return 1;
102}
#define i
Definition 17.names.cpp:126
#define buf
Definition 17.names.cpp:452
IMPORT_C SInt32 printf(const Char *,...)
char * append(char *dst, const char *src)
#define long_long
int chkfmt(const char *fmt)
long_long get_big_num()
int main()