• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2 // (C) Copyright 2004-2007 Jonathan Turkanis
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
5 
6 // See http://www.boost.org/libs/iostreams for documentation.
7 
8 #ifndef BOOST_IOSTREAMS_TEST_FILES_HPP_INCLUDED
9 #define BOOST_IOSTREAMS_TEST_FILES_HPP_INCLUDED
10 
11 #include <cctype>                             // toupper, tolower
12 #include <cstdio>                             // tmpname, TMP_MAX, remove
13 #include <cstdlib>                            // rand, toupper, tolower (VC6)
14 #include <fstream>
15 #include <string>
16 #include <boost/filesystem/operations.hpp>
17 #include "./constants.hpp"
18 
19 #ifdef BOOST_NO_STDC_NAMESPACE
20 # undef toupper
21 # undef tolower
22 # undef remove
23 # undef rand
24 namespace std {
25     using ::toupper; using ::tolower; using ::remove; using ::rand;
26 }
27 #endif
28 
29 namespace boost { namespace iostreams { namespace test {
30 
31 // Represents a temp file, deleted upon destruction.
32 class temp_file {
33 public:
34 
35     // Constructs a temp file which does not initially exist.
temp_file()36     temp_file() { set_name(); }
~temp_file()37     ~temp_file() { (void)std::remove(name_.c_str()); }
name() const38     const ::std::string name() const { return name_; }
operator const::std::string() const39     operator const ::std::string() const { return name_; }
40 private:
set_name()41     void set_name() {
42         name_ = boost::filesystem::unique_path().string();
43     }
44 
45     ::std::string name_;
46 };
47 
48 struct test_file : public temp_file {
test_fileboost::iostreams::test::test_file49     test_file()
50         {
51             BOOST_IOS::openmode mode =
52                 BOOST_IOS::out | BOOST_IOS::binary;
53             ::std::ofstream f(name().c_str(), mode);
54             const ::std::string n(name());
55             const char* buf = narrow_data();
56             for (int z = 0; z < data_reps; ++z)
57                 f.write(buf, data_length());
58         }
59 };
60 
61 
62 struct uppercase_file : public temp_file {
uppercase_fileboost::iostreams::test::uppercase_file63     uppercase_file()
64         {
65             BOOST_IOS::openmode mode =
66                 BOOST_IOS::out | BOOST_IOS::binary;
67             ::std::ofstream f(name().c_str(), mode);
68             const char* buf = narrow_data();
69             for (int z = 0; z < data_reps; ++z)
70                 for (int w = 0; w < data_length(); ++w)
71                     f.put((char) std::toupper(buf[w]));
72         }
73 };
74 
75 struct lowercase_file : public temp_file {
lowercase_fileboost::iostreams::test::lowercase_file76     lowercase_file()
77         {
78             BOOST_IOS::openmode mode =
79                 BOOST_IOS::out | BOOST_IOS::binary;
80             ::std::ofstream f(name().c_str(), mode);
81             const char* buf = narrow_data();
82             for (int z = 0; z < data_reps; ++z)
83                 for (int w = 0; w < data_length(); ++w)
84                     f.put((char) std::tolower(buf[w]));
85         }
86 };
87 
88 //----------------------------------------------------------------------------//
89 
90 } } } // End namespaces test, iostreams, boost.
91 
92 #endif // #ifndef BOOST_IOSTREAMS_TEST_FILES_HPP_INCLUDED
93