output.hh revision 8634
12SN/A/*
211526Sdavid.guillen@arm.com * Copyright (c) 2005 The Regents of The University of Michigan
38707Sandreas.hansson@arm.com * All rights reserved.
48707Sandreas.hansson@arm.com *
58707Sandreas.hansson@arm.com * Redistribution and use in source and binary forms, with or without
68707Sandreas.hansson@arm.com * modification, are permitted provided that the following conditions are
78707Sandreas.hansson@arm.com * met: redistributions of source code must retain the above copyright
88707Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer;
98707Sandreas.hansson@arm.com * redistributions in binary form must reproduce the above copyright
108707Sandreas.hansson@arm.com * notice, this list of conditions and the following disclaimer in the
118707Sandreas.hansson@arm.com * documentation and/or other materials provided with the distribution;
128707Sandreas.hansson@arm.com * neither the name of the copyright holders nor the names of its
138707Sandreas.hansson@arm.com * contributors may be used to endorse or promote products derived from
141762SN/A * this software without specific prior written permission.
157897Shestness@cs.utexas.edu *
169983Sstever@gmail.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
179983Sstever@gmail.com * "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__
342SN/A
352SN/A#include <ios>
362SN/A#include <map>
372SN/A#include <string>
382SN/A
392SN/A/** Interface for creating files in a gem5 output directory. */
402SN/Aclass OutputDirectory
412SN/A{
422665Ssaidi@eecs.umich.edu  private:
432665Ssaidi@eecs.umich.edu    /** File names and associated stream handles */
442665Ssaidi@eecs.umich.edu    typedef std::map<std::string, std::ostream *> map_t;
457897Shestness@cs.utexas.edu
462SN/A    /** Open file streams within this directory */
472SN/A    map_t files;
481388SN/A
498229Snate@binkert.org    /** Name of this directory */
502SN/A    std::string dir;
512SN/A
527781SAli.Saidi@ARM.com    /** System-specific path separator character */
538229Snate@binkert.org    static const char PATH_SEPARATOR = '/';
541191SN/A
551191SN/A    /**
561388SN/A     * Returns relative file names prepended with name of this directory.
575529Snate@binkert.org     * Returns absolute file names unaltered.
5810529Smorr@cs.wisc.edu     *
591717SN/A     * @param name file name to prepend with directory name
602651Ssaidi@eecs.umich.edu     * @return file name prepended with base directory name or unaltered
618229Snate@binkert.org     *          absolute file name
622680Sktlim@umich.edu     */
6310529Smorr@cs.wisc.edu    std::string resolve(const std::string &name) const;
648232Snate@binkert.org
6510529Smorr@cs.wisc.edu  protected:
665529Snate@binkert.org    /**
6711526Sdavid.guillen@arm.com     * Determines whether given file name corresponds to standard output
688779Sgblack@eecs.umich.edu     * streams.
692190SN/A     *
7056SN/A     * @param name name of file to check
718229Snate@binkert.org     * @return output stream for standard output or error stream if name
722190SN/A     *         corresponds to one or the other; NULL otherwise
732SN/A     */
742359SN/A    std::ostream *checkForStdio(const std::string &name) const;
752359SN/A
762359SN/A    /** Opens a file (optionally compressed).
772SN/A     *
782SN/A     * Will open a file as a compressed stream if filename ends in .gz.
792SN/A     *
802SN/A     * @param filename file to open
812SN/A     * @param mode attributes to open file with
822SN/A     * @return stream pointer to opened file; will cause sim fail on error
832SN/A     */
842SN/A    std::ostream *openFile(const std::string &filename,
852SN/A                        std::ios_base::openmode mode = std::ios::trunc);
865606Snate@binkert.org
876144Sksewell@umich.edu  public:
886144Sksewell@umich.edu    /** Constructor. */
893126Sktlim@umich.edu    OutputDirectory();
906144Sksewell@umich.edu
917823Ssteve.reinhardt@amd.com    /** Destructor. */
923126Sktlim@umich.edu    ~OutputDirectory();
933126Sktlim@umich.edu
942356SN/A    /**
952356SN/A     * Sets name of this directory.
962356SN/A     * @param dir name of this directory
978834Satgutier@umich.edu     */
9810786Smalek.musleh@gmail.com    void setDirectory(const std::string &dir);
9910786Smalek.musleh@gmail.com
10010786Smalek.musleh@gmail.com    /**
10110786Smalek.musleh@gmail.com     * Gets name of this directory.
10211321Ssteve.reinhardt@amd.com     * @return name of this directory
10310786Smalek.musleh@gmail.com     */
10410786Smalek.musleh@gmail.com    const std::string &directory() const;
10510786Smalek.musleh@gmail.com
1062356SN/A    /**
1079179Sandreas.hansson@arm.com     * Creates a file in this directory (optionally compressed).
1082367SN/A     *
1096144Sksewell@umich.edu     * Will open a file as a compressed stream if filename ends in .gz.
1106144Sksewell@umich.edu     *
1116144Sksewell@umich.edu     * @param name name of file to create (without this directory's name
1122356SN/A     *          leading it)
1132367SN/A     * @param binary true to create a binary file; false otherwise
1146144Sksewell@umich.edu     * @return stream to the opened file
1157823Ssteve.reinhardt@amd.com     */
1166144Sksewell@umich.edu    std::ostream *create(const std::string &name, bool binary = false);
1172367SN/A
1182356SN/A    /**
1192356SN/A     * Closes a file stream.
1202356SN/A     *
1212356SN/A     * Stream must have been opened through this interface, or sim will fail.
1225336Shines@cs.fsu.edu     *
1232356SN/A     * @param openStream open stream to close
1244873Sstever@eecs.umich.edu     */
1252356SN/A    void close(std::ostream *openStream);
1262356SN/A
1278876Sandreas.hansson@arm.com    /**
12810190Sakash.bagdia@arm.com     * Finds stream associated with a file.
1298832SAli.Saidi@ARM.com     * @param name of file
1308832SAli.Saidi@ARM.com     * @return stream to specified file or NULL if file does not exist
13111050Sandreas.hansson@arm.com     */
1329814Sandreas.hansson@arm.com    std::ostream *find(const std::string &name) const;
1339220Shestness@cs.wisc.edu
13410529Smorr@cs.wisc.edu    /**
13510537Sandreas.hansson@arm.com     * Returns true if stream is open and not standard output or error.
13610537Sandreas.hansson@arm.com     * @param os output stream to evaluate
13711148Smitch.hayenga@arm.com     * @return true if os is non-NULL and not cout or cerr
1382SN/A     */
1395712Shsul@eecs.umich.edu    static bool isFile(const std::ostream *os);
1405712Shsul@eecs.umich.edu
1415712Shsul@eecs.umich.edu    /**
1425712Shsul@eecs.umich.edu     * Determines whether a file name corresponds to a file in this directory.
1435712Shsul@eecs.umich.edu     * @param name name of file to evaluate
1442SN/A     * @return true iff file has been opened in this directory or exists on the
1452SN/A     *          file system within this directory
1462SN/A     */
14710190Sakash.bagdia@arm.com    bool isFile(const std::string &name) const;
14810190Sakash.bagdia@arm.com
1495712Shsul@eecs.umich.edu    /**
1506221Snate@binkert.org     * Returns true if stream is open and not standard output or error.
1516221Snate@binkert.org     * @param os output stream to evaluate
1522SN/A     * @return true if os is non-NULL and not cout or cerr
1532SN/A     */
1546221Snate@binkert.org    static inline bool isFile(const std::ostream &os) {
1556221Snate@binkert.org        return isFile(&os);
1566221Snate@binkert.org    }
1576221Snate@binkert.org
1582SN/A    /**
1592SN/A     * Creates a subdirectory within this directory.
1602SN/A     * @param name name of subdirectory
1612SN/A     * @return the new subdirectory's name suffixed with a path separator
1625606Snate@binkert.org     */
1635606Snate@binkert.org    std::string createSubdirectory(const std::string &name) const;
1649749Sandreas@sandberg.pp.se
1659749Sandreas@sandberg.pp.se    /**
1665606Snate@binkert.org     * Removes a specified file or subdirectory.
1672SN/A     *
1689647Sdam.sunwoo@arm.com     * Will cause sim to fail for most errors.  However, it will only warn the
1699647Sdam.sunwoo@arm.com     * user if a directory could not be removed.  This is in place to
1709647Sdam.sunwoo@arm.com     * accommodate slow file systems where file deletions within a subdirectory
1719647Sdam.sunwoo@arm.com     * may not be recognized quickly enough thereby causing the subsequent call
1729647Sdam.sunwoo@arm.com     * to remove the directory to fail (seemingly unempty directory).
1739647Sdam.sunwoo@arm.com     *
1749749Sandreas@sandberg.pp.se     * @param name name of file or subdirectory to remove; name should not
1759749Sandreas@sandberg.pp.se     *              be prepended with the name of this directory object
1769647Sdam.sunwoo@arm.com     * @param recursive set to true to attempt to recursively delete a
1779647Sdam.sunwoo@arm.com     *                  subdirectory and its contents
1781400SN/A     */
1795606Snate@binkert.org    void remove(const std::string &name, bool recursive=false);
1805606Snate@binkert.org};
1812SN/A
1822SN/Aextern OutputDirectory simout;
1832SN/A
1842SN/A#endif // __BASE_OUTPUT_HH__
1856221Snate@binkert.org