add support for a cout/cerr analog (outs()/errs()) as well as
[oota-llvm.git] / lib / Support / raw_ostream.cpp
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"
15 #include <ostream>
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) {
38   // Handle "-" as stdout.
39   if (Filename[0] == '-' && Filename[1] == 0) {
40     FD = STDOUT_FILENO;
41     ShouldClose = false;
42     return;
43   }
44   
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)
62     ::write(FD, OutBufStart, OutBufCur-OutBufStart);
63   HandleFlush();
64 }
65
66 //===----------------------------------------------------------------------===//
67 //  raw_stdout/err_ostream
68 //===----------------------------------------------------------------------===//
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() {}
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 }