1 | // (C) Copyright Jonathan Turkanis 2004 |
---|
2 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
---|
3 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.) |
---|
4 | |
---|
5 | // See http://www.boost.org/libs/iostreams for documentation. |
---|
6 | |
---|
7 | #ifndef BOOST_IOSTREAMS_TEST_FILES_HPP_INCLUDED |
---|
8 | #define BOOST_IOSTREAMS_TEST_FILES_HPP_INCLUDED |
---|
9 | |
---|
10 | #include <cctype> // toupper, tolower |
---|
11 | #include <cstdio> // tmpname, TMP_MAX, remove |
---|
12 | #include <cstdlib> // rand, toupper, tolower (VC6) |
---|
13 | #include <fstream> |
---|
14 | #include <string> |
---|
15 | #if defined(__CYGWIN__) |
---|
16 | # include <boost/random/linear_congruential.hpp> |
---|
17 | # include <boost/random/uniform_smallint.hpp> |
---|
18 | #endif |
---|
19 | #include "./constants.hpp" |
---|
20 | |
---|
21 | #ifdef BOOST_NO_STDC_NAMESPACE |
---|
22 | # undef toupper |
---|
23 | # undef tolower |
---|
24 | # undef remove |
---|
25 | # undef rand |
---|
26 | namespace std { |
---|
27 | using ::toupper; using ::tolower; using ::remove; using ::rand; |
---|
28 | } |
---|
29 | #endif |
---|
30 | |
---|
31 | namespace boost { namespace iostreams { namespace test { |
---|
32 | |
---|
33 | // Represents a temp file, deleted upon destruction. |
---|
34 | class temp_file { |
---|
35 | public: |
---|
36 | |
---|
37 | // Constructs a temp file which does not initially exist. |
---|
38 | temp_file() { set_name(); } |
---|
39 | ~temp_file() { std::remove(name_.c_str()); } |
---|
40 | const ::std::string name() const { return name_; } |
---|
41 | operator const ::std::string() const { return name_; } |
---|
42 | private: |
---|
43 | void set_name() { |
---|
44 | // Windows CreateFileMapping API function doesn't accept some |
---|
45 | // names generated by std::tmpnam. |
---|
46 | #if defined(_WIN32) || defined(__WIN32__) || \ |
---|
47 | defined(WIN32) || defined(__CYGWIN__) \ |
---|
48 | /**/ |
---|
49 | for (int z = 0; z < 5; ++z) |
---|
50 | name_ += static_cast<char>('0' + rand()); |
---|
51 | #else |
---|
52 | using namespace std; |
---|
53 | char tmp[L_tmpnam]; name_ = tmpnam(tmp); |
---|
54 | #endif |
---|
55 | } |
---|
56 | #if defined(__CYGWIN__) |
---|
57 | int rand() |
---|
58 | { |
---|
59 | static rand48 random_gen; |
---|
60 | static uniform_smallint<int> random_dist(0, 9); |
---|
61 | return random_dist(random_gen); |
---|
62 | } |
---|
63 | #else |
---|
64 | # if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) |
---|
65 | int rand() |
---|
66 | { |
---|
67 | return (std::rand() * 10) / RAND_MAX; |
---|
68 | } |
---|
69 | # endif |
---|
70 | #endif |
---|
71 | ::std::string name_; |
---|
72 | }; |
---|
73 | |
---|
74 | struct test_file : public temp_file { |
---|
75 | test_file() |
---|
76 | { |
---|
77 | BOOST_IOS::openmode mode = |
---|
78 | BOOST_IOS::out | BOOST_IOS::binary; |
---|
79 | ::std::ofstream f(name().c_str(), mode); |
---|
80 | const ::std::string n(name()); |
---|
81 | const char* buf = narrow_data(); |
---|
82 | for (int z = 0; z < data_reps; ++z) |
---|
83 | f.write(buf, data_length()); |
---|
84 | } |
---|
85 | }; |
---|
86 | |
---|
87 | |
---|
88 | struct uppercase_file : public temp_file { |
---|
89 | uppercase_file() |
---|
90 | { |
---|
91 | BOOST_IOS::openmode mode = |
---|
92 | BOOST_IOS::out | BOOST_IOS::binary; |
---|
93 | ::std::ofstream f(name().c_str(), mode); |
---|
94 | const char* buf = narrow_data(); |
---|
95 | for (int z = 0; z < data_reps; ++z) |
---|
96 | for (int w = 0; w < data_length(); ++w) |
---|
97 | f.put((char) std::toupper(buf[w])); |
---|
98 | } |
---|
99 | }; |
---|
100 | |
---|
101 | struct lowercase_file : public temp_file { |
---|
102 | lowercase_file() |
---|
103 | { |
---|
104 | BOOST_IOS::openmode mode = |
---|
105 | BOOST_IOS::out | BOOST_IOS::binary; |
---|
106 | ::std::ofstream f(name().c_str(), mode); |
---|
107 | const char* buf = narrow_data(); |
---|
108 | for (int z = 0; z < data_reps; ++z) |
---|
109 | for (int w = 0; w < data_length(); ++w) |
---|
110 | f.put((char) std::tolower(buf[w])); |
---|
111 | } |
---|
112 | }; |
---|
113 | |
---|
114 | //----------------------------------------------------------------------------// |
---|
115 | |
---|
116 | } } } // End namespaces test, iostreams, boost. |
---|
117 | |
---|
118 | #endif // #ifndef BOOST_IOSTREAMS_TEST_FILES_HPP_INCLUDED |
---|