HPCToolkit
IOUtil.cpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //****************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 // Author:
59 // Nathan Tallent
60 //
61 //****************************************************************************
62 
63 //************************** System Include Files ****************************
64 
65 #include <iostream>
66 #include <fstream>
67 
68 //*************************** User Include Files *****************************
69 
70 #include "IOUtil.hpp"
71 #include "diagnostics.h"
72 
73 //************************** Forward Declarations ****************************
74 
75 //****************************************************************************
76 
77 //****************************************************************************
78 // IOUtil
79 //****************************************************************************
80 
81 namespace IOUtil {
82 
83 
84 std::istream*
85 OpenIStream(const char* filenm)
86 {
87  if (!filenm || filenm[0] == '\0') {
88  // Use cin
89  return &std::cin;
90  }
91  else {
92  std::ifstream* ifs = new std::ifstream;
93  try {
94  OpenIFile(*ifs, filenm);
95  return ifs;
96  }
97  catch (const Diagnostics::Exception& /*ex*/) {
98  delete ifs;
99  throw;
100  }
101  }
102 }
103 
104 
105 std::ostream*
106 OpenOStream(const char* filenm)
107 {
108  if (!filenm || filenm[0] == '\0') {
109  // Use cout
110  return &std::cout;
111  }
112  else {
113  std::ofstream* ofs = new std::ofstream;
114  try {
115  OpenOFile(*ofs, filenm);
116  return ofs;
117  }
118  catch (Diagnostics::Exception& /*ex*/) {
119  delete ofs;
120  throw;
121  }
122  }
123 }
124 
125 
126 void
127 CloseStream(std::istream* s)
128 {
129  if (s != &std::cin) {
130  delete s;
131  }
132 }
133 
134 
135 void
136 CloseStream(std::ostream* s)
137 {
138  if (s != &std::cout) {
139  delete s;
140  }
141 }
142 
143 
144 void
145 CloseStream(std::iostream* s)
146 {
147  if (s != &std::cin && s != &std::cout) {
148  delete s;
149  }
150 }
151 
152 
153 void
154 OpenIFile(std::ifstream& fs, const char* filenm)
155 {
156  using namespace std;
157 
158  fs.open(filenm, ios::in);
159  if (!fs.is_open() || fs.fail()) {
160  DIAG_Throw("Cannot open file '" << filenm << "'");
161  }
162 }
163 
164 
165 void
166 OpenOFile(std::ofstream& fs, const char* filenm)
167 {
168  using namespace std;
169 
170  fs.open(filenm, ios::out | ios::trunc);
171  if (!fs.is_open() || fs.fail()) {
172  DIAG_Throw("Cannot open file '" << filenm << "'");
173  }
174 }
175 
176 
177 void
178 CloseFile(std::fstream& fs)
179 {
180  fs.close();
181 }
182 
183 
184 std::string
185 Get(std::istream& is, char end)
186 {
187  static const int bufSz = 256;
188  static char buf[bufSz];
189  std::string str;
190 
191  while ( (!is.eof() && !is.fail()) && is.peek() != end) {
192  is.get(buf, bufSz, end);
193  str += buf;
194  }
195 
196  return str;
197 }
198 
199 
200 std::string
201 GetLine(std::istream& is, char end)
202 {
203  std::string str = Get(is, end);
204  char c; is.get(c); // eat up 'end'
205  return str;
206 }
207 
208 
209 bool
210 Skip(std::istream& is, const char* s)
211 {
212  DIAG_Assert(s, DIAG_UnexpectedInput);
213  char c;
214  for (int i = 0; s[i] != '\0'; i++) {
215  is.get(c);
216  if (c != s[i]) { return false; }
217  }
218  return true;
219 }
220 
221 
222 //****************************************************************************
223 
224 } // end of IOUtil namespace
225 
void CloseStream(std::istream *s)
Definition: IOUtil.cpp:127
std::istream * OpenIStream(const char *filenm)
Definition: IOUtil.cpp:85
Definition: fmt.c:108
std::ostream * OpenOStream(const char *filenm)
Definition: IOUtil.cpp:106
void OpenOFile(std::ofstream &fs, const char *filenm)
Definition: IOUtil.cpp:166
bool Skip(std::istream &is, const char *s)
Definition: IOUtil.cpp:210
static char buf[32]
Definition: StrUtil.cpp:240
std::string GetLine(std::istream &is, char end)
Definition: IOUtil.cpp:201
std::string Get(std::istream &is, char end)
Definition: IOUtil.cpp:185
void CloseFile(std::fstream &fs)
Definition: IOUtil.cpp:178
const char * DIAG_UnexpectedInput
void OpenIFile(std::ifstream &fs, const char *filenm)
Definition: IOUtil.cpp:154