1 //===--- raw_ostream.cpp - Implement the raw_ostream classes --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This implements support for bulk buffered stream output.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/raw_ostream.h"
18 #if !defined(_MSC_VER)
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)
27 // An out of line virtual method to provide a home for the class vtable.
28 void raw_ostream::handle() {}
30 //===----------------------------------------------------------------------===//
32 //===----------------------------------------------------------------------===//
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) {
45 FD = open(Filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
47 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
54 raw_fd_ostream::~raw_fd_ostream() {
60 void raw_fd_ostream::flush_impl() {
61 if (OutBufCur-OutBufStart)
62 ::write(FD, OutBufStart, OutBufCur-OutBufStart);
66 //===----------------------------------------------------------------------===//
67 // raw_stdout/err_ostream
68 //===----------------------------------------------------------------------===//
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) {}
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() {}
77 /// outs() - This returns a reference to a raw_ostream for standard output.
78 /// Use it like: outs() << "foo" << "bar";
80 static raw_stdout_ostream S;
84 /// errs() - This returns a reference to a raw_ostream for standard error.
85 /// Use it like: errs() << "foo" << "bar";
87 static raw_stderr_ostream S;
91 //===----------------------------------------------------------------------===//
93 //===----------------------------------------------------------------------===//
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
98 void raw_os_ostream::flush_impl() {
99 if (OutBufCur-OutBufStart)
100 OS.write(OutBufStart, OutBufCur-OutBufStart);