blob: 96864277170772a24fc8869d3ab92f7b954e4076 [file] [log] [blame]
Chris Lattner60d39622008-08-17 01:35:29 +00001//===--- 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 Lattner07f51f72008-08-17 04:13:37 +000015#include <ostream>
Chris Lattner60d39622008-08-17 01:35:29 +000016using 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.
28void 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.
37raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo) {
Chris Lattnerc52b1282008-08-17 03:53:23 +000038 // Handle "-" as stdout.
39 if (Filename[0] == '-' && Filename[1] == 0) {
40 FD = STDOUT_FILENO;
41 ShouldClose = false;
42 return;
43 }
44
Chris Lattner60d39622008-08-17 01:35:29 +000045 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
54raw_fd_ostream::~raw_fd_ostream() {
55 flush();
56 if (ShouldClose)
57 close(FD);
58}
59
60void raw_fd_ostream::flush_impl() {
61 if (OutBufCur-OutBufStart)
Chris Lattnerd497df52008-08-17 01:46:05 +000062 ::write(FD, OutBufStart, OutBufCur-OutBufStart);
Chris Lattner60d39622008-08-17 01:35:29 +000063 HandleFlush();
64}
65
Chris Lattner07f51f72008-08-17 04:13:37 +000066//===----------------------------------------------------------------------===//
67// raw_stdout/err_ostream
68//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +000069
70raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
71raw_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.
74void raw_stdout_ostream::handle() {}
75void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +000076
77/// outs() - This returns a reference to a raw_ostream for standard output.
78/// Use it like: outs() << "foo" << "bar";
79raw_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";
86raw_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.
98void raw_os_ostream::flush_impl() {
99 if (OutBufCur-OutBufStart)
100 OS.write(OutBufStart, OutBufCur-OutBufStart);
101 HandleFlush();
102}