$extrastylesheet
libMesh::Xdr Class Reference

#include <xdr_cxx.h>

List of all members.

Public Member Functions

 Xdr (const std::string &name="", const XdrMODE m=UNKNOWN)
 ~Xdr ()
void open (const std::string &name)
void close ()
bool is_open () const
bool reading () const
bool writing () const
XdrMODE access_mode () const
template<typename T >
void data (T &a, const char *comment="")
template<typename T >
Xdroperator<< (T &a)
template<typename T >
Xdroperator>> (T &a)
template<typename T >
void data_stream (T *val, const unsigned int len, const unsigned int line_break=libMesh::invalid_uint)
void comment (std::string &)
void set_version (int ver)
int version () const
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 (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)
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)
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)

Private Attributes

const XdrMODE mode
std::string file_name
XDR * xdrs
FILE * fp
UniquePtr< std::istream > in
UniquePtr< std::ostream > out
const int comm_len
char comm [xdr_MAX_STRING_LENGTH]
bool gzipped_file
bool bzipped_file
bool xzipped_file
int version_number

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.

Definition at line 70 of file xdr_cxx.h.


Constructor & Destructor Documentation

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

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

Definition at line 140 of file xdr_cxx.C.

References open().

                                                :
  mode(m),
  file_name(name),
#ifdef LIBMESH_HAVE_XDR
  xdrs(NULL),
  fp(NULL),
#endif
  in(),
  out(),
  comm_len(xdr_MAX_STRING_LENGTH),
  gzipped_file(false),
  bzipped_file(false),
  xzipped_file(false)
{
  this->open(name);
}

Destructor. Closes the file if it is open.

Definition at line 159 of file xdr_cxx.C.

References close().

{
  this->close();
}

Member Function Documentation

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 118 of file xdr_cxx.h.

References mode.

{ return mode; }

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::EquationSystems::_read_impl(), libMesh::XdrIO::read(), libMesh::XdrIO::write(), and ~Xdr().

{
  switch (mode)
    {
    case ENCODE:
    case DECODE:
      {
#ifdef LIBMESH_HAVE_XDR

        if (xdrs)
          {
            xdr_destroy (xdrs);
            delete xdrs;
            xdrs = NULL;
          }

        if (fp)
          {
            fflush(fp);
            fclose(fp);
            fp = NULL;
          }
#else

        libmesh_error_msg("ERROR: Functionality is not available.\n" \
                          << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
                          << "The XDR interface is not available in this installation");

#endif
        file_name = "";
        return;
      }

    case READ:
      {
        if (in.get() != NULL)
          {
            in.reset();

            if (bzipped_file || xzipped_file)
              remove_unzipped_file(file_name);
          }
        file_name = "";
        return;
      }

    case WRITE:
      {
        if (out.get() != NULL)
          {
            out.reset();

            if (bzipped_file)
              bzip_file(std::string(file_name.begin(), file_name.end()-4));

            else if (xzipped_file)
              xzip_file(std::string(file_name.begin(), file_name.end()-3));
          }
        file_name = "";
        return;
      }

    default:
      libmesh_error_msg("Invalid mode = " << mode);
    }
}
void libMesh::Xdr::comment ( std::string &  comment_in)

Writes or reads (ignores) a comment line.

Definition at line 1351 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().

{
  switch (mode)
    {
    case ENCODE:
    case DECODE:
      {
        return;
      }

    case READ:
      {
        libmesh_assert(in.get());
        libmesh_assert (in->good());
        in->getline(comm, comm_len);
        return;
      }

    case WRITE:
      {
        libmesh_assert(out.get());
        libmesh_assert (out->good());
        *out << "\t " << comment_in << '\n';
        return;
      }

    default:
      libmesh_error_msg("Invalid mode = " << mode);
    }
}
template<typename T >
template void libMesh::Xdr::data< long double > ( T &  a,
const char *  comment = "" 
)

Inputs or outputs a single value.

Definition at line 614 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 libMesh::EquationSystems::_read_impl(), do_read(), do_write(), operator<<(), operator>>(), libMesh::CheckpointIO::read(), libMesh::XdrIO::read(), libMesh::CheckpointIO::read_bc_names(), libMesh::CheckpointIO::read_bcs(), libMesh::CheckpointIO::read_connectivity(), libMesh::System::read_header(), libMesh::System::read_legacy_data(), libMesh::CheckpointIO::read_nodes(), libMesh::CheckpointIO::read_nodesets(), libMesh::System::read_parallel_data(), libMesh::XdrIO::read_serialized_bc_names(), libMesh::XdrIO::read_serialized_bcs(), libMesh::XdrIO::read_serialized_connectivity(), 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::MeshData::read_xdr(), libMesh::CheckpointIO::write(), libMesh::XdrIO::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::XdrIO::write_serialized_bc_names(), libMesh::XdrIO::write_serialized_bcs(), libMesh::XdrIO::write_serialized_connectivity(), libMesh::XdrIO::write_serialized_nodesets(), libMesh::XdrIO::write_serialized_subdomain_names(), libMesh::System::write_serialized_vector(), libMesh::System::write_serialized_vectors(), libMesh::CheckpointIO::write_subdomain_names(), and libMesh::MeshData::write_xdr().

{
  switch (mode)
    {
    case ENCODE:
    case DECODE:
      {
#ifdef LIBMESH_HAVE_XDR

        libmesh_assert (is_open());

        xdr_translate(xdrs, a);

#else

        libmesh_error_msg("ERROR: Functionality is not available.\n"    \
                          << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
                          << "The XDR interface is not available in this installation");

#endif
        return;
      }

    case READ:
      {
        libmesh_assert(in.get());
        libmesh_assert (in->good());

        this->do_read(a);

        return;
      }

    case WRITE:
      {
        libmesh_assert(out.get());
        libmesh_assert (out->good());

        this->do_write(a);
        *out << "\t " << comment_in << '\n';

        return;
      }

    default:
      libmesh_error_msg("Invalid mode = " << mode);
    }
}
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 665 of file xdr_cxx.C.

References libMesh::DECODE, libMesh::ENCODE, in, libMesh::invalid_uint, is_open(), libMesh::libmesh_assert(), std::min(), 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(), 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(), libMesh::XdrIO::write_serialized_connectivity(), libMesh::XdrIO::write_serialized_nodes(), and libMesh::XdrIO::write_serialized_nodesets().

{
  switch (mode)
    {
    case ENCODE:
      {
#ifdef LIBMESH_HAVE_XDR

        libmesh_assert (this->is_open());

        unsigned int size_of_type = cast_int<unsigned int>(sizeof(T));

        if (size_of_type <= 4) // 32-bit types
          {
            xdr_vector(xdrs,
                       (char*) val,
                       len,
                       size_of_type,
                       (xdrproc_t) xdr_u_int);
          }
        else // 64-bit types
          {
            xdr_vector(xdrs,
                       (char*) val,
                       len,
                       size_of_type,
                       (xdrproc_t) xdr_u_hyper);
          }

#else

        libmesh_error_msg("ERROR: Functionality is not available.\n"    \
                          << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
                          << "The XDR interface is not available in this installation");

#endif
        return;
      }

    case DECODE:
      {
#ifdef LIBMESH_HAVE_XDR

        libmesh_assert (this->is_open());

        unsigned int size_of_type = cast_int<unsigned int>(sizeof(T));

        if (size_of_type <= 4) // 32-bit types
          {
            if (len > 0)
              xdr_vector(xdrs,
                         (char*) val,
                         len,
                         size_of_type,
                         (xdrproc_t) xdr_u_int);
          }
        else // 64-bit types
          {
            if (len > 0)
              xdr_vector(xdrs,
                         (char*) val,
                         len,
                         size_of_type,
                         (xdrproc_t) xdr_u_hyper);

          }

#else

        libmesh_error_msg("ERROR: Functionality is not available.\n"    \
                          << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
                          << "The XDR interface is not available in this installation");

#endif
        return;
      }

    case READ:
      {
        libmesh_assert(in.get());
        libmesh_assert (in->good());

        for (unsigned int i=0; i<len; i++)
          {
            libmesh_assert(in.get());
            libmesh_assert (in->good());
            *in >> val[i];
          }

        return;
      }

    case WRITE:
      {
        libmesh_assert(out.get());
        libmesh_assert (out->good());

        if (line_break == libMesh::invalid_uint)
          for (unsigned int i=0; i<len; i++)
            {
              libmesh_assert(out.get());
              libmesh_assert (out->good());
              *out << val[i] << " ";
            }
        else
          {
            unsigned int cnt=0;
            while (cnt < len)
              {
                for (unsigned int i=0; i<std::min(line_break,len); i++)
                  {
                    libmesh_assert(out.get());
                    libmesh_assert (out->good());
                    *out << val[cnt++] << " ";
                  }
                libmesh_assert(out.get());
                libmesh_assert (out->good());
                *out << '\n';
              }
          }

        return;
      }

    default:
      libmesh_error_msg("Invalid mode = " << mode);
    }
}
template<>
void libMesh::Xdr::data_stream ( double *  val,
const unsigned int  len,
const unsigned int  line_break 
)

Definition at line 797 of file xdr_cxx.C.

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

{
  switch (mode)
    {
    case ENCODE:
    case DECODE:
      {
#ifdef LIBMESH_HAVE_XDR

        libmesh_assert (this->is_open());

        if (len > 0)
          xdr_vector(xdrs,
                     (char*) val,
                     len,
                     sizeof(double),
                     (xdrproc_t) xdr_double);

#else

        libmesh_error_msg("ERROR: Functionality is not available.\n"    \
                          << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
                          << "The XDR interface is not available in this installation");

#endif
        return;
      }

    case READ:
      {
        libmesh_assert(in.get());
        libmesh_assert (in->good());

        for (unsigned int i=0; i<len; i++)
          {
            libmesh_assert(in.get());
            libmesh_assert (in->good());
            *in >> val[i];
          }

        return;
      }

    case WRITE:
      {
        libmesh_assert(out.get());
        libmesh_assert (out->good());

        // Save stream flags
        std::ios_base::fmtflags out_flags = out->flags();

        // We will use scientific notation with a precision of 16
        // digits in the following output.  The desired precision and
        // format will automatically determine the width.
        *out << std::scientific
             << std::setprecision(16);

        if (line_break == libMesh::invalid_uint)
          for (unsigned int i=0; i<len; i++)
            {
              libmesh_assert(out.get());
              libmesh_assert (out->good());
              *out << val[i] << ' ';
            }
        else
          {
            unsigned int cnt=0;
            while (cnt < len)
              {
                for (unsigned int i=0; i<std::min(line_break,len); i++)
                  {
                    libmesh_assert(out.get());
                    libmesh_assert (out->good());
                    *out << val[cnt++] << ' ';
                  }
                libmesh_assert(out.get());
                libmesh_assert (out->good());
                *out << '\n';
              }
          }

        // Restore stream flags
        out->flags(out_flags);

        return;
      }

    default:
      libmesh_error_msg("Invalid mode = " << mode);
    }
}
template<>
void libMesh::Xdr::data_stream ( float *  val,
const unsigned int  len,
const unsigned int  line_break 
)

Definition at line 891 of file xdr_cxx.C.

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

{
  switch (mode)
    {
    case ENCODE:
    case DECODE:
      {
#ifdef LIBMESH_HAVE_XDR

        libmesh_assert (this->is_open());

        if (len > 0)
          xdr_vector(xdrs,
                     (char*) val,
                     len,
                     sizeof(float),
                     (xdrproc_t) xdr_float);

#else

        libmesh_error_msg("ERROR: Functionality is not available.\n"    \
                          << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
                          << "The XDR interface is not available in this installation");

#endif
        return;
      }

    case READ:
      {
        libmesh_assert(in.get());
        libmesh_assert (in->good());

        for (unsigned int i=0; i<len; i++)
          {
            libmesh_assert(in.get());
            libmesh_assert (in->good());
            *in >> val[i];
          }

        return;
      }

    case WRITE:
      {
        libmesh_assert(out.get());
        libmesh_assert (out->good());

        // Save stream flags
        std::ios_base::fmtflags out_flags = out->flags();

        // We will use scientific notation with a precision of 16
        // digits in the following output.  The desired precision and
        // format will automatically determine the width.
        *out << std::scientific
             << std::setprecision(16);

        if (line_break == libMesh::invalid_uint)
          for (unsigned int i=0; i<len; i++)
            {
              libmesh_assert(out.get());
              libmesh_assert (out->good());
              *out << val[i] << ' ';
            }
        else
          {
            unsigned int cnt=0;
            while (cnt < len)
              {
                for (unsigned int i=0; i<std::min(line_break,len); i++)
                  {
                    libmesh_assert(out.get());
                    libmesh_assert (out->good());
                    *out << val[cnt++] << ' ';
                  }
                libmesh_assert(out.get());
                libmesh_assert (out->good());
                *out << '\n';
              }
          }

        // Restore stream flags
        out->flags(out_flags);

        return;
      }

    default:
      libmesh_error_msg("Invalid mode = " << mode);
    }
}
template<>
void libMesh::Xdr::data_stream ( long double *  val,
const unsigned int  len,
const unsigned int  line_break 
)

Definition at line 983 of file xdr_cxx.C.

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

{
  switch (mode)
    {
    case ENCODE:
    case DECODE:
      {
#ifdef LIBMESH_HAVE_XDR

        libmesh_assert (this->is_open());

        // FIXME[JWP]: How to implement this for long double?  Mac OS
        // X defines 'xdr_quadruple' but AFAICT, it does not exist for
        // Linux... for now, reading/writing XDR files with long
        // doubles drops back to double precision, but you can still
        // write long double ASCII files of course.
        // if (len > 0)
        //   xdr_vector(xdrs,
        //      (char*) val,
        //      len,
        //      sizeof(double),
        //      (xdrproc_t) xdr_quadruple);

        if (len > 0)
          {
            std::vector<double> io_buffer (len);

            // Fill io_buffer if we are writing.
            if (mode == ENCODE)
              for (unsigned int i=0, cnt=0; i<len; i++)
                io_buffer[cnt++] = val[i];

            xdr_vector(xdrs,
                       (char*) &io_buffer[0],
                       len,
                       sizeof(double),
                       (xdrproc_t) xdr_double);

            // Fill val array if we are reading.
            if (mode == DECODE)
              for (unsigned int i=0, cnt=0; i<len; i++)
                {
                  val[i] = io_buffer[cnt++];
                }
          }

#else

        libmesh_error_msg("ERROR: Functionality is not available.\n"    \
                          << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
                          << "The XDR interface is not available in this installation");

#endif
        return;
      }

    case READ:
      {
        libmesh_assert(in.get());
        libmesh_assert (in->good());

        for (unsigned int i=0; i<len; i++)
          {
            libmesh_assert(in.get());
            libmesh_assert (in->good());
            *in >> val[i];
          }

        return;
      }

    case WRITE:
      {
        libmesh_assert(out.get());
        libmesh_assert (out->good());

        // Save stream flags
        std::ios_base::fmtflags out_flags = out->flags();

        // We will use scientific notation with a precision of 16
        // digits in the following output.  The desired precision and
        // format will automatically determine the width.
        *out << std::scientific
             << std::setprecision(16);

        if (line_break == libMesh::invalid_uint)
          for (unsigned int i=0; i<len; i++)
            {
              libmesh_assert(out.get());
              libmesh_assert (out->good());
              *out << val[i] << ' ';
            }
        else
          {
            unsigned int cnt=0;
            while (cnt < len)
              {
                for (unsigned int i=0; i<std::min(line_break,len); i++)
                  {
                    libmesh_assert(out.get());
                    libmesh_assert (out->good());
                    *out << val[cnt++] << ' ';
                  }
                libmesh_assert(out.get());
                libmesh_assert (out->good());
                *out << '\n';
              }
          }

        // Restore stream flags
        out->flags(out_flags);

        return;
      }

    default:
      libmesh_error_msg("Invalid mode = " << mode);
    }
}
template<>
void libMesh::Xdr::data_stream ( std::complex< double > *  val,
const unsigned int  len,
const unsigned int  line_break 
)

Definition at line 1106 of file xdr_cxx.C.

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

{
  switch (mode)
    {
    case ENCODE:
    case DECODE:
      {
#ifdef LIBMESH_HAVE_XDR

        libmesh_assert (this->is_open());


        if (len > 0)
          {
            std::vector<double> io_buffer (2*len);

            // Fill io_buffer if we are writing.
            if (mode == ENCODE)
              for (unsigned int i=0, cnt=0; i<len; i++)
                {
                  io_buffer[cnt++] = val[i].real();
                  io_buffer[cnt++] = val[i].imag();
                }

            xdr_vector(xdrs,
                       (char*) &io_buffer[0],
                       2*len,
                       sizeof(double),
                       (xdrproc_t) xdr_double);

            // Fill val array if we are reading.
            if (mode == DECODE)
              for (unsigned int i=0, cnt=0; i<len; i++)
                {
                  double re = io_buffer[cnt++];
                  double im = io_buffer[cnt++];
                  val[i] = std::complex<double>(re,im);
                }
          }
#else

        libmesh_error_msg("ERROR: Functionality is not available.\n"    \
                          << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
                          << "The XDR interface is not available in this installation");

#endif
        return;
      }

    case READ:
      {
        libmesh_assert(in.get());
        libmesh_assert (in->good());

        for (unsigned int i=0; i<len; i++)
          {
            libmesh_assert(in.get());
            libmesh_assert (in->good());
            double re, im;
            *in >> re >> im;
            val[i] = std::complex<double>(re,im);
          }

        return;
      }

    case WRITE:
      {
        libmesh_assert(out.get());
        libmesh_assert (out->good());

        // Save stream flags
        std::ios_base::fmtflags out_flags = out->flags();

        // We will use scientific notation with a precision of 16
        // digits in the following output.  The desired precision and
        // format will automatically determine the width.
        *out << std::scientific
             << std::setprecision(16);

        if (line_break == libMesh::invalid_uint)
          for (unsigned int i=0; i<len; i++)
            {
              libmesh_assert(out.get());
              libmesh_assert (out->good());
              *out << val[i].real() << ' ';
              *out << val[i].imag() << ' ';
            }
        else
          {
            unsigned int cnt=0;
            while (cnt < len)
              {
                for (unsigned int i=0; i<std::min(line_break,len); i++)
                  {
                    libmesh_assert(out.get());
                    libmesh_assert (out->good());
                    *out << val[cnt].real() << ' ';
                    *out << val[cnt].imag() << ' ';
                    cnt++;
                  }
                libmesh_assert(out.get());
                libmesh_assert (out->good());
                *out << '\n';
              }
          }

        // Restore stream flags
        out->flags(out_flags);

        return;
      }

    default:
      libmesh_error_msg("Invalid mode = " << mode);
    }
}
template<>
void libMesh::Xdr::data_stream ( std::complex< long double > *  val,
const unsigned int  len,
const unsigned int  line_break 
)

Definition at line 1225 of file xdr_cxx.C.

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

{
  switch (mode)
    {
    case ENCODE:
    case DECODE:
      {
#ifdef LIBMESH_HAVE_XDR

        libmesh_assert (this->is_open());

        // FIXME[JWP]: How to implement this for long double?  Mac OS
        // X defines 'xdr_quadruple' but AFAICT, it does not exist for
        // Linux... for now, reading/writing XDR files with long
        // doubles drops back to double precision, but you can still
        // write long double ASCII files of course.

        if (len > 0)
          {
            std::vector<double> io_buffer (2*len);

            // Fill io_buffer if we are writing.
            if (mode == ENCODE)
              for (unsigned int i=0, cnt=0; i<len; i++)
                {
                  io_buffer[cnt++] = val[i].real();
                  io_buffer[cnt++] = val[i].imag();
                }

            xdr_vector(xdrs,
                       (char*) &io_buffer[0],
                       2*len,
                       sizeof(double),
                       (xdrproc_t) xdr_double);

            // Fill val array if we are reading.
            if (mode == DECODE)
              for (unsigned int i=0, cnt=0; i<len; i++)
                {
                  double re = io_buffer[cnt++];
                  double im = io_buffer[cnt++];
                  val[i] = std::complex<long double>(re, im);
                }
          }
#else

        libmesh_error_msg("ERROR: Functionality is not available.\n"    \
                          << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
                          << "The XDR interface is not available in this installation");

#endif
        return;
      }

    case READ:
      {
        libmesh_assert(in.get());
        libmesh_assert (in->good());

        for (unsigned int i=0; i<len; i++)
          {
            libmesh_assert(in.get());
            libmesh_assert (in->good());
            long double re, im;
            *in >> re >> im;
            val[i] = std::complex<long double>(re,im);
          }

        return;
      }

    case WRITE:
      {
        libmesh_assert(out.get());
        libmesh_assert (out->good());


        // Save stream flags
        std::ios_base::fmtflags out_flags = out->flags();

        // We will use scientific notation with a precision of
        // 'digits10' digits in the following output.  The desired
        // precision and format will automatically determine the
        // width.  Note: digit10 is the number of digits (in decimal
        // base) that can be represented without change.  Equivalent
        // to FLT_DIG, DBL_DIG or LDBL_DIG for floating types.
        *out << std::scientific
             << std::setprecision(std::numeric_limits<long double>::digits10);

        if (line_break == libMesh::invalid_uint)
          for (unsigned int i=0; i<len; i++)
            {
              libmesh_assert(out.get());
              libmesh_assert (out->good());
              *out << val[i].real() << ' ' << val[i].imag() << ' ';
            }
        else
          {
            unsigned int cnt=0;
            while (cnt < len)
              {
                for (unsigned int i=0; i<std::min(line_break,len); i++)
                  {
                    libmesh_assert(out.get());
                    libmesh_assert (out->good());
                    *out << val[cnt].real() << ' ' << val[cnt].imag() << ' ';
                    cnt++;
                  }
                libmesh_assert(out.get());
                libmesh_assert (out->good());
                *out << '\n';
              }
          }

        // Restore stream flags
        out->flags(out_flags);

        return;
      }

    default:
      libmesh_error_msg("Invalid mode = " << mode);
    }
}
template<typename T >
void libMesh::Xdr::do_read ( T &  a) [private]

Helper method for reading different data types

Definition at line 516 of file xdr_cxx.C.

References comm, comm_len, and in.

Referenced by data().

                      {
  *in >> a;
  in->getline(comm, comm_len);
}
template<typename T >
void libMesh::Xdr::do_read ( std::complex< T > &  a) [private]

Definition at line 522 of file xdr_cxx.C.

References comm, comm_len, and in.

                                  {
  T r, i;
  *in >> r >> i;
  a = std::complex<T>(r,i);
  in->getline(comm, comm_len);
}
template<typename T >
void libMesh::Xdr::do_read ( std::vector< T > &  a) [private]

Definition at line 544 of file xdr_cxx.C.

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

                                 {
  unsigned int length=0;
  data(length, "# vector length");
  a.resize(length);

  for (unsigned int i=0; i<a.size(); i++)
    {
      libmesh_assert(in.get());
      libmesh_assert (in->good());
      *in >> a[i];
    }
  in->getline(comm, comm_len);
}
template<typename T >
void libMesh::Xdr::do_read ( std::vector< std::complex< T > > &  a) [private]

Definition at line 559 of file xdr_cxx.C.

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

                                              {
  unsigned int length=0;
  data(length, "# vector length x 2 (complex)");
  a.resize(length);

  for (unsigned int i=0; i<a.size(); i++)
    {
      T r, im;
      libmesh_assert(in.get());
      libmesh_assert (in->good());
      *in >> r >> im;
      a[i] = std::complex<T>(r,im);
    }
  in->getline(comm, comm_len);
}
template<>
void libMesh::Xdr::do_read ( std::string &  a)

Definition at line 530 of file xdr_cxx.C.

References comm, comm_len, and in.

                              {
  in->getline(comm, comm_len);

  a = "";

  for (unsigned int c=0; c<std::strlen(comm); c++)
    {
      if (comm[c] == '\t')
        break;
      a.push_back(comm[c]);
    }
}
template<typename T >
void libMesh::Xdr::do_write ( T &  a) [private]

Helper method for writing different data types

Definition at line 576 of file xdr_cxx.C.

References out.

Referenced by data(), and do_write().

{ *out << a; }
template<typename T >
void libMesh::Xdr::do_write ( std::complex< T > &  a) [private]

Definition at line 579 of file xdr_cxx.C.

References out.

                                   {
  *out << a.real() << "\t " << a.imag();
}
template<typename T >
void libMesh::Xdr::do_write ( std::vector< T > &  a) [private]

Definition at line 584 of file xdr_cxx.C.

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

                                  {
  std::size_t length = a.size();
  data(length, "# vector length");

  for (std::size_t i=0; i<a.size(); i++)
    {
      libmesh_assert(out.get());
      libmesh_assert (out->good());
      this->do_write(a[i]);
      *out << "\t ";
    }
}
template<typename T >
void libMesh::Xdr::do_write ( std::vector< std::complex< T > > &  a) [private]

Definition at line 598 of file xdr_cxx.C.

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

                                               {
  std::size_t length=a.size();
  data(length, "# vector length x 2 (complex)");

  for (std::size_t i=0; i<a.size(); i++)
    {
      libmesh_assert(out.get());
      libmesh_assert (out->good());
      this->do_write(a[i]);
      *out << "\t ";
    }
}
bool libMesh::Xdr::is_open ( ) const

Returns true if the Xdr file is open, false if it is closed.

Definition at line 346 of file xdr_cxx.C.

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

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

{
  switch (mode)
    {
    case ENCODE:
    case DECODE:
      {
#ifdef LIBMESH_HAVE_XDR

        if (fp)
          if (xdrs)
            return true;

        return false;

#else

        libmesh_error_msg("ERROR: Functionality is not available.\n"    \
                          << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
                          << "The XDR interface is not available in this installation");

        return false;

#endif

      }

    case READ:
      {
        if (in.get() != NULL)
          return in->good();
        return false;
      }

    case WRITE:
      {
        if (out.get() != NULL)
          return out->good();
        return false;
      }

    default:
      libmesh_error_msg("Invalid mode = " << mode);
    }

  return false;
}
void libMesh::Xdr::open ( const 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(), out, libMesh::READ, libMesh::WRITE, xdrs, and xzipped_file.

Referenced by Xdr().

{
  file_name = name;

  if (name == "")
    return;

  switch (mode)
    {
    case ENCODE:
    case DECODE:
      {
#ifdef LIBMESH_HAVE_XDR

        fp = fopen(name.c_str(), (mode == ENCODE) ? "w" : "r");
        if (!fp)
          libmesh_file_error(name.c_str());
        xdrs = new XDR;
        xdrstdio_create (xdrs, fp, (mode == ENCODE) ? XDR_ENCODE : XDR_DECODE);
#else

        libmesh_error_msg("ERROR: Functionality is not available.\n" \
                          << "Make sure LIBMESH_HAVE_XDR is defined at build time\n" \
                          << "The XDR interface is not available in this installation");

#endif
        return;

      }

    case READ:
      {
        gzipped_file = (name.size() - name.rfind(".gz")  == 3);
        bzipped_file = (name.size() - name.rfind(".bz2") == 4);
        xzipped_file = (name.size() - name.rfind(".xz") == 3);

        if (gzipped_file)
          {
#ifdef LIBMESH_HAVE_GZSTREAM
            igzstream *inf = new igzstream;
            libmesh_assert(inf);
            in.reset(inf);
            inf->open(name.c_str(), std::ios::in);
#else
            libmesh_error_msg("ERROR: need gzstream to handle .gz files!!!");
#endif
          }
        else
          {
            std::ifstream *inf = new std::ifstream;
            libmesh_assert(inf);
            in.reset(inf);

            std::string new_name = unzip_file(name);

            inf->open(new_name.c_str(), std::ios::in);
          }

        libmesh_assert(in.get());

        if (!in->good())
          libmesh_file_error(name);
        return;
      }

    case WRITE:
      {
        gzipped_file = (name.size() - name.rfind(".gz")  == 3);
        bzipped_file = (name.size() - name.rfind(".bz2") == 4);
        xzipped_file = (name.size() - name.rfind(".xz")  == 3);

        if (gzipped_file)
          {
#ifdef LIBMESH_HAVE_GZSTREAM
            ogzstream *outf = new ogzstream;
            libmesh_assert(outf);
            out.reset(outf);
            outf->open(name.c_str(), std::ios::out);
#else
            libmesh_error_msg("ERROR: need gzstream to handle .gz files!!!");
#endif
          }
        else
          {
            std::ofstream *outf = new std::ofstream;
            libmesh_assert(outf);
            out.reset(outf);

            std::string new_name = name;

            if (bzipped_file)
              new_name.erase(new_name.end() - 4, new_name.end());

            if (xzipped_file)
              new_name.erase(new_name.end() - 3, new_name.end());

            outf->open(new_name.c_str(), std::ios::out);
          }

        libmesh_assert(out.get());
        libmesh_assert (out->good());
        return;
      }

    default:
      libmesh_error_msg("Invalid mode = " << mode);
    }
}
template<typename T >
Xdr& libMesh::Xdr::operator<< ( T &  a) [inline]

Same, but provides an ostream like interface.

Definition at line 132 of file xdr_cxx.h.

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

{ libmesh_assert (writing()); data(a); return *this; }
template<typename T >
Xdr& libMesh::Xdr::operator>> ( T &  a) [inline]

Same, but provides an istream like interface.

Definition at line 138 of file xdr_cxx.h.

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

{ libmesh_assert (reading()); data(a); return *this; }
void libMesh::Xdr::set_version ( int  ver) [inline]

Sets the version of the file that is being read

Definition at line 154 of file xdr_cxx.h.

References version_number.

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

{ version_number = ver; }
int libMesh::Xdr::version ( ) const [inline]

Gets the version of the file that is being read

Definition at line 159 of file xdr_cxx.h.

References version_number.

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

{ return version_number; }

Member Data Documentation

Definition at line 239 of file xdr_cxx.h.

Referenced by close(), and open().

Definition at line 234 of file xdr_cxx.h.

Referenced by comment(), and do_read().

const int libMesh::Xdr::comm_len [private]

A buffer to put comment strings into.

Definition at line 233 of file xdr_cxx.h.

Referenced by comment(), and do_read().

std::string libMesh::Xdr::file_name [private]

The file name

Definition at line 201 of file xdr_cxx.h.

Referenced by close(), and open().

FILE* libMesh::Xdr::fp [private]

File pointer.

Definition at line 216 of file xdr_cxx.h.

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

Are we reading/writing zipped files?

Definition at line 239 of file xdr_cxx.h.

Referenced by open().

UniquePtr<std::istream> libMesh::Xdr::in [private]

The input file stream.

Definition at line 223 of file xdr_cxx.h.

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

const XdrMODE libMesh::Xdr::mode [private]

The mode used for accessing the file.

Definition at line 196 of file xdr_cxx.h.

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

UniquePtr<std::ostream> libMesh::Xdr::out [private]

The output file stream.

Definition at line 228 of file xdr_cxx.h.

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

Version of the file being read

Definition at line 244 of file xdr_cxx.h.

Referenced by set_version(), and version().

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 211 of file xdr_cxx.h.

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

Definition at line 239 of file xdr_cxx.h.

Referenced by close(), and open().


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