output.hh revision 11259
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  protected:
56    /**
57     * Determines whether given file name corresponds to standard output
58     * streams.
59     *
60     * @param name name of file to check
61     * @return output stream for standard output or error stream if name
62     *         corresponds to one or the other; NULL otherwise
63     */
64    std::ostream *checkForStdio(const std::string &name) const;
65
66  public:
67    /** Constructor. */
68    OutputDirectory();
69
70    /** Destructor. */
71    ~OutputDirectory();
72
73    /**
74     * Returns relative file names prepended with name of this directory.
75     * Returns absolute file names unaltered.
76     *
77     * @param name file name to prepend with directory name
78     * @return file name prepended with base directory name or unaltered
79     *          absolute file name
80     */
81    std::string resolve(const std::string &name) const;
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     * @param no_gz true to disable opening the file as a gzip compressed output
90     *     stream; false otherwise
91     * @return stream pointer to opened file; will cause sim fail on error
92     */
93    std::ostream *openFile(const std::string &filename,
94                        std::ios_base::openmode mode = std::ios::trunc,
95                        bool no_gz = false);
96
97    /**
98     * Sets name of this directory.
99     * @param dir name of this directory
100     */
101    void setDirectory(const std::string &dir);
102
103    /**
104     * Gets name of this directory.
105     * @return name of this directory
106     */
107    const std::string &directory() const;
108
109    /**
110     * Creates a file in this directory (optionally compressed).
111     *
112     * Will open a file as a compressed stream if filename ends in .gz.
113     *
114     * @param name name of file to create (without this directory's name
115     *          leading it)
116     * @param binary true to create a binary file; false otherwise
117     * @param no_gz true to disable creating a gzip compressed output stream;
118     *     false otherwise
119     * @return stream to the opened file
120     */
121    std::ostream *create(const std::string &name, bool binary = false,
122                         bool no_gz = false);
123
124    /**
125     * Closes a file stream.
126     *
127     * Stream must have been opened through this interface, or sim will fail.
128     *
129     * @param openStream open stream to close
130     */
131    void close(std::ostream *openStream);
132
133    /**
134     * Finds stream associated with a file.
135     * @param name of file
136     * @return stream to specified file or NULL if file does not exist
137     */
138    std::ostream *find(const std::string &name) const;
139
140    /**
141     * Returns true if stream is open and not standard output or error.
142     * @param os output stream to evaluate
143     * @return true if os is non-NULL and not cout or cerr
144     */
145    static bool isFile(const std::ostream *os);
146
147    /**
148     * Determines whether a file name corresponds to a file in this directory.
149     * @param name name of file to evaluate
150     * @return true iff file has been opened in this directory or exists on the
151     *          file system within this directory
152     */
153    bool isFile(const std::string &name) const;
154
155    /**
156     * Returns true if stream is open and not standard output or error.
157     * @param os output stream to evaluate
158     * @return true if os is non-NULL and not cout or cerr
159     */
160    static inline bool isFile(const std::ostream &os) {
161        return isFile(&os);
162    }
163
164    /**
165     * Creates a subdirectory within this directory.
166     * @param name name of subdirectory
167     * @return the new subdirectory's name suffixed with a path separator
168     */
169    std::string createSubdirectory(const std::string &name) const;
170
171    /**
172     * Removes a specified file or subdirectory.
173     *
174     * Will cause sim to fail for most errors.  However, it will only warn the
175     * user if a directory could not be removed.  This is in place to
176     * accommodate slow file systems where file deletions within a subdirectory
177     * may not be recognized quickly enough thereby causing the subsequent call
178     * to remove the directory to fail (seemingly unempty directory).
179     *
180     * @param name name of file or subdirectory to remove; name should not
181     *              be prepended with the name of this directory object
182     * @param recursive set to true to attempt to recursively delete a
183     *                  subdirectory and its contents
184     */
185    void remove(const std::string &name, bool recursive=false);
186};
187
188extern OutputDirectory simout;
189
190#endif // __BASE_OUTPUT_HH__
191