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

📄 bytesrc.cpp

📁 ncbi源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * =========================================================================== * PRODUCTION $Log: bytesrc.cpp,v $ * PRODUCTION Revision 1000.3  2004/06/01 19:39:53  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.33 * PRODUCTION * =========================================================================== *//*  $Id: bytesrc.cpp,v 1000.3 2004/06/01 19:39:53 gouriano Exp $ * =========================================================================== * *                            PUBLIC DOMAIN NOTICE *               National Center for Biotechnology Information * *  This software/database is a "United States Government Work" under the *  terms of the United States Copyright Act.  It was written as part of *  the author's official duties as a United States Government employee and *  thus cannot be copyrighted.  This software/database is freely available *  to the public for use. The National Library of Medicine and the U.S. *  Government have not placed any restriction on its use or reproduction. * *  Although all reasonable efforts have been taken to ensure the accuracy *  and reliability of the software and data, the NLM and the U.S. *  Government do not and cannot warrant the performance or results that *  may be obtained by using this software or data. The NLM and the U.S. *  Government disclaim all warranties, express or implied, including *  warranties of performance, merchantability or fitness for any particular *  purpose. * *  Please cite the author in any work or product based on this material. * * =========================================================================== * * Author: Eugene Vasilchenko * * File Description: *   Implementation of CByteSource and CByteSourceReader *   and their specializations'. * */#include <ncbi_pch.hpp>#include <corelib/ncbistd.hpp>#include <util/bytesrc.hpp>#include <util/util_exception.hpp>#include <util/stream_utils.hpp>#include <algorithm>BEGIN_NCBI_SCOPEtypedef CFileSourceCollector::TFilePos TFilePos;typedef CFileSourceCollector::TFileOff TFileOff;/////////////////////////////////////////////////////////////////////////////// CByteSource/////////////////////////////////////////////////////////////////////////////CByteSource::CByteSource(void){}CByteSource::~CByteSource(void){}/////////////////////////////////////////////////////////////////////////////// CByteSourceReader/////////////////////////////////////////////////////////////////////////////CByteSourceReader::CByteSourceReader(void){}CByteSourceReader::~CByteSourceReader(void){}bool CByteSourceReader::EndOfData(void) const{    return true;}bool CByteSourceReader::Pushback(const char* /*data*/, size_t size){    if ( size ) {        ERR_POST("CByteSourceReader::Pushback: "                 "cannot push back " << size << " bytes");        return false;    }    return true;}void CByteSourceReader::Seekg(size_t pos){    NCBI_THROW(CUtilException,eWrongCommand,"CByteSourceReader::Seekg: unable to seek");}CRef<CSubSourceCollector>CByteSourceReader::SubSource(size_t /*prevent*/,                             CRef<CSubSourceCollector> parent){    return CRef<CSubSourceCollector>(new CMemorySourceCollector(parent));}/////////////////////////////////////////////////////////////////////////////// CSubSourceCollector/////////////////////////////////////////////////////////////////////////////CSubSourceCollector::CSubSourceCollector(CRef<CSubSourceCollector> parent)    : m_ParentSubSource(parent){    return;}CSubSourceCollector::~CSubSourceCollector(void){    return;}void CSubSourceCollector::AddChunk(const char* buffer, size_t bufferLength){    if ( !m_ParentSubSource.IsNull() ) {        m_ParentSubSource->AddChunk(buffer,bufferLength);    }}/////////////////////////////////////////////////////////////////////////////// CStreamByteSource//////////////////////////////////////////////////////////////////////////////* Mac compiler doesn't like getting these flags as unsigned int (thiessen)static inlineunsigned IFStreamFlags(bool binary){    return binary ? (IOS_BASE::in | IOS_BASE::binary) : IOS_BASE::in;}*/#define IFStreamFlags(isBinary) \  (isBinary? (IOS_BASE::in | IOS_BASE::binary): IOS_BASE::in)CStreamByteSource::CStreamByteSource(CNcbiIstream& in)    : m_Stream(&in){}CStreamByteSource::~CStreamByteSource(void){}CRef<CByteSourceReader> CStreamByteSource::Open(void){    return CRef<CByteSourceReader>        (new CStreamByteSourceReader(this, m_Stream));}/////////////////////////////////////////////////////////////////////////////// CStreamByteSourceReader/////////////////////////////////////////////////////////////////////////////CStreamByteSourceReader::CStreamByteSourceReader(const CByteSource* source,                                                 CNcbiIstream* stream)    : m_Source(source), m_Stream(stream){}CStreamByteSourceReader::~CStreamByteSourceReader(void){}size_t CStreamByteSourceReader::Read(char* buffer, size_t bufferLength){    return CStreamUtils::Readsome(*m_Stream, buffer, bufferLength);}bool CStreamByteSourceReader::EndOfData(void) const{    return m_Stream->eof();}bool CStreamByteSourceReader::Pushback(const char* data, size_t size){    CStreamUtils::Pushback(*m_Stream, data, size);    return true;}void CStreamByteSourceReader::Seekg(size_t pos){    m_Stream->seekg(pos);}/////////////////////////////////////////////////////////////////////////////// CIRByteSourceReader/////////////////////////////////////////////////////////////////////////////CIRByteSourceReader::CIRByteSourceReader(IReader* reader)    : m_Reader(reader), m_EOF(false){}CIRByteSourceReader::~CIRByteSourceReader(void){}size_t CIRByteSourceReader::Read(char* buffer, size_t bufferLength){    size_t bytes_read;    m_Reader->Read(buffer, bufferLength, &bytes_read);    if ( bytes_read < bufferLength ) {        m_EOF = true;    }    return bytes_read;}bool CIRByteSourceReader::EndOfData(void) const{    return m_EOF;}/////////////////////////////////////////////////////////////////////////////// CFStreamByteSource/////////////////////////////////////////////////////////////////////////////CFStreamByteSource::CFStreamByteSource(CNcbiIstream& in)    : CStreamByteSource(in){}CFStreamByteSource::CFStreamByteSource(const string& fileName, bool binary)    : CStreamByteSource(*new CNcbiIfstream(fileName.c_str(),                                           IFStreamFlags(binary))){    if ( !*m_Stream ) {        NCBI_THROW(CUtilException,eNoInput,"file not found: " + fileName);    }}CFStreamByteSource::~CFStreamByteSource(void){    delete m_Stream;}/////////////////////////////////////////////////////////////////////////////// CFileByteSource/////////////////////////////////////////////////////////////////////////////CFileByteSource::CFileByteSource(const string& fileName, bool binary)    : m_FileName(fileName), m_Binary(binary){    return;}CFileByteSource::CFileByteSource(const CFileByteSource& file)    : m_FileName(file.m_FileName), m_Binary(file.m_Binary){    return;}CFileByteSource::~CFileByteSource(void){}CRef<CByteSourceReader> CFileByteSource::Open(void){    return CRef<CByteSourceReader>(new CFileByteSourceReader(this));}/////////////////////////////////////////////////////////////////////////////// CSubFileByteSource/////////////////////////////////////////////////////////////////////////////CSubFileByteSource::CSubFileByteSource(const CFileByteSource& file,                                       TFilePos start, TFileOff length)    : CParent(file), m_Start(start), m_Length(length){    return;}CSubFileByteSource::~CSubFileByteSource(void){}CRef<CByteSourceReader> CSubFileByteSource::Open(void){    return CRef<CByteSourceReader>        (new CSubFileByteSourceReader(this, m_Start, m_Length));}/////////////////////////////////////////////////////////////////////////////// CFileByteSourceReader/////////////////////////////////////////////////////////////////////////////CFileByteSourceReader::CFileByteSourceReader(const CFileByteSource* source)    : CStreamByteSourceReader(source, 0),      m_FileSource(source),      m_FStream(source->GetFileName().c_str(),                IFStreamFlags(source->IsBinary())){    if ( !m_FStream ) {//        THROW1_TRACE(runtime_error, "file not found: " +//                     source->GetFileName());        NCBI_THROW(CUtilException,eNoInput,                   "file not found: " +source->GetFileName());    }    m_Stream = &m_FStream;}CFileByteSourceReader::~CFileByteSourceReader(void){}CRef<CSubSourceCollector> CFileByteSourceReader::SubSource(size_t prepend,                                  CRef<CSubSourceCollector> parent){    return CRef<CSubSourceCollector>(new CFileSourceCollector(m_FileSource,                                    m_Stream->tellg() - TFileOff(prepend),                                    parent));}/////////////////////////////////////////////////////////////////////////////// CSubFileByteSourceReader/////////////////////////////////////////////////////////////////////////////CSubFileByteSourceReader::CSubFileByteSourceReader(const CFileByteSource* s,                                                   TFilePos start,                                                   TFileOff length)    : CParent(s), m_Length(length){    m_Stream->seekg(start);}CSubFileByteSourceReader::~CSubFileByteSourceReader(void){}size_t CSubFileByteSourceReader::Read(char* buffer, size_t bufferLength){    if ( TFileOff(bufferLength) > m_Length ) {        bufferLength = size_t(m_Length);    }    size_t count = CParent::Read(buffer, bufferLength);    m_Length -= TFileOff(count);    return count;}bool CSubFileByteSourceReader::EndOfData(void) const{    return m_Length == 0;}/////////////////////////////////////////////////////////////////////////////// CFileSourceCollector/////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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