raw_ostream.cpp revision cb3718832375a581c5ea23f15918f3ea447a446c
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> 16using namespace llvm; 17 18#include <fcntl.h> 19 20#if defined(_MSC_VER) 21#include <io.h> 22#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 31#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) { 44 // Handle "-" as stdout. 45 if (Filename[0] == '-' && Filename[1] == 0) { 46 FD = STDOUT_FILENO; 47 ShouldClose = false; 48 return; 49 } 50 51 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) 68 ::write(FD, OutBufStart, OutBufCur-OutBufStart); 69 HandleFlush(); 70} 71 72//===----------------------------------------------------------------------===// 73// raw_stdout/err_ostream 74//===----------------------------------------------------------------------===// 75 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() {} 82 83/// outs() - This returns a reference to a raw_ostream for standard output. 84/// Use it like: outs() << "foo" << "bar"; 85raw_ostream &llvm::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 &llvm::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} 109