PSCF v1.1
ioUtil.cpp
1/*
2* Util Package - C++ Utilities for Scientific Computation
3*
4* Copyright 2010 - 2017, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include <util/global.h>
9#include "ioUtil.h"
10#include "Log.h"
11
12namespace Util
13{
14
15 /*
16 * Strip trailing whitespace from a string.
17 */
18 int rStrip(std::string& str)
19 {
20 size_t found;
21 std::string whitespaces(" \t\n\r");
22 found = str.find_last_not_of(whitespaces);
23 if (found != std::string::npos) {
24 str.erase(found + 1);
25 return int(found + 1);
26 } else {
27 str.clear();
28 return 0;
29 }
30 }
31
32 /*
33 * Read string, and compare to expected value.
34 *
35 * Throw Exception if input value differs from expected value.
36 */
37 void checkString(std::istream& in, const std::string& expected)
38 {
39 std::string actual;
40 in >> actual;
41 if (actual != expected) {
42 Log::file() << "Error in checkString" << std::endl;
43 Log::file() << "Expected: " << expected << std::endl;
44 Log::file() << "Scanned: " << actual << std::endl;
45 UTIL_THROW("Incorrect string");
46 };
47 }
48
49 /*
50 * Return std::string representation of an integer.
51 */
52 std::string toString(int n)
53 {
54 std::stringstream ss;
55 ss << n;
56 return ss.str();
57 }
58
59 /*
60 * Transfer the next line of input to a stringstream
61 */
62 bool getLine(std::istream& in, std::stringstream& line)
63 {
64 std::string string;
65 line.str(string);
66 line.clear();
67 if (!in.eof()) {
68 getline(in, string);
69 line.str(string);
70 return true;
71 } else {
72 return false;
73 }
74 }
75
76 /*
77 * Get the next non-blank line of input, strip trailing whitespace.
78 */
79 bool getNextLine(std::istream& in, std::string& line)
80 {
81 while (true) {
82 if (!in.eof()) {
83 getline(in, line);
84 rStrip(line);
85 if (!line.empty()) {
86 // Return true indicates eof not reached
87 return true;
88 }
89 } else {
90 line.clear();
91 // Return false indicates eof was reached
92 return false;
93 }
94 }
95 }
96
97 /*
98 * Assign the next non-blank line of input to a stringstream
99 */
100 bool getNextLine(std::istream& in, std::stringstream& line)
101 {
102 std::string string;
103 line.str(string);
104 line.clear();
105 while (true) {
106 if (!in.eof()) {
107 getline(in, string);
108 rStrip(string);
109 if (!string.empty()) {
110 line.str(string);
111 // Return true indicates eof not reached
112 return true;
113 }
114 } else {
115 // Return false indicates eof reached
116 return false;
117 }
118 }
119 }
120
121 /*
122 * Check status of an istream before required read.
123 */
124 void checkRequiredIstream(std::istream& in)
125 {
126 if (!in.good()) {
127 if (in.eof()) {
128 if (in.fail()) {
129 if (in.bad()) {
130 UTIL_THROW("Error: istream eof and badbit");
131 } else {
132 UTIL_THROW("Error: istream eof and failbit");
133 }
134 } else {
135 UTIL_THROW("Error: istream eof");
136 }
137 }
138 if (in.fail()) {
139 if (in.bad()) {
140 UTIL_THROW("Error: istream badbit");
141 } else {
142 UTIL_THROW("Error: istream failbit");
143 }
144 }
145 }
146 }
147}
static std::ostream & file()
Get log ostream by reference.
Definition: Log.cpp:57
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
void checkString(std::istream &in, const std::string &expected)
Extract string from stream, and compare to expected value.
Definition: ioUtil.cpp:37
bool getNextLine(std::istream &in, std::string &line)
Read the next non-empty line into a string, strip trailing whitespace.
Definition: ioUtil.cpp:79
std::string toString(int n)
Return string representation of an integer.
Definition: ioUtil.cpp:52
int rStrip(std::string &str)
Strip trailing whitespace from a string.
Definition: ioUtil.cpp:18
bool getLine(std::istream &in, std::stringstream &line)
Read the next line into a stringstream.
Definition: ioUtil.cpp:62
Utility classes for scientific computation.
Definition: accumulators.mod:1
void checkRequiredIstream(std::istream &in)
Check status of a std::istream just before reading required variable.
Definition: ioUtil.cpp:124