output.cc revision 1388
111291Sgabor.dozsa@arm.com/* 211291Sgabor.dozsa@arm.com * Copyright (c) 2005 The Regents of The University of Michigan 311291Sgabor.dozsa@arm.com * All rights reserved. 411291Sgabor.dozsa@arm.com * 511291Sgabor.dozsa@arm.com * Redistribution and use in source and binary forms, with or without 611291Sgabor.dozsa@arm.com * modification, are permitted provided that the following conditions are 711291Sgabor.dozsa@arm.com * met: redistributions of source code must retain the above copyright 811291Sgabor.dozsa@arm.com * notice, this list of conditions and the following disclaimer; 911291Sgabor.dozsa@arm.com * redistributions in binary form must reproduce the above copyright 1011291Sgabor.dozsa@arm.com * notice, this list of conditions and the following disclaimer in the 1111291Sgabor.dozsa@arm.com * documentation and/or other materials provided with the distribution; 1211291Sgabor.dozsa@arm.com * neither the name of the copyright holders nor the names of its 1311291Sgabor.dozsa@arm.com * contributors may be used to endorse or promote products derived from 1411291Sgabor.dozsa@arm.com * this software without specific prior written permission. 1511291Sgabor.dozsa@arm.com * 1611291Sgabor.dozsa@arm.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1711291Sgabor.dozsa@arm.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1811291Sgabor.dozsa@arm.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1911291Sgabor.dozsa@arm.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2011291Sgabor.dozsa@arm.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2111291Sgabor.dozsa@arm.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2211291Sgabor.dozsa@arm.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2311291Sgabor.dozsa@arm.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2411291Sgabor.dozsa@arm.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2511291Sgabor.dozsa@arm.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2611291Sgabor.dozsa@arm.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2711291Sgabor.dozsa@arm.com */ 2811291Sgabor.dozsa@arm.com 2911291Sgabor.dozsa@arm.com#include <errno.h> 3011291Sgabor.dozsa@arm.com#include <limits.h> 3111291Sgabor.dozsa@arm.com#include <stdlib.h> 3211291Sgabor.dozsa@arm.com#include <sys/stat.h> 3311291Sgabor.dozsa@arm.com#include <sys/types.h> 3411291Sgabor.dozsa@arm.com 3511291Sgabor.dozsa@arm.com#include <fstream> 3611291Sgabor.dozsa@arm.com 3711291Sgabor.dozsa@arm.com#include "base/misc.hh" 3811291Sgabor.dozsa@arm.com#include "base/output.hh" 3911291Sgabor.dozsa@arm.com 4011291Sgabor.dozsa@arm.comusing namespace std; 4111291Sgabor.dozsa@arm.com 4211291Sgabor.dozsa@arm.comOutputDirectory simout; 4311291Sgabor.dozsa@arm.com 4411291Sgabor.dozsa@arm.com/** 4511291Sgabor.dozsa@arm.com * 4611291Sgabor.dozsa@arm.com */ 4711291Sgabor.dozsa@arm.comOutputDirectory::OutputDirectory() 4811291Sgabor.dozsa@arm.com{} 4911291Sgabor.dozsa@arm.com 5011291Sgabor.dozsa@arm.comOutputDirectory::~OutputDirectory() 5111291Sgabor.dozsa@arm.com{} 5211291Sgabor.dozsa@arm.com 5311291Sgabor.dozsa@arm.comvoid 5411291Sgabor.dozsa@arm.comOutputDirectory::setDirectory(const string &d) 5511444Sm.alian1369@gmail.com{ 5611291Sgabor.dozsa@arm.com if (!dir.empty()) 5711291Sgabor.dozsa@arm.com panic("Output directory already set!\n"); 5811291Sgabor.dozsa@arm.com 5911291Sgabor.dozsa@arm.com dir = d; 6011291Sgabor.dozsa@arm.com 6111291Sgabor.dozsa@arm.com if (dir != ".") { 6211291Sgabor.dozsa@arm.com if (mkdir(dir.c_str(), 0777) < 0 && errno != EEXIST) 6311291Sgabor.dozsa@arm.com panic("couldn't make output dir %s: %s\n", 6411291Sgabor.dozsa@arm.com dir, strerror(errno)); 6511291Sgabor.dozsa@arm.com } 6611291Sgabor.dozsa@arm.com 6711291Sgabor.dozsa@arm.com // guarantee that directory ends with a '/' 6811291Sgabor.dozsa@arm.com if (dir[dir.size() - 1] != '/') 6911291Sgabor.dozsa@arm.com dir += "/"; 7011291Sgabor.dozsa@arm.com} 7111291Sgabor.dozsa@arm.com 7211291Sgabor.dozsa@arm.comconst string & 7311291Sgabor.dozsa@arm.comOutputDirectory::directory() 7411291Sgabor.dozsa@arm.com{ 7511291Sgabor.dozsa@arm.com if (dir.empty()) 7611291Sgabor.dozsa@arm.com panic("Output directory not set!"); 7711291Sgabor.dozsa@arm.com 7811291Sgabor.dozsa@arm.com return dir; 7911291Sgabor.dozsa@arm.com} 8011291Sgabor.dozsa@arm.com 8111291Sgabor.dozsa@arm.comstring 8211291Sgabor.dozsa@arm.comOutputDirectory::resolve(const string &name) 83{ 84 return (name[0] != '/') ? dir + name : name; 85} 86 87ostream * 88OutputDirectory::create(const string &name) 89{ 90 if (name == "cerr" || name == "stderr") 91 return &cerr; 92 93 if (name == "cout" || name == "stdout") 94 return &cout; 95 96 ofstream *file = new ofstream(resolve(name).c_str(), ios::trunc); 97 if (!file->is_open()) 98 panic("Cannot open file %s", name); 99 100 return file; 101} 102 103ostream * 104OutputDirectory::find(const string &name) 105{ 106 if (name == "cerr" || name == "stderr") 107 return &cerr; 108 109 if (name == "cout" || name == "stdout") 110 return &cout; 111 112 string filename = resolve(name); 113 map_t::iterator i = files.find(filename); 114 if (i != files.end()) 115 return (*i).second; 116 117 ofstream *file = new ofstream(filename.c_str(), ios::trunc); 118 if (!file->is_open()) 119 panic("Cannot open file %s", filename); 120 121 files[filename] = file; 122 return file; 123} 124 125bool 126OutputDirectory::isFile(const std::ostream *os) 127{ 128 return os && os != &cerr && os != &cout; 129} 130