output.hh revision 8734
1/*
2 * Copyright (c) 2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 *          Chris Emmons
30 */
31
32#ifndef __BASE_OUTPUT_HH__
33#define __BASE_OUTPUT_HH__
34
35#include <ios>
36#include <map>
37#include <string>
38
39/** Interface for creating files in a gem5 output directory. */
40class OutputDirectory
41{
42  private:
43    /** File names and associated stream handles */
44    typedef std::map<std::string, std::ostream *> map_t;
45
46    /** Open file streams within this directory */
47    map_t files;
48
49    /** Name of this directory */
50    std::string dir;
51
52    /** System-specific path separator character */
53    static const char PATH_SEPARATOR = '/';
54
55    /**
56     * Returns relative file names prepended with name of this directory.
57     * Returns absolute file names unaltered.
58     *
59     * @param name file name to prepend with directory name
60     * @return file name prepended with base directory name or unaltered
61     *          absolute file name
62     */
63    std::string resolve(const std::string &name) const;
64
65  protected:
66    /**
67     * Determines whether given file name corresponds to standard output
68     * streams.
69     *
70     * @param name name of file to check
71     * @return output stream for standard output or error stream if name
72     *         corresponds to one or the other; NULL otherwise
73     */
74    std::ostream *checkForStdio(const std::string &name) const;
75
76  public:
77    /** Constructor. */
78    OutputDirectory();
79
80    /** Destructor. */
81    ~OutputDirectory();
82
83    /** Opens a file (optionally compressed).
84     *
85     * Will open a file as a compressed stream if filename ends in .gz.
86     *
87     * @param filename file to open
88     * @param mode attributes to open file with
89     * @return stream pointer to opened file; will cause sim fail on error
90     */
91    std::ostream *openFile(const std::string &filename,
92                        std::ios_base::openmode mode = std::ios::trunc);
93
94    /**
95     * Sets name of this directory.
96     * @param dir name of this directory
97     */
98    void setDirectory(const std::string &dir);
99
100    /**
101     * Gets name of this directory.
102     * @return name of this directory
103     */
104    const std::string &directory() const;
105
106    /**
107     * Creates a file in this directory (optionally compressed).
108     *
109     * Will open a file as a compressed stream if filename ends in .gz.
110     *
111     * @param name name of file to create (without this directory's name
112     *          leading it)
113     * @param binary true to create a binary file; false otherwise
114     * @return stream to the opened file
115     */
116    std::ostream *create(const std::string &name, bool binary = false);
117
118    /**
119     * Closes a file stream.
120     *
121     * 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