Commit 79bc3501 authored by thomas.forbriger's avatar thomas.forbriger Committed by thomas.forbriger
Browse files

support magic numbers

This is a legacy commit from before 2015-03-01.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.


SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/trunk
SVN Revision: 1134
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent 811cdb9b
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: fortranio.cc,v 1.6 2002-11-28 14:04:43 forbrig Exp $
* $Id: fortranio.cc,v 1.7 2002-11-28 19:47:41 forbrig Exp $
* \author Thomas Forbriger
* \date 13/01/2002
*
......@@ -21,21 +21,23 @@
* - 19/11/2002 V1.6
* - extract functions are replaced by function template
* - use ioswap functionality
* - 28/11/2002 V1.7
* - flush buffer only if filled
* - support magic numbers
*
* ============================================================================
*/
#define TF_FORTRANIO_CC_VERSION \
"TF_FORTRANIO_CC V1.0 "
"TF_FORTRANIO_CC V1.7 "
#define TF_FORTRANIO_CC_CVSID \
"$Id: fortranio.cc,v 1.6 2002-11-28 14:04:43 forbrig Exp $"
"$Id: fortranio.cc,v 1.7 2002-11-28 19:47:41 forbrig Exp $"
#include <tfxx/fortranio.h>
#include <tfxx/misc.h>
#include <iostream>
#include <string>
#include <cassert>
// abort is presented in cstdlib
#include <cstdlib>
#include <tfxx/fortranio.h>
#include <tfxx/ioswap.h>
#include <tfxx/misc.h>
#include <tfxx/error.h>
using std::cerr;
using std::endl;
......@@ -48,28 +50,6 @@ namespace fortranio {
// FortranBinInput
// ===============
// unnamed namespace to hide this function
namespace {
/*! \brief abort if byte count at end of block does not match the count at
* the beginning
*
* \note
* This function is only used internally by
* \c FortranBinInput::read_block_size().
* It will be replaced in future versions by throwing an appropriate
* exception class.
*/
void bytecountmismatch()
{
cerr << "FortranBinInput: Byte count mismatch in file!" << endl;
abort();
}
}
/*----------------------------------------------------------------------*/
//! return next character from buffer
void FortranBinInput::read_block_size()
{
......@@ -78,18 +58,21 @@ namespace {
// byte-count at the end of the block
if (Mnbytes != 0)
{
assert(Mnremain==0);
TFXX_assert((Mnremain==0),
"FotranBinInput: internal error - remain count is not zero!");
Mistream.read(buf.bytes, tfxx::ioswap::IOTsize<int>::Msize);
int endcount=buf.value;
if (Mswap) { endcount=tfxx::ioswap::swap(buf.value); }
if (Mnbytes != endcount) { bytecountmismatch(); }
TFXX_assert((Mnbytes == endcount),
"FortranBinInput: bount count at end of block does not match!");
}
// read byte-count at beginning of next block
Mistream.read(buf.bytes, tfxx::ioswap::IOTsize<int>::Msize);
Mnremain=buf.value;
if (Mswap) { Mnremain=tfxx::ioswap::swap(buf.value); }
Mnbytes=Mnremain;
assert(Mistream.good());
TFXX_assert((Mistream.good()),
"FortranBinInput: stream is not good after reading block size");
}
/*----------------------------------------------------------------------*/
......@@ -98,7 +81,8 @@ namespace {
char FortranBinInput::extract_next_char()
{
char result;
assert(Mistream.good());
TFXX_assert((Mistream.good()),
"FortranBinInput: stream is not good upon reading next char");
if (Mnremain < 1) { read_block_size(); }
Mistream.read(&result, 1);
Mnremain--;
......@@ -114,6 +98,19 @@ namespace {
{ buf[i]=extract_next_char(); }
}
/*----------------------------------------------------------------------*/
//! look out for expected magic number and adjust swapping flag
bool FortranBinInput::match_magic(const char* cmagic)
{
tfxx::ioswap::Emagic_type result
=tfxx::ioswap::file_magic_test(Mistream, cmagic,true);
if (result==tfxx::ioswap::magic_nomatch) return(false);
if (result==tfxx::ioswap::magic_swap) { Mswap=true; }
else { Mswap=false; }
return(true);
}
/*======================================================================*/
//
// FortranBinOutput
......@@ -125,10 +122,13 @@ namespace {
std::string buffer=Mbuffer.str();
tfxx::ioswap::IOUnion<int> count;
count.value=buffer.size();
Mostream.write(count.bytes, tfxx::ioswap::IOTsize<int>::Msize);
Mostream.write(buffer.c_str(), count.value);
Mostream.write(count.bytes, tfxx::ioswap::IOTsize<int>::Msize);
Mbuffer.str(std::string());
if (count.value>0)
{
Mostream.write(count.bytes, tfxx::ioswap::IOTsize<int>::Msize);
Mostream.write(buffer.c_str(), count.value);
Mostream.write(count.bytes, tfxx::ioswap::IOTsize<int>::Msize);
Mbuffer.str(std::string());
}
}
/*----------------------------------------------------------------------*/
......@@ -143,6 +143,16 @@ namespace {
void FortranBinOutput::put_char(const char& c)
{ Mbuffer.write(&c, 1); }
/*----------------------------------------------------------------------*/
//! write magic number to file
void FortranBinOutput::write_magic(const char* cmagic)
{
this->end_block();
this->put(tfxx::ioswap::magic(cmagic));
this->end_block();
}
} // namespace fortranio
} // namespace tfxx
......
......@@ -3,7 +3,7 @@
*
* ----------------------------------------------------------------------------
*
* $Id: fortranio.h,v 1.14 2002-11-28 14:51:08 forbrig Exp $
* $Id: fortranio.h,v 1.15 2002-11-28 19:47:49 forbrig Exp $
* \author Thomas Forbriger
* \date 13/01/2002
*
......@@ -18,6 +18,7 @@
* - 15/11/2002 V1.3 no arrays for buffering
* - 19/11/2002 V1.4 remove all access functions
* rather use I/O operator
* - 28/11/2002 V1.5 support reading and writing of magic numbers
*
* ============================================================================
*/
......@@ -28,7 +29,7 @@
#define TF_FORTRANIO_H_VERSION \
"TF_ V1.0 "
#define TF_FORTRANIO_H_CVSID \
"$Id: fortranio.h,v 1.14 2002-11-28 14:51:08 forbrig Exp $"
"$Id: fortranio.h,v 1.15 2002-11-28 19:47:49 forbrig Exp $"
#include<tfxx/misc.h>
#include<fstream>
......@@ -131,6 +132,8 @@ class FortranBinInput {
* \param value value of type \c T read from stream
*/
template<typename T> void get(T& value);
//! expect magic number and adjust swapping flag
bool match_magic(const char* cmagic);
private:
//! return next element from input stream
......@@ -184,6 +187,8 @@ class FortranBinOutput {
void put(const T& value);
//! finish data block
void end_block();
//! write magic number (to separate block)
void write_magic(const char* cmagic);
private:
//! put the next char to the output
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment