libMesh
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | List of all members
libMesh::Xdr Class Reference

This class implements a C++ interface to the XDR (eXternal Data Representation) format. More...

#include <xdr_cxx.h>

Public Member Functions

 Xdr (std::string name="", const XdrMODE m=UNKNOWN)
 File-based constructor.
 
 Xdr (std::ostream &stream)
 Output stream based constructor.
 
 Xdr (std::istream &stream)
 Input stream based constructor.
 
 ~Xdr ()
 Destructor.
 
void open (std::string name)
 Opens the file.
 
void close ()
 Closes the file if it is open.
 
bool is_open () const
 
bool is_eof ()
 
bool reading () const
 
bool writing () const
 
XdrMODE access_mode () const
 
template<typename T >
void data (T &a, std::string_view comment="")
 Inputs or outputs a single value.
 
template<typename T >
Xdroperator<< (T &a)
 Same, but provides an ostream like interface.
 
template<typename T >
Xdroperator>> (T &a)
 Same, but provides an istream like interface.
 
template<typename T >
void data_stream (T *val, const unsigned int len, const unsigned int line_break=libMesh::invalid_uint)
 Inputs or outputs a raw data stream.
 
void comment (std::string &)
 Writes or reads (ignores) a comment line.
 
void set_version (int ver)
 Sets the version of the file that is being read.
 
int version () const
 Gets the version of the file that is being read.
 
template<>
void do_read (std::string &a)
 
template<>
void data_stream (double *val, const unsigned int len, const unsigned int line_break)
 
template<>
void data_stream (float *val, const unsigned int len, const unsigned int line_break)
 
template<>
void data_stream (long double *val, const unsigned int len, const unsigned int line_break)
 
template<>
void data_stream (Real *val, const unsigned int len, const unsigned int line_break)
 
template<typename XFP >
void _xfp_data_stream (XFP *val, const unsigned int len, #ifdef LIBMESH_HAVE_XDR xdrproc_t xdr_proc, #else void *, #endif const unsigned int line_break, const int n_digits)
 
template<>
void data_stream (std::complex< double > *val, const unsigned int len, const unsigned int line_break)
 
template<>
void data_stream (std::complex< long double > *val, const unsigned int len, const unsigned int line_break)
 

Private Member Functions

template<typename T >
void do_read (T &a)
 Helper method for reading different data types.
 
template<typename T >
void do_read (std::complex< T > &a)
 
template<typename T >
void do_read (std::vector< T > &a)
 
template<typename T >
void do_read (std::vector< std::complex< T > > &a)
 
template<typename T >
void do_write (T &a)
 Helper method for writing different data types.
 
template<typename T >
void do_write (std::complex< T > &a)
 
template<typename T >
void do_write (std::vector< T > &a)
 
template<typename T >
void do_write (std::vector< std::complex< T > > &a)
 
template<typename T >
void _complex_data_stream (std::complex< T > *val, const unsigned int len, const unsigned int line_break)
 Helper method for complex types.
 
template<typename XFP >
void _xfp_data_stream (XFP *val, const unsigned int len, #ifdef LIBMESH_HAVE_XDR xdrproc_t #else void *#endif xdr_proc, const unsigned int line_break, const int n_digits)
 Helper method for extended FP types.
 

Private Attributes

const XdrMODE mode
 The mode used for accessing the file.
 
std::string file_name
 The file name.
 
std::unique_ptr< XDR > xdrs
 Pointer to the standard XDR struct.
 
FILE * fp
 File pointer.
 
std::unique_ptr< std::istream > in
 The input file stream.
 
std::unique_ptr< std::ostream > out
 The output file stream.
 
const int comm_len
 A buffer to put comment strings into.
 
char comm [xdr_MAX_STRING_LENGTH]
 
bool gzipped_file
 Are we reading/writing zipped files?
 
bool bzipped_file
 
bool xzipped_file
 
int version_number
 Version of the file being read.
 

Detailed Description

This class implements a C++ interface to the XDR (eXternal Data Representation) format.

XDR is useful for creating platform-independent binary files. This class was created to handle equation system output as a replacement for XdrIO since that is somewhat limited.

Author
Benjamin Kirk
Date
2003

C++ interface for the XDR (eXternal Data Representation) format.

Definition at line 67 of file xdr_cxx.h.

Constructor & Destructor Documentation

◆ Xdr() [1/3]

libMesh::Xdr::Xdr ( std::string  name = "",
const XdrMODE  m = UNKNOWN 
)

File-based constructor.

Takes the filename and the mode. Valid modes are ENCODE, DECODE, READ, and WRITE.

Definition at line 104 of file xdr_cxx.C.

105 :
106 mode(m),
107 file_name(std::move(name)),
108#ifdef LIBMESH_HAVE_XDR
109 fp(nullptr),
110#endif
111 in(),
112 out(),
114 gzipped_file(false),
115 bzipped_file(false),
116 xzipped_file(false),
117 version_number(LIBMESH_VERSION_ID(LIBMESH_MAJOR_VERSION, LIBMESH_MINOR_VERSION, LIBMESH_MICRO_VERSION))
118{
119 this->open(file_name);
120}
std::string file_name
The file name.
Definition xdr_cxx.h:239
bool bzipped_file
Definition xdr_cxx.h:275
const int comm_len
A buffer to put comment strings into.
Definition xdr_cxx.h:269
const XdrMODE mode
The mode used for accessing the file.
Definition xdr_cxx.h:234
FILE * fp
File pointer.
Definition xdr_cxx.h:252
bool gzipped_file
Are we reading/writing zipped files?
Definition xdr_cxx.h:275
std::unique_ptr< std::istream > in
The input file stream.
Definition xdr_cxx.h:259
std::unique_ptr< std::ostream > out
The output file stream.
Definition xdr_cxx.h:264
int version_number
Version of the file being read.
Definition xdr_cxx.h:280
bool xzipped_file
Definition xdr_cxx.h:275
void open(std::string name)
Opens the file.
Definition xdr_cxx.C:167
const unsigned int xdr_MAX_STRING_LENGTH
Definition xdr_cxx.h:45

References file_name, and open().

◆ Xdr() [2/3]

libMesh::Xdr::Xdr ( std::ostream &  stream)

Output stream based constructor.

Assumes mode WRITE.

Definition at line 124 of file xdr_cxx.C.

124 :
125 mode(WRITE),
126 file_name(),
127#ifdef LIBMESH_HAVE_XDR
128 fp(nullptr),
129#endif
130 in(),
131 out(std::make_unique<std::ostream>(stream.rdbuf())),
133 gzipped_file(false),
134 bzipped_file(false),
135 xzipped_file(false),
136 version_number(LIBMESH_VERSION_ID(LIBMESH_MAJOR_VERSION, LIBMESH_MINOR_VERSION, LIBMESH_MICRO_VERSION))
137{
138}

◆ Xdr() [3/3]

libMesh::Xdr::Xdr ( std::istream &  stream)

Input stream based constructor.

Assumes mode READ.

Definition at line 142 of file xdr_cxx.C.

142 :
143 mode(READ),
144 file_name(),
145#ifdef LIBMESH_HAVE_XDR
146 fp(nullptr),
147#endif
148 in(std::make_unique<std::istream>(stream.rdbuf())),
149 out(),
151 gzipped_file(false),
152 bzipped_file(false),
153 xzipped_file(false),
154 version_number(LIBMESH_VERSION_ID(LIBMESH_MAJOR_VERSION, LIBMESH_MINOR_VERSION, LIBMESH_MICRO_VERSION))
155{
156}

◆ ~Xdr()

libMesh::Xdr::~Xdr ( )

Destructor.

Closes the file if it is open.

Definition at line 160 of file xdr_cxx.C.

161{
162 this->close();
163}
void close()
Closes the file if it is open.
Definition xdr_cxx.C:278

References close().

Member Function Documentation

◆ _complex_data_stream()

template<typename T >
void libMesh::Xdr::_complex_data_stream ( std::complex< T > *  val,
const unsigned int  len,
const unsigned int  line_break 
)
private

Helper method for complex types.

Definition at line 1248 of file xdr_cxx.C.

1249{
1250 switch (mode)
1251 {
1252 case ENCODE:
1253 case DECODE:
1254 {
1255#ifdef LIBMESH_HAVE_XDR
1256
1257 libmesh_assert (this->is_open());
1258
1259 // FIXME[JWP]: How to implement this for long double? Mac OS
1260 // X defines 'xdr_quadruple' but AFAICT, it does not exist for
1261 // Linux... for now, reading/writing XDR files with long
1262 // doubles drops back to double precision, but you can still
1263 // write long double ASCII files of course.
1264
1265 if (len > 0)
1266 {
1267 std::vector<double> io_buffer (2*len);
1268
1269 // Fill io_buffer if we are writing.
1270 if (mode == ENCODE)
1271 for (unsigned int i=0, cnt=0; i<len; i++)
1272 {
1273 io_buffer[cnt++] = val[i].real();
1274 io_buffer[cnt++] = val[i].imag();
1275 }
1276
1277 xdr_vector(xdrs.get(),
1278 reinterpret_cast<char *>(io_buffer.data()),
1279 2*len,
1280 sizeof(double),
1281 (xdrproc_t) libmesh_xdr_double);
1282
1283 // Fill val array if we are reading.
1284 if (mode == DECODE)
1285 for (unsigned int i=0, cnt=0; i<len; i++)
1286 {
1287 double re = io_buffer[cnt++];
1288 double im = io_buffer[cnt++];
1289 val[i] = std::complex<T>(re, im);
1290 }
1291 }
1292#else
1293
1294 libmesh_error_msg("ERROR: Functionality is not available.\n" \
1295 << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
1296 << "The XDR interface is not available in this installation");
1297
1298#endif
1299 return;
1300 }
1301
1302 case READ:
1303 {
1304 libmesh_assert(in.get());
1305 libmesh_assert (in->good());
1306
1307 for (unsigned int i=0; i<len; i++)
1308 {
1309 libmesh_assert(in.get());
1310 libmesh_assert (in->good());
1311 T re, im;
1312 *in >> re >> im;
1313 val[i] = std::complex<T>(re,im);
1314 }
1315
1316 return;
1317 }
1318
1319 case WRITE:
1320 {
1321 libmesh_assert(out.get());
1322 libmesh_assert (out->good());
1323
1324
1325 // Save stream flags
1326 std::ios_base::fmtflags out_flags = out->flags();
1327
1328 // We will use scientific notation with a precision of
1329 // 'max_digits10' digits in the following output. The desired
1330 // precision and format will automatically determine the
1331 // width. Note: digit10 is the number of digits (in decimal
1332 // base) that can be represented without change. Equivalent
1333 // to FLT_DIG, DBL_DIG or LDBL_DIG for floating types.
1334 *out << std::scientific
1335 << std::setprecision(std::numeric_limits<T>::max_digits10);
1336
1337 if (line_break == libMesh::invalid_uint)
1338 for (unsigned int i=0; i<len; i++)
1339 {
1340 libmesh_assert(out.get());
1341 libmesh_assert (out->good());
1342 *out << val[i].real() << ' ' << val[i].imag() << ' ';
1343 }
1344 else
1345 {
1346 const unsigned imax = std::min(line_break, len);
1347 unsigned int cnt=0;
1348 while (cnt < len)
1349 {
1350 for (unsigned int i=0; (i<imax && cnt<len); i++)
1351 {
1352 libmesh_assert(out.get());
1353 libmesh_assert (out->good());
1354 *out << val[cnt].real() << ' ' << val[cnt].imag();
1355 cnt++;
1356
1357 // Write a space unless this is the last character on the current line.
1358 if (i+1 != imax)
1359 *out << " ";
1360 }
1361 libmesh_assert(out.get());
1362 libmesh_assert (out->good());
1363 *out << '\n';
1364 }
1365 }
1366
1367 // Restore stream flags
1368 out->flags(out_flags);
1369
1370 return;
1371 }
1372
1373 default:
1374 libmesh_error_msg("Invalid mode = " << mode);
1375 }
1376}
bool is_open() const
Definition xdr_cxx.C:346
std::unique_ptr< XDR > xdrs
Pointer to the standard XDR struct.
Definition xdr_cxx.h:247
libmesh_assert(ctx)
const unsigned int invalid_uint
A number which is used quite often to represent an invalid or uninitialized value for an unsigned int...
Definition libmesh.h:303

References libMesh::DECODE, libMesh::ENCODE, in, libMesh::invalid_uint, is_open(), libMesh::libmesh_assert(), mode, out, libMesh::READ, libMesh::WRITE, and xdrs.

Referenced by data_stream(), and data_stream().

◆ _xfp_data_stream() [1/2]

template<typename XFP >
void libMesh::Xdr::_xfp_data_stream ( XFP *  val,
const unsigned int  len,
#ifdef LIBMESH_HAVE_XDR xdrproc_t #else void *#endif  xdr_proc,
const unsigned int  line_break,
const int  n_digits 
)
private

Helper method for extended FP types.

Referenced by data_stream(), data_stream(), data_stream(), and data_stream().

◆ _xfp_data_stream() [2/2]

template<typename XFP >
void libMesh::Xdr::_xfp_data_stream ( XFP *  val,
const unsigned int  len,
#ifdef LIBMESH_HAVE_XDR xdrproc_t  xdr_proc,
#else void *  ,
#endif const unsigned int  line_break,
const int  n_digits 
)

Definition at line 1090 of file xdr_cxx.C.

1098{
1099 switch (mode)
1100 {
1101 case ENCODE:
1102 case DECODE:
1103 {
1104#ifdef LIBMESH_HAVE_XDR
1105 libmesh_assert (this->is_open());
1106
1107 if (len > 0)
1108 {
1109 if (xdr_proc)
1110 {
1111 xdr_vector(xdrs.get(),
1112 (char *) val,
1113 len,
1114 sizeof(XFP),
1115 xdr_proc);
1116 return;
1117 }
1118
1119 // FIXME[JWP]: How to implement this for long double? Mac
1120 // OS X defines 'xdr_quadruple' but AFAICT, it does not
1121 // exist for Linux... for now, reading/writing XDR files
1122 // with long doubles drops back to double precision, but
1123 // you can still write long double ASCII files of course.
1124
1125 // FIXME[RHS]: 128 bit FP has the same problem as long
1126 // double, only much worse since even _Quad/__float128
1127 // aren't standard either.
1128
1129 std::vector<double> io_buffer (len);
1130
1131 // Fill io_buffer if we are writing.
1132 if (mode == ENCODE)
1133 for (unsigned int i=0, cnt=0; i<len; i++)
1134 io_buffer[cnt++] = double(val[i]);
1135
1136 xdr_vector(xdrs.get(),
1137 reinterpret_cast<char *>(io_buffer.data()),
1138 len,
1139 sizeof(double),
1140 (xdrproc_t) libmesh_xdr_double);
1141
1142 // Fill val array if we are reading.
1143 if (mode == DECODE)
1144 for (unsigned int i=0, cnt=0; i<len; i++)
1145 {
1146 val[i] = io_buffer[cnt++];
1147 }
1148 }
1149
1150#else
1151
1152 libmesh_error_msg("ERROR: Functionality is not available.\n" \
1153 << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
1154 << "The XDR interface is not available in this installation");
1155
1156#endif
1157 return;
1158 }
1159
1160 case READ:
1161 {
1162 libmesh_assert(in.get());
1163 libmesh_assert (in->good());
1164
1165 for (unsigned int i=0; i<len; i++)
1166 {
1167 libmesh_assert(in.get());
1168 libmesh_assert (in->good());
1169 *in >> val[i];
1170 }
1171
1172 return;
1173 }
1174
1175 case WRITE:
1176 {
1177 libmesh_assert(out.get());
1178 libmesh_assert (out->good());
1179
1180 // Save stream flags
1181 std::ios_base::fmtflags out_flags = out->flags();
1182
1183 // We will use scientific notation with specified digit
1184 // count in the following output. The desired precision and
1185 // format will automatically determine the width.
1186 *out << std::scientific
1187 << std::setprecision(n_digits);
1188
1189 if (line_break == libMesh::invalid_uint)
1190 for (unsigned int i=0; i<len; i++)
1191 {
1192 libmesh_assert(out.get());
1193 libmesh_assert (out->good());
1194 *out << val[i] << ' ';
1195 }
1196 else
1197 {
1198 const unsigned imax = std::min(line_break, len);
1199 unsigned int cnt=0;
1200 while (cnt < len)
1201 {
1202 for (unsigned int i=0; (i<imax && cnt<len); i++)
1203 {
1204 libmesh_assert(out.get());
1205 libmesh_assert (out->good());
1206 *out << val[cnt++];
1207
1208 // Write a space unless this is the last character on the current line.
1209 if (i+1 != imax)
1210 *out << " ";
1211 }
1212 libmesh_assert(out.get());
1213 libmesh_assert (out->good());
1214 *out << '\n';
1215 }
1216 }
1217
1218 // Restore stream flags
1219 out->flags(out_flags);
1220
1221 return;
1222 }
1223
1224 default:
1225 libmesh_error_msg("Invalid mode = " << mode);
1226 }
1227}

References libMesh::DECODE, libMesh::ENCODE, in, libMesh::invalid_uint, is_open(), libMesh::libmesh_assert(), mode, out, libMesh::READ, libMesh::WRITE, and xdrs.

◆ access_mode()

XdrMODE libMesh::Xdr::access_mode ( ) const
inline
Returns
The mode used to access the file. Valid modes are ENCODE, DECODE, READ, or WRITE.

Definition at line 135 of file xdr_cxx.h.

135{ return mode; }

References mode.

◆ close()

void libMesh::Xdr::close ( )

Closes the file if it is open.

Definition at line 278 of file xdr_cxx.C.

279{
280 switch (mode)
281 {
282 case ENCODE:
283 case DECODE:
284 {
285#ifdef LIBMESH_HAVE_XDR
286
287 if (xdrs)
288 {
289 xdr_destroy (xdrs.get());
290 xdrs.reset();
291 }
292
293 if (fp)
294 {
295 fflush(fp);
296 fclose(fp);
297 fp = nullptr;
298 }
299#else
300
301 libmesh_error_msg("ERROR: Functionality is not available.\n" \
302 << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
303 << "The XDR interface is not available in this installation");
304
305#endif
306 file_name = "";
307 return;
308 }
309
310 case READ:
311 {
312 if (in.get() != nullptr)
313 {
314 in.reset();
315
317 remove_unzipped_file(file_name);
318 }
319 file_name = "";
320 return;
321 }
322
323 case WRITE:
324 {
325 if (out.get() != nullptr)
326 {
327 out.reset();
328
329 if (bzipped_file)
330 bzip_file(std::string(file_name.begin(), file_name.end()-4));
331
332 else if (xzipped_file)
333 xzip_file(std::string(file_name.begin(), file_name.end()-3));
334 }
335 file_name = "";
336 return;
337 }
338
339 default:
340 libmesh_error_msg("Invalid mode = " << mode);
341 }
342}

References bzipped_file, libMesh::DECODE, libMesh::ENCODE, file_name, fp, in, mode, out, libMesh::READ, libMesh::WRITE, xdrs, and xzipped_file.

Referenced by libMesh::RBEvaluation::legacy_read_offline_data_from_files(), libMesh::TransientRBEvaluation::legacy_read_offline_data_from_files(), libMesh::RBSCMEvaluation::legacy_read_offline_data_from_files(), libMesh::RBEvaluation::legacy_write_offline_data_to_files(), libMesh::RBSCMEvaluation::legacy_write_offline_data_to_files(), libMesh::TransientRBEvaluation::legacy_write_offline_data_to_files(), libMesh::CheckpointIO::read(), libMesh::EquationSystems::read(), libMesh::RBParametrized::read_parameter_ranges_from_file(), libMesh::XdrIO::write(), libMesh::CheckpointIO::write(), libMesh::EquationSystems::write(), libMesh::RBParametrized::write_parameter_ranges_to_file(), and ~Xdr().

◆ comment()

void libMesh::Xdr::comment ( std::string &  comment_in)

Writes or reads (ignores) a comment line.

Definition at line 1380 of file xdr_cxx.C.

1381{
1382 switch (mode)
1383 {
1384 case ENCODE:
1385 case DECODE:
1386 {
1387 return;
1388 }
1389
1390 case READ:
1391 {
1392 libmesh_assert(in.get());
1393 libmesh_assert (in->good());
1394 in->getline(comm, comm_len);
1395 return;
1396 }
1397
1398 case WRITE:
1399 {
1400 libmesh_assert(out.get());
1401 libmesh_assert (out->good());
1402 *out << "\t " << comment_in << '\n';
1403 return;
1404 }
1405
1406 default:
1407 libmesh_error_msg("Invalid mode = " << mode);
1408 }
1409}
char comm[xdr_MAX_STRING_LENGTH]
Definition xdr_cxx.h:270

References comm, comm_len, libMesh::DECODE, libMesh::ENCODE, in, libMesh::libmesh_assert(), mode, out, libMesh::READ, and libMesh::WRITE.

Referenced by libMesh::System::read_serialized_data(), and libMesh::System::write_serialized_data().

◆ data()

template<typename T >
template LIBMESH_EXPORT void libMesh::Xdr::data< Real > ( T &  a,
std::string_view  comment = "" 
)

Inputs or outputs a single value.

Definition at line 860 of file xdr_cxx.C.

861{
862 switch (mode)
863 {
864 case ENCODE:
865 case DECODE:
866 {
867#ifdef LIBMESH_HAVE_XDR
868
870
871 xdr_translate(xdrs.get(), a);
872
873#else
874
875 libmesh_error_msg("ERROR: Functionality is not available.\n" \
876 << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
877 << "The XDR interface is not available in this installation");
878
879#endif
880 return;
881 }
882
883 case READ:
884 {
885 libmesh_assert(in.get());
886 libmesh_assert (in->good());
887
888 this->do_read(a);
889
890 return;
891 }
892
893 case WRITE:
894 {
895 libmesh_assert(out.get());
896 libmesh_assert (out->good());
897
898 // We will use scientific notation sufficient to exactly
899 // represent our floating point precision in the following
900 // output. The desired precision and format will
901 // automatically determine the width.
902 *out << std::scientific
903 << std::setprecision(std::numeric_limits<T>::max_digits10);
904
905 this->do_write(a);
906
907 // If there's a comment provided, write a tab character and
908 // then the comment.
909 if (comment_in != "")
910 *out << "\t " << comment_in;
911
912 // Go to the next line.
913 *out << '\n';
914
915 return;
916 }
917
918 default:
919 libmesh_error_msg("Invalid mode = " << mode);
920 }
921}
void do_write(T &a)
Helper method for writing different data types.
Definition xdr_cxx.C:811
void do_read(T &a)
Helper method for reading different data types.
Definition xdr_cxx.C:746

References libMesh::DECODE, do_read(), do_write(), libMesh::ENCODE, in, is_open(), libMesh::libmesh_assert(), mode, out, libMesh::READ, libMesh::WRITE, and xdrs.

Referenced by do_read(), do_read(), do_write(), do_write(), operator<<(), operator>>(), libMesh::XdrIO::read(), libMesh::EquationSystems::read(), libMesh::CheckpointIO::read_bc_names(), libMesh::CheckpointIO::read_bcs(), libMesh::CheckpointIO::read_connectivity(), libMesh::CheckpointIO::read_header(), libMesh::System::read_header(), libMesh::XdrIO::read_header(), libMesh::RBEIMEvaluation::read_in_interior_basis_functions(), libMesh::RBEIMEvaluation::read_in_node_basis_functions(), libMesh::RBEIMEvaluation::read_in_side_basis_functions(), libMesh::RBEvaluation::read_in_vectors_from_multiple_files(), libMesh::CheckpointIO::read_integers_names(), libMesh::CheckpointIO::read_nodes(), libMesh::CheckpointIO::read_nodesets(), libMesh::System::read_parallel_data(), libMesh::CheckpointIO::read_remote_elem(), libMesh::XdrIO::read_serialized_bc_names(), libMesh::XdrIO::read_serialized_bcs_helper(), libMesh::XdrIO::read_serialized_connectivity(), libMesh::XdrIO::read_serialized_nodes(), libMesh::XdrIO::read_serialized_nodesets(), libMesh::XdrIO::read_serialized_subdomain_names(), libMesh::System::read_serialized_vector(), libMesh::System::read_serialized_vectors(), libMesh::CheckpointIO::read_subdomain_names(), libMesh::CheckpointIO::select_split_config(), libMesh::XdrIO::write(), libMesh::CheckpointIO::write(), libMesh::EquationSystems::write(), libMesh::CheckpointIO::write_bc_names(), libMesh::CheckpointIO::write_bcs(), libMesh::CheckpointIO::write_connectivity(), libMesh::System::write_header(), libMesh::CheckpointIO::write_nodes(), libMesh::CheckpointIO::write_nodesets(), libMesh::RBEIMEvaluation::write_out_node_basis_functions(), libMesh::RBEIMEvaluation::write_out_side_basis_functions(), libMesh::System::write_parallel_data(), libMesh::CheckpointIO::write_remote_elem(), libMesh::XdrIO::write_serialized_bc_names(), libMesh::XdrIO::write_serialized_bcs_helper(), libMesh::XdrIO::write_serialized_connectivity(), libMesh::XdrIO::write_serialized_nodes(), libMesh::XdrIO::write_serialized_nodesets(), libMesh::XdrIO::write_serialized_subdomain_names(), libMesh::System::write_serialized_vector(), libMesh::System::write_serialized_vectors(), and libMesh::CheckpointIO::write_subdomain_names().

◆ data_stream() [1/7]

template<>
void libMesh::Xdr::data_stream ( double *  val,
const unsigned int  len,
const unsigned int  line_break 
)

Definition at line 1041 of file xdr_cxx.C.

1042{
1043 this->_xfp_data_stream
1044 (val, len,
1045#ifdef LIBMESH_HAVE_XDR
1046 (xdrproc_t)libmesh_xdr_double,
1047#else
1048 nullptr,
1049#endif
1050 line_break, std::numeric_limits<double>::max_digits10);
1051}
void _xfp_data_stream(XFP *val, const unsigned int len, #ifdef LIBMESH_HAVE_XDR xdrproc_t #else void *#endif xdr_proc, const unsigned int line_break, const int n_digits)
Helper method for extended FP types.

References _xfp_data_stream().

◆ data_stream() [2/7]

template<>
void libMesh::Xdr::data_stream ( float *  val,
const unsigned int  len,
const unsigned int  line_break 
)

Definition at line 1056 of file xdr_cxx.C.

1057{
1058 this->_xfp_data_stream
1059 (val, len,
1060#ifdef LIBMESH_HAVE_XDR
1061 (xdrproc_t)libmesh_xdr_float,
1062#else
1063 nullptr,
1064#endif
1065 line_break, std::numeric_limits<float>::max_digits10);
1066}

References _xfp_data_stream().

◆ data_stream() [3/7]

template<>
void libMesh::Xdr::data_stream ( long double *  val,
const unsigned int  len,
const unsigned int  line_break 
)

Definition at line 1071 of file xdr_cxx.C.

1072{
1073 this->_xfp_data_stream
1074 (val, len, nullptr, line_break,
1075 std::numeric_limits<long double>::max_digits10);
1076}

References _xfp_data_stream().

◆ data_stream() [4/7]

template<>
void libMesh::Xdr::data_stream ( Real val,
const unsigned int  len,
const unsigned int  line_break 
)

Definition at line 1081 of file xdr_cxx.C.

1082{
1083 this->_xfp_data_stream(val, len, nullptr, line_break, 36);
1084}

References _xfp_data_stream().

◆ data_stream() [5/7]

template<>
void libMesh::Xdr::data_stream ( std::complex< double > *  val,
const unsigned int  len,
const unsigned int  line_break 
)

Definition at line 1232 of file xdr_cxx.C.

1233{
1234 this->_complex_data_stream(val, len, line_break);
1235}
void _complex_data_stream(std::complex< T > *val, const unsigned int len, const unsigned int line_break)
Helper method for complex types.
Definition xdr_cxx.C:1248

References _complex_data_stream().

◆ data_stream() [6/7]

template<>
void libMesh::Xdr::data_stream ( std::complex< long double > *  val,
const unsigned int  len,
const unsigned int  line_break 
)

Definition at line 1240 of file xdr_cxx.C.

1241{
1242 this->_complex_data_stream(val, len, line_break);
1243}

References _complex_data_stream().

◆ data_stream() [7/7]

template<typename T >
void libMesh::Xdr::data_stream ( T *  val,
const unsigned int  len,
const unsigned int  line_break = libMesh::invalid_uint 
)

Inputs or outputs a raw data stream.

Definition at line 925 of file xdr_cxx.C.

926{
927 switch (mode)
928 {
929 case ENCODE:
930 {
931#ifdef LIBMESH_HAVE_XDR
932
933 libmesh_assert (this->is_open());
934
935 unsigned int size_of_type = cast_int<unsigned int>(sizeof(T));
936
937 xdr_vector(xdrs.get(),
938 (char *) val,
939 len,
940 size_of_type,
941 xdr_translator<T>());
942#else
943 libmesh_error_msg("ERROR: Functionality is not available.\n" \
944 << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
945 << "The XDR interface is not available in this installation");
946
947#endif
948 return;
949 }
950
951 case DECODE:
952 {
953#ifdef LIBMESH_HAVE_XDR
954
955 libmesh_assert (this->is_open());
956
957 unsigned int size_of_type = cast_int<unsigned int>(sizeof(T));
958
959 if (len > 0)
960 xdr_vector(xdrs.get(),
961 (char *) val,
962 len,
963 size_of_type,
964 xdr_translator<T>());
965#else
966 libmesh_error_msg("ERROR: Functionality is not available.\n" \
967 << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
968 << "The XDR interface is not available in this installation");
969
970#endif
971 return;
972 }
973
974 case READ:
975 {
976 libmesh_assert(in.get());
977 libmesh_assert (in->good());
978
979 for (unsigned int i=0; i<len; i++)
980 {
981 libmesh_assert(in.get());
982 libmesh_assert (in->good());
983 *in >> val[i];
984 }
985
986 return;
987 }
988
989 case WRITE:
990 {
991 libmesh_assert(out.get());
992 libmesh_assert (out->good());
993
994 // We will use scientific notation sufficient to exactly
995 // represent our floating point precision in the following
996 // output. The desired precision and format will
997 // automatically determine the width.
998 *out << std::scientific
999 << std::setprecision(std::numeric_limits<T>::max_digits10);
1000
1001 if (line_break == libMesh::invalid_uint)
1002 for (unsigned int i=0; i<len; i++)
1003 {
1004 libmesh_assert(out.get());
1005 libmesh_assert (out->good());
1006 *out << val[i] << " ";
1007 }
1008 else
1009 {
1010 const unsigned imax = std::min(line_break, len);
1011 unsigned int cnt=0;
1012 while (cnt < len)
1013 {
1014 for (unsigned int i=0; (i<imax && cnt<len); i++)
1015 {
1016 libmesh_assert(out.get());
1017 libmesh_assert (out->good());
1018 *out << val[cnt++];
1019
1020 // Write a space unless this is the last character on the current line.
1021 if (i+1 != imax)
1022 *out << " ";
1023 }
1024 libmesh_assert(out.get());
1025 libmesh_assert (out->good());
1026 *out << '\n';
1027 }
1028 }
1029
1030 return;
1031 }
1032
1033 default:
1034 libmesh_error_msg("Invalid mode = " << mode);
1035 }
1036}

References libMesh::DECODE, libMesh::ENCODE, in, libMesh::invalid_uint, is_open(), libMesh::libmesh_assert(), mode, out, libMesh::READ, libMesh::WRITE, and xdrs.

Referenced by libMesh::CheckpointIO::read_connectivity(), libMesh::CheckpointIO::read_nodes(), libMesh::System::read_SCALAR_dofs(), libMesh::XdrIO::read_serialized_bcs_helper(), libMesh::XdrIO::read_serialized_connectivity(), libMesh::XdrIO::read_serialized_nodes(), libMesh::XdrIO::read_serialized_nodesets(), libMesh::CheckpointIO::write_connectivity(), libMesh::CheckpointIO::write_nodes(), libMesh::System::write_SCALAR_dofs(), libMesh::XdrIO::write_serialized_bcs_helper(), libMesh::XdrIO::write_serialized_connectivity(), libMesh::XdrIO::write_serialized_nodes(), and libMesh::XdrIO::write_serialized_nodesets().

◆ do_read() [1/5]

template<typename T >
void libMesh::Xdr::do_read ( std::complex< T > &  a)
private

Definition at line 753 of file xdr_cxx.C.

754{
755 T r, i;
756 *in >> r >> i;
757 a = std::complex<T>(r,i);
758 in->getline(comm, comm_len);
759}

References comm, comm_len, and in.

◆ do_read() [2/5]

template<>
void libMesh::Xdr::do_read ( std::string &  a)

Definition at line 762 of file xdr_cxx.C.

763{
764 in->getline(comm, comm_len);
765
766 a = "";
767
768 for (unsigned int c=0, sl=std::strlen(comm); c!=sl; c++)
769 {
770 if (comm[c] == '\t')
771 break;
772 a.push_back(comm[c]);
773 }
774}

References comm, comm_len, and in.

◆ do_read() [3/5]

template<typename T >
void libMesh::Xdr::do_read ( std::vector< std::complex< T > > &  a)
private

Definition at line 793 of file xdr_cxx.C.

794{
795 unsigned int length=0;
796 data(length, "# vector length x 2 (complex)");
797 a.resize(length);
798
799 for (std::complex<T> & a_i : a)
800 {
801 T r, im;
802 libmesh_assert(in.get());
803 libmesh_assert (in->good());
804 *in >> r >> im;
805 a_i = std::complex<T>(r,im);
806 }
807 in->getline(comm, comm_len);
808}
void data(T &a, std::string_view comment="")
Inputs or outputs a single value.
Definition xdr_cxx.C:860

References comm, comm_len, data(), in, and libMesh::libmesh_assert().

◆ do_read() [4/5]

template<typename T >
void libMesh::Xdr::do_read ( std::vector< T > &  a)
private

Definition at line 777 of file xdr_cxx.C.

778{
779 unsigned int length=0;
780 data(length, "# vector length");
781 a.resize(length);
782
783 for (T & a_i : a)
784 {
785 libmesh_assert(in.get());
786 libmesh_assert (in->good());
787 *in >> a_i;
788 }
789 in->getline(comm, comm_len);
790}

References comm, comm_len, data(), in, and libMesh::libmesh_assert().

◆ do_read() [5/5]

template<typename T >
void libMesh::Xdr::do_read ( T &  a)
private

Helper method for reading different data types.

Definition at line 746 of file xdr_cxx.C.

747{
748 *in >> a;
749 in->getline(comm, comm_len);
750}

References comm, comm_len, and in.

Referenced by data().

◆ do_write() [1/4]

template<typename T >
void libMesh::Xdr::do_write ( std::complex< T > &  a)
private

Definition at line 814 of file xdr_cxx.C.

815{
816 *out << a.real() << "\t " << a.imag();
817}

References out.

◆ do_write() [2/4]

template<typename T >
void libMesh::Xdr::do_write ( std::vector< std::complex< T > > &  a)
private

Definition at line 839 of file xdr_cxx.C.

840{
841 std::size_t length=a.size();
842 data(length, "# vector length x 2 (complex)");
843
844 // Use scientific precision with lots of digits for the original type T.
845 *out << std::scientific
846 << std::setprecision(std::numeric_limits<T>::max_digits10);
847
848 for (std::complex<T> & a_i : a)
849 {
850 libmesh_assert(out.get());
851 libmesh_assert (out->good());
852 this->do_write(a_i);
853 *out << "\t ";
854 }
855}

References data(), do_write(), libMesh::libmesh_assert(), and out.

◆ do_write() [3/4]

template<typename T >
void libMesh::Xdr::do_write ( std::vector< T > &  a)
private

Definition at line 820 of file xdr_cxx.C.

821{
822 std::size_t length = a.size();
823 data(length, "# vector length");
824
825 // Use scientific precision with lots of digits for the original type T.
826 *out << std::scientific
827 << std::setprecision(std::numeric_limits<T>::max_digits10);
828
829 for (T & a_i : a)
830 {
831 libmesh_assert(out.get());
832 libmesh_assert (out->good());
833 this->do_write(a_i);
834 *out << "\t ";
835 }
836}

References data(), do_write(), libMesh::libmesh_assert(), and out.

◆ do_write() [4/4]

template<typename T >
void libMesh::Xdr::do_write ( T &  a)
private

Helper method for writing different data types.

Definition at line 811 of file xdr_cxx.C.

811{ *out << a; }

References out.

Referenced by data(), do_write(), and do_write().

◆ is_eof()

bool libMesh::Xdr::is_eof ( )
Returns
true if the Xdr file being read is at End-Of-File.
Note
This is not a const method - the only portable way to test for an impending EOF is to peek at the next byte of the file first, which may set the eof flag on the istream.

Definition at line 396 of file xdr_cxx.C.

397{
398 switch (mode)
399 {
400 case ENCODE:
401 case DECODE:
402 {
403#ifdef LIBMESH_HAVE_XDR
405
406 // Are we already at eof?
407 if (feof(fp))
408 return true;
409
410 // Or about to reach eof?
411 int next = fgetc(fp);
412 if (next == EOF)
413 {
414 // We should *only* be at EOF, not otherwise broken
415 libmesh_assert(feof(fp));
416 libmesh_assert(!ferror(fp));
417
418 // Reset the EOF indicator
419 clearerr(fp);
420 libmesh_assert(!ferror(fp));
421
422 // We saw EOF
423 return true;
424 }
425
426 // We didn't see EOF; restore whatever we did see.
427 ungetc(next, fp);
428 break;
429#else
430
431 libmesh_error_msg("ERROR: Functionality is not available.\n" \
432 << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
433 << "The XDR interface is not available in this installation");
434
435 return false;
436
437#endif
438
439 }
440 case READ:
441 {
442 libmesh_assert(in.get());
443
444 // Are we already at eof?
445 if (in->eof())
446 return true;
447
448 // Or about to reach eof?
449 int next = in->peek();
450 if (next == EOF)
451 {
452 // We should *only* be at EOF, not otherwise broken
453 libmesh_assert(in->eof());
454 libmesh_assert(!in->fail());
455
456 // Reset the EOF indicator
457 in->clear();
458 libmesh_assert(in->good());
459
460 // We saw EOF
461 return true;
462 }
463 break;
464 }
465 default:
466 libmesh_error();
467 }
468
469 return false;
470}

References libMesh::DECODE, libMesh::ENCODE, fp, in, libMesh::libmesh_assert(), mode, and libMesh::READ.

◆ is_open()

bool libMesh::Xdr::is_open ( ) const
Returns
true if the Xdr file is open, false if it is closed.

Definition at line 346 of file xdr_cxx.C.

347{
348 switch (mode)
349 {
350 case ENCODE:
351 case DECODE:
352 {
353#ifdef LIBMESH_HAVE_XDR
354
355 if (fp)
356 if (xdrs)
357 return true;
358
359 return false;
360
361#else
362
363 libmesh_error_msg("ERROR: Functionality is not available.\n" \
364 << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
365 << "The XDR interface is not available in this installation");
366
367 return false;
368
369#endif
370
371 }
372
373 case READ:
374 {
375 if (in.get() != nullptr)
376 return in->good();
377 return false;
378 }
379
380 case WRITE:
381 {
382 if (out.get() != nullptr)
383 return out->good();
384 return false;
385 }
386
387 default:
388 libmesh_error_msg("Invalid mode = " << mode);
389 }
390
391 return false;
392}

References libMesh::DECODE, libMesh::ENCODE, fp, in, mode, out, libMesh::READ, libMesh::WRITE, and xdrs.

Referenced by _complex_data_stream(), _xfp_data_stream(), data(), data_stream(), and libMesh::System::read_parallel_data().

◆ open()

void libMesh::Xdr::open ( std::string  name)

Opens the file.

Definition at line 167 of file xdr_cxx.C.

168{
169 file_name = std::move(name);
170
171 if (file_name == "")
172 return;
173
174 switch (mode)
175 {
176 case ENCODE:
177 case DECODE:
178 {
179#ifdef LIBMESH_HAVE_XDR
180
181 fp = fopen(file_name.c_str(), (mode == ENCODE) ? "w" : "r");
182 if (!fp)
183 libmesh_file_error(file_name.c_str());
184 xdrs = std::make_unique<XDR>();
185 xdrstdio_create (xdrs.get(), fp, (mode == ENCODE) ? XDR_ENCODE : XDR_DECODE);
186#else
187
188 libmesh_error_msg("ERROR: Functionality is not available.\n" \
189 << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
190 << "The XDR interface is not available in this installation");
191
192#endif
193 return;
194
195 }
196
197 case READ:
198 {
202
203 if (gzipped_file)
204 {
205#ifdef LIBMESH_HAVE_GZSTREAM
206 auto inf = std::make_unique<igzstream>();
207 libmesh_assert(inf);
208 inf->open(file_name.c_str(), std::ios::in);
209 in = std::move(inf);
210#else
211 libmesh_error_msg("ERROR: need gzstream to handle .gz files!!!");
212#endif
213 }
214 else
215 {
216 auto inf = std::make_unique<std::ifstream>();
217 libmesh_assert(inf);
218
219 std::string new_name = Utility::unzip_file(file_name);
220
221 inf->open(new_name.c_str(), std::ios::in);
222 in = std::move(inf);
223 }
224
225 libmesh_assert(in.get());
226
227 if (!in->good())
228 libmesh_file_error(file_name);
229 return;
230 }
231
232 case WRITE:
233 {
234 gzipped_file = (file_name.rfind(".gz") == file_name.size() - 3);
235 bzipped_file = (file_name.rfind(".bz2") == file_name.size() - 4);
236 xzipped_file = (file_name.rfind(".xz") == file_name.size() - 3);
237
238 if (gzipped_file)
239 {
240#ifdef LIBMESH_HAVE_GZSTREAM
241 auto outf = std::make_unique<ogzstream>();
242 libmesh_assert(outf);
243 outf->open(file_name.c_str(), std::ios::out);
244 out = std::move(outf);
245#else
246 libmesh_error_msg("ERROR: need gzstream to handle .gz files!!!");
247#endif
248 }
249 else
250 {
251 auto outf = std::make_unique<std::ofstream>();
252 libmesh_assert(outf);
253
254 std::string new_name = file_name;
255
256 if (bzipped_file)
257 new_name.erase(new_name.end() - 4, new_name.end());
258
259 if (xzipped_file)
260 new_name.erase(new_name.end() - 3, new_name.end());
261
262 outf->open(new_name.c_str(), std::ios::out);
263 out = std::move(outf);
264 }
265
266 libmesh_assert(out.get());
267 libmesh_assert (out->good());
268 return;
269 }
270
271 default:
272 libmesh_error_msg("Invalid mode = " << mode);
273 }
274}
bool ends_with(std::string_view superstring, std::string_view suffix)
Look for a substring at the very end of a string.
Definition utility.C:213
std::string unzip_file(std::string_view name)
Create an unzipped copy of a bz2 or xz file, returning the name of the now-unzipped file that can be ...
Definition utility.C:164

References bzipped_file, libMesh::DECODE, libMesh::ENCODE, libMesh::Utility::ends_with(), file_name, fp, gzipped_file, in, libMesh::libmesh_assert(), mode, out, libMesh::READ, libMesh::Utility::unzip_file(), libMesh::WRITE, xdrs, and xzipped_file.

Referenced by Xdr().

◆ operator<<()

template<typename T >
Xdr & libMesh::Xdr::operator<< ( T &  a)
inline

Same, but provides an ostream like interface.

Definition at line 149 of file xdr_cxx.h.

149{ libmesh_assert (writing()); data(a); return *this; }
bool writing() const
Definition xdr_cxx.h:129

References data(), libMesh::libmesh_assert(), and writing().

◆ operator>>()

template<typename T >
Xdr & libMesh::Xdr::operator>> ( T &  a)
inline

Same, but provides an istream like interface.

Definition at line 155 of file xdr_cxx.h.

155{ libmesh_assert (reading()); data(a); return *this; }
bool reading() const
Definition xdr_cxx.h:123

References data(), libMesh::libmesh_assert(), and reading().

◆ reading()

bool libMesh::Xdr::reading ( ) const
inline

◆ set_version()

void libMesh::Xdr::set_version ( int  ver)
inline

Sets the version of the file that is being read.

Definition at line 171 of file xdr_cxx.h.

171{ version_number = ver; }

References version_number.

Referenced by libMesh::EquationSystems::read(), and libMesh::EquationSystems::write().

◆ version()

int libMesh::Xdr::version ( ) const
inline

Gets the version of the file that is being read.

Definition at line 176 of file xdr_cxx.h.

176{ return version_number; }

References version_number.

Referenced by libMesh::System::read_header(), and libMesh::System::read_serialized_vector().

◆ writing()

bool libMesh::Xdr::writing ( ) const
inline

Member Data Documentation

◆ bzipped_file

bool libMesh::Xdr::bzipped_file
private

Definition at line 275 of file xdr_cxx.h.

Referenced by close(), and open().

◆ comm

char libMesh::Xdr::comm[xdr_MAX_STRING_LENGTH]
private

Definition at line 270 of file xdr_cxx.h.

Referenced by comment(), do_read(), do_read(), do_read(), do_read(), and do_read().

◆ comm_len

const int libMesh::Xdr::comm_len
private

A buffer to put comment strings into.

Definition at line 269 of file xdr_cxx.h.

Referenced by comment(), do_read(), do_read(), do_read(), do_read(), and do_read().

◆ file_name

std::string libMesh::Xdr::file_name
private

The file name.

Definition at line 239 of file xdr_cxx.h.

Referenced by close(), open(), and Xdr().

◆ fp

FILE* libMesh::Xdr::fp
private

File pointer.

Definition at line 252 of file xdr_cxx.h.

Referenced by close(), is_eof(), is_open(), and open().

◆ gzipped_file

bool libMesh::Xdr::gzipped_file
private

Are we reading/writing zipped files?

Definition at line 275 of file xdr_cxx.h.

Referenced by open().

◆ in

std::unique_ptr<std::istream> libMesh::Xdr::in
private

◆ mode

const XdrMODE libMesh::Xdr::mode
private

The mode used for accessing the file.

Definition at line 234 of file xdr_cxx.h.

Referenced by _complex_data_stream(), _xfp_data_stream(), access_mode(), close(), comment(), data(), data_stream(), is_eof(), is_open(), open(), reading(), and writing().

◆ out

std::unique_ptr<std::ostream> libMesh::Xdr::out
private

The output file stream.

Definition at line 264 of file xdr_cxx.h.

Referenced by _complex_data_stream(), _xfp_data_stream(), close(), comment(), data(), data_stream(), do_write(), do_write(), do_write(), do_write(), is_open(), and open().

◆ version_number

int libMesh::Xdr::version_number
private

Version of the file being read.

Definition at line 280 of file xdr_cxx.h.

Referenced by set_version(), and version().

◆ xdrs

std::unique_ptr<XDR> libMesh::Xdr::xdrs
private

Pointer to the standard XDR struct.

See the standard header file rpc/rpc.h for more information.

Definition at line 247 of file xdr_cxx.h.

Referenced by _complex_data_stream(), _xfp_data_stream(), close(), data(), data_stream(), is_open(), and open().

◆ xzipped_file

bool libMesh::Xdr::xzipped_file
private

Definition at line 275 of file xdr_cxx.h.

Referenced by close(), and open().


The documentation for this class was generated from the following files: