output.hh (5749:7015e400bd1d) output.hh (8634:8390f2d80227)
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;

--- 12 unchanged lines hidden (view full) ---

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
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;

--- 12 unchanged lines hidden (view full) ---

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
29 */
30
31#ifndef __BASE_OUTPUT_HH__
32#define __BASE_OUTPUT_HH__
33
34#include <ios>
35#include <map>
36#include <string>
37
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. */
38class OutputDirectory
39{
40 private:
40class OutputDirectory
41{
42 private:
43 /** File names and associated stream handles */
41 typedef std::map<std::string, std::ostream *> map_t;
42
44 typedef std::map<std::string, std::ostream *> map_t;
45
46 /** Open file streams within this directory */
43 map_t files;
47 map_t files;
48
49 /** Name of this directory */
44 std::string dir;
45
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 */
46 std::string resolve(const std::string &name) const;
47
48 protected:
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 */
49 std::ostream *checkForStdio(const std::string &name) const;
74 std::ostream *checkForStdio(const std::string &name) const;
75
76 /** Opens a file (optionally compressed).
77 *
78 * Will open a file as a compressed stream if filename ends in .gz.
79 *
80 * @param filename file to open
81 * @param mode attributes to open file with
82 * @return stream pointer to opened file; will cause sim fail on error
83 */
50 std::ostream *openFile(const std::string &filename,
84 std::ostream *openFile(const std::string &filename,
51 std::ios_base::openmode mode = std::ios::trunc) const;
85 std::ios_base::openmode mode = std::ios::trunc);
52
53 public:
86
87 public:
88 /** Constructor. */
54 OutputDirectory();
89 OutputDirectory();
90
91 /** Destructor. */
55 ~OutputDirectory();
56
92 ~OutputDirectory();
93
94 /**
95 * Sets name of this directory.
96 * @param dir name of this directory
97 */
57 void setDirectory(const std::string &dir);
98 void setDirectory(const std::string &dir);
99
100 /**
101 * Gets name of this directory.
102 * @return name of this directory
103 */
58 const std::string &directory() const;
59
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 */
60 std::ostream *create(const std::string &name, bool binary = false);
116 std::ostream *create(const std::string &name, bool binary = false);
61 std::ostream *find(const std::string &name);
62
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 */
63 static bool isFile(const std::ostream *os);
139 static bool isFile(const std::ostream *os);
64 static inline bool isFile(const std::ostream &os) { return isFile(&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);
65};
66
67extern OutputDirectory simout;
68
69#endif // __BASE_OUTPUT_HH__
180};
181
182extern OutputDirectory simout;
183
184#endif // __BASE_OUTPUT_HH__