monotone

monotone Mtn Source Tree

Root/m4/syncwithstdio.m4

1# Check whether sync_with_stdio(false) run on C++ streams works correctly.
2# It causes strange problems (EOF returned too early) on some versions of
3# MinGW and OS X. The first test is for the MinGW issue, the second for
4# the OS X issue.
5#
6# In order to check for this issue, we first generate a large input file
7# (over 128KB) and then try to read it (using a buffer size over 512
8# bytes). If the amount of bytes read does not match the generated input,
9# the implementation is broken.
10AC_DEFUN([AC_CXX_SYNC_WITH_STDIO_WORKS],
11[AC_CACHE_CHECK([whether C++ streams support sync_with_stdio],
12ac_cv_cxx_sync_with_stdio_works, [
13 if test x$cross_compiling = xyes; then
14 AC_MSG_FAILURE([cannot run test program while cross compiling])
15 fi
16 # Note that we can't use AC_RUN_IFELSE for these because we have to
17 # redirect standard input. Instead, we use AC_LINK_IFELSE to
18 # generate the binaries and then run them by hand. Hence the manual
19 # failure-if-cross-compiling above.
20 AC_RUN_IFELSE([AC_LANG_SOURCE([
21 /*
22 * Generates the input file.
23 */
24 #include <cstdlib>
25 #include <fstream>
26
27 int
28 main(void)
29 {
30 std::ofstream ofs("_conftest.dat");
31 for (int i = 0; i < 50000; i++)
32 ofs << "0123456789\r\n";
33 ofs.close();
34
35 return EXIT_SUCCESS;
36 }
37 ])], [], [AC_MSG_FAILURE([could not generate input for test 1])])
38
39 AC_LINK_IFELSE([AC_LANG_SOURCE([[
40 /*
41 * Reads the input file.
42 */
43 #include <cstdlib>
44 #include <fstream>
45 #include <iostream>
46
47 int
48 main(void)
49 {
50 std::cin.sync_with_stdio(false);
51 int length = 0;
52 while (std::cin.good())
53 {
54 char buffer[1024];
55 (void)std::cin.read(buffer, sizeof(buffer));
56 length += std::cin.gcount();
57 }
58
59 return length == 600000 ? EXIT_SUCCESS : EXIT_FAILURE;
60 }
61 ]])],
62 [if ./conftest$ac_exeext <_conftest.dat; then
63 :
64 else
65 ac_cv_sync_with_stdio_works=no
66 fi],
67 [AC_MSG_FAILURE([could not link test program for test 1])])
68
69 rm -f _conftest.dat
70 if test "x$ac_cv_sync_with_stdio_works" != xno; then
71 # To check for issue 2, we have a writer with a short delay between
72 # writes, and a reader who has set sync_with_stdio(false).
73 AC_LINK_IFELSE([AC_LANG_SOURCE([[
74 #include <cstdlib>
75 #include <iostream>
76 #ifdef WIN32
77 #define WIN32_LEAN_AND_MEAN
78 #include <windows.h>
79 #define sleep(x) Sleep((x) * 1000)
80 #else
81 #include <unistd.h>
82 #endif
83
84 int
85 main(int argc, char * argv[])
86 {
87 if (argc != 2)
88 return EXIT_FAILURE;
89
90 if (argv[1][0] == 'R')
91 {
92 std::cin.sync_with_stdio(false);
93 int length = 0;
94 while (std::cin.good())
95 {
96 char buffer@<:@1024@:>@;
97 (void)std::cin.read(buffer, sizeof(buffer));
98 length += std::cin.gcount();
99 }
100
101 return length == 82 ? EXIT_SUCCESS : EXIT_FAILURE;
102 }
103 else
104 {
105 std::cout << "1234567890123456789012345678901234567890" << std::endl;
106 sleep(1);
107 std::cout << "1234567890123456789012345678901234567890" << std::endl;
108 }
109 return EXIT_SUCCESS;
110 }
111 ]])],
112 [if ./conftest$ac_exeext W | ./conftest$ac_exeext R; then
113 ac_cv_cxx_sync_with_stdio_works=yes
114 else
115 ac_cv_cxx_sync_with_stdio_works=no
116 fi],
117 [AC_MSG_FAILURE([could not link test program for test 2])])
118 fi # skip test 2
119 ])
120 if test $ac_cv_cxx_sync_with_stdio_works = yes; then
121 AC_DEFINE(SYNC_WITH_STDIO_WORKS, 1,
122 [Define to 1 if ios::sync_with_stdio(false) works])
123 fi
124])

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status