-
Notifications
You must be signed in to change notification settings - Fork 1
/
streamIO.h
100 lines (87 loc) · 3.12 KB
/
streamIO.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/// @file streamIO.h
///
/// Functions for input/output operations on file streams.
///
//*****************************************************************************
//
// (c) J. Blazek, CFD Consulting & Analysis, www.cfd-ca.de
// Created February 15, 2014
// Last modification: June 30, 2014
//
//=============================================================================
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
//*****************************************************************************
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <string>
using std::ifstream;
using std::ofstream;
using std::runtime_error;
using std::string;
/// Reads a text line from file, strips any comment, leading and trailing spaces.
/// It is assumed that a comment starts with the hash (#) sign.
///
/// @param stream file stream
/// @return processed line as a string (might be empty!)
/// @exception std::runtime_error problem with the stream
///
inline string ReadLine( ifstream &stream )
{
string str;
string::size_type ic;
if (stream.good())
{
getline( stream,str );
// get rid of comment
ic = str.find_first_of( '#' );
if (ic != string::npos) str.erase( str.begin()+ic,str.end() );
// get rid of leading spaces
ic = str.find_first_not_of( ' ' );
if ((int)ic > 0) str.erase( str.begin(),str.begin()+ic );
// get rid of trailing spaces
ic = str.find_last_not_of( ' ' );
if (ic != string::npos) str.erase( str.begin()+ic+1,str.end() );
}
else throw runtime_error( "could not read line from file." );
return str;
}
//*****************************************************************************
/// Reads the next value from a binary file.
///
/// @param stream file stream
/// @param val value in the file
/// @exception std::runtime_error problem with the stream
///
template <class T> void ReadBinary( ifstream &stream, T &val )
{
if (stream.good())
stream.read( (char*) &val,sizeof(T) );
else
throw runtime_error( "could not read data from file." );
}
//*****************************************************************************
/// Writes given value to a binary file.
///
/// @param stream file stream
/// @param val value to be written
///
template <class T> void WriteBinary( ofstream &stream, const T &val )
{
stream.write( (char*) &val,sizeof(T) );
}