1 // Copyright 2011 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/stream.hpp"
36 #include "utils/format/macros.hpp"
37 #include "utils/fs/path.hpp"
38 #include "utils/sanity.hpp"
40 namespace fs = utils::fs;
46 /// Constant that represents the path to stdout.
47 static const fs::path stdout_path("/dev/stdout");
50 /// Constant that represents the path to stderr.
51 static const fs::path stderr_path("/dev/stderr");
54 } // anonymous namespace
57 /// Opens a new file for output, respecting the stdout and stderr streams.
59 /// \param path The path to the output file to be created.
61 /// \return A pointer to a new output stream.
62 std::auto_ptr< std::ostream >
63 utils::open_ostream(const fs::path& path)
65 std::auto_ptr< std::ostream > out;
66 if (path == stdout_path) {
67 out.reset(new std::ofstream());
68 out->copyfmt(std::cout);
69 out->clear(std::cout.rdstate());
70 out->rdbuf(std::cout.rdbuf());
71 } else if (path == stderr_path) {
72 out.reset(new std::ofstream());
73 out->copyfmt(std::cerr);
74 out->clear(std::cerr.rdstate());
75 out->rdbuf(std::cerr.rdbuf());
77 out.reset(new std::ofstream(path.c_str()));
79 throw std::runtime_error(F("Cannot open output file %s") % path);
82 INV(out.get() != NULL);
87 /// Gets the length of a stream.
89 /// \param is The input stream for which to calculate its length.
91 /// \return The length of the stream. This is of size_t type instead of
92 /// directly std::streampos to simplify the caller. Some systems do not
93 /// support comparing a std::streampos directly to an integer (see
94 /// NetBSD 1.5.x), which is what we often want to do.
96 /// \throw std::exception If calculating the length fails due to a stream error.
98 utils::stream_length(std::istream& is)
100 const std::streampos current_pos = is.tellg();
102 is.seekg(0, std::ios::end);
103 const std::streampos length = is.tellg();
104 is.seekg(current_pos, std::ios::beg);
105 return static_cast< std::size_t >(length);
107 is.seekg(current_pos, std::ios::beg);
113 /// Reads a whole file into memory.
115 /// \param path The file to read.
117 /// \return A plain string containing the raw contents of the file.
119 /// \throw std::runtime_error If the file cannot be opened.
121 utils::read_file(const fs::path& path)
123 std::ifstream input(path.c_str());
125 throw std::runtime_error(F("Failed to open '%s' for read") % path);
126 return read_stream(input);
130 /// Reads the whole contents of a stream into memory.
132 /// \param input The input stream from which to read.
134 /// \return A plain string containing the raw contents of the file.
136 utils::read_stream(std::istream& input)
138 std::ostringstream buffer;
141 while (input.good()) {
142 input.read(tmp, sizeof(tmp));
143 if (input.good() || input.eof()) {
144 buffer.write(tmp, input.gcount());