output.hh revision 8734
12SN/A/* 21762SN/A * Copyright (c) 2005 The Regents of The University of Michigan 32SN/A * All rights reserved. 42SN/A * 52SN/A * Redistribution and use in source and binary forms, with or without 62SN/A * modification, are permitted provided that the following conditions are 72SN/A * met: redistributions of source code must retain the above copyright 82SN/A * notice, this list of conditions and the following disclaimer; 92SN/A * redistributions in binary form must reproduce the above copyright 102SN/A * notice, this list of conditions and the following disclaimer in the 112SN/A * documentation and/or other materials provided with the distribution; 122SN/A * neither the name of the copyright holders nor the names of its 132SN/A * contributors may be used to endorse or promote products derived from 142SN/A * this software without specific prior written permission. 152SN/A * 162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272SN/A * 282SN/A * Authors: Nathan Binkert 292SN/A * Chris Emmons 302SN/A */ 312SN/A 322SN/A#ifndef __BASE_OUTPUT_HH__ 332SN/A#define __BASE_OUTPUT_HH__ 342432SN/A 351147SN/A#include <ios> 362090SN/A#include <map> 371147SN/A#include <string> 382517SN/A 3956SN/A/** Interface for creating files in a gem5 output directory. */ 402SN/Aclass OutputDirectory 412SN/A{ 422SN/A private: 43674SN/A /** File names and associated stream handles */ 442SN/A typedef std::map<std::string, std::ostream *> map_t; 452SN/A 462SN/A /** Open file streams within this directory */ 472SN/A map_t files; 482SN/A 492SN/A /** Name of this directory */ 502SN/A std::string dir; 512SN/A 522SN/A /** System-specific path separator character */ 532SN/A static const char PATH_SEPARATOR = '/'; 542SN/A 552SN/A /** 562SN/A * Returns relative file names prepended with name of this directory. 57674SN/A * Returns absolute file names unaltered. 58674SN/A * 592SN/A * @param name file name to prepend with directory name 602SN/A * @return file name prepended with base directory name or unaltered 612SN/A * absolute file name 62555SN/A */ 632SN/A std::string resolve(const std::string &name) const; 642SN/A 652SN/A protected: 662SN/A /** 672SN/A * Determines whether given file name corresponds to standard output 682SN/A * streams. 692SN/A * 702SN/A * @param name name of file to check 712SN/A * @return output stream for standard output or error stream if name 721147SN/A * corresponds to one or the other; NULL otherwise 731147SN/A */ 742SN/A std::ostream *checkForStdio(const std::string &name) const; 752SN/A 762532SN/A public: 772SN/A /** Constructor. */ 782SN/A OutputDirectory(); 79217SN/A 80237SN/A /** Destructor. */ 812SN/A ~OutputDirectory(); 822SN/A 83674SN/A /** Opens a file (optionally compressed). 842SN/A * 852SN/A * Will open a file as a compressed stream if filename ends in .gz. 86729SN/A * 87729SN/A * @param filename file to open 88729SN/A * @param mode attributes to open file with 89729SN/A * @return stream pointer to opened file; will cause sim fail on error 902SN/A */ 912SN/A std::ostream *openFile(const std::string &filename, 92674SN/A std::ios_base::openmode mode = std::ios::trunc); 932SN/A 942SN/A /** 952532SN/A * Sets name of this directory. 962SN/A * @param dir name of this directory 972SN/A */ 98674SN/A void setDirectory(const std::string &dir); 992SN/A 1002SN/A /** 101729SN/A * Gets name of this directory. 102729SN/A * @return name of this directory 103729SN/A */ 104729SN/A const std::string &directory() const; 105729SN/A 106729SN/A /** 107729SN/A * Creates a file in this directory (optionally compressed). 108729SN/A * 109729SN/A * Will open a file as a compressed stream if filename ends in .gz. 110729SN/A * 111729SN/A * @param name name of file to create (without this directory's name 112729SN/A * leading it) 1132SN/A * @param binary true to create a binary file; false otherwise 1142SN/A * @return stream to the opened file 115674SN/A */ 1162SN/A std::ostream *create(const std::string &name, bool binary = false); 1172SN/A 1182532SN/A /** 1192SN/A * Closes a file stream. 1202SN/A * 1212SN/A * Stream must have been opened through this interface, or sim will fail. 122 * 123 * @param openStream open stream to close 124 */ 125 void close(std::ostream *openStream); 126 127 /** 128 * Finds stream associated with a file. 129 * @param name of file 130 * @return stream to specified file or NULL if file does not exist 131 */ 132 std::ostream *find(const std::string &name) const; 133 134 /** 135 * Returns true if stream is open and not standard output or error. 136 * @param os output stream to evaluate 137 * @return true if os is non-NULL and not cout or cerr 138 */ 139 static bool isFile(const std::ostream *os); 140 141 /** 142 * Determines whether a file name corresponds to a file in this directory. 143 * @param name name of file to evaluate 144 * @return true iff file has been opened in this directory or exists on the 145 * file system within this directory 146 */ 147 bool isFile(const std::string &name) const; 148 149 /** 150 * Returns true if stream is open and not standard output or error. 151 * @param os output stream to evaluate 152 * @return true if os is non-NULL and not cout or cerr 153 */ 154 static inline bool isFile(const std::ostream &os) { 155 return isFile(&os); 156 } 157 158 /** 159 * Creates a subdirectory within this directory. 160 * @param name name of subdirectory 161 * @return the new subdirectory's name suffixed with a path separator 162 */ 163 std::string createSubdirectory(const std::string &name) const; 164 165 /** 166 * Removes a specified file or subdirectory. 167 * 168 * Will cause sim to fail for most errors. However, it will only warn the 169 * user if a directory could not be removed. This is in place to 170 * accommodate slow file systems where file deletions within a subdirectory 171 * may not be recognized quickly enough thereby causing the subsequent call 172 * to remove the directory to fail (seemingly unempty directory). 173 * 174 * @param name name of file or subdirectory to remove; name should not 175 * be prepended with the name of this directory object 176 * @param recursive set to true to attempt to recursively delete a 177 * subdirectory and its contents 178 */ 179 void remove(const std::string &name, bool recursive=false); 180}; 181 182extern OutputDirectory simout; 183 184#endif // __BASE_OUTPUT_HH__ 185