output.hh revision 8734
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
558634Schris.emmons@arm.com    /**
568634Schris.emmons@arm.com     * Returns relative file names prepended with name of this directory.
578634Schris.emmons@arm.com     * Returns absolute file names unaltered.
588634Schris.emmons@arm.com     *
598634Schris.emmons@arm.com     * @param name file name to prepend with directory name
608634Schris.emmons@arm.com     * @return file name prepended with base directory name or unaltered
618634Schris.emmons@arm.com     *          absolute file name
628634Schris.emmons@arm.com     */
635749Scws3k@cs.virginia.edu    std::string resolve(const std::string &name) const;
645749Scws3k@cs.virginia.edu
655749Scws3k@cs.virginia.edu  protected:
668634Schris.emmons@arm.com    /**
678634Schris.emmons@arm.com     * Determines whether given file name corresponds to standard output
688634Schris.emmons@arm.com     * streams.
698634Schris.emmons@arm.com     *
708634Schris.emmons@arm.com     * @param name name of file to check
718634Schris.emmons@arm.com     * @return output stream for standard output or error stream if name
728634Schris.emmons@arm.com     *         corresponds to one or the other; NULL otherwise
738634Schris.emmons@arm.com     */
745749Scws3k@cs.virginia.edu    std::ostream *checkForStdio(const std::string &name) const;
758634Schris.emmons@arm.com
768734Sdam.sunwoo@arm.com  public:
778734Sdam.sunwoo@arm.com    /** Constructor. */
788734Sdam.sunwoo@arm.com    OutputDirectory();
798734Sdam.sunwoo@arm.com
808734Sdam.sunwoo@arm.com    /** Destructor. */
818734Sdam.sunwoo@arm.com    ~OutputDirectory();
828734Sdam.sunwoo@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
898634Schris.emmons@arm.com     * @return stream pointer to opened file; will cause sim fail on error
908634Schris.emmons@arm.com     */
915749Scws3k@cs.virginia.edu    std::ostream *openFile(const std::string &filename,
928634Schris.emmons@arm.com                        std::ios_base::openmode mode = std::ios::trunc);
935402Ssaidi@eecs.umich.edu
948634Schris.emmons@arm.com    /**
958634Schris.emmons@arm.com     * Sets name of this directory.
968634Schris.emmons@arm.com     * @param dir name of this directory
978634Schris.emmons@arm.com     */
981388SN/A    void setDirectory(const std::string &dir);
998634Schris.emmons@arm.com
1008634Schris.emmons@arm.com    /**
1018634Schris.emmons@arm.com     * Gets name of this directory.
1028634Schris.emmons@arm.com     * @return name of this directory
1038634Schris.emmons@arm.com     */
1045749Scws3k@cs.virginia.edu    const std::string &directory() const;
1051388SN/A
1068634Schris.emmons@arm.com    /**
1078634Schris.emmons@arm.com     * Creates a file in this directory (optionally compressed).
1088634Schris.emmons@arm.com     *
1098634Schris.emmons@arm.com     * Will open a file as a compressed stream if filename ends in .gz.
1108634Schris.emmons@arm.com     *
1118634Schris.emmons@arm.com     * @param name name of file to create (without this directory's name
1128634Schris.emmons@arm.com     *          leading it)
1138634Schris.emmons@arm.com     * @param binary true to create a binary file; false otherwise
1148634Schris.emmons@arm.com     * @return stream to the opened file
1158634Schris.emmons@arm.com     */
1164840Ssaidi@eecs.umich.edu    std::ostream *create(const std::string &name, bool binary = false);
1171388SN/A
1188634Schris.emmons@arm.com    /**
1198634Schris.emmons@arm.com     * Closes a file stream.
1208634Schris.emmons@arm.com     *
1218634Schris.emmons@arm.com     * Stream must have been opened through this interface, or sim will fail.
1228634Schris.emmons@arm.com     *
1238634Schris.emmons@arm.com     * @param openStream open stream to close
1248634Schris.emmons@arm.com     */
1258634Schris.emmons@arm.com    void close(std::ostream *openStream);
1268634Schris.emmons@arm.com
1278634Schris.emmons@arm.com    /**
1288634Schris.emmons@arm.com     * Finds stream associated with a file.
1298634Schris.emmons@arm.com     * @param name of file
1308634Schris.emmons@arm.com     * @return stream to specified file or NULL if file does not exist
1318634Schris.emmons@arm.com     */
1328634Schris.emmons@arm.com    std::ostream *find(const std::string &name) const;
1338634Schris.emmons@arm.com
1348634Schris.emmons@arm.com    /**
1358634Schris.emmons@arm.com     * Returns true if stream is open and not standard output or error.
1368634Schris.emmons@arm.com     * @param os output stream to evaluate
1378634Schris.emmons@arm.com     * @return true if os is non-NULL and not cout or cerr
1388634Schris.emmons@arm.com     */
1391388SN/A    static bool isFile(const std::ostream *os);
1408634Schris.emmons@arm.com
1418634Schris.emmons@arm.com    /**
1428634Schris.emmons@arm.com     * Determines whether a file name corresponds to a file in this directory.
1438634Schris.emmons@arm.com     * @param name name of file to evaluate
1448634Schris.emmons@arm.com     * @return true iff file has been opened in this directory or exists on the
1458634Schris.emmons@arm.com     *          file system within this directory
1468634Schris.emmons@arm.com     */
1478634Schris.emmons@arm.com    bool isFile(const std::string &name) const;
1488634Schris.emmons@arm.com
1498634Schris.emmons@arm.com    /**
1508634Schris.emmons@arm.com     * Returns true if stream is open and not standard output or error.
1518634Schris.emmons@arm.com     * @param os output stream to evaluate
1528634Schris.emmons@arm.com     * @return true if os is non-NULL and not cout or cerr
1538634Schris.emmons@arm.com     */
1548634Schris.emmons@arm.com    static inline bool isFile(const std::ostream &os) {
1558634Schris.emmons@arm.com        return isFile(&os);
1568634Schris.emmons@arm.com    }
1578634Schris.emmons@arm.com
1588634Schris.emmons@arm.com    /**
1598634Schris.emmons@arm.com     * Creates a subdirectory within this directory.
1608634Schris.emmons@arm.com     * @param name name of subdirectory
1618634Schris.emmons@arm.com     * @return the new subdirectory's name suffixed with a path separator
1628634Schris.emmons@arm.com     */
1638634Schris.emmons@arm.com    std::string createSubdirectory(const std::string &name) const;
1648634Schris.emmons@arm.com
1658634Schris.emmons@arm.com    /**
1668634Schris.emmons@arm.com     * Removes a specified file or subdirectory.
1678634Schris.emmons@arm.com     *
1688634Schris.emmons@arm.com     * Will cause sim to fail for most errors.  However, it will only warn the
1698634Schris.emmons@arm.com     * user if a directory could not be removed.  This is in place to
1708634Schris.emmons@arm.com     * accommodate slow file systems where file deletions within a subdirectory
1718634Schris.emmons@arm.com     * may not be recognized quickly enough thereby causing the subsequent call
1728634Schris.emmons@arm.com     * to remove the directory to fail (seemingly unempty directory).
1738634Schris.emmons@arm.com     *
1748634Schris.emmons@arm.com     * @param name name of file or subdirectory to remove; name should not
1758634Schris.emmons@arm.com     *              be prepended with the name of this directory object
1768634Schris.emmons@arm.com     * @param recursive set to true to attempt to recursively delete a
1778634Schris.emmons@arm.com     *                  subdirectory and its contents
1788634Schris.emmons@arm.com     */
1798634Schris.emmons@arm.com    void remove(const std::string &name, bool recursive=false);
1801388SN/A};
1811388SN/A
1821388SN/Aextern OutputDirectory simout;
1831388SN/A
1841388SN/A#endif // __BASE_OUTPUT_HH__
185