libMesh
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. More...
 
 Xdr (std::ostream &stream)
 Output stream based constructor. More...
 
 Xdr (std::istream &stream)
 Input stream based constructor. More...
 
 ~Xdr ()
 Destructor. More...
 
void open (std::string name)
 Opens the file. More...
 
void close ()
 Closes the file if it is open. More...
 
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. More...
 
template<typename T >
Xdroperator<< (T &a)
 Same, but provides an ostream like interface. More...
 
template<typename T >
Xdroperator>> (T &a)
 Same, but provides an istream like interface. More...
 
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. More...
 
void comment (std::string &)
 Writes or reads (ignores) a comment line. More...
 
void set_version (int ver)
 Sets the version of the file that is being read. More...
 
int version () const
 Gets the version of the file that is being read. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 

Private Attributes

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

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 105 of file xdr_cxx.C.

References file_name, and open().

106  :
107  mode(m),
108  file_name(std::move(name)),
109 #ifdef LIBMESH_HAVE_XDR
110  fp(nullptr),
111 #endif
112  in(),
113  out(),
115  gzipped_file(false),
116  bzipped_file(false),
117  xzipped_file(false),
118  version_number(LIBMESH_VERSION_ID(LIBMESH_MAJOR_VERSION, LIBMESH_MINOR_VERSION, LIBMESH_MICRO_VERSION))
119 {
120  this->open(file_name);
121 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
FILE * fp
File pointer.
Definition: xdr_cxx.h:252
const int comm_len
A buffer to put comment strings into.
Definition: xdr_cxx.h:269
std::string file_name
The file name.
Definition: xdr_cxx.h:239
const unsigned int xdr_MAX_STRING_LENGTH
Definition: xdr_cxx.h:45
bool xzipped_file
Definition: xdr_cxx.h:275
bool bzipped_file
Definition: xdr_cxx.h:275
const XdrMODE mode
The mode used for accessing the file.
Definition: xdr_cxx.h:234
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
int version_number
Version of the file being read.
Definition: xdr_cxx.h:280
std::unique_ptr< std::ostream > out
The output file stream.
Definition: xdr_cxx.h:264
void open(std::string name)
Opens the file.
Definition: xdr_cxx.C:168

◆ Xdr() [2/3]

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

Output stream based constructor.

Assumes mode WRITE.

Definition at line 125 of file xdr_cxx.C.

125  :
126  mode(WRITE),
127  file_name(),
128 #ifdef LIBMESH_HAVE_XDR
129  fp(nullptr),
130 #endif
131  in(),
132  out(std::make_unique<std::ostream>(stream.rdbuf())),
134  gzipped_file(false),
135  bzipped_file(false),
136  xzipped_file(false),
137  version_number(LIBMESH_VERSION_ID(LIBMESH_MAJOR_VERSION, LIBMESH_MINOR_VERSION, LIBMESH_MICRO_VERSION))
138 {
139 }
FILE * fp
File pointer.
Definition: xdr_cxx.h:252
const int comm_len
A buffer to put comment strings into.
Definition: xdr_cxx.h:269
std::string file_name
The file name.
Definition: xdr_cxx.h:239
const unsigned int xdr_MAX_STRING_LENGTH
Definition: xdr_cxx.h:45
bool xzipped_file
Definition: xdr_cxx.h:275
bool bzipped_file
Definition: xdr_cxx.h:275
const XdrMODE mode
The mode used for accessing the file.
Definition: xdr_cxx.h:234
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
int version_number
Version of the file being read.
Definition: xdr_cxx.h:280
std::unique_ptr< std::ostream > out
The output file stream.
Definition: xdr_cxx.h:264

◆ Xdr() [3/3]

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

Input stream based constructor.

Assumes mode READ.

Definition at line 143 of file xdr_cxx.C.

143  :
144  mode(READ),
145  file_name(),
146 #ifdef LIBMESH_HAVE_XDR
147  fp(nullptr),
148 #endif
149  in(std::make_unique<std::istream>(stream.rdbuf())),
150  out(),
152  gzipped_file(false),
153  bzipped_file(false),
154  xzipped_file(false),
155  version_number(LIBMESH_VERSION_ID(LIBMESH_MAJOR_VERSION, LIBMESH_MINOR_VERSION, LIBMESH_MICRO_VERSION))
156 {
157 }
FILE * fp
File pointer.
Definition: xdr_cxx.h:252
const int comm_len
A buffer to put comment strings into.
Definition: xdr_cxx.h:269
std::string file_name
The file name.
Definition: xdr_cxx.h:239
const unsigned int xdr_MAX_STRING_LENGTH
Definition: xdr_cxx.h:45
bool xzipped_file
Definition: xdr_cxx.h:275
bool bzipped_file
Definition: xdr_cxx.h:275
const XdrMODE mode
The mode used for accessing the file.
Definition: xdr_cxx.h:234
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
int version_number
Version of the file being read.
Definition: xdr_cxx.h:280
std::unique_ptr< std::ostream > out
The output file stream.
Definition: xdr_cxx.h:264

◆ ~Xdr()

libMesh::Xdr::~Xdr ( )

Destructor.

Closes the file if it is open.

Definition at line 161 of file xdr_cxx.C.

References close().

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

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 1168 of file xdr_cxx.C.

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().

1169 {
1170  switch (mode)
1171  {
1172  case ENCODE:
1173  case DECODE:
1174  {
1175 #ifdef LIBMESH_HAVE_XDR
1176 
1177  libmesh_assert (this->is_open());
1178 
1179  // FIXME[JWP]: How to implement this for long double? Mac OS
1180  // X defines 'xdr_quadruple' but AFAICT, it does not exist for
1181  // Linux... for now, reading/writing XDR files with long
1182  // doubles drops back to double precision, but you can still
1183  // write long double ASCII files of course.
1184 
1185  if (len > 0)
1186  {
1187  std::vector<double> io_buffer (2*len);
1188 
1189  // Fill io_buffer if we are writing.
1190  if (mode == ENCODE)
1191  for (unsigned int i=0, cnt=0; i<len; i++)
1192  {
1193  io_buffer[cnt++] = val[i].real();
1194  io_buffer[cnt++] = val[i].imag();
1195  }
1196 
1197  xdr_vector(xdrs.get(),
1198  reinterpret_cast<char *>(io_buffer.data()),
1199  2*len,
1200  sizeof(double),
1201  (xdrproc_t) xdr_double);
1202 
1203  // Fill val array if we are reading.
1204  if (mode == DECODE)
1205  for (unsigned int i=0, cnt=0; i<len; i++)
1206  {
1207  double re = io_buffer[cnt++];
1208  double im = io_buffer[cnt++];
1209  val[i] = std::complex<T>(re, im);
1210  }
1211  }
1212 #else
1213 
1214  libmesh_error_msg("ERROR: Functionality is not available.\n" \
1215  << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
1216  << "The XDR interface is not available in this installation");
1217 
1218 #endif
1219  return;
1220  }
1221 
1222  case READ:
1223  {
1224  libmesh_assert(in.get());
1225  libmesh_assert (in->good());
1226 
1227  for (unsigned int i=0; i<len; i++)
1228  {
1229  libmesh_assert(in.get());
1230  libmesh_assert (in->good());
1231  T re, im;
1232  *in >> re >> im;
1233  val[i] = std::complex<T>(re,im);
1234  }
1235 
1236  return;
1237  }
1238 
1239  case WRITE:
1240  {
1241  libmesh_assert(out.get());
1242  libmesh_assert (out->good());
1243 
1244 
1245  // Save stream flags
1246  std::ios_base::fmtflags out_flags = out->flags();
1247 
1248  // We will use scientific notation with a precision of
1249  // 'max_digits10' digits in the following output. The desired
1250  // precision and format will automatically determine the
1251  // width. Note: digit10 is the number of digits (in decimal
1252  // base) that can be represented without change. Equivalent
1253  // to FLT_DIG, DBL_DIG or LDBL_DIG for floating types.
1254  *out << std::scientific
1255  << std::setprecision(std::numeric_limits<T>::max_digits10);
1256 
1257  if (line_break == libMesh::invalid_uint)
1258  for (unsigned int i=0; i<len; i++)
1259  {
1260  libmesh_assert(out.get());
1261  libmesh_assert (out->good());
1262  *out << val[i].real() << ' ' << val[i].imag() << ' ';
1263  }
1264  else
1265  {
1266  const unsigned imax = std::min(line_break, len);
1267  unsigned int cnt=0;
1268  while (cnt < len)
1269  {
1270  for (unsigned int i=0; (i<imax && cnt<len); i++)
1271  {
1272  libmesh_assert(out.get());
1273  libmesh_assert (out->good());
1274  *out << val[cnt].real() << ' ' << val[cnt].imag();
1275  cnt++;
1276 
1277  // Write a space unless this is the last character on the current line.
1278  if (i+1 != imax)
1279  *out << " ";
1280  }
1281  libmesh_assert(out.get());
1282  libmesh_assert (out->good());
1283  *out << '\n';
1284  }
1285  }
1286 
1287  // Restore stream flags
1288  out->flags(out_flags);
1289 
1290  return;
1291  }
1292 
1293  default:
1294  libmesh_error_msg("Invalid mode = " << mode);
1295  }
1296 }
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:286
const XdrMODE mode
The mode used for accessing the file.
Definition: xdr_cxx.h:234
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
libmesh_assert(ctx)
std::unique_ptr< XDR > xdrs
Pointer to the standard XDR struct.
Definition: xdr_cxx.h:247
bool is_open() const
Definition: xdr_cxx.C:347

◆ _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().

◆ _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 1010 of file xdr_cxx.C.

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

1018 {
1019  switch (mode)
1020  {
1021  case ENCODE:
1022  case DECODE:
1023  {
1024 #ifdef LIBMESH_HAVE_XDR
1025  libmesh_assert (this->is_open());
1026 
1027  if (len > 0)
1028  {
1029  if (xdr_proc)
1030  {
1031  xdr_vector(xdrs.get(),
1032  (char *) val,
1033  len,
1034  sizeof(XFP),
1035  xdr_proc);
1036  return;
1037  }
1038 
1039  // FIXME[JWP]: How to implement this for long double? Mac
1040  // OS X defines 'xdr_quadruple' but AFAICT, it does not
1041  // exist for Linux... for now, reading/writing XDR files
1042  // with long doubles drops back to double precision, but
1043  // you can still write long double ASCII files of course.
1044 
1045  // FIXME[RHS]: 128 bit FP has the same problem as long
1046  // double, only much worse since even _Quad/__float128
1047  // aren't standard either.
1048 
1049  std::vector<double> io_buffer (len);
1050 
1051  // Fill io_buffer if we are writing.
1052  if (mode == ENCODE)
1053  for (unsigned int i=0, cnt=0; i<len; i++)
1054  io_buffer[cnt++] = double(val[i]);
1055 
1056  xdr_vector(xdrs.get(),
1057  reinterpret_cast<char *>(io_buffer.data()),
1058  len,
1059  sizeof(double),
1060  (xdrproc_t) xdr_double);
1061 
1062  // Fill val array if we are reading.
1063  if (mode == DECODE)
1064  for (unsigned int i=0, cnt=0; i<len; i++)
1065  {
1066  val[i] = io_buffer[cnt++];
1067  }
1068  }
1069 
1070 #else
1071 
1072  libmesh_error_msg("ERROR: Functionality is not available.\n" \
1073  << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
1074  << "The XDR interface is not available in this installation");
1075 
1076 #endif
1077  return;
1078  }
1079 
1080  case READ:
1081  {
1082  libmesh_assert(in.get());
1083  libmesh_assert (in->good());
1084 
1085  for (unsigned int i=0; i<len; i++)
1086  {
1087  libmesh_assert(in.get());
1088  libmesh_assert (in->good());
1089  *in >> val[i];
1090  }
1091 
1092  return;
1093  }
1094 
1095  case WRITE:
1096  {
1097  libmesh_assert(out.get());
1098  libmesh_assert (out->good());
1099 
1100  // Save stream flags
1101  std::ios_base::fmtflags out_flags = out->flags();
1102 
1103  // We will use scientific notation with specified digit
1104  // count in the following output. The desired precision and
1105  // format will automatically determine the width.
1106  *out << std::scientific
1107  << std::setprecision(n_digits);
1108 
1109  if (line_break == libMesh::invalid_uint)
1110  for (unsigned int i=0; i<len; i++)
1111  {
1112  libmesh_assert(out.get());
1113  libmesh_assert (out->good());
1114  *out << val[i] << ' ';
1115  }
1116  else
1117  {
1118  const unsigned imax = std::min(line_break, len);
1119  unsigned int cnt=0;
1120  while (cnt < len)
1121  {
1122  for (unsigned int i=0; (i<imax && cnt<len); i++)
1123  {
1124  libmesh_assert(out.get());
1125  libmesh_assert (out->good());
1126  *out << val[cnt++];
1127 
1128  // Write a space unless this is the last character on the current line.
1129  if (i+1 != imax)
1130  *out << " ";
1131  }
1132  libmesh_assert(out.get());
1133  libmesh_assert (out->good());
1134  *out << '\n';
1135  }
1136  }
1137 
1138  // Restore stream flags
1139  out->flags(out_flags);
1140 
1141  return;
1142  }
1143 
1144  default:
1145  libmesh_error_msg("Invalid mode = " << mode);
1146  }
1147 }
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:286
const XdrMODE mode
The mode used for accessing the file.
Definition: xdr_cxx.h:234
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
libmesh_assert(ctx)
std::unique_ptr< XDR > xdrs
Pointer to the standard XDR struct.
Definition: xdr_cxx.h:247
bool is_open() const
Definition: xdr_cxx.C:347

◆ 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.

References mode.

135 { return mode; }
const XdrMODE mode
The mode used for accessing the file.
Definition: xdr_cxx.h:234

◆ close()

void libMesh::Xdr::close ( )

Closes the file if it is open.

Definition at line 279 of file xdr_cxx.C.

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

Referenced by libMesh::RBSCMEvaluation::legacy_read_offline_data_from_files(), libMesh::RBEvaluation::legacy_read_offline_data_from_files(), libMesh::RBSCMEvaluation::legacy_write_offline_data_to_files(), libMesh::RBEvaluation::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().

280 {
281  switch (mode)
282  {
283  case ENCODE:
284  case DECODE:
285  {
286 #ifdef LIBMESH_HAVE_XDR
287 
288  if (xdrs)
289  {
290  xdr_destroy (xdrs.get());
291  xdrs.reset();
292  }
293 
294  if (fp)
295  {
296  fflush(fp);
297  fclose(fp);
298  fp = nullptr;
299  }
300 #else
301 
302  libmesh_error_msg("ERROR: Functionality is not available.\n" \
303  << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
304  << "The XDR interface is not available in this installation");
305 
306 #endif
307  file_name = "";
308  return;
309  }
310 
311  case READ:
312  {
313  if (in.get() != nullptr)
314  {
315  in.reset();
316 
317  if (bzipped_file || xzipped_file)
318  remove_unzipped_file(file_name);
319  }
320  file_name = "";
321  return;
322  }
323 
324  case WRITE:
325  {
326  if (out.get() != nullptr)
327  {
328  out.reset();
329 
330  if (bzipped_file)
331  bzip_file(std::string(file_name.begin(), file_name.end()-4));
332 
333  else if (xzipped_file)
334  xzip_file(std::string(file_name.begin(), file_name.end()-3));
335  }
336  file_name = "";
337  return;
338  }
339 
340  default:
341  libmesh_error_msg("Invalid mode = " << mode);
342  }
343 }
FILE * fp
File pointer.
Definition: xdr_cxx.h:252
std::string file_name
The file name.
Definition: xdr_cxx.h:239
bool xzipped_file
Definition: xdr_cxx.h:275
bool bzipped_file
Definition: xdr_cxx.h:275
const XdrMODE mode
The mode used for accessing the file.
Definition: xdr_cxx.h:234
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
std::unique_ptr< XDR > xdrs
Pointer to the standard XDR struct.
Definition: xdr_cxx.h:247

◆ comment()

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

Writes or reads (ignores) a comment line.

Definition at line 1300 of file xdr_cxx.C.

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

Referenced by libMesh::System::write_serialized_data().

1301 {
1302  switch (mode)
1303  {
1304  case ENCODE:
1305  case DECODE:
1306  {
1307  return;
1308  }
1309 
1310  case READ:
1311  {
1312  libmesh_assert(in.get());
1313  libmesh_assert (in->good());
1314  in->getline(comm, comm_len);
1315  return;
1316  }
1317 
1318  case WRITE:
1319  {
1320  libmesh_assert(out.get());
1321  libmesh_assert (out->good());
1322  *out << "\t " << comment_in << '\n';
1323  return;
1324  }
1325 
1326  default:
1327  libmesh_error_msg("Invalid mode = " << mode);
1328  }
1329 }
const int comm_len
A buffer to put comment strings into.
Definition: xdr_cxx.h:269
char comm[xdr_MAX_STRING_LENGTH]
Definition: xdr_cxx.h:270
const XdrMODE mode
The mode used for accessing the file.
Definition: xdr_cxx.h:234
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
libmesh_assert(ctx)

◆ 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 780 of file xdr_cxx.C.

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_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::XdrIO::read_header(), libMesh::System::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::System::read_legacy_data(), 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::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().

781 {
782  switch (mode)
783  {
784  case ENCODE:
785  case DECODE:
786  {
787 #ifdef LIBMESH_HAVE_XDR
788 
790 
791  xdr_translate(xdrs.get(), a);
792 
793 #else
794 
795  libmesh_error_msg("ERROR: Functionality is not available.\n" \
796  << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
797  << "The XDR interface is not available in this installation");
798 
799 #endif
800  return;
801  }
802 
803  case READ:
804  {
805  libmesh_assert(in.get());
806  libmesh_assert (in->good());
807 
808  this->do_read(a);
809 
810  return;
811  }
812 
813  case WRITE:
814  {
815  libmesh_assert(out.get());
816  libmesh_assert (out->good());
817 
818  // We will use scientific notation sufficient to exactly
819  // represent our floating point precision in the following
820  // output. The desired precision and format will
821  // automatically determine the width.
822  *out << std::scientific
823  << std::setprecision(std::numeric_limits<T>::max_digits10);
824 
825  this->do_write(a);
826 
827  // If there's a comment provided, write a tab character and
828  // then the comment.
829  if (comment_in != "")
830  *out << "\t " << comment_in;
831 
832  // Go to the next line.
833  *out << '\n';
834 
835  return;
836  }
837 
838  default:
839  libmesh_error_msg("Invalid mode = " << mode);
840  }
841 }
void do_write(T &a)
Helper method for writing different data types.
Definition: xdr_cxx.C:731
const XdrMODE mode
The mode used for accessing the file.
Definition: xdr_cxx.h:234
void do_read(T &a)
Helper method for reading different data types.
Definition: xdr_cxx.C:666
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
libmesh_assert(ctx)
std::unique_ptr< XDR > xdrs
Pointer to the standard XDR struct.
Definition: xdr_cxx.h:247
bool is_open() const
Definition: xdr_cxx.C:347

◆ data_stream() [1/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 845 of file xdr_cxx.C.

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().

846 {
847  switch (mode)
848  {
849  case ENCODE:
850  {
851 #ifdef LIBMESH_HAVE_XDR
852 
853  libmesh_assert (this->is_open());
854 
855  unsigned int size_of_type = cast_int<unsigned int>(sizeof(T));
856 
857  xdr_vector(xdrs.get(),
858  (char *) val,
859  len,
860  size_of_type,
861  xdr_translator<T>());
862 #else
863  libmesh_error_msg("ERROR: Functionality is not available.\n" \
864  << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
865  << "The XDR interface is not available in this installation");
866 
867 #endif
868  return;
869  }
870 
871  case DECODE:
872  {
873 #ifdef LIBMESH_HAVE_XDR
874 
875  libmesh_assert (this->is_open());
876 
877  unsigned int size_of_type = cast_int<unsigned int>(sizeof(T));
878 
879  if (len > 0)
880  xdr_vector(xdrs.get(),
881  (char *) val,
882  len,
883  size_of_type,
884  xdr_translator<T>());
885 #else
886  libmesh_error_msg("ERROR: Functionality is not available.\n" \
887  << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
888  << "The XDR interface is not available in this installation");
889 
890 #endif
891  return;
892  }
893 
894  case READ:
895  {
896  libmesh_assert(in.get());
897  libmesh_assert (in->good());
898 
899  for (unsigned int i=0; i<len; i++)
900  {
901  libmesh_assert(in.get());
902  libmesh_assert (in->good());
903  *in >> val[i];
904  }
905 
906  return;
907  }
908 
909  case WRITE:
910  {
911  libmesh_assert(out.get());
912  libmesh_assert (out->good());
913 
914  // We will use scientific notation sufficient to exactly
915  // represent our floating point precision in the following
916  // output. The desired precision and format will
917  // automatically determine the width.
918  *out << std::scientific
919  << std::setprecision(std::numeric_limits<T>::max_digits10);
920 
921  if (line_break == libMesh::invalid_uint)
922  for (unsigned int i=0; i<len; i++)
923  {
924  libmesh_assert(out.get());
925  libmesh_assert (out->good());
926  *out << val[i] << " ";
927  }
928  else
929  {
930  const unsigned imax = std::min(line_break, len);
931  unsigned int cnt=0;
932  while (cnt < len)
933  {
934  for (unsigned int i=0; (i<imax && cnt<len); i++)
935  {
936  libmesh_assert(out.get());
937  libmesh_assert (out->good());
938  *out << val[cnt++];
939 
940  // Write a space unless this is the last character on the current line.
941  if (i+1 != imax)
942  *out << " ";
943  }
944  libmesh_assert(out.get());
945  libmesh_assert (out->good());
946  *out << '\n';
947  }
948  }
949 
950  return;
951  }
952 
953  default:
954  libmesh_error_msg("Invalid mode = " << mode);
955  }
956 }
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:286
const XdrMODE mode
The mode used for accessing the file.
Definition: xdr_cxx.h:234
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
libmesh_assert(ctx)
std::unique_ptr< XDR > xdrs
Pointer to the standard XDR struct.
Definition: xdr_cxx.h:247
bool is_open() const
Definition: xdr_cxx.C:347

◆ data_stream() [2/7]

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

Definition at line 961 of file xdr_cxx.C.

References _xfp_data_stream().

962 {
963  this->_xfp_data_stream
964  (val, len,
965 #ifdef LIBMESH_HAVE_XDR
966  (xdrproc_t)xdr_double,
967 #else
968  nullptr,
969 #endif
970  line_break, std::numeric_limits<double>::max_digits10);
971 }
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.

◆ data_stream() [3/7]

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

Definition at line 976 of file xdr_cxx.C.

References _xfp_data_stream().

977 {
978  this->_xfp_data_stream
979  (val, len,
980 #ifdef LIBMESH_HAVE_XDR
981  (xdrproc_t)xdr_float,
982 #else
983  nullptr,
984 #endif
985  line_break, std::numeric_limits<float>::max_digits10);
986 }
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.

◆ data_stream() [4/7]

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

Definition at line 991 of file xdr_cxx.C.

References _xfp_data_stream().

992 {
993  this->_xfp_data_stream
994  (val, len, nullptr, line_break,
995  std::numeric_limits<long double>::max_digits10);
996 }
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.

◆ data_stream() [5/7]

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

Definition at line 1001 of file xdr_cxx.C.

References _xfp_data_stream().

1002 {
1003  this->_xfp_data_stream(val, len, nullptr, line_break, 36);
1004 }
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.

◆ data_stream() [6/7]

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

Definition at line 1152 of file xdr_cxx.C.

References _complex_data_stream().

1153 {
1154  this->_complex_data_stream(val, len, line_break);
1155 }
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:1168

◆ data_stream() [7/7]

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

Definition at line 1160 of file xdr_cxx.C.

References _complex_data_stream().

1161 {
1162  this->_complex_data_stream(val, len, line_break);
1163 }
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:1168

◆ do_read() [1/5]

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

Helper method for reading different data types.

Definition at line 666 of file xdr_cxx.C.

References comm, comm_len, and in.

Referenced by data().

667 {
668  *in >> a;
669  in->getline(comm, comm_len);
670 }
const int comm_len
A buffer to put comment strings into.
Definition: xdr_cxx.h:269
char comm[xdr_MAX_STRING_LENGTH]
Definition: xdr_cxx.h:270
std::unique_ptr< std::istream > in
The input file stream.
Definition: xdr_cxx.h:259

◆ do_read() [2/5]

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

Definition at line 673 of file xdr_cxx.C.

References comm, comm_len, and in.

674 {
675  T r, i;
676  *in >> r >> i;
677  a = std::complex<T>(r,i);
678  in->getline(comm, comm_len);
679 }
const int comm_len
A buffer to put comment strings into.
Definition: xdr_cxx.h:269
char comm[xdr_MAX_STRING_LENGTH]
Definition: xdr_cxx.h:270
std::unique_ptr< std::istream > in
The input file stream.
Definition: xdr_cxx.h:259

◆ do_read() [3/5]

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

Definition at line 697 of file xdr_cxx.C.

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

698 {
699  unsigned int length=0;
700  data(length, "# vector length");
701  a.resize(length);
702 
703  for (T & a_i : a)
704  {
705  libmesh_assert(in.get());
706  libmesh_assert (in->good());
707  *in >> a_i;
708  }
709  in->getline(comm, comm_len);
710 }
const int comm_len
A buffer to put comment strings into.
Definition: xdr_cxx.h:269
char comm[xdr_MAX_STRING_LENGTH]
Definition: xdr_cxx.h:270
void data(T &a, std::string_view comment="")
Inputs or outputs a single value.
Definition: xdr_cxx.C:780
std::unique_ptr< std::istream > in
The input file stream.
Definition: xdr_cxx.h:259
libmesh_assert(ctx)

◆ do_read() [4/5]

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

Definition at line 713 of file xdr_cxx.C.

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

714 {
715  unsigned int length=0;
716  data(length, "# vector length x 2 (complex)");
717  a.resize(length);
718 
719  for (std::complex<T> & a_i : a)
720  {
721  T r, im;
722  libmesh_assert(in.get());
723  libmesh_assert (in->good());
724  *in >> r >> im;
725  a_i = std::complex<T>(r,im);
726  }
727  in->getline(comm, comm_len);
728 }
const int comm_len
A buffer to put comment strings into.
Definition: xdr_cxx.h:269
char comm[xdr_MAX_STRING_LENGTH]
Definition: xdr_cxx.h:270
void data(T &a, std::string_view comment="")
Inputs or outputs a single value.
Definition: xdr_cxx.C:780
std::unique_ptr< std::istream > in
The input file stream.
Definition: xdr_cxx.h:259
libmesh_assert(ctx)

◆ do_read() [5/5]

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

Definition at line 682 of file xdr_cxx.C.

References comm, comm_len, and in.

683 {
684  in->getline(comm, comm_len);
685 
686  a = "";
687 
688  for (unsigned int c=0, sl=std::strlen(comm); c!=sl; c++)
689  {
690  if (comm[c] == '\t')
691  break;
692  a.push_back(comm[c]);
693  }
694 }
const int comm_len
A buffer to put comment strings into.
Definition: xdr_cxx.h:269
char comm[xdr_MAX_STRING_LENGTH]
Definition: xdr_cxx.h:270
std::unique_ptr< std::istream > in
The input file stream.
Definition: xdr_cxx.h:259

◆ do_write() [1/4]

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

Helper method for writing different data types.

Definition at line 731 of file xdr_cxx.C.

References out.

Referenced by data(), and do_write().

731 { *out << a; }
std::unique_ptr< std::ostream > out
The output file stream.
Definition: xdr_cxx.h:264

◆ do_write() [2/4]

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

Definition at line 734 of file xdr_cxx.C.

References out.

735 {
736  *out << a.real() << "\t " << a.imag();
737 }
std::unique_ptr< std::ostream > out
The output file stream.
Definition: xdr_cxx.h:264

◆ do_write() [3/4]

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

Definition at line 740 of file xdr_cxx.C.

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

741 {
742  std::size_t length = a.size();
743  data(length, "# vector length");
744 
745  // Use scientific precision with lots of digits for the original type T.
746  *out << std::scientific
747  << std::setprecision(std::numeric_limits<T>::max_digits10);
748 
749  for (T & a_i : a)
750  {
751  libmesh_assert(out.get());
752  libmesh_assert (out->good());
753  this->do_write(a_i);
754  *out << "\t ";
755  }
756 }
void do_write(T &a)
Helper method for writing different data types.
Definition: xdr_cxx.C:731
void data(T &a, std::string_view comment="")
Inputs or outputs a single value.
Definition: xdr_cxx.C:780
std::unique_ptr< std::ostream > out
The output file stream.
Definition: xdr_cxx.h:264
libmesh_assert(ctx)

◆ do_write() [4/4]

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

Definition at line 759 of file xdr_cxx.C.

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

760 {
761  std::size_t length=a.size();
762  data(length, "# vector length x 2 (complex)");
763 
764  // Use scientific precision with lots of digits for the original type T.
765  *out << std::scientific
766  << std::setprecision(std::numeric_limits<T>::max_digits10);
767 
768  for (std::complex<T> & a_i : a)
769  {
770  libmesh_assert(out.get());
771  libmesh_assert (out->good());
772  this->do_write(a_i);
773  *out << "\t ";
774  }
775 }
void do_write(T &a)
Helper method for writing different data types.
Definition: xdr_cxx.C:731
void data(T &a, std::string_view comment="")
Inputs or outputs a single value.
Definition: xdr_cxx.C:780
std::unique_ptr< std::ostream > out
The output file stream.
Definition: xdr_cxx.h:264
libmesh_assert(ctx)

◆ 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 397 of file xdr_cxx.C.

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

398 {
399  switch (mode)
400  {
401  case ENCODE:
402  case DECODE:
403  {
404 #ifdef LIBMESH_HAVE_XDR
406 
407  // Are we already at eof?
408  if (feof(fp))
409  return true;
410 
411  // Or about to reach eof?
412  int next = fgetc(fp);
413  if (next == EOF)
414  {
415  // We should *only* be at EOF, not otherwise broken
416  libmesh_assert(feof(fp));
417  libmesh_assert(!ferror(fp));
418 
419  // Reset the EOF indicator
420  clearerr(fp);
421  libmesh_assert(!ferror(fp));
422 
423  // We saw EOF
424  return true;
425  }
426 
427  // We didn't see EOF; restore whatever we did see.
428  ungetc(next, fp);
429  break;
430 #else
431 
432  libmesh_error_msg("ERROR: Functionality is not available.\n" \
433  << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
434  << "The XDR interface is not available in this installation");
435 
436  return false;
437 
438 #endif
439 
440  }
441  case READ:
442  {
443  libmesh_assert(in.get());
444 
445  // Are we already at eof?
446  if (in->eof())
447  return true;
448 
449  // Or about to reach eof?
450  int next = in->peek();
451  if (next == EOF)
452  {
453  // We should *only* be at EOF, not otherwise broken
454  libmesh_assert(in->eof());
455  libmesh_assert(!in->fail());
456 
457  // Reset the EOF indicator
458  in->clear();
459  libmesh_assert(in->good());
460 
461  // We saw EOF
462  return true;
463  }
464  break;
465  }
466  default:
467  libmesh_error();
468  }
469 
470  return false;
471 }
FILE * fp
File pointer.
Definition: xdr_cxx.h:252
const XdrMODE mode
The mode used for accessing the file.
Definition: xdr_cxx.h:234
std::unique_ptr< std::istream > in
The input file stream.
Definition: xdr_cxx.h:259
libmesh_assert(ctx)
static const unsigned int next[3]
A lookup table for the increment modulo 3 operation, for iterating through the three nodes per elemen...

◆ is_open()

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

Definition at line 347 of file xdr_cxx.C.

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().

348 {
349  switch (mode)
350  {
351  case ENCODE:
352  case DECODE:
353  {
354 #ifdef LIBMESH_HAVE_XDR
355 
356  if (fp)
357  if (xdrs)
358  return true;
359 
360  return false;
361 
362 #else
363 
364  libmesh_error_msg("ERROR: Functionality is not available.\n" \
365  << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
366  << "The XDR interface is not available in this installation");
367 
368  return false;
369 
370 #endif
371 
372  }
373 
374  case READ:
375  {
376  if (in.get() != nullptr)
377  return in->good();
378  return false;
379  }
380 
381  case WRITE:
382  {
383  if (out.get() != nullptr)
384  return out->good();
385  return false;
386  }
387 
388  default:
389  libmesh_error_msg("Invalid mode = " << mode);
390  }
391 
392  return false;
393 }
FILE * fp
File pointer.
Definition: xdr_cxx.h:252
const XdrMODE mode
The mode used for accessing the file.
Definition: xdr_cxx.h:234
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
std::unique_ptr< XDR > xdrs
Pointer to the standard XDR struct.
Definition: xdr_cxx.h:247

◆ open()

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

Opens the file.

Definition at line 168 of file xdr_cxx.C.

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

Referenced by Xdr().

169 {
170  file_name = std::move(name);
171 
172  if (file_name == "")
173  return;
174 
175  switch (mode)
176  {
177  case ENCODE:
178  case DECODE:
179  {
180 #ifdef LIBMESH_HAVE_XDR
181 
182  fp = fopen(file_name.c_str(), (mode == ENCODE) ? "w" : "r");
183  if (!fp)
184  libmesh_file_error(file_name.c_str());
185  xdrs = std::make_unique<XDR>();
186  xdrstdio_create (xdrs.get(), fp, (mode == ENCODE) ? XDR_ENCODE : XDR_DECODE);
187 #else
188 
189  libmesh_error_msg("ERROR: Functionality is not available.\n" \
190  << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
191  << "The XDR interface is not available in this installation");
192 
193 #endif
194  return;
195 
196  }
197 
198  case READ:
199  {
200  gzipped_file = (file_name.rfind(".gz") == file_name.size() - 3);
201  bzipped_file = (file_name.rfind(".bz2") == file_name.size() - 4);
202  xzipped_file = (file_name.rfind(".xz") == file_name.size() - 3);
203 
204  if (gzipped_file)
205  {
206 #ifdef LIBMESH_HAVE_GZSTREAM
207  igzstream * inf = new igzstream;
208  libmesh_assert(inf);
209  in.reset(inf);
210  inf->open(file_name.c_str(), std::ios::in);
211 #else
212  libmesh_error_msg("ERROR: need gzstream to handle .gz files!!!");
213 #endif
214  }
215  else
216  {
217  std::ifstream * inf = new std::ifstream;
218  libmesh_assert(inf);
219  in.reset(inf);
220 
221  std::string new_name = Utility::unzip_file(file_name);
222 
223  inf->open(new_name.c_str(), std::ios::in);
224  }
225 
226  libmesh_assert(in.get());
227 
228  if (!in->good())
229  libmesh_file_error(file_name);
230  return;
231  }
232 
233  case WRITE:
234  {
235  gzipped_file = (file_name.rfind(".gz") == file_name.size() - 3);
236  bzipped_file = (file_name.rfind(".bz2") == file_name.size() - 4);
237  xzipped_file = (file_name.rfind(".xz") == file_name.size() - 3);
238 
239  if (gzipped_file)
240  {
241 #ifdef LIBMESH_HAVE_GZSTREAM
242  ogzstream * outf = new ogzstream;
243  libmesh_assert(outf);
244  out.reset(outf);
245  outf->open(file_name.c_str(), std::ios::out);
246 #else
247  libmesh_error_msg("ERROR: need gzstream to handle .gz files!!!");
248 #endif
249  }
250  else
251  {
252  std::ofstream * outf = new std::ofstream;
253  libmesh_assert(outf);
254  out.reset(outf);
255 
256  std::string new_name = file_name;
257 
258  if (bzipped_file)
259  new_name.erase(new_name.end() - 4, new_name.end());
260 
261  if (xzipped_file)
262  new_name.erase(new_name.end() - 3, new_name.end());
263 
264  outf->open(new_name.c_str(), std::ios::out);
265  }
266 
267  libmesh_assert(out.get());
268  libmesh_assert (out->good());
269  return;
270  }
271 
272  default:
273  libmesh_error_msg("Invalid mode = " << mode);
274  }
275 }
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Definition: elem_quality.C:42
FILE * fp
File pointer.
Definition: xdr_cxx.h:252
std::string file_name
The file name.
Definition: xdr_cxx.h:239
bool xzipped_file
Definition: xdr_cxx.h:275
bool bzipped_file
Definition: xdr_cxx.h:275
const XdrMODE mode
The mode used for accessing the file.
Definition: xdr_cxx.h:234
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
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:156
libmesh_assert(ctx)
std::unique_ptr< XDR > xdrs
Pointer to the standard XDR struct.
Definition: xdr_cxx.h:247
OStreamProxy out

◆ 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.

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

149 { libmesh_assert (writing()); data(a); return *this; }
bool writing() const
Definition: xdr_cxx.h:129
void data(T &a, std::string_view comment="")
Inputs or outputs a single value.
Definition: xdr_cxx.C:780
libmesh_assert(ctx)

◆ 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.

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

155 { libmesh_assert (reading()); data(a); return *this; }
void data(T &a, std::string_view comment="")
Inputs or outputs a single value.
Definition: xdr_cxx.C:780
libmesh_assert(ctx)
bool reading() const
Definition: xdr_cxx.h:123

◆ 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.

References version_number.

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

171 { version_number = ver; }
int version_number
Version of the file being read.
Definition: xdr_cxx.h:280

◆ 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.

References version_number.

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

176 { return version_number; }
int version_number
Version of the file being read.
Definition: xdr_cxx.h:280

◆ 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(), 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(), 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

The input file stream.

Definition at line 259 of file xdr_cxx.h.

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

◆ 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(), 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: