28 #include "libmesh/xdr_cxx.h" 29 #include "libmesh/libmesh_logging.h" 30 #ifdef LIBMESH_HAVE_GZSTREAM 31 # include "gzstream.h" 33 #include "libmesh/utility.h" 35 #ifdef LIBMESH_HAVE_UNISTD_H 38 #ifdef LIBMESH_HAVE_PROCESS_H 46 void bzip_file (std::string_view unzipped_name)
48 #ifdef LIBMESH_HAVE_BZIP 49 LOG_SCOPE(
"system(bzip2)",
"XdrIO");
51 std::string system_string =
"bzip2 -f ";
52 system_string += unzipped_name;
53 if (std::system(system_string.c_str()))
54 libmesh_file_error(system_string);
56 libmesh_error_msg(
"ERROR: need bzip2/bunzip2 to create " << unzipped_name <<
".bz2");
60 void xzip_file (std::string_view unzipped_name)
62 #ifdef LIBMESH_HAVE_XZ 63 LOG_SCOPE(
"system(xz)",
"XdrIO");
65 std::string system_string =
"xz -f ";
66 system_string += unzipped_name;
67 if (std::system(system_string.c_str()))
68 libmesh_file_error(system_string);
70 libmesh_error_msg(
"ERROR: need xz to create " << unzipped_name <<
".xz");
76 void remove_unzipped_file (std::string_view
name)
78 std::ostringstream pid_suffix;
79 pid_suffix <<
'_' << getpid();
83 if (
name.rfind(
".bz2") ==
name.size() - 4)
85 std::string new_name(
name.begin(),
name.end()-4);
86 new_name += pid_suffix.str();
87 std::remove(new_name.c_str());
89 if (
name.rfind(
".xz") ==
name.size() - 3)
91 std::string new_name(
name.begin(),
name.end()-3);
92 new_name += pid_suffix.str();
93 std::remove(new_name.c_str());
107 #ifdef LIBMESH_HAVE_XDR
116 version_number(LIBMESH_VERSION_ID(LIBMESH_MAJOR_VERSION, LIBMESH_MINOR_VERSION, LIBMESH_MICRO_VERSION))
126 #ifdef LIBMESH_HAVE_XDR
130 out(
std::make_unique<
std::ostream>(stream.rdbuf())),
135 version_number(LIBMESH_VERSION_ID(LIBMESH_MAJOR_VERSION, LIBMESH_MINOR_VERSION, LIBMESH_MICRO_VERSION))
144 #ifdef LIBMESH_HAVE_XDR
147 in(
std::make_unique<
std::istream>(stream.rdbuf())),
153 version_number(LIBMESH_VERSION_ID(LIBMESH_MAJOR_VERSION, LIBMESH_MINOR_VERSION, LIBMESH_MICRO_VERSION))
178 #ifdef LIBMESH_HAVE_XDR 183 xdrs = std::make_unique<XDR>();
184 xdrstdio_create (
xdrs.get(),
fp, (
mode ==
ENCODE) ? XDR_ENCODE : XDR_DECODE);
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");
204 #ifdef LIBMESH_HAVE_GZSTREAM 205 auto inf = std::make_unique<igzstream>();
207 inf->open(
file_name.c_str(), std::ios::in);
210 libmesh_error_msg(
"ERROR: need gzstream to handle .gz files!!!");
215 auto inf = std::make_unique<std::ifstream>();
220 inf->open(new_name.c_str(), std::ios::in);
239 #ifdef LIBMESH_HAVE_GZSTREAM 240 auto outf = std::make_unique<ogzstream>();
243 out = std::move(outf);
245 libmesh_error_msg(
"ERROR: need gzstream to handle .gz files!!!");
250 auto outf = std::make_unique<std::ofstream>();
256 new_name.erase(new_name.end() - 4, new_name.end());
259 new_name.erase(new_name.end() - 3, new_name.end());
262 out = std::move(outf);
271 libmesh_error_msg(
"Invalid mode = " <<
mode);
284 #ifdef LIBMESH_HAVE_XDR 288 xdr_destroy (
xdrs.get());
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");
311 if (
in.get() !=
nullptr)
324 if (
out.get() !=
nullptr)
339 libmesh_error_msg(
"Invalid mode = " <<
mode);
352 #ifdef LIBMESH_HAVE_XDR 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");
374 if (
in.get() !=
nullptr)
381 if (
out.get() !=
nullptr)
387 libmesh_error_msg(
"Invalid mode = " <<
mode);
402 #ifdef LIBMESH_HAVE_XDR 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");
473 #ifdef LIBMESH_HAVE_XDR 479 template <
typename T>
480 xdrproc_t xdr_translator();
482 template <
typename T>
483 bool xdr_translate(XDR * x, T & a)
485 return (xdr_translator<T>())(x, &a, 0);
489 bool xdr_translate(XDR * x,
493 std::copy(s.begin(), s.end(), sptr);
500 bool b = xdr_string(x, &begin, length);
503 length = cast_int<unsigned int>(std::strlen(sptr));
505 std::copy(sptr, sptr+length, s.begin());
510 template <
typename T>
511 bool xdr_translate(XDR * x, std::complex<T> & a)
513 T r = a.real(), i = a.imag();
514 bool b1 = xdr_translate(x, r);
515 bool b2 = xdr_translate(x, i);
516 a = std::complex<T>(r,i);
520 template <
typename T>
521 bool xdr_translate(XDR * x, std::vector<T> & a)
523 unsigned int length = cast_int<unsigned int>(a.size());
524 xdr_u_int(x, &length);
528 return xdr_vector(x, reinterpret_cast<char *>(a.data()), length,
sizeof(T),
529 xdr_translator<T>());
535 template <
typename T>
536 bool xdr_translate(XDR * x, std::vector<std::complex<T>> & a)
538 unsigned int length = cast_int<unsigned int>(a.size());
539 bool b = xdr_u_int(x, &length);
542 if (!xdr_translate(x, val))
548 bool xdr_translate(XDR * x, std::vector<std::string> & s)
550 unsigned int length = cast_int<unsigned int>(s.size());
551 bool b = xdr_u_int(x, &length);
554 if (!xdr_translate(x, val))
560 xdrproc_t xdr_translator<int>()
564 if (
sizeof(
int) <= 4)
565 return (xdrproc_t)(xdr_int);
566 else if (
sizeof(
int) ==
sizeof(
long long))
567 return (xdrproc_t)(xdr_longlong_t);
568 else if (
sizeof(
int) ==
sizeof(long))
569 return (xdrproc_t)(xdr_long);
575 xdrproc_t xdr_translator<unsigned int>()
579 if (
sizeof(
unsigned int) <= 4)
580 return (xdrproc_t)(xdr_u_int);
581 else if (
sizeof(
unsigned int) ==
sizeof(
unsigned long))
582 return (xdrproc_t)(xdr_u_long);
583 else if (
sizeof(
unsigned int) ==
sizeof(
unsigned long long))
584 return (xdrproc_t)(xdr_u_longlong_t);
590 xdrproc_t xdr_translator<long int>()
594 if (
sizeof(
long int) <= 4)
595 return (xdrproc_t)(xdr_long);
596 else if (
sizeof(
long int) ==
sizeof(
long long))
597 return (xdrproc_t)(xdr_longlong_t);
603 xdrproc_t xdr_translator<unsigned long int>()
607 if (
sizeof(
unsigned long int) <= 4)
608 return (xdrproc_t)(xdr_u_long);
609 else if (
sizeof(
unsigned long int) ==
sizeof(
unsigned long long))
610 return (xdrproc_t)(xdr_u_longlong_t);
624 xdrproc_t xdr_translator<long long>() {
return (xdrproc_t)(xdr_longlong_t); }
627 xdrproc_t xdr_translator<unsigned long long>() {
return (xdrproc_t)(xdr_u_longlong_t); }
630 xdrproc_t xdr_translator<short int>() {
return (xdrproc_t)(xdr_short); }
633 xdrproc_t xdr_translator<unsigned short int>() {
return (xdrproc_t)(xdr_u_short); }
636 xdrproc_t xdr_translator<char>() {
return (xdrproc_t)(xdr_char); }
639 xdrproc_t xdr_translator<signed char>() {
return (xdrproc_t)(xdr_char); }
642 xdrproc_t xdr_translator<unsigned char>() {
return (xdrproc_t)(xdr_u_char); }
645 xdrproc_t xdr_translator<float>() {
return (xdrproc_t)(xdr_float); }
648 xdrproc_t xdr_translator<double>() {
return (xdrproc_t)(xdr_double); }
652 xdrproc_t xdr_translator<long double>() {
return (xdrproc_t)(xdr_double); }
654 #ifdef LIBMESH_DEFAULT_QUADRUPLE_PRECISION 656 xdrproc_t xdr_translator<Real>() {
return (xdrproc_t)(xdr_double); }
663 template <
typename T>
670 template <
typename T>
675 a = std::complex<T>(r,i);
686 for (
unsigned int c=0, sl=std::strlen(
comm); c!=sl; c++)
690 a.push_back(
comm[c]);
694 template <
typename T>
697 unsigned int length=0;
698 data(length,
"# vector length");
710 template <
typename T>
713 unsigned int length=0;
714 data(length,
"# vector length x 2 (complex)");
717 for (std::complex<T> & a_i : a)
723 a_i = std::complex<T>(r,im);
728 template <
typename T>
731 template <
typename T>
734 *
out << a.real() <<
"\t " << a.imag();
737 template <
typename T>
740 std::size_t length = a.size();
741 data(length,
"# vector length");
744 *
out << std::scientific
745 << std::setprecision(std::numeric_limits<T>::max_digits10);
756 template <
typename T>
759 std::size_t length=a.size();
760 data(length,
"# vector length x 2 (complex)");
763 *
out << std::scientific
764 << std::setprecision(std::numeric_limits<T>::max_digits10);
766 for (std::complex<T> & a_i : a)
777 template <
typename T>
785 #ifdef LIBMESH_HAVE_XDR 789 xdr_translate(
xdrs.get(), a);
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");
820 *
out << std::scientific
821 << std::setprecision(std::numeric_limits<T>::max_digits10);
827 if (comment_in !=
"")
828 *
out <<
"\t " << comment_in;
837 libmesh_error_msg(
"Invalid mode = " <<
mode);
842 template <
typename T>
849 #ifdef LIBMESH_HAVE_XDR 853 unsigned int size_of_type = cast_int<unsigned int>(
sizeof(T));
855 xdr_vector(
xdrs.get(),
859 xdr_translator<T>());
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");
871 #ifdef LIBMESH_HAVE_XDR 875 unsigned int size_of_type = cast_int<unsigned int>(
sizeof(T));
878 xdr_vector(
xdrs.get(),
882 xdr_translator<T>());
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");
897 for (
unsigned int i=0; i<len; i++)
916 *
out << std::scientific
917 << std::setprecision(std::numeric_limits<T>::max_digits10);
920 for (
unsigned int i=0; i<len; i++)
924 *
out << val[i] <<
" ";
928 const unsigned imax = std::min(line_break, len);
932 for (
unsigned int i=0; (i<imax && cnt<len); i++)
952 libmesh_error_msg(
"Invalid mode = " <<
mode);
959 void Xdr::data_stream (
double * val,
const unsigned int len,
const unsigned int line_break)
963 #ifdef LIBMESH_HAVE_XDR
964 (xdrproc_t)xdr_double,
968 line_break, std::numeric_limits<double>::max_digits10);
978 #ifdef LIBMESH_HAVE_XDR
979 (xdrproc_t)xdr_float,
983 line_break, std::numeric_limits<float>::max_digits10);
989 void Xdr::data_stream (
long double * val,
const unsigned int len,
const unsigned int line_break)
992 (val, len,
nullptr, line_break,
993 std::numeric_limits<long double>::max_digits10);
997 #ifdef LIBMESH_DEFAULT_QUADRUPLE_PRECISION 1003 #endif // LIBMESH_DEFAULT_QUADRUPLE_PRECISION 1007 template <
typename XFP>
1009 #ifdef LIBMESH_HAVE_XDR
1014 const unsigned int line_break,
1022 #ifdef LIBMESH_HAVE_XDR 1029 xdr_vector(
xdrs.get(),
1047 std::vector<double> io_buffer (len);
1051 for (
unsigned int i=0, cnt=0; i<len; i++)
1052 io_buffer[cnt++] =
double(val[i]);
1054 xdr_vector(
xdrs.get(),
1055 reinterpret_cast<char *
>(io_buffer.data()),
1058 (xdrproc_t) xdr_double);
1062 for (
unsigned int i=0, cnt=0; i<len; i++)
1064 val[i] = io_buffer[cnt++];
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");
1083 for (
unsigned int i=0; i<len; i++)
1099 std::ios_base::fmtflags out_flags =
out->flags();
1104 *
out << std::scientific
1105 << std::setprecision(n_digits);
1108 for (
unsigned int i=0; i<len; i++)
1112 *
out << val[i] <<
' ';
1116 const unsigned imax = std::min(line_break, len);
1120 for (
unsigned int i=0; (i<imax && cnt<len); i++)
1137 out->flags(out_flags);
1143 libmesh_error_msg(
"Invalid mode = " <<
mode);
1150 void Xdr::data_stream (std::complex<double> * val,
const unsigned int len,
const unsigned int line_break)
1158 void Xdr::data_stream (std::complex<long double> * val,
const unsigned int len,
const unsigned int line_break)
1165 template <
typename T>
1173 #ifdef LIBMESH_HAVE_XDR 1185 std::vector<double> io_buffer (2*len);
1189 for (
unsigned int i=0, cnt=0; i<len; i++)
1191 io_buffer[cnt++] = val[i].real();
1192 io_buffer[cnt++] = val[i].imag();
1195 xdr_vector(
xdrs.get(),
1196 reinterpret_cast<char *
>(io_buffer.data()),
1199 (xdrproc_t) xdr_double);
1203 for (
unsigned int i=0, cnt=0; i<len; i++)
1205 double re = io_buffer[cnt++];
1206 double im = io_buffer[cnt++];
1207 val[i] = std::complex<T>(re, im);
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");
1225 for (
unsigned int i=0; i<len; i++)
1231 val[i] = std::complex<T>(re,im);
1244 std::ios_base::fmtflags out_flags =
out->flags();
1252 *
out << std::scientific
1253 << std::setprecision(std::numeric_limits<T>::max_digits10);
1256 for (
unsigned int i=0; i<len; i++)
1260 *
out << val[i].real() <<
' ' << val[i].imag() <<
' ';
1264 const unsigned imax = std::min(line_break, len);
1268 for (
unsigned int i=0; (i<imax && cnt<len); i++)
1272 *
out << val[cnt].real() <<
' ' << val[cnt].imag();
1286 out->flags(out_flags);
1292 libmesh_error_msg(
"Invalid mode = " <<
mode);
1320 *
out <<
"\t " << comment_in <<
'\n';
1325 libmesh_error_msg(
"Invalid mode = " <<
mode);
1334 template LIBMESH_EXPORT
void Xdr::data<int> (
int &, std::string_view);
1335 template LIBMESH_EXPORT
void Xdr::data<unsigned int> (
unsigned int &, std::string_view);
1336 template LIBMESH_EXPORT
void Xdr::data<unsigned short int> (
unsigned short int &, std::string_view);
1337 template LIBMESH_EXPORT
void Xdr::data<short int> (
short int &, std::string_view);
1338 template LIBMESH_EXPORT
void Xdr::data<unsigned long int> (
unsigned long int &, std::string_view);
1339 template LIBMESH_EXPORT
void Xdr::data<unsigned long long> (
unsigned long long &, std::string_view);
1340 template LIBMESH_EXPORT
void Xdr::data<long int> (
long int &, std::string_view);
1341 template LIBMESH_EXPORT
void Xdr::data<long long> (
long long &, std::string_view);
1342 template LIBMESH_EXPORT
void Xdr::data<char> (
char &, std::string_view);
1343 template LIBMESH_EXPORT
void Xdr::data<signed char> (
signed char &, std::string_view);
1344 template LIBMESH_EXPORT
void Xdr::data<unsigned char> (
unsigned char &, std::string_view);
1345 template LIBMESH_EXPORT
void Xdr::data<float> (
float &, std::string_view);
1346 template LIBMESH_EXPORT
void Xdr::data<double> (
double &, std::string_view);
1347 template LIBMESH_EXPORT
void Xdr::data<long double> (
long double &, std::string_view);
1348 template LIBMESH_EXPORT
void Xdr::data<std::complex<float>> (std::complex<float> &, std::string_view);
1349 template LIBMESH_EXPORT
void Xdr::data<std::complex<double>> (std::complex<double> &, std::string_view);
1350 template LIBMESH_EXPORT
void Xdr::data<std::complex<long double>> (std::complex<long double> &, std::string_view);
1351 template LIBMESH_EXPORT
void Xdr::data<std::string> (std::string &, std::string_view);
1352 template LIBMESH_EXPORT
void Xdr::data<std::vector<int>> (std::vector<int> &, std::string_view);
1353 template LIBMESH_EXPORT
void Xdr::data<std::vector<unsigned int>> (std::vector<unsigned int> &, std::string_view);
1354 template LIBMESH_EXPORT
void Xdr::data<std::vector<short int>> (std::vector<short int> &, std::string_view);
1355 template LIBMESH_EXPORT
void Xdr::data<std::vector<unsigned short int>> (std::vector<unsigned short int> &, std::string_view);
1356 template LIBMESH_EXPORT
void Xdr::data<std::vector<long int>> (std::vector<long int> &, std::string_view);
1357 template LIBMESH_EXPORT
void Xdr::data<std::vector<long long>> (std::vector<long long> &, std::string_view);
1358 template LIBMESH_EXPORT
void Xdr::data<std::vector<unsigned long int>> (std::vector<unsigned long int> &, std::string_view);
1359 template LIBMESH_EXPORT
void Xdr::data<std::vector<unsigned long long>> (std::vector<unsigned long long> &, std::string_view);
1360 template LIBMESH_EXPORT
void Xdr::data<std::vector<char>> (std::vector<char> &, std::string_view);
1361 template LIBMESH_EXPORT
void Xdr::data<std::vector<signed char>> (std::vector<signed char> &, std::string_view);
1362 template LIBMESH_EXPORT
void Xdr::data<std::vector<unsigned char>> (std::vector<unsigned char> &, std::string_view);
1363 template LIBMESH_EXPORT
void Xdr::data<std::vector<float>> (std::vector<float> &, std::string_view);
1364 template LIBMESH_EXPORT
void Xdr::data<std::vector<double>> (std::vector<double> &, std::string_view);
1365 template LIBMESH_EXPORT
void Xdr::data<std::vector<long double>> (std::vector<long double> &, std::string_view);
1366 template LIBMESH_EXPORT
void Xdr::data<std::vector<std::complex<float>>> (std::vector<std::complex<float>> &, std::string_view);
1367 template LIBMESH_EXPORT
void Xdr::data<std::vector<std::complex<double>>> (std::vector<std::complex<double>> &, std::string_view);
1368 template LIBMESH_EXPORT
void Xdr::data<std::vector<std::complex<long double>>> (std::vector<std::complex<long double>> &, std::string_view);
1369 template LIBMESH_EXPORT
void Xdr::data<std::vector<std::string>> (std::vector<std::string> &, std::string_view);
1370 template LIBMESH_EXPORT
void Xdr::data_stream<unsigned char> (
unsigned char * val,
const unsigned int len,
const unsigned int line_break);
1371 template LIBMESH_EXPORT
void Xdr::data_stream<short int> (
short int * val,
const unsigned int len,
const unsigned int line_break);
1372 template LIBMESH_EXPORT
void Xdr::data_stream<int> (
int * val,
const unsigned int len,
const unsigned int line_break);
1373 template LIBMESH_EXPORT
void Xdr::data_stream<long long> (
long long * val,
const unsigned int len,
const unsigned int line_break);
1374 template LIBMESH_EXPORT
void Xdr::data_stream<unsigned short int> (
unsigned short int * val,
const unsigned int len,
const unsigned int line_break);
1375 template LIBMESH_EXPORT
void Xdr::data_stream<unsigned int> (
unsigned int * val,
const unsigned int len,
const unsigned int line_break);
1376 template LIBMESH_EXPORT
void Xdr::data_stream<unsigned long int> (
unsigned long int * val,
const unsigned int len,
const unsigned int line_break);
1377 template LIBMESH_EXPORT
void Xdr::data_stream<unsigned long long> (
unsigned long long * val,
const unsigned int len,
const unsigned int line_break);
1379 #ifdef LIBMESH_DEFAULT_QUADRUPLE_PRECISION 1380 template LIBMESH_EXPORT
void Xdr::data<Real> (
Real &, std::string_view);
1381 template LIBMESH_EXPORT
void Xdr::data<std::complex<Real>> (std::complex<Real> &, std::string_view);
1382 template LIBMESH_EXPORT
void Xdr::data<std::vector<Real>> (std::vector<Real> &, std::string_view);
1383 template LIBMESH_EXPORT
void Xdr::data<std::vector<std::complex<Real>>> (std::vector<std::complex<Real>> &, std::string_view);
std::string name(const ElemQuality q)
This function returns a string containing some name for q.
Xdr(std::string name="", const XdrMODE m=UNKNOWN)
File-based constructor.
const int comm_len
A buffer to put comment strings into.
std::string file_name
The file name.
const unsigned int xdr_MAX_STRING_LENGTH
void do_write(T &a)
Helper method for writing different data types.
const unsigned int invalid_uint
A number which is used quite often to represent an invalid or uninitialized value for an unsigned int...
char comm[xdr_MAX_STRING_LENGTH]
void comment(std::string &)
Writes or reads (ignores) a comment line.
void close()
Closes the file if it is open.
The libMesh namespace provides an interface to certain functionality in the library.
const XdrMODE mode
The mode used for accessing the file.
void do_read(T &a)
Helper method for reading different data types.
bool gzipped_file
Are we reading/writing zipped files?
void _complex_data_stream(std::complex< T > *val, const unsigned int len, const unsigned int line_break)
Helper method for complex types.
XdrMODE
Defines an enum for read/write mode in Xdr format.
void data(T &a, std::string_view comment="")
Inputs or outputs a single value.
std::unique_ptr< std::istream > in
The input file stream.
std::unique_ptr< std::ostream > out
The output file stream.
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 ...
std::unique_ptr< XDR > xdrs
Pointer to the standard XDR struct.
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.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void open(std::string name)
Opens the file.
void data_stream(T *val, const unsigned int len, const unsigned int line_break=libMesh::invalid_uint)
Inputs or outputs a raw data stream.