1 /* 2 This file is part of MADNESS. 3 4 Copyright (C) 2007,2010 Oak Ridge National Laboratory 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 20 For more information please contact: 21 22 Robert J. Harrison 23 Oak Ridge National Laboratory 24 One Bethel Valley Road 25 P.O. Box 2008, MS-6367 26 27 email: harrisonrj@ornl.gov 28 tel: 865-241-3937 29 fax: 865-572-0680 30 */ 31 32 /** 33 \file binary_fstream_archive.cc 34 \brief Implements an archive wrapping a binary filestream. 35 \ingroup serialization 36 */ 37 38 #include <madness/world/binary_fstream_archive.h> 39 #include <madness/world/madness_exception.h> 40 #include <cstring> 41 #include <memory> 42 43 namespace madness { 44 namespace archive { 45 BinaryFstreamOutputArchive(const char * filename,std::ios_base::openmode mode)46 BinaryFstreamOutputArchive::BinaryFstreamOutputArchive(const char* filename, std::ios_base::openmode mode) 47 : iobuf() 48 { 49 if (filename) open(filename, mode); 50 } 51 open(const char * filename,std::ios_base::openmode mode)52 void BinaryFstreamOutputArchive::open(const char* filename, std::ios_base::openmode mode) { 53 iobuf.reset(new char[IOBUFSIZE], std::default_delete<char[]>() ); 54 os.open(filename, mode); 55 #ifndef ON_A_MAC 56 os.rdbuf()->pubsetbuf(iobuf.get(), IOBUFSIZE); 57 #endif 58 59 store(ARCHIVE_COOKIE, strlen(ARCHIVE_COOKIE)+1); 60 } 61 close()62 void BinaryFstreamOutputArchive::close() { 63 if (iobuf) { 64 os.close(); 65 iobuf.reset(); 66 } 67 }; 68 flush()69 void BinaryFstreamOutputArchive::flush() { 70 os.flush(); 71 } 72 BinaryFstreamInputArchive(const char * filename,std::ios_base::openmode mode)73 BinaryFstreamInputArchive::BinaryFstreamInputArchive(const char* filename, std::ios_base::openmode mode) 74 : iobuf() { 75 if (filename) open(filename, mode); 76 } 77 78 open(const char * filename,std::ios_base::openmode mode)79 void BinaryFstreamInputArchive::open(const char* filename, std::ios_base::openmode mode) { 80 iobuf.reset(new char[IOBUFSIZE], std::default_delete<char[]>() ); 81 is.open(filename, mode); 82 if (!is) MADNESS_EXCEPTION("BinaryFstreamInputArchive: open: failed", 1); 83 is.rdbuf()->pubsetbuf(iobuf.get(), IOBUFSIZE); 84 char cookie[255]; 85 int n = strlen(ARCHIVE_COOKIE)+1; 86 load(cookie, n); 87 if (strncmp(cookie,ARCHIVE_COOKIE,n) != 0) 88 MADNESS_EXCEPTION("BinaryFstreamInputArchive: open: not an archive?", 1); 89 } 90 close()91 void BinaryFstreamInputArchive::close() { 92 if (iobuf) { 93 is.close(); 94 iobuf.reset(); 95 } 96 } 97 98 } // namespace archive 99 } // namespace madness 100