IO in binary mode

This commit is contained in:
Manuel Caroli 2009-05-06 09:07:10 +00:00
parent ac1b06a265
commit ed87889d39
3 changed files with 81 additions and 60 deletions

View File

@ -118,15 +118,26 @@ inline Point_3<K> operator+(const Point_3<K> &p, const Periodic_3_offset_3 &off)
inline std::ostream
&operator<<(std::ostream &os, const Periodic_3_offset_3 &off) {
return os << off.x() << " "
<< off.y() << " "
<< off.z();
if (is_ascii(os))
os << off.x() << " " << off.y() << " " << off.z();
else {
write(os,off.x());
write(os,off.y());
write(os,off.z());
}
return os;
}
inline std::istream
&operator>>(std::istream &is, Periodic_3_offset_3 &off) {
int x,y,z;
is >> x >> y >> z;
if (is_ascii(is))
is >> x >> y >> z;
else {
read(is,x);
read(is,y);
read(is,z);
}
off = Periodic_3_offset_3(x,y,z);
return is;
}

View File

@ -3308,12 +3308,22 @@ operator>> (std::istream& is, Periodic_3_triangulation_3<GT,TDS> &tr)
tr.clear();
int n, cx, cy, cz;
unsigned int n;
int cx, cy, cz;
Iso_cuboid domain;
is >> n;
is >> domain;
is >> cx >> cy >> cz;
if (is_ascii(is)) {
is >> n;
is >> domain;
is >> cx >> cy >> cz;
}
else {
read(is,n);
is >> domain;
read(is,cx);
read(is,cy);
read(is,cz);
}
CGAL_assertion((n/(cx*cy*cz))*cx*cy*cz == n);
@ -3327,7 +3337,7 @@ operator>> (std::istream& is, Periodic_3_triangulation_3<GT,TDS> &tr)
std::map< int, Vertex_handle > V;
if (cx==1 && cy==1 && cz==1) {
for (int i=0; i < n; i++) {
for (unsigned int i=0; i < n; i++) {
V[i] = tr.tds().create_vertex();
is >> *V[i];
}
@ -3335,7 +3345,7 @@ operator>> (std::istream& is, Periodic_3_triangulation_3<GT,TDS> &tr)
Vertex_handle v,w;
std::vector<Vertex_handle> vv;
Offset off;
for (int i=0; i < n; i++) {
for (unsigned int i=0; i < n; i++) {
v = tr.tds().create_vertex();
V[i] = v;
is >> *V[i] >> off;
@ -3359,7 +3369,14 @@ operator>> (std::istream& is, Periodic_3_triangulation_3<GT,TDS> &tr)
// read offsets
int off[4];
for (int j=0 ; j < m; j++) {
is >> off[0] >> off[1] >> off[2] >> off[3];
if (is_ascii(is))
is >> off[0] >> off[1] >> off[2] >> off[3];
else {
read(is,off[0]);
read(is,off[1]);
read(is,off[2]);
read(is,off[3]);
}
C[j]->set_offsets(off[0],off[1],off[2],off[3]);
}
@ -3388,7 +3405,6 @@ operator>> (std::istream& is, Periodic_3_triangulation_3<GT,TDS> &tr)
template < class GT, class TDS >
std::ostream &
operator<< (std::ostream& os,const Periodic_3_triangulation_3<GT,TDS> &tr)
// TODO: new version into the doc
// writes :
// the number of vertices
// the domain as six coordinates: xmin ymin zmin xmax ymax zmax
@ -3424,9 +3440,11 @@ operator<< (std::ostream& os,const Periodic_3_triangulation_3<GT,TDS> &tr)
<< domain << std::endl
<< cover[0] << " " << cover[1] << " " << cover[2] << std::endl;
else {
os << n*cover[0]*cover[1]*cover[2]
<< domain
<< cover[0] << cover[1] << cover[2];
write(os,n*cover[0]*cover[1]*cover[2]);
os << domain;
write(os,cover[0]);
write(os,cover[1]);
write(os,cover[2]);
}
if (n == 0)
@ -3479,13 +3497,14 @@ operator<< (std::ostream& os,const Periodic_3_triangulation_3<GT,TDS> &tr)
//Cell_handle ch = std::find(tr.cells_begin(), tr.cells_end(), i);
Cell_handle ch(it);
for (int j=0; j<4; j++) {
os << ch->offset(j);
if(is_ascii(os)) {
os << ch->offset(j);
if ( j==3 )
os << std::endl;
else
os << ' ';
}
else write(os,ch->offset(j));
}
}

View File

@ -21,6 +21,7 @@
#include <cassert>
#include <iostream>
#include <fstream>
#include <sstream>
#include <list>
#include <vector>
@ -577,25 +578,19 @@ _test_cls_periodic_3_triangulation_3(const PeriodicTriangulation &T,
assert(PT3.mirror_facet(Facet(ch,0)).first == ch->neighbor(0));
std::cout << "I/O" << std::endl;
// TODO: write to string streams instead of file streams
std::cout << " ascii" << std::endl;
std::ofstream os1("data/test_PT1.tri");
std::ofstream os3("data/test_PT3.tri");
os1 << PT1;
os3 << PT3;
std::ifstream is1("data/test_PT1.tri");
std::ifstream is3("data/test_PT3.tri");
std::stringstream ss1;
std::stringstream ss3;
ss1 << PT1;
ss3 << PT3;
P3T3 PT1r, PT3r;
is1 >> PT1r;
is3 >> PT3r;
assert(CGAL::is_ascii(os1));
assert(CGAL::is_ascii(os3));
assert(CGAL::is_ascii(is1));
assert(CGAL::is_ascii(is3));
ss1 >> PT1r;
ss3 >> PT3r;
assert(CGAL::is_ascii(ss1));
assert(CGAL::is_ascii(ss3));
if (!ex) assert(PT1 == PT1r);
if (!ex) assert(PT3 == PT3r);
@ -603,39 +598,35 @@ _test_cls_periodic_3_triangulation_3(const PeriodicTriangulation &T,
PT1r.clear();
PT3r.clear();
std::ofstream os1b("data/test_PT1.btr");
std::ofstream os3b("data/test_PT3.btr");
CGAL::set_binary_mode(os1b);
CGAL::set_binary_mode(os3b);
os1b << PT1;
os3b << PT3;
std::ifstream is1b("data/test_PT1.btr");
std::ifstream is3b("data/test_PT3.btr");
CGAL::set_binary_mode(is1b);
CGAL::set_binary_mode(is3b);
// TODO: fix the bug
// is1b >> PT1r;
//is3b >> PT3r;
assert(CGAL::is_binary(os1b));
assert(CGAL::is_binary(os3b));
assert(CGAL::is_binary(is1b));
assert(CGAL::is_binary(is3b));
// assert(PT1 == PT1r);
//6 87 25 66 81 assert(PT3 == PT3r);
// There are problems with the IO of exact number types in binary mode.
if (!ex) {
std::stringstream ss1b;
std::stringstream ss3b;
CGAL::set_binary_mode(ss1b);
CGAL::set_binary_mode(ss3b);
ss1b << PT1;
ss3b << PT3;
ss1b >> PT1r;
ss3b >> PT3r;
assert(CGAL::is_binary(ss1b));
assert(CGAL::is_binary(ss3b));
assert(PT1 == PT1r);
assert(PT3 == PT3r);
}
std::cout << " pretty" << std::endl;
PT1r.clear();
PT3r.clear();
std::ofstream os1p("data/test_PT1.ptr");
std::ofstream os3p("data/test_PT3.ptr");
CGAL::set_pretty_mode(os1p);
CGAL::set_pretty_mode(os3p);
os1p << PT1;
os3p << PT3;
std::stringstream ss1p;
std::stringstream ss3p;
CGAL::set_pretty_mode(ss1p);
CGAL::set_pretty_mode(ss3p);
ss1p << PT1;
ss3p << PT3;
assert(CGAL::is_pretty(os1p));
assert(CGAL::is_pretty(os3p));
assert(CGAL::is_pretty(ss1p));
assert(CGAL::is_pretty(ss3p));
}