Loading...
Searching...
No Matches
lineend.h
Go to the documentation of this file.
/**********************************************************************
lineend.h - Stream buffer for filtering line endings, converting \r or \r\n -> \n
Copyright (C) 1998 by James Kanze
Copyright (C) 2007 by Chris Morley
This file is part of the Open Babel project.
For more information, see <http://openbabel.org/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
This program 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
General Public License for more details.
***********************************************************************/
#ifndef OB_LINEEND_H
#define OB_LINEEND_H
#include <streambuf>
#include <climits>
#ifndef OBCONV
#define OBCONV
#endif
namespace OpenBabel
{
template< class Extractor >
class FilteringInputStreambuf : public std::streambuf
{
public:
FilteringInputStreambuf(
std::istream* source = nullptr) ;
virtual ~FilteringInputStreambuf()
{
//sync(); comment out so can be deleted in OBConversion destructor
};
virtual int overflow( int ) {return EOF;};
virtual int underflow() ;
virtual int sync() ;
//Pass the random acess functions to the source rdbuf and synchronize
virtual std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way,
std::ios_base::openmode which = std::ios_base::in | std::ios_base::out )
{
setg( &myBuffer , &myBuffer , &myBuffer ) ; //ensure next character is from new position
mySource->seekg(off, way);
std::streampos ret = mySource->tellg();
// sync();
return ret;
};
virtual std::streampos seekpos(std::streampos sp,
std::ios_base::openmode which = std::ios_base::in | std::ios_base::out )
{
setg( &myBuffer , &myBuffer , &myBuffer ) ;
//slight hack - if mySource has read past the end, won't let us seek
//and there's no good way to propagate a clear to the original stream
mySource->clear();
mySource->seekg(sp);
std::streampos ret = mySource->tellg();
// sync();
return ret;
};
std::istream* GetSource()const
{
return mySource;
};
void SetSource(std::istream* newsource)
{
mySource = newsource;
setg( &myBuffer , &myBuffer , &myBuffer + 1 ) ;
}
// Extractor& extractor() {return myExtractor;};
private:
std::istream* mySource ;
Extractor myExtractor ;
char myBuffer ;
} ;
//*******************************************************
template< class Extractor >
FilteringInputStreambuf< Extractor >::FilteringInputStreambuf(
std::istream* source )
: mySource(source)
{
setg( &myBuffer , &myBuffer , &myBuffer ) ;
}
template< class Extractor >
int
FilteringInputStreambuf< Extractor >::underflow()
{
int result( EOF ) ;
if( gptr() < egptr() )
result = *gptr() ;
else if ( mySource != nullptr )
{
result = myExtractor( *mySource ) ;
if ( result != EOF )
{
if( result < 0 || result > UCHAR_MAX )
std::cerr << "FilteringInputStreambuf error" << std::endl;
myBuffer = result ;
setg( &myBuffer , &myBuffer , &myBuffer + 1 ) ;
}
}
return result ;
}
template< class Extractor >
int
FilteringInputStreambuf< Extractor >::sync()
{
int result( 0 ) ;
if ( mySource != nullptr )
{
if ( gptr() < egptr() )
{ //have read something but not provided it
mySource->putback(*gptr() ) ;
setg( &myBuffer , &myBuffer , &myBuffer ) ;
}
result = mySource->sync();
}
return result ;
}
//*********************************************
class OBCONV LineEndingExtractor
{
public:
int operator()( std::istream& src )
{
int ch( src.get() ) ;
switch (ch)
{
case 13: //CR or CRLF
if(src.peek() == 10)
src.get(); //CRLF
//fall through
case 10: //LF
return '\n';
break;
default:
return ch;
}
}
void finalize( std::streambuf& ) {}
};
template <class Extractor >
class FilteringInputStream :
virtual private FilteringInputStreambuf<Extractor>,
public std::istream
{
public:
typedef std::istream& istream_reference;
typedef std::istream istream_type;
explicit FilteringInputStream(istream_reference istream):
FilteringInputStreambuf<Extractor>(&istream),std::istream(this) {}
virtual ~FilteringInputStream() {}
};
} //namespace
#endif //OB_LINEEND_H
OpenBabel::FilteringInputStream::~FilteringInputStream
virtual ~FilteringInputStream()
Definition lineend.h:205
OpenBabel::FilteringInputStream::FilteringInputStream
FilteringInputStream(istream_reference istream)
Definition lineend.h:203
OpenBabel::FilteringInputStream::istream_reference
std::istream & istream_reference
Definition lineend.h:200
OpenBabel::FilteringInputStream::istream_type
std::istream istream_type
Definition lineend.h:201
OpenBabel::FilteringInputStreambuf::underflow
virtual int underflow()
Definition lineend.h:129
OpenBabel::FilteringInputStreambuf::sync
virtual int sync()
Definition lineend.h:151
OpenBabel::FilteringInputStreambuf::FilteringInputStreambuf
FilteringInputStreambuf(std::istream *source=nullptr)
Definition lineend.h:119
OpenBabel::FilteringInputStreambuf::overflow
virtual int overflow(int)
Definition lineend.h:68
OpenBabel::FilteringInputStreambuf::~FilteringInputStreambuf
virtual ~FilteringInputStreambuf()
Definition lineend.h:64
OpenBabel::FilteringInputStreambuf::GetSource
std::istream * GetSource() const
Returns current source.
Definition lineend.h:97
OpenBabel::FilteringInputStreambuf::seekpos
virtual std::streampos seekpos(std::streampos sp, std::ios_base::openmode which=std::ios_base::in|std::ios_base::out)
Definition lineend.h:83
OpenBabel::FilteringInputStreambuf::SetSource
void SetSource(std::istream *newsource)
Changes the source.
Definition lineend.h:103
OpenBabel::FilteringInputStreambuf::seekoff
virtual std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way, std::ios_base::openmode which=std::ios_base::in|std::ios_base::out)
Definition lineend.h:73
OpenBabel::LineEndingExtractor
Replaces CRLF (DOS) and CR (Mac OS 9) line endings by LF (POSIX).
Definition lineend.h:170
OpenBabel::LineEndingExtractor::finalize
void finalize(std::streambuf &)
Definition lineend.h:188
OpenBabel::LineEndingExtractor::operator()
int operator()(std::istream &src)
Definition lineend.h:172
OpenBabel
Global namespace for all Open Babel code.
Definition alias.h:23
std
STL namespace.