Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F69271255
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
Mon, Jul 1, 01:11
Size
5 KB
Mime Type
text/x-c++
Expires
Wed, Jul 3, 01:11 (2 d)
Engine
blob
Format
Raw Data
Handle
18690467
Attached To
rTAMAAS tamaas
gzfstream.hh
View Options
#ifndef __GZFSTREAM_HH__
#define __GZFSTREAM_HH__
/* -------------------------------------------------------------------------- */
#include <string>
#include <sstream>
#include <zlib.h>
/* -------------------------------------------------------------------------- */
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
);
}
/* -------------------------------------------------------------------------- */
#endif
/* __GZFSTREAM_HH__ */
Event Timeline
Log In to Comment