output.hh revision 11259
11388SN/A/* 21388SN/A * Copyright (c) 2005 The Regents of The University of Michigan 31388SN/A * All rights reserved. 41388SN/A * 51388SN/A * Redistribution and use in source and binary forms, with or without 61388SN/A * modification, are permitted provided that the following conditions are 71388SN/A * met: redistributions of source code must retain the above copyright 81388SN/A * notice, this list of conditions and the following disclaimer; 91388SN/A * redistributions in binary form must reproduce the above copyright 101388SN/A * notice, this list of conditions and the following disclaimer in the 111388SN/A * documentation and/or other materials provided with the distribution; 121388SN/A * neither the name of the copyright holders nor the names of its 131388SN/A * contributors may be used to endorse or promote products derived from 141388SN/A * this software without specific prior written permission. 151388SN/A * 161388SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 171388SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 181388SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 191388SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 201388SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 211388SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 221388SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 231388SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 241388SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 251388SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 261388SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 272665Ssaidi@eecs.umich.edu * 282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert 298634Schris.emmons@arm.com * Chris Emmons 301388SN/A */ 311388SN/A 321388SN/A#ifndef __BASE_OUTPUT_HH__ 331388SN/A#define __BASE_OUTPUT_HH__ 341388SN/A 355749Scws3k@cs.virginia.edu#include <ios> 361388SN/A#include <map> 371388SN/A#include <string> 381388SN/A 398634Schris.emmons@arm.com/** Interface for creating files in a gem5 output directory. */ 401388SN/Aclass OutputDirectory 411388SN/A{ 421388SN/A private: 438634Schris.emmons@arm.com /** File names and associated stream handles */ 441388SN/A typedef std::map<std::string, std::ostream *> map_t; 451388SN/A 468634Schris.emmons@arm.com /** Open file streams within this directory */ 471388SN/A map_t files; 488634Schris.emmons@arm.com 498634Schris.emmons@arm.com /** Name of this directory */ 501388SN/A std::string dir; 511388SN/A 528634Schris.emmons@arm.com /** System-specific path separator character */ 538634Schris.emmons@arm.com static const char PATH_SEPARATOR = '/'; 548634Schris.emmons@arm.com 555749Scws3k@cs.virginia.edu protected: 568634Schris.emmons@arm.com /** 578634Schris.emmons@arm.com * Determines whether given file name corresponds to standard output 588634Schris.emmons@arm.com * streams. 598634Schris.emmons@arm.com * 608634Schris.emmons@arm.com * @param name name of file to check 618634Schris.emmons@arm.com * @return output stream for standard output or error stream if name 628634Schris.emmons@arm.com * corresponds to one or the other; NULL otherwise 638634Schris.emmons@arm.com */ 645749Scws3k@cs.virginia.edu std::ostream *checkForStdio(const std::string &name) const; 658634Schris.emmons@arm.com 668734Sdam.sunwoo@arm.com public: 678734Sdam.sunwoo@arm.com /** Constructor. */ 688734Sdam.sunwoo@arm.com OutputDirectory(); 698734Sdam.sunwoo@arm.com 708734Sdam.sunwoo@arm.com /** Destructor. */ 718734Sdam.sunwoo@arm.com ~OutputDirectory(); 728734Sdam.sunwoo@arm.com 739398Sandreas.hansson@arm.com /** 749398Sandreas.hansson@arm.com * Returns relative file names prepended with name of this directory. 759398Sandreas.hansson@arm.com * Returns absolute file names unaltered. 769398Sandreas.hansson@arm.com * 779398Sandreas.hansson@arm.com * @param name file name to prepend with directory name 789398Sandreas.hansson@arm.com * @return file name prepended with base directory name or unaltered 799398Sandreas.hansson@arm.com * absolute file name 809398Sandreas.hansson@arm.com */ 819398Sandreas.hansson@arm.com std::string resolve(const std::string &name) const; 829398Sandreas.hansson@arm.com 838634Schris.emmons@arm.com /** Opens a file (optionally compressed). 848634Schris.emmons@arm.com * 858634Schris.emmons@arm.com * Will open a file as a compressed stream if filename ends in .gz. 868634Schris.emmons@arm.com * 878634Schris.emmons@arm.com * @param filename file to open 888634Schris.emmons@arm.com * @param mode attributes to open file with 8911259Ssascha.bischoff@ARM.com * @param no_gz true to disable opening the file as a gzip compressed output 9011259Ssascha.bischoff@ARM.com * stream; false otherwise 918634Schris.emmons@arm.com * @return stream pointer to opened file; will cause sim fail on error 928634Schris.emmons@arm.com */ 935749Scws3k@cs.virginia.edu std::ostream *openFile(const std::string &filename, 9411259Ssascha.bischoff@ARM.com std::ios_base::openmode mode = std::ios::trunc, 9511259Ssascha.bischoff@ARM.com bool no_gz = false); 965402Ssaidi@eecs.umich.edu 978634Schris.emmons@arm.com /** 988634Schris.emmons@arm.com * Sets name of this directory. 998634Schris.emmons@arm.com * @param dir name of this directory 1008634Schris.emmons@arm.com */ 1011388SN/A void setDirectory(const std::string &dir); 1028634Schris.emmons@arm.com 1038634Schris.emmons@arm.com /** 1048634Schris.emmons@arm.com * Gets name of this directory. 1058634Schris.emmons@arm.com * @return name of this directory 1068634Schris.emmons@arm.com */ 1075749Scws3k@cs.virginia.edu const std::string &directory() const; 1081388SN/A 1098634Schris.emmons@arm.com /** 1108634Schris.emmons@arm.com * Creates a file in this directory (optionally compressed). 1118634Schris.emmons@arm.com * 1128634Schris.emmons@arm.com * Will open a file as a compressed stream if filename ends in .gz. 1138634Schris.emmons@arm.com * 1148634Schris.emmons@arm.com * @param name name of file to create (without this directory's name 1158634Schris.emmons@arm.com * leading it) 1168634Schris.emmons@arm.com * @param binary true to create a binary file; false otherwise 11711259Ssascha.bischoff@ARM.com * @param no_gz true to disable creating a gzip compressed output stream; 11811259Ssascha.bischoff@ARM.com * false otherwise 1198634Schris.emmons@arm.com * @return stream to the opened file 1208634Schris.emmons@arm.com */ 12111259Ssascha.bischoff@ARM.com std::ostream *create(const std::string &name, bool binary = false, 12211259Ssascha.bischoff@ARM.com bool no_gz = false); 1231388SN/A 1248634Schris.emmons@arm.com /** 1258634Schris.emmons@arm.com * Closes a file stream. 1268634Schris.emmons@arm.com * 1278634Schris.emmons@arm.com * Stream must have been opened through this interface, or sim will fail. 1288634Schris.emmons@arm.com * 1298634Schris.emmons@arm.com * @param openStream open stream to close 1308634Schris.emmons@arm.com */ 1318634Schris.emmons@arm.com void close(std::ostream *openStream); 1328634Schris.emmons@arm.com 1338634Schris.emmons@arm.com /** 1348634Schris.emmons@arm.com * Finds stream associated with a file. 1358634Schris.emmons@arm.com * @param name of file 1368634Schris.emmons@arm.com * @return stream to specified file or NULL if file does not exist 1378634Schris.emmons@arm.com */ 1388634Schris.emmons@arm.com std::ostream *find(const std::string &name) const; 1398634Schris.emmons@arm.com 1408634Schris.emmons@arm.com /** 1418634Schris.emmons@arm.com * Returns true if stream is open and not standard output or error. 1428634Schris.emmons@arm.com * @param os output stream to evaluate 1438634Schris.emmons@arm.com * @return true if os is non-NULL and not cout or cerr 1448634Schris.emmons@arm.com */ 1451388SN/A static bool isFile(const std::ostream *os); 1468634Schris.emmons@arm.com 1478634Schris.emmons@arm.com /** 1488634Schris.emmons@arm.com * Determines whether a file name corresponds to a file in this directory. 1498634Schris.emmons@arm.com * @param name name of file to evaluate 1508634Schris.emmons@arm.com * @return true iff file has been opened in this directory or exists on the 1518634Schris.emmons@arm.com * file system within this directory 1528634Schris.emmons@arm.com */ 1538634Schris.emmons@arm.com bool isFile(const std::string &name) const; 1548634Schris.emmons@arm.com 1558634Schris.emmons@arm.com /** 1568634Schris.emmons@arm.com * Returns true if stream is open and not standard output or error. 1578634Schris.emmons@arm.com * @param os output stream to evaluate 1588634Schris.emmons@arm.com * @return true if os is non-NULL and not cout or cerr 1598634Schris.emmons@arm.com */ 1608634Schris.emmons@arm.com static inline bool isFile(const std::ostream &os) { 1618634Schris.emmons@arm.com return isFile(&os); 1628634Schris.emmons@arm.com } 1638634Schris.emmons@arm.com 1648634Schris.emmons@arm.com /** 1658634Schris.emmons@arm.com * Creates a subdirectory within this directory. 1668634Schris.emmons@arm.com * @param name name of subdirectory 1678634Schris.emmons@arm.com * @return the new subdirectory's name suffixed with a path separator 1688634Schris.emmons@arm.com */ 1698634Schris.emmons@arm.com std::string createSubdirectory(const std::string &name) const; 1708634Schris.emmons@arm.com 1718634Schris.emmons@arm.com /** 1728634Schris.emmons@arm.com * Removes a specified file or subdirectory. 1738634Schris.emmons@arm.com * 1748634Schris.emmons@arm.com * Will cause sim to fail for most errors. However, it will only warn the 1758634Schris.emmons@arm.com * user if a directory could not be removed. This is in place to 1768634Schris.emmons@arm.com * accommodate slow file systems where file deletions within a subdirectory 1778634Schris.emmons@arm.com * may not be recognized quickly enough thereby causing the subsequent call 1788634Schris.emmons@arm.com * to remove the directory to fail (seemingly unempty directory). 1798634Schris.emmons@arm.com * 1808634Schris.emmons@arm.com * @param name name of file or subdirectory to remove; name should not 1818634Schris.emmons@arm.com * be prepended with the name of this directory object 1828634Schris.emmons@arm.com * @param recursive set to true to attempt to recursively delete a 1838634Schris.emmons@arm.com * subdirectory and its contents 1848634Schris.emmons@arm.com */ 1858634Schris.emmons@arm.com void remove(const std::string &name, bool recursive=false); 1861388SN/A}; 1871388SN/A 1881388SN/Aextern OutputDirectory simout; 1891388SN/A 1901388SN/A#endif // __BASE_OUTPUT_HH__ 191