forked from FreeFem/FreeFem-sources
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlotStream.hpp
257 lines (223 loc) · 9.35 KB
/
PlotStream.hpp
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* PlotStream.hpp
*
* Created by Frederic Hecht on 27/10/08.
* Copyright 2008 UPMC.
*
*/
#include <cstdio>
#ifdef _WIN32
#include <fcntl.h>
#endif
#include "endian.hpp"
//FFCS visualization stream redirection
#include "ffapi.hpp"
using Fem2D::Mesh;
using Fem2D::Mesh3;
namespace Fem2D {
}
class PlotStream
{
public:
FILE * TheStream;
PlotStream(FILE *Stream) :TheStream(Stream) { }
operator bool() const { return TheStream;}
// datatype mush be < 0 to have no collistion with arg number.
// FFCS: <<PlotStream::datatype>>
enum datatype { dt_meshes=-1,dt_plots=-2,dt_endplot=-3,dt_meshes3=-10,dt_plots3=-11,dt_endarg=99999,dt_newplot=-5 };
//FFCS:need to send control data to FFCS at least once per plot
void SendNewPlot() { ffapi::newplot();write((long )dt_newplot); set_binary_mode(); }
void SendEndArgPlot() {write((long )dt_endarg); }
//FFCS:redirect visualization stream
void SendEndPlot() { write((long )dt_endplot);ffapi::ff_fflush(TheStream); set_text_mode() ;}
void SendPlots() { write((long )dt_plots); }
void SendMeshes() { write((long )dt_meshes);}
void SendMeshes3() { write((long )dt_meshes3);}
//FFCS: divert stream to FFCS
void write(const void *data,size_t l) {ffapi::ff_fwrite(data,1,l,TheStream);}
PlotStream& write(const bool& bb) {bool b=w_endian(bb);write(reinterpret_cast<const void *> (&b),sizeof(bool));return *this;}
PlotStream& write(const long long& bb) {long long b=w_endian(bb);write(reinterpret_cast<const void *> (&b),sizeof(long long));return *this;}
PlotStream& write(const long& bb) { // always write 8 bits for a long FH.
long long ll=bb;ll=w_endian(ll);write(reinterpret_cast<const void *> (&ll),sizeof(long long));
return *this;}
PlotStream& write(const int& bb) {int b=w_endian(bb);write(reinterpret_cast<const void *> (&b),sizeof(int));return *this;}
PlotStream& write(const double& bb) {double b=w_endian(bb);write(reinterpret_cast<const void *> (&b),sizeof(double));return *this;}
PlotStream& write(const complex<double>& bb) {return write(bb.real()),write(bb.imag());}
PlotStream &write(const Fem2D::R1 & P) { return write(P.x);}
PlotStream &write(const Fem2D::R2 & P) { return write(P.x),write(P.y);}
PlotStream &write(const Fem2D::R3 & P) { return write(P.x),write(P.y),write(P.z);}
PlotStream& write(const string& b) {
int l=b.size();
write(l);
write(b.data(),l);
return *this;
}
void set_text_mode()
{
//FFCS:visualization stream redirection
ffapi::wintextmode(TheStream);
}
void set_binary_mode()
{
//FFCS:visualization stream redirection
ffapi::winbinmode(TheStream);
}
PlotStream & operator << (const bool& b) { return write(b); }
PlotStream & operator << (const long& b) { return write(b); }
PlotStream & operator << (const long long & b) { return write(b); }
PlotStream & operator << (const int& b) { return write(b); }
PlotStream & operator << (const double& b) { return write(b); }
PlotStream & operator << (const complex<double>& b) { return write(b); }
PlotStream & operator << (const string& s) { return write(s); }
PlotStream & operator << (const string* s) { ffassert(s); return write(*s); }
template<class T>
PlotStream & operator << (const KN_<T>& b)
{
long n=b.N();
write(n);
// cout << "PlotStream :<< n " << n << endl;
for (int i=0;i<n;++i)
write(b[i]);
return *this;
}
PlotStream & operator << (const Mesh& Th) {
/*
Serialize s=Th.serialize();
long n=s.size();
write( n );
write(s,s.size());*/
GSave2(TheStream , Th) ;
return *this;}
PlotStream & operator << (const Fem2D::Mesh2& Th) { Th.GSave(TheStream); return *this;}
PlotStream & operator << (const Fem2D::Mesh3& Th) { Th.GSave(TheStream); return *this;}
void read( void *data,size_t l) {
char * p= (char*)data;
for(int i=0;i<l;++i)
*p++ = (char) getc(TheStream);
// fread( p++,1,1,TheStream);
// read(data,l);
}
//FFCS:visualization stream redirection
bool good() const {return ffapi::ff_ferror(TheStream)==0;}
void GetNewPlot() { get(dt_newplot) ; set_binary_mode();}
void GetEndArgPlot() {get(dt_endarg); }
void GetEndPlot() {get(dt_endplot); set_text_mode();}
void GetPlots() { get(dt_plots); }
void GetMeshes() { get(dt_meshes);}
bool GetMeshes3() { long tt; read(tt);
if(tt== dt_meshes3) return true;
else if (tt== dt_plots) return false;
cout << " Error Check : get " << tt << " == wait for "<< dt_meshes3 << " or "<< dt_plots << endl;
ffassert(0);
}
void get(datatype t) { long tt; read(tt);
if( tt !=(long) t)
cout << " Error Check : get " << tt << " == wait for "<< t << endl;
ffassert(tt==(long) t);}
//FFCS:visualization stream redirection
bool eof() {return ffapi::ff_feof(TheStream);}
PlotStream& read( bool& b) {read(reinterpret_cast< void *> (&b),sizeof(bool)); b=r_endian(b);return *this;}
PlotStream& read( long long& b) {read(reinterpret_cast< void *> (&b),sizeof(long long)); b=r_endian(b);return *this;}
PlotStream& read( long& b) { long long l;
read(reinterpret_cast< void *> (&l),sizeof(long long));
l=r_endian(l);
b=(long) l;
if(( b-(long) l) !=0)
{ cout << " err err read long : error " << b << " != " << l << endl;
assert( (b-(long) l)==0);}
return *this;}
PlotStream& read( int& b) {read(reinterpret_cast< void *> (&b),sizeof(int)); b=r_endian(b);return *this;}
PlotStream& read( double& b) {read(reinterpret_cast< void *> (&b),sizeof(double)); b=r_endian(b);return *this;}
PlotStream &read( complex<double> &C) { double re,im; read(re); read(im); C=complex<double>(re,im); return
*this;}
PlotStream &read( Fem2D::R1 & P) { return read(P.x);}
PlotStream &read( Fem2D::R2 & P) { return read(P.x),read(P.y);}
PlotStream &read( Fem2D::R3 & P) { return read(P.x),read(P.y),read(P.z);}
PlotStream& read( string& b) {
int l;
read(l);
b.resize(l);
read(& (b[0]),l);
return *this;
}
PlotStream & operator >> ( bool& b) { return read(b); }
PlotStream & operator >> ( long& b) { return read(b); }
PlotStream & operator >> ( long long& b) { return read(b); }
PlotStream & operator >> ( int& b) { return read(b); }
PlotStream & operator >> ( double& b) { return read(b); }
PlotStream & operator >> ( complex<double>& b) { return read(b); }
PlotStream & operator >> ( string& s) { return read(s); }
PlotStream & operator >> ( string *& s)
{ if(!s) s= new string(); return read(*s);
// cout << " fread string " << s <<endl;
}
PlotStream & operator >> ( Mesh *& Th)
{
long n;
read(n);
Serialize s(n,Mesh::magicmesh);
read(s,n );
Th= new Mesh(s);
return *this;
}
template<class T>
PlotStream & operator >> ( KN<T>& b)
{
long n;
read(n);
// cout << "PlotStream >> : n " << n << endl;
// read empty array .... (n ==0)
if( ! b.N() && n) b.init(n);// if n ==0 do nothing .. Add FH nov. 2016
ffassert( b.N()==n);
for (int i=0;i<n;++i)
read(b[i]);
return *this;
}
// PlotStream & operator << (const Mesh3& Th);
// PlotStream & operator >> ( Mesh3 *& Th);
PlotStream & operator >> ( Fem2D::Mesh3 *& Th) { Th= new Fem2D::Mesh3(TheStream); return *this;}
PlotStream & operator >> ( Fem2D::Mesh2 *& Th) { Th= new Fem2D::Mesh2(TheStream); return *this;}
// --- I also write the type .. to skip data if we need to skip data
// just change >> and << by : <= and >=
PlotStream & operator <= (const bool& b) { return write(1),write(b); }
PlotStream & operator <= (const long& b) { return write(2),write(b); }
PlotStream & operator <= (const long long & b) { return write(3),write(b); }
PlotStream & operator <= (const int& b) { return write(4),write(b); }
PlotStream & operator <= (const double& b) { return write(5),write(b); }
PlotStream & operator <= (const string& s) { return write(6),write(s); }
PlotStream & operator <= (const string* s) { return write(6),write(*s); }
template<class T>
PlotStream & operator <= (const KN_<T>& b) { return write(10),write((int) sizeof(T)),operator<<(b);}
PlotStream & operator >= ( bool& b) { return readc(1)>>b; }
PlotStream & operator >= ( long& b) { return readc(2)>>b; }
PlotStream & operator >= ( long long & b) {return readc(3)>>b; }
PlotStream & operator >= ( int& b) { return readc(4)>>b; }
PlotStream & operator >= ( double& b) { return readc(5)>>b; }
PlotStream & operator >= ( string& s) { return readc(6)>>s; }
PlotStream & operator >= ( string*& s) { return readc(6)>>s; }
template<class T>
PlotStream & operator >= ( KN<T>& b) { return readc(10), readc(sizeof(T)), operator>>(b);}
PlotStream & readc(int cc) { int c; read(c); assert(c==cc); return *this;}
void SkipData() { int c; read(c);
bool b;
int i;
long l,n;
long long ll;
string s;
double d;
char buf[100];
switch (c) {
case 1: read(b);break;
case 2: read(l);break;
case 3: read(ll);break;
case 4: read(i);break;
case 5: read(d);break;
case 6: read(s);break;
case 10: read(l); assert(l>0 && l <100);
read(n);
for(int i=0;i<n;++n) read(buf,l);
default:
break;
}
}
};