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

References file_name, and open().

104  :
105  mode(m),
106  file_name(std::move(name)),
107 #ifdef LIBMESH_HAVE_XDR
108  fp(nullptr),
109 #endif
110  in(),
111  out(),
113  gzipped_file(false),
114  bzipped_file(false),
115  xzipped_file(false),
116  version_number(LIBMESH_VERSION_ID(LIBMESH_MAJOR_VERSION, LIBMESH_MINOR_VERSION, LIBMESH_MICRO_VERSION))
117 {
118  this->open(file_name);
119 }
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:166

◆ Xdr() [2/3]

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

Output stream based constructor.

Assumes mode WRITE.

Definition at line 123 of file xdr_cxx.C.

123  :
124  mode(WRITE),
125  file_name(),
126 #ifdef LIBMESH_HAVE_XDR
127  fp(nullptr),
128 #endif
129  in(),
130  out(std::make_unique<std::ostream>(stream.rdbuf())),
132  gzipped_file(false),
133  bzipped_file(false),
134  xzipped_file(false),
135  version_number(LIBMESH_VERSION_ID(LIBMESH_MAJOR_VERSION, LIBMESH_MINOR_VERSION, LIBMESH_MICRO_VERSION))
136 {
137 }
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 141 of file xdr_cxx.C.

141  :
142  mode(READ),
143  file_name(),
144 #ifdef LIBMESH_HAVE_XDR
145  fp(nullptr),
146 #endif
147  in(std::make_unique<std::istream>(stream.rdbuf())),
148  out(),
150  gzipped_file(false),
151  bzipped_file(false),
152  xzipped_file(false),
153  version_number(LIBMESH_VERSION_ID(LIBMESH_MAJOR_VERSION, LIBMESH_MINOR_VERSION, LIBMESH_MICRO_VERSION))
154 {
155 }
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 159 of file xdr_cxx.C.

References close().

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

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

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

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

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

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

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

1299 {
1300  switch (mode)
1301  {
1302  case ENCODE:
1303  case DECODE:
1304  {
1305  return;
1306  }
1307 
1308  case READ:
1309  {
1310  libmesh_assert(in.get());
1311  libmesh_assert (in->good());
1312  in->getline(comm, comm_len);
1313  return;
1314  }
1315 
1316  case WRITE:
1317  {
1318  libmesh_assert(out.get());
1319  libmesh_assert (out->good());
1320  *out << "\t " << comment_in << '\n';
1321  return;
1322  }
1323 
1324  default:
1325  libmesh_error_msg("Invalid mode = " << mode);
1326  }
1327 }
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 778 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().

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

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

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

◆ data_stream() [2/7]

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

Definition at line 959 of file xdr_cxx.C.

References _xfp_data_stream().

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

References _xfp_data_stream().

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

References _xfp_data_stream().

990 {
991  this->_xfp_data_stream
992  (val, len, nullptr, line_break,
993  std::numeric_limits<long double>::max_digits10);
994 }
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 999 of file xdr_cxx.C.

References _xfp_data_stream().

1000 {
1001  this->_xfp_data_stream(val, len, nullptr, line_break, 36);
1002 }
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 1150 of file xdr_cxx.C.

References _complex_data_stream().

1151 {
1152  this->_complex_data_stream(val, len, line_break);
1153 }
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:1166

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

References _complex_data_stream().

1159 {
1160  this->_complex_data_stream(val, len, line_break);
1161 }
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:1166

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

References comm, comm_len, and in.

Referenced by data().

665 {
666  *in >> a;
667  in->getline(comm, comm_len);
668 }
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 671 of file xdr_cxx.C.

References comm, comm_len, and in.

672 {
673  T r, i;
674  *in >> r >> i;
675  a = std::complex<T>(r,i);
676  in->getline(comm, comm_len);
677 }
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 695 of file xdr_cxx.C.

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

696 {
697  unsigned int length=0;
698  data(length, "# vector length");
699  a.resize(length);
700 
701  for (T & a_i : a)
702  {
703  libmesh_assert(in.get());
704  libmesh_assert (in->good());
705  *in >> a_i;
706  }
707  in->getline(comm, comm_len);
708 }
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:778
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 711 of file xdr_cxx.C.

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

712 {
713  unsigned int length=0;
714  data(length, "# vector length x 2 (complex)");
715  a.resize(length);
716 
717  for (std::complex<T> & a_i : a)
718  {
719  T r, im;
720  libmesh_assert(in.get());
721  libmesh_assert (in->good());
722  *in >> r >> im;
723  a_i = std::complex<T>(r,im);
724  }
725  in->getline(comm, comm_len);
726 }
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:778
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 680 of file xdr_cxx.C.

References comm, comm_len, and in.

681 {
682  in->getline(comm, comm_len);
683 
684  a = "";
685 
686  for (unsigned int c=0, sl=std::strlen(comm); c!=sl; c++)
687  {
688  if (comm[c] == '\t')
689  break;
690  a.push_back(comm[c]);
691  }
692 }
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 729 of file xdr_cxx.C.

References out.

Referenced by data(), and do_write().

729 { *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 732 of file xdr_cxx.C.

References out.

733 {
734  *out << a.real() << "\t " << a.imag();
735 }
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 738 of file xdr_cxx.C.

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

739 {
740  std::size_t length = a.size();
741  data(length, "# vector length");
742 
743  // Use scientific precision with lots of digits for the original type T.
744  *out << std::scientific
745  << std::setprecision(std::numeric_limits<T>::max_digits10);
746 
747  for (T & a_i : a)
748  {
749  libmesh_assert(out.get());
750  libmesh_assert (out->good());
751  this->do_write(a_i);
752  *out << "\t ";
753  }
754 }
void do_write(T &a)
Helper method for writing different data types.
Definition: xdr_cxx.C:729
void data(T &a, std::string_view comment="")
Inputs or outputs a single value.
Definition: xdr_cxx.C:778
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 757 of file xdr_cxx.C.

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

758 {
759  std::size_t length=a.size();
760  data(length, "# vector length x 2 (complex)");
761 
762  // Use scientific precision with lots of digits for the original type T.
763  *out << std::scientific
764  << std::setprecision(std::numeric_limits<T>::max_digits10);
765 
766  for (std::complex<T> & a_i : a)
767  {
768  libmesh_assert(out.get());
769  libmesh_assert (out->good());
770  this->do_write(a_i);
771  *out << "\t ";
772  }
773 }
void do_write(T &a)
Helper method for writing different data types.
Definition: xdr_cxx.C:729
void data(T &a, std::string_view comment="")
Inputs or outputs a single value.
Definition: xdr_cxx.C:778
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 395 of file xdr_cxx.C.

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

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

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

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