blob: 144b90f65ee926d69449c8a7c4ac0aefc57b9a72 [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
Chris Lattner60d39622008-08-17 01:35:29 +000018#include <fcntl.h>
Argyrios Kyrtzidisb68dc362008-08-17 09:25:21 +000019
20#if defined(_MSC_VER)
Chris Lattner60d39622008-08-17 01:35:29 +000021#include <io.h>
Argyrios Kyrtzidisb68dc362008-08-17 09:25:21 +000022#ifndef STDIN_FILENO
23# define STDIN_FILENO 0
24#endif
25#ifndef STDOUT_FILENO
26# define STDOUT_FILENO 1
27#endif
28#ifndef STDERR_FILENO
29# define STDERR_FILENO 2
30#endif
Chris Lattner60d39622008-08-17 01:35:29 +000031#endif
32
33// An out of line virtual method to provide a home for the class vtable.
34void raw_ostream::handle() {}
35
36//===----------------------------------------------------------------------===//
37// raw_fd_ostream
38//===----------------------------------------------------------------------===//
39
40/// raw_fd_ostream - Open the specified file for writing. If an error occurs,
41/// information about the error is put into ErrorInfo, and the stream should
42/// be immediately destroyed.
43raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo) {
Chris Lattnerc52b1282008-08-17 03:53:23 +000044 // Handle "-" as stdout.
45 if (Filename[0] == '-' && Filename[1] == 0) {
46 FD = STDOUT_FILENO;
47 ShouldClose = false;
48 return;
49 }
50
Chris Lattner60d39622008-08-17 01:35:29 +000051 FD = open(Filename, O_WRONLY|O_CREAT|O_TRUNC, 0644);
52 if (FD < 0) {
53 ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
54 ShouldClose = false;
55 } else {
56 ShouldClose = true;
57 }
58}
59
60raw_fd_ostream::~raw_fd_ostream() {
61 flush();
62 if (ShouldClose)
63 close(FD);
64}
65
66void raw_fd_ostream::flush_impl() {
67 if (OutBufCur-OutBufStart)
Chris Lattnerd497df52008-08-17 01:46:05 +000068 ::write(FD, OutBufStart, OutBufCur-OutBufStart);
Chris Lattner60d39622008-08-17 01:35:29 +000069 HandleFlush();
70}
71
Chris Lattner07f51f72008-08-17 04:13:37 +000072//===----------------------------------------------------------------------===//
73// raw_stdout/err_ostream
74//===----------------------------------------------------------------------===//
Chris Lattner60d39622008-08-17 01:35:29 +000075
76raw_stdout_ostream::raw_stdout_ostream():raw_fd_ostream(STDOUT_FILENO, false) {}
77raw_stderr_ostream::raw_stderr_ostream():raw_fd_ostream(STDERR_FILENO, false) {}
78
79// An out of line virtual method to provide a home for the class vtable.
80void raw_stdout_ostream::handle() {}
81void raw_stderr_ostream::handle() {}
Chris Lattner07f51f72008-08-17 04:13:37 +000082
83/// outs() - This returns a reference to a raw_ostream for standard output.
84/// Use it like: outs() << "foo" << "bar";
85raw_ostream &outs() {
86 static raw_stdout_ostream S;
87 return S;
88}
89
90/// errs() - This returns a reference to a raw_ostream for standard error.
91/// Use it like: errs() << "foo" << "bar";
92raw_ostream &errs() {
93 static raw_stderr_ostream S;
94 return S;
95}
96
97//===----------------------------------------------------------------------===//
98// raw_os_ostream
99//===----------------------------------------------------------------------===//
100
101/// flush_impl - The is the piece of the class that is implemented by
102/// subclasses. This outputs the currently buffered data and resets the
103/// buffer to empty.
104void raw_os_ostream::flush_impl() {
105 if (OutBufCur-OutBufStart)
106 OS.write(OutBufStart, OutBufCur-OutBufStart);
107 HandleFlush();
108}