2 * Copyright (c) 2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Nathan Binkert
32 #ifndef __BASE_OUTPUT_HH__
33 #define __BASE_OUTPUT_HH__
39 /** Interface for creating files in a gem5 output directory. */
43 /** File names and associated stream handles */
44 typedef std::map<std::string, std::ostream *> map_t;
46 /** Open file streams within this directory */
49 /** Name of this directory */
52 /** System-specific path separator character */
53 static const char PATH_SEPARATOR = '/';
57 * Determines whether given file name corresponds to standard output
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
64 std::ostream *checkForStdio(const std::string &name) const;
74 * Returns relative file names prepended with name of this directory.
75 * Returns absolute file names unaltered.
77 * @param name file name to prepend with directory name
78 * @return file name prepended with base directory name or unaltered
81 std::string resolve(const std::string &name) const;
83 /** Opens a file (optionally compressed).
85 * Will open a file as a compressed stream if filename ends in .gz.
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
93 std::ostream *openFile(const std::string &filename,
94 std::ios_base::openmode mode = std::ios::trunc,
98 * Sets name of this directory.
99 * @param dir name of this directory
101 void setDirectory(const std::string &dir);
104 * Gets name of this directory.
105 * @return name of this directory
107 const std::string &directory() const;
110 * Creates a file in this directory (optionally compressed).
112 * Will open a file as a compressed stream if filename ends in .gz.
114 * @param name name of file to create (without this directory's name
116 * @param binary true to create a binary file; false otherwise
117 * @param no_gz true to disable creating a gzip compressed output stream;
119 * @return stream to the opened file
121 std::ostream *create(const std::string &name, bool binary = false,
125 * Closes a file stream.
127 * Stream must have been opened through this interface, or sim will fail.
129 * @param openStream open stream to close
131 void close(std::ostream *openStream);
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
138 std::ostream *find(const std::string &name) const;
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
145 static bool isFile(const std::ostream *os);
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
153 bool isFile(const std::string &name) const;
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
160 static inline bool isFile(const std::ostream &os) {
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
169 std::string createSubdirectory(const std::string &name) const;
172 * Removes a specified file or subdirectory.
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).
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
185 void remove(const std::string &name, bool recursive=false);
188 extern OutputDirectory simout;
190 #endif // __BASE_OUTPUT_HH__