output.cc revision 1388
12330SN/A/* 22330SN/A * Copyright (c) 2005 The Regents of The University of Michigan 32330SN/A * All rights reserved. 42330SN/A * 52330SN/A * Redistribution and use in source and binary forms, with or without 62330SN/A * modification, are permitted provided that the following conditions are 72330SN/A * met: redistributions of source code must retain the above copyright 82330SN/A * notice, this list of conditions and the following disclaimer; 92330SN/A * redistributions in binary form must reproduce the above copyright 102330SN/A * notice, this list of conditions and the following disclaimer in the 112330SN/A * documentation and/or other materials provided with the distribution; 122330SN/A * neither the name of the copyright holders nor the names of its 132330SN/A * contributors may be used to endorse or promote products derived from 142330SN/A * this software without specific prior written permission. 152330SN/A * 162330SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172330SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182330SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192330SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202330SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212330SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222330SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232330SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242330SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252330SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262330SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272330SN/A */ 282292SN/A 292292SN/A#include <errno.h> 302292SN/A#include <limits.h> 312292SN/A#include <stdlib.h> 322292SN/A#include <sys/stat.h> 332292SN/A#include <sys/types.h> 342678Sktlim@umich.edu 352678Sktlim@umich.edu#include <fstream> 362678Sktlim@umich.edu 372678Sktlim@umich.edu#include "base/misc.hh" 382292SN/A#include "base/output.hh" 392292SN/A 402292SN/Ausing namespace std; 412292SN/A 422330SN/AOutputDirectory simout; 432330SN/A 442330SN/A/** 452292SN/A * 462330SN/A */ 472292SN/AOutputDirectory::OutputDirectory() 482292SN/A{} 492292SN/A 502330SN/AOutputDirectory::~OutputDirectory() 512330SN/A{} 522330SN/A 532330SN/Avoid 542330SN/AOutputDirectory::setDirectory(const string &d) 552330SN/A{ 562292SN/A if (!dir.empty()) 572292SN/A panic("Output directory already set!\n"); 582678Sktlim@umich.edu 592678Sktlim@umich.edu dir = d; 602292SN/A 612292SN/A if (dir != ".") { 622678Sktlim@umich.edu if (mkdir(dir.c_str(), 0777) < 0 && errno != EEXIST) 632292SN/A panic("couldn't make output dir %s: %s\n", 642678Sktlim@umich.edu dir, strerror(errno)); 652292SN/A } 662292SN/A 672292SN/A // guarantee that directory ends with a '/' 682292SN/A if (dir[dir.size() - 1] != '/') 692678Sktlim@umich.edu dir += "/"; 702678Sktlim@umich.edu} 712678Sktlim@umich.edu 722678Sktlim@umich.educonst string & 732678Sktlim@umich.eduOutputDirectory::directory() 742678Sktlim@umich.edu{ 752678Sktlim@umich.edu if (dir.empty()) 762678Sktlim@umich.edu panic("Output directory not set!"); 772678Sktlim@umich.edu 782678Sktlim@umich.edu return dir; 792292SN/A} 802292SN/A 812292SN/Astring 822292SN/AOutputDirectory::resolve(const string &name) 832292SN/A{ 842292SN/A return (name[0] != '/') ? dir + name : name; 852292SN/A} 862292SN/A 872292SN/Aostream * 882292SN/AOutputDirectory::create(const string &name) 892292SN/A{ 902292SN/A if (name == "cerr" || name == "stderr") 912292SN/A return &cerr; 922292SN/A 932292SN/A if (name == "cout" || name == "stdout") 942292SN/A return &cout; 952292SN/A 962292SN/A ofstream *file = new ofstream(resolve(name).c_str(), ios::trunc); 972292SN/A if (!file->is_open()) 982292SN/A panic("Cannot open file %s", name); 992292SN/A 1002292SN/A return file; 1012292SN/A} 1022292SN/A 1032292SN/Aostream * 1042292SN/AOutputDirectory::find(const string &name) 1052292SN/A{ 1062330SN/A if (name == "cerr" || name == "stderr") 1072292SN/A return &cerr; 1082678Sktlim@umich.edu 1092678Sktlim@umich.edu if (name == "cout" || name == "stdout") 1102292SN/A return &cout; 1112292SN/A 1122292SN/A string filename = resolve(name); 1132292SN/A map_t::iterator i = files.find(filename); 1142292SN/A if (i != files.end()) 1152292SN/A return (*i).second; 1162292SN/A 1172292SN/A ofstream *file = new ofstream(filename.c_str(), ios::trunc); 1182292SN/A if (!file->is_open()) 1192292SN/A panic("Cannot open file %s", filename); 1202292SN/A 1212292SN/A files[filename] = file; 1222292SN/A return file; 1232292SN/A} 1242292SN/A 1252292SN/Abool 1262292SN/AOutputDirectory::isFile(const std::ostream *os) 1272292SN/A{ 1282292SN/A return os && os != &cerr && os != &cout; 1292292SN/A} 1302292SN/A