Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F79878254
gzfstream.hh
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Wed, Aug 28, 18:27
Size
6 KB
Mime Type
text/x-c++
Expires
Fri, Aug 30, 18:27 (2 d)
Engine
blob
Format
Raw Data
Handle
20250178
Attached To
rTAMAAS tamaas
gzfstream.hh
View Options
/**
*
* @author Guillaume Anciaux <guillaume.anciaux@epfl.ch>
*
* @section LICENSE
*
* Copyright (©) 2016 EPFL (Ecole Polytechnique Fédérale de
* Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des
* Solides)
*
* Tamaas is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* Tamaas 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 Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Tamaas. If not, see <http://www.gnu.org/licenses/>.
*
*/
/* -------------------------------------------------------------------------- */
#ifndef __GZFSTREAM_HH__
#define __GZFSTREAM_HH__
/* -------------------------------------------------------------------------- */
#include <string>
#include <sstream>
#include <zlib.h>
/* -------------------------------------------------------------------------- */
__BEGIN_TAMAAS__
class
GZfstream
{
/* ------------------------------------------------------------------------ */
/* Constructors/Destructors */
/* ------------------------------------------------------------------------ */
public
:
inline
GZfstream
();
inline
virtual
~
GZfstream
();
/* ------------------------------------------------------------------------ */
/* Methods */
/* ------------------------------------------------------------------------ */
public
:
inline
void
open
(
const
std
::
string
&
filename
,
std
::
ios_base
::
openmode
mode
);
inline
void
flush
();
inline
void
precision
(
int
i
);
inline
void
getline
(
std
::
string
&
str
);
template
<
typename
T
>
inline
GZfstream
&
operator
<<
(
const
T
&
value
);
template
<
typename
T
>
inline
GZfstream
&
operator
>>
(
T
&
value
);
inline
GZfstream
&
operator
<<
(
std
::
ostream
&
(
*
op
)(
std
::
ostream
&
));
bool
&
getFlagBinary
(){
return
flag_binary
;};
/* ------------------------------------------------------------------------ */
/* Accessors */
/* ------------------------------------------------------------------------ */
public
:
/* ------------------------------------------------------------------------ */
/* Class Members */
/* ------------------------------------------------------------------------ */
private
:
bool
flag_binary
;
gzFile
gzfile
;
std
::
stringstream
buffer
;
bool
opened
;
};
/* -------------------------------------------------------------------------- */
inline
GZfstream
::
GZfstream
(){
opened
=
false
;
flag_binary
=
false
;
}
/* -------------------------------------------------------------------------- */
inline
void
GZfstream
::
flush
(){
gzwrite
(
gzfile
,
buffer
.
str
().
c_str
(),
buffer
.
str
().
length
());
// cout << "flushing " << buffer.str() << endl;
buffer
.
str
(
""
);
}
/* -------------------------------------------------------------------------- */
inline
GZfstream
::~
GZfstream
(){
if
(
opened
){
flush
();
gzclose
(
gzfile
);
}
}
/* -------------------------------------------------------------------------- */
inline
void
GZfstream
::
precision
(
int
p
){
buffer
.
precision
(
p
);
}
/* -------------------------------------------------------------------------- */
inline
void
GZfstream
::
open
(
const
std
::
string
&
filename
,
std
::
ios_base
::
openmode
mode
){
std
::
string
_mode
=
""
;
if
(
mode
&
std
::
ios
::
app
)
_mode
+=
"a+"
;
if
(
mode
&
std
::
ios
::
ate
)
_mode
+=
"a+"
;
if
(
mode
&
std
::
ios
::
in
)
_mode
+=
"r"
;
if
(
mode
&
std
::
ios
::
out
)
_mode
+=
"w+"
;
if
(
mode
&
std
::
ios
::
trunc
)
_mode
+=
"w"
;
// string fn = filename + ".gz";
std
::
string
fn
=
filename
;
std
::
cout
<<
"opening "
<<
fn
<<
std
::
endl
;
gzfile
=
NULL
;
gzfile
=
gzopen
(
fn
.
c_str
(),
_mode
.
c_str
());
if
(
gzfile
)
opened
=
true
;
}
/* -------------------------------------------------------------------------- */
template
<
typename
T
>
inline
GZfstream
&
GZfstream
::
operator
<<
(
const
T
&
value
){
buffer
<<
value
;
if
(
buffer
.
str
().
length
()
>
1024
)
flush
();
return
(
*
this
);
}
/* -------------------------------------------------------------------------- */
template
<>
inline
GZfstream
&
GZfstream
::
operator
<<
(
const
Real
&
value
){
if
(
flag_binary
){
gzwrite
(
gzfile
,
&
value
,
sizeof
(
Real
));
}
else
{
buffer
<<
value
;
if
(
buffer
.
str
().
length
()
>
1024
)
flush
();
}
return
(
*
this
);
}
/* -------------------------------------------------------------------------- */
template
<
typename
T
>
inline
GZfstream
&
GZfstream
::
operator
>>
(
T
&
value
){
std
::
cout
<<
"not working"
<<
std
::
endl
;
exit
(
EXIT_FAILURE
);
return
(
*
this
);
}
/* -------------------------------------------------------------------------- */
template
<>
inline
GZfstream
&
GZfstream
::
operator
>>
(
Real
&
value
){
if
(
flag_binary
){
int
ret
=
gzread
(
gzfile
,
&
value
,
sizeof
(
Real
));
if
(
ret
==
0
){
std
::
cout
<<
"end of file"
<<
std
::
endl
;
exit
(
EXIT_FAILURE
);
}
if
(
ret
==
-
1
){
std
::
cout
<<
"error"
<<
std
::
endl
;
exit
(
EXIT_FAILURE
);
}
}
return
(
*
this
);
}
/* -------------------------------------------------------------------------- */
inline
GZfstream
&
GZfstream
::
operator
<<
(
std
::
ostream
&
(
*
op
)(
std
::
ostream
&
)){
(
*
op
)(
buffer
);
flush
();
return
(
*
this
);
}
/* -------------------------------------------------------------------------- */
inline
void
GZfstream
::
getline
(
std
::
string
&
str
){
if
(
!
opened
)
{
std
::
cout
<<
"file not opened"
<<
std
::
endl
;
exit
(
EXIT_FAILURE
);
}
int
len
=
512
;
char
buf
[
len
];
char
*
read
=
gzgets
(
gzfile
,
buf
,
len
);
if
(
read
){
str
=
buf
;
}
else
{
std
::
cout
<<
"could not read the content"
<<
std
::
endl
;
exit
(
EXIT_FAILURE
);
}
}
/* -------------------------------------------------------------------------- */
void
getline
(
GZfstream
&
file
,
std
::
string
&
str
){
file
.
getline
(
str
);
}
/* -------------------------------------------------------------------------- */
__END_TAMAAS__
#endif
/* __GZFSTREAM_HH__ */
Event Timeline
Log In to Comment