⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xdr_cxx.c

📁 一个用来实现偏微分方程中网格的计算库
💻 C
📖 第 1 页 / 共 3 页
字号:
// "$Id: xdr_cxx.C 2950 2008-08-01 15:15:11Z jwpeterson $\n"// The libMesh Finite Element Library.// Copyright (C) 2002-2007  Benjamin S. Kirk, John W. Peterson  // This library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.  // This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU// Lesser General Public License for more details.  // You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA// C/C++ includes#include <cstring>// Local includes#include "xdr_cxx.h"#include "libmesh_logging.h"#include "parallel.h"#include "o_f_stream.h"#ifdef HAVE_GZSTREAM# include "gzstream.h"#endif// Anonymous namespace for implementation details.namespace {  // Nasty hacks for reading/writing zipped files  void zip_file (const std::string &unzipped_name)  {#ifdef HAVE_BZIP    START_LOG("system(bzip2)", "XdrIO");    std::string system_string = "bzip2 -f ";    system_string += unzipped_name;    std::system(system_string.c_str());         STOP_LOG("system(bzip2)", "XdrIO");#else    std::cerr << "ERROR: need bzip2/bunzip2 to handle .bz2 files!!!"	      << std::endl;    libmesh_error();#endif  }  std::string unzip_file (const std::string &name)  {    std::string new_name = name;    if (name.size() - name.rfind(".bz2") == 4)      {	new_name.erase(new_name.end() - 4, new_name.end());#ifdef HAVE_BZIP	START_LOG("system(bunzip2)", "XdrIO");	std::string system_string = "bunzip2 -f -k ";	system_string += name;	std::system(system_string.c_str());	STOP_LOG("system(bunzip2)", "XdrIO");#else	std::cerr << "ERROR: need bzip2/bunzip2 to handle .bz2 files!!!"		  << std::endl;	libmesh_error();#endif      }    return new_name;  }  // remove an unzipped file  void remove_unzipped_file (const std::string &name)  {    // If we temporarily decompressed a .bz2 file, remove the    // uncompressed version    if (name.size() - name.rfind(".bz2") == 4)      {	const std::string new_name(name.begin(), name.end()-4);	std::remove(new_name.c_str());      }  }}//-------------------------------------------------------------// Xdr class implementationXdr::Xdr (const std::string& name, const XdrMODE m) :  mode(m),  file_name(name),#ifdef HAVE_XDR  xdrs(NULL),  fp(NULL),#endif  in(NULL),  out(NULL),  comm_len(xdr_MAX_STRING_LENGTH),  gzipped_file(false),  bzipped_file(false){  this->open(name);}Xdr::~Xdr(){  this->close();}void Xdr::open (const std::string& name){  file_name = name;  if (name == "")    return;  switch (mode)    {    case ENCODE:    case DECODE:      {#ifdef HAVE_XDR	fp = fopen(name.c_str(), (mode == ENCODE) ? "w" : "r");	libmesh_assert (fp);	xdrs = new XDR;	xdrstdio_create (xdrs, fp, (mode == ENCODE) ? XDR_ENCODE : XDR_DECODE);#else		std::cerr << "ERROR: Functionality is not available." << std::endl		  << "Make sure HAVE_XDR is defined at build time" 		  << std::endl		  << "The XDR interface is not available in this installation"		  << std::endl;	libmesh_error();	#endif	return;      }    case READ:      {	gzipped_file = (name.size() - name.rfind(".gz")  == 3);	bzipped_file = (name.size() - name.rfind(".bz2") == 4);	if (gzipped_file)	  {#ifdef HAVE_GZSTREAM	    igzstream *inf = new igzstream;	    libmesh_assert (inf != NULL);	    in.reset(inf);	    inf->open(name.c_str(), std::ios::in);#else	    std::cerr << "ERROR: need gzstream to handle .gz files!!!"		      << std::endl;	    libmesh_error();#endif	  }	else	  {	    std::ifstream *inf = new std::ifstream;	    libmesh_assert (inf != NULL);	    in.reset(inf);	    	    std::string new_name(bzipped_file ? unzip_file(name) : name);	    inf->open(new_name.c_str(), std::ios::in);	  }	libmesh_assert (in.get() != NULL); libmesh_assert (in->good());	return;      }    case WRITE:      {	gzipped_file = (name.size() - name.rfind(".gz")  == 3);	bzipped_file = (name.size() - name.rfind(".bz2") == 4);	if (gzipped_file)	  {#ifdef HAVE_GZSTREAM	    ogzstream *outf = new ogzstream;	    libmesh_assert (outf != NULL);	    out.reset(outf);	    outf->open(name.c_str(), std::ios::out);#else	    std::cerr << "ERROR: need gzstream to handle .gz files!!!"		      << std::endl;	    libmesh_error();#endif	  }	else	  {	    std::ofstream *outf = new std::ofstream;	    libmesh_assert (outf != NULL);	    out.reset(outf);	    std::string new_name = name;	    if (bzipped_file)	      new_name.erase(new_name.end() - 4, new_name.end());	    outf->open(new_name.c_str(), std::ios::out);	  }		libmesh_assert (out.get() != NULL); libmesh_assert (out->good());	return;      }          default:      libmesh_error();    }  }void Xdr::close (){  switch (mode)    {    case ENCODE:    case DECODE:      {#ifdef HAVE_XDR	if (xdrs)	  {	    xdr_destroy (xdrs);	    delete xdrs;	    xdrs = NULL;	  }		if (fp)	  {	    fflush(fp);	    fclose(fp);	    fp = NULL;	  }#else		std::cerr << "ERROR: Functionality is not available." << std::endl		  << "Make sure HAVE_XDR is defined at build time" 		  << std::endl		  << "The XDR interface is not available in this installation"		  << std::endl;	libmesh_error();	#endif	file_name = "";	return;      }          case READ:      {	if (in.get() != NULL)	  {	    in.reset();    	    	    if (bzipped_file)	      remove_unzipped_file(file_name);	  }	file_name = "";	return;      }    case WRITE:      {	if (out.get() != NULL)	  {	    out.reset();      	    if (bzipped_file)	      zip_file(std::string(file_name.begin(), file_name.end()-4));	  }	file_name = "";	return;      }    default:      libmesh_error();    }}bool Xdr::is_open() const{  switch (mode)    {    case ENCODE:    case DECODE:      {#ifdef HAVE_XDR	if (fp)	  if (xdrs)	    return true;	return false;#else		std::cerr << "ERROR: Functionality is not available." << std::endl		  << "Make sure HAVE_XDR is defined at build time" 		  << std::endl		  << "The XDR interface is not available in this installation"		  << std::endl;	libmesh_error();	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();    }  return false;}void Xdr::data (int& a, const char* comment){  switch (mode)    {    case ENCODE:    case DECODE:      {#ifdef HAVE_XDR	libmesh_assert (is_open());	xdr_int(xdrs, &a);#else		std::cerr << "ERROR: Functionality is not available." << std::endl		  << "Make sure HAVE_XDR is defined at build time" 		  << std::endl		  << "The XDR interface is not available in this installation"		  << std::endl;	libmesh_error();#endif	return;      }    case READ:      {	libmesh_assert (in.get() != NULL); libmesh_assert (in->good());		*in >> a; in->getline(comm, comm_len);	return;      }    case WRITE:      {	libmesh_assert (out.get() != NULL); libmesh_assert (out->good());		*out << a << "\t " << comment << '\n';	return;      }    default:      libmesh_error();    }}void Xdr::data (unsigned int& a, const char* comment){  switch (mode)    {    case ENCODE:    case DECODE:      {#ifdef HAVE_XDR	libmesh_assert (this->is_open());	xdr_u_int(xdrs, &a);#else		std::cerr << "ERROR: Functionality is not available." << std::endl		  << "Make sure HAVE_XDR is defined at build time" 		  << std::endl		  << "The XDR interface is not available in this installation"		  << std::endl;	libmesh_error();#endif	return;      }    case READ:      {	libmesh_assert (in.get() != NULL); libmesh_assert (in->good());		*in >> a; in->getline(comm, comm_len);	return;      }    case WRITE:      {	libmesh_assert (out.get() != NULL); libmesh_assert (out->good());	*out << a << "\t " << comment << '\n';		return;      }    default:      libmesh_error();    }}void Xdr::data (short int& a, const char* comment){  switch (mode)    {    case ENCODE:    case DECODE:      {#ifdef HAVE_XDR	libmesh_assert (is_open());	xdr_short(xdrs, &a);#else		std::cerr << "ERROR: Functionality is not available." << std::endl		  << "Make sure HAVE_XDR is defined at build time" 		  << std::endl		  << "The XDR interface is not available in this installation"		  << std::endl;	libmesh_error();#endif	return;      }    case READ:      {	libmesh_assert (in.get() != NULL); libmesh_assert (in->good());		*in >> a; in->getline(comm, comm_len);	return;      }    case WRITE:      {	libmesh_assert (out.get() != NULL); libmesh_assert (out->good());	*out << a << "\t " << comment << '\n';		return;      }    default:      libmesh_error();    }}void Xdr::data (unsigned short int& a, const char* comment){  switch (mode)    {    case ENCODE:    case DECODE:      {#ifdef HAVE_XDR	libmesh_assert (is_open());	xdr_u_short(xdrs, &a);#else		std::cerr << "ERROR: Functionality is not available." << std::endl		  << "Make sure HAVE_XDR is defined at build time" 		  << std::endl		  << "The XDR interface is not available in this installation"		  << std::endl;	libmesh_error();#endif	return;      }    case READ:      {	libmesh_assert (in.get() != NULL); libmesh_assert (in->good());		*in >> a; in->getline(comm, comm_len);	return;      }    case WRITE:      {	libmesh_assert (out.get() != NULL); libmesh_assert (out->good());	*out << a << "\t " << comment << '\n';		return;      }    default:      libmesh_error();    }}void Xdr::data (float& a, const char* comment){  switch (mode)    {    case ENCODE:    case DECODE:      {#ifdef HAVE_XDR	libmesh_assert (is_open());	xdr_float(xdrs, &a);#else		std::cerr << "ERROR: Functionality is not available." << std::endl		  << "Make sure HAVE_XDR is defined at build time" 		  << std::endl		  << "The XDR interface is not available in this installation"		  << std::endl;	libmesh_error();#endif	return;      }    case READ:      {	libmesh_assert (in.get() != NULL); libmesh_assert (in->good());		*in >> a; in->getline(comm, comm_len);	return;      }    case WRITE:      {	libmesh_assert (out.get() != NULL); libmesh_assert (out->good());	*out << a << "\t " << comment << '\n';		return;      }    default:      libmesh_error();    }}void Xdr::data (double& a, const char* comment){  switch (mode)    {    case ENCODE:    case DECODE:      {#ifdef HAVE_XDR	libmesh_assert (is_open());	xdr_double(xdrs, &a);#else		std::cerr << "ERROR: Functionality is not available." << std::endl		  << "Make sure HAVE_XDR is defined at build time" 		  << std::endl		  << "The XDR interface is not available in this installation"		  << std::endl;	libmesh_error();#endif	return;      }    case READ:      {	libmesh_assert (in.get() != NULL); libmesh_assert (in->good());		*in >> a; in->getline(comm, comm_len);	return;      }    case WRITE:      {	libmesh_assert (out.get() != NULL); libmesh_assert (out->good());	*out << a << "\t " << comment << '\n';		return;      }    default:      libmesh_error();    }}#ifdef USE_COMPLEX_NUMBERSvoid Xdr::data (std::complex<double>& a, const char* comment){  switch (mode)    {    case ENCODE:    case DECODE:      {#ifdef HAVE_XDR	libmesh_assert (is_open());	double	  _r=a.real(),	  _i=a.imag();	xdr_double(xdrs, &_r);	xdr_double(xdrs, &_i);	a = std::complex<double>(_r,_i);#else		std::cerr << "ERROR: Functionality is not available." << std::endl		  << "Make sure HAVE_XDR is defined at build time" 		  << std::endl		  << "The XDR interface is not available in this installation"		  << std::endl;	libmesh_error();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -