Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 1 | //===--- raw_ostream.cpp - Implement the raw_ostream classes --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This implements support for bulk buffered stream output. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 15 | #include <ostream> |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | |
| 18 | #if !defined(_MSC_VER) |
| 19 | #include <fcntl.h> |
| 20 | #else |
| 21 | #include <io.h> |
| 22 | #define open(x,y,z) _open(x,y) |
| 23 | #define write(fd, start, size) _write(fd, start, size) |
| 24 | #define close(fd) _close(fd) |
| 25 | #endif |
| 26 | |
| 27 | // An out of line virtual method to provide a home for the class vtable. |
| 28 | void raw_ostream::handle() {} |
| 29 | |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | // raw_fd_ostream |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | /// raw_fd_ostream - Open the specified file for writing. If an error occurs, |
| 35 | /// information about the error is put into ErrorInfo, and the stream should |
| 36 | /// be immediately destroyed. |
| 37 | raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo) { |
Chris Lattner | c52b128 | 2008-08-17 03:53:23 +0000 | [diff] [blame] | 38 | // Handle "-" as stdout. |
| 39 | if (Filename[0] == '-' && Filename[1] == 0) { |
| 40 | FD = STDOUT_FILENO; |
| 41 | ShouldClose = false; |
| 42 | return; |
| 43 | } |
| 44 | |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 45 | FD = open(Filename, O_WRONLY|O_CREAT|O_TRUNC, 0644); |
| 46 | if (FD < 0) { |
| 47 | ErrorInfo = "Error opening output file '" + std::string(Filename) + "'"; |
| 48 | ShouldClose = false; |
| 49 | } else { |
| 50 | ShouldClose = true; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | raw_fd_ostream::~raw_fd_ostream() { |
| 55 | flush(); |
| 56 | if (ShouldClose) |
| 57 | close(FD); |
| 58 | } |
| 59 | |
| 60 | void raw_fd_ostream::flush_impl() { |
| 61 | if (OutBufCur-OutBufStart) |
Chris Lattner | d497df5 | 2008-08-17 01:46:05 +0000 | [diff] [blame] | 62 | ::write(FD, OutBufStart, OutBufCur-OutBufStart); |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 63 | HandleFlush(); |
| 64 | } |
| 65 | |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 66 | //===----------------------------------------------------------------------===// |
| 67 | // raw_stdout/err_ostream |
| 68 | //===----------------------------------------------------------------------===// |
Chris Lattner | 60d3962 | 2008-08-17 01:35:29 +0000 | [diff] [blame] | 69 | |
| 70 | raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {} |
| 71 | raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false) {} |
| 72 | |
| 73 | // An out of line virtual method to provide a home for the class vtable. |
| 74 | void raw_stdout_ostream::handle() {} |
| 75 | void raw_stderr_ostream::handle() {} |
Chris Lattner | 07f51f7 | 2008-08-17 04:13:37 +0000 | [diff] [blame] | 76 | |
| 77 | /// outs() - This returns a reference to a raw_ostream for standard output. |
| 78 | /// Use it like: outs() << "foo" << "bar"; |
| 79 | raw_ostream &outs() { |
| 80 | static raw_stdout_ostream S; |
| 81 | return S; |
| 82 | } |
| 83 | |
| 84 | /// errs() - This returns a reference to a raw_ostream for standard error. |
| 85 | /// Use it like: errs() << "foo" << "bar"; |
| 86 | raw_ostream &errs() { |
| 87 | static raw_stderr_ostream S; |
| 88 | return S; |
| 89 | } |
| 90 | |
| 91 | //===----------------------------------------------------------------------===// |
| 92 | // raw_os_ostream |
| 93 | //===----------------------------------------------------------------------===// |
| 94 | |
| 95 | /// flush_impl - The is the piece of the class that is implemented by |
| 96 | /// subclasses. This outputs the currently buffered data and resets the |
| 97 | /// buffer to empty. |
| 98 | void raw_os_ostream::flush_impl() { |
| 99 | if (OutBufCur-OutBufStart) |
| 100 | OS.write(OutBufStart, OutBufCur-OutBufStart); |
| 101 | HandleFlush(); |
| 102 | } |