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

📄 strbuffer.cpp

📁 ncbi源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * =========================================================================== * PRODUCTION $Log: strbuffer.cpp,v $ * PRODUCTION Revision 1000.3  2004/06/01 19:40:32  gouriano * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.42 * PRODUCTION * =========================================================================== *//*  $Id: strbuffer.cpp,v 1000.3 2004/06/01 19:40:32 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:*   Input buffer** ---------------------------------------------------------------------------* $Log: strbuffer.cpp,v $* Revision 1000.3  2004/06/01 19:40:32  gouriano* PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.42** Revision 1.42  2004/05/24 18:13:01  gouriano* In text output files make indentation optional** Revision 1.41  2004/05/17 21:06:02  gorelenk* Added include of PCH ncbi_pch.hpp** Revision 1.40  2004/05/10 14:51:13  gouriano* Work with unsigned number (division/modulus ops) when writing an integer** Revision 1.39  2003/12/31 20:51:33  gouriano* added possibility to seek (when possible) in CByteSourceReader** Revision 1.38  2003/11/19 15:41:11  vasilche* Pushback unused data in destructor of CIStreamBuffer.** Revision 1.37  2003/09/25 12:49:14  kuznets* Change to allowed more than one subsource collector** Revision 1.36  2003/03/21 17:15:36  vasilche* Avoid unnecessary buffer growth in GetChars().** Revision 1.35  2003/02/26 21:32:00  gouriano* modify C++ exceptions thrown by this library** Revision 1.34  2002/11/18 19:49:37  grichenk* More details in error messages** Revision 1.33  2002/11/04 21:29:22  grichenk* Fixed usage of const CRef<> and CRef<> constructor** Revision 1.32  2002/10/22 20:22:55  gouriano* undo the prev change** Revision 1.31  2002/10/22 19:01:36  gouriano* replaced THROW0_TRACE by throw in CIStreamBuffer::FillBuffer** Revision 1.30  2002/01/29 16:01:21  grichenk* COStreamBuffer destructor fixed - no exceptions thrown** Revision 1.29  2001/08/08 18:35:55  grichenk* CIStreamBuffer::FindChar() -- fixed bug with buffer pointers** Revision 1.28  2001/06/20 17:36:19  grichenk* Updated FillBuffer() to work with GCC 3.0** Revision 1.27  2001/06/06 15:50:01  grichenk* Fixed auto-wrapping of long lines** Revision 1.26  2001/05/17 15:07:15  lavr* Typos corrected** Revision 1.25  2001/05/11 13:58:52  grichenk* Removed "while" reading loop in CIStreamBuffer::FillBuffer()** Revision 1.24  2001/04/17 21:47:29  vakatov* COStreamBuffer::Flush() -- try to flush the underlying output stream* regardless of its state (clear the state temporarily before flushing,* restore it afterwards).** Revision 1.23  2001/03/14 17:59:26  vakatov* COStreamBuffer::  renamed GetFreeSpace() -> GetAvailableSpace()* to avoid clash with MS-Win system headers' #define** Revision 1.22  2001/01/05 20:09:05  vasilche* Added util directory for various algorithms and utility classes.** Revision 1.21  2000/12/15 15:38:46  vasilche* Added support of Int8 and long double.* Enum values now have type Int4 instead of long.** Revision 1.20  2000/10/20 15:51:44  vasilche* Fixed data error processing.* Added interface for constructing container objects directly into output stream.* object.hpp, object.inl and object.cpp were split to* objectinfo.*, objecttype.*, objectiter.* and objectio.*.** Revision 1.19  2000/10/13 20:59:21  vasilche* Avoid using of ssize_t absent on some compilers.** Revision 1.18  2000/10/13 20:22:57  vasilche* Fixed warnings on 64 bit compilers.* Fixed missing typename in templates.** Revision 1.17  2000/08/15 19:44:51  vasilche* Added Read/Write hooks:* CReadObjectHook/CWriteObjectHook for objects of specified type.* CReadClassMemberHook/CWriteClassMemberHook for specified members.* CReadChoiceVariantHook/CWriteChoiceVariant for specified choice variants.* CReadContainerElementHook/CWriteContainerElementsHook for containers.** Revision 1.16  2000/07/03 18:42:47  vasilche* Added interface to typeinfo via CObjectInfo and CConstObjectInfo.* Reduced header dependency.** Revision 1.15  2000/06/16 20:01:26  vasilche* Avoid use of unexpected_exception() which is unimplemented on Mac.** Revision 1.14  2000/06/01 19:07:05  vasilche* Added parsing of XML data.** Revision 1.13  2000/05/24 20:08:50  vasilche* Implemented XML dump.** Revision 1.12  2000/05/03 14:38:14  vasilche* SERIAL: added support for delayed reading to generated classes.* DATATOOL: added code generation for delayed reading.** Revision 1.11  2000/04/28 16:58:14  vasilche* Added classes CByteSource and CByteSourceReader for generic reading.* Added delayed reading of choice variants.** Revision 1.10  2000/04/13 14:50:28  vasilche* Added CObjectIStream::Open() and CObjectOStream::Open() for easier use.** Revision 1.9  2000/04/06 16:11:01  vasilche* Fixed bug with iterators in choices.* Removed unneeded calls to ReadExternalObject/WriteExternalObject.* Added output buffering to text ASN.1 data.** Revision 1.8  2000/03/29 15:55:29  vasilche* Added two versions of object info - CObjectInfo and CConstObjectInfo.* Added generic iterators by class -* 	CTypeIterator<class>, CTypeConstIterator<class>,* 	CStdTypeIterator<type>, CStdTypeConstIterator<type>,* 	CObjectsIterator and CObjectsConstIterator.** Revision 1.7  2000/03/10 21:16:47  vasilche* Removed EOF workaround code.** Revision 1.6  2000/03/10 17:59:21  vasilche* Fixed error reporting.* Added EOF bug workaround on MIPSpro compiler (not finished).** Revision 1.5  2000/03/07 14:06:24  vasilche* Added stream buffering to ASN.1 binary input.* Optimized class loading/storing.* Fixed bugs in processing OPTIONAL fields.* Added generation of reference counted objects.** Revision 1.4  2000/02/17 20:02:45  vasilche* Added some standard serialization exceptions.* Optimized text/binary ASN.1 reading.* Fixed wrong encoding of StringStore in ASN.1 binary format.* Optimized logic of object collection.** Revision 1.3  2000/02/11 17:10:25  vasilche* Optimized text parsing.** Revision 1.2  2000/02/02 19:07:41  vasilche* Added THROWS_NONE to constructor/destructor of exception.** Revision 1.1  2000/02/01 21:47:23  vasilche* Added CGeneratedChoiceTypeInfo for generated choice classes.* Added buffering to CObjectIStreamAsn.* Removed CMemberInfo subclasses.* Added support for DEFAULT/OPTIONAL members.*** ===========================================================================*/#include <ncbi_pch.hpp>#include <corelib/ncbistre.hpp>#include <corelib/ncbi_limits.hpp>#include <util/strbuffer.hpp>#include <util/bytesrc.hpp>#include <algorithm>BEGIN_NCBI_SCOPEstatic const size_t KInitialBufferSize = 4096;static inlinesize_t BiggerBufferSize(size_t size) THROWS1_NONE{    return size * 2;}CIStreamBuffer::CIStreamBuffer(void)    THROWS1((bad_alloc))    : m_Error(0), m_BufferOffset(0),      m_BufferSize(KInitialBufferSize), m_Buffer(new char[KInitialBufferSize]),      m_CurrentPos(m_Buffer), m_DataEndPos(m_Buffer),      m_Line(1),      m_CollectPos(0){}CIStreamBuffer::~CIStreamBuffer(void){    try {        Close();    }    catch ( exception& exc ) {        ERR_POST(Warning <<                 "~CIStreamBuffer: exception while closing: " << exc.what());    }    delete[] m_Buffer;    }void CIStreamBuffer::Open(CByteSourceReader& reader){    Close();    m_Input = &reader;    m_Error = 0;}void CIStreamBuffer::Close(void){    if ( m_Input ) {        size_t unused = m_DataEndPos - m_CurrentPos;        if ( unused ) {            m_Input->Pushback(m_CurrentPos, unused);        }        m_Input.Reset();    }    m_BufferOffset = 0;    m_CurrentPos = m_Buffer;    m_DataEndPos = m_Buffer;    m_Line = 1;    m_Error = 0;}void CIStreamBuffer::StartSubSource(void){    _ASSERT(!m_CollectPos);    m_CollectPos = m_CurrentPos;    m_Collector = m_Input->SubSource(m_DataEndPos - m_CurrentPos, m_Collector);}CRef<CByteSource> CIStreamBuffer::EndSubSource(void){    _ASSERT(m_Collector);    _ASSERT(m_CollectPos);    _ASSERT(m_CollectPos <= m_CurrentPos);    if ( m_CurrentPos != m_CollectPos )        m_Collector->AddChunk(m_CollectPos, m_CurrentPos - m_CollectPos);    CRef<CByteSource> source = m_Collector->GetSource();    m_CollectPos = 0;    m_Collector.Reset();    return source;}// this method is highly optimizedchar CIStreamBuffer::SkipSpaces(void)    THROWS1((CIOException)){    // cache pointers    char* pos = m_CurrentPos;    char* end = m_DataEndPos;    // make sure thire is at least one char in buffer    if ( pos == end ) {        // fill buffer        pos = FillBuffer(pos);        // cache m_DataEndPos        end = m_DataEndPos;    }    // main cycle    // at the beginning:    //     pos == m_CurrentPos    //     end == m_DataEndPos    //     pos < end    for (;;) {        // we use do{}while() cycle because        // condition is true at the beginning ( pos < end )        do {            // cache current char            char c = *pos;            if ( c != ' ' ) { // it's not space (' ')                // point m_CurrentPos to first non space char                m_CurrentPos = pos;                // return char value                return c;            }            // skip space char        } while ( ++pos < end );        // here pos == end == m_DataEndPos        // point m_CurrentPos to end of buffer        m_CurrentPos = pos;        // fill next portion        pos = FillBuffer(pos);        // cache m_DataEndPos        end = m_DataEndPos;    }}// this method is highly optimizedvoid CIStreamBuffer::FindChar(char c)    THROWS1((CIOException)){    // cache pointers    char* pos = m_CurrentPos;    char* end = m_DataEndPos;    // make sure thire is at least one char in buffer    if ( pos == end ) {        // fill buffer        pos = FillBuffer(pos);        // cache m_DataEndPos        end = m_DataEndPos;    }    // main cycle    // at the beginning:    //     pos == m_CurrentPos    //     end == m_DataEndPos    //     pos < end    for (;;) {        char* found = static_cast<char*>(memchr(pos, c, end - pos));        if ( found ) {            m_CurrentPos = found;            return;        }        // point m_CurrentPos to end of buffer        m_CurrentPos = end;        // fill next portion        pos = FillBuffer(end);        // cache m_DataEndPos        end = m_DataEndPos;    }}// this method is highly optimizedsize_t CIStreamBuffer::PeekFindChar(char c, size_t limit)    THROWS1((CIOException)){    _ASSERT(limit > 0);    PeekCharNoEOF(limit - 1);    // cache pointers    char* pos = m_CurrentPos;    size_t bufferSize = m_DataEndPos - pos;    if ( bufferSize != 0 ) {        char* found =            static_cast<char*>(memchr(pos, c, min(limit, bufferSize)));        if ( found )            return found - pos;    }    return limit;}char* CIStreamBuffer::FillBuffer(char* pos, bool noEOF)    THROWS1((CIOException, bad_alloc)){    _ASSERT(pos >= m_DataEndPos);    // remove unused portion of buffer at the beginning    _ASSERT(m_CurrentPos >= m_Buffer);    size_t erase = m_CurrentPos - m_Buffer;    if ( erase > 0 ) {        char* newPos = m_CurrentPos - erase;        if ( m_Collector ) {            _ASSERT(m_CollectPos);            size_t count = m_CurrentPos - m_CollectPos;            if ( count > 0 )                m_Collector->AddChunk(m_CollectPos, count);            m_CollectPos = newPos;        }        size_t copy_count = m_DataEndPos - m_CurrentPos;        if ( copy_count )            memmove(newPos, m_CurrentPos, copy_count);        m_CurrentPos = newPos;        m_DataEndPos -= erase;        m_BufferOffset += erase;        pos -= erase;    }    size_t dataSize = m_DataEndPos - m_Buffer;    size_t newPosOffset = pos - m_Buffer;    if ( newPosOffset >= m_BufferSize ) {        // reallocate buffer        size_t newSize = BiggerBufferSize(m_BufferSize);        while ( newPosOffset >= newSize ) {            newSize = BiggerBufferSize(newSize);        }        char* newBuffer = new char[newSize];        memcpy(newBuffer, m_Buffer, dataSize);        m_CurrentPos = newBuffer + (m_CurrentPos - m_Buffer);        if ( m_CollectPos )            m_CollectPos = newBuffer + (m_CollectPos - m_Buffer);        pos = newBuffer + newPosOffset;        m_DataEndPos = newBuffer + dataSize;        delete[] m_Buffer;        m_Buffer = newBuffer;        m_BufferSize = newSize;    }    size_t load = m_BufferSize - dataSize;    while ( load > 0  &&  pos >= m_DataEndPos ) {        size_t count = m_Input->Read(m_DataEndPos, load);        if ( count == 0 ) {            if ( pos < m_DataEndPos )                return pos;            if ( m_Input->EndOfData() ) {                if ( noEOF ) {                    // ignore EOF                    _ASSERT(m_Buffer <= m_CurrentPos);                    _ASSERT(m_CurrentPos <= pos);                    _ASSERT(m_DataEndPos <= m_Buffer + m_BufferSize);                    _ASSERT(!m_CollectPos ||                            (m_CollectPos>=m_Buffer &&                             m_CollectPos<=m_CurrentPos));                    return pos;                }                m_Error = "end of file";//                THROW0_TRACE(CEofException());                NCBI_THROW(CEofException,eEof,m_Error);            }            else {                m_Error = "read fault";//                THROW1_TRACE(CIOException, "read fault");                NCBI_THROW(CIOException,eRead,m_Error);            }        }        m_DataEndPos += count;        load -= count;    }    _ASSERT(m_Buffer <= m_CurrentPos);    _ASSERT(m_CurrentPos <= pos);    _ASSERT(pos < m_DataEndPos);    _ASSERT(m_DataEndPos <= m_Buffer + m_BufferSize);    _ASSERT(!m_CollectPos || (m_CollectPos>=m_Buffer && m_CollectPos<=m_CurrentPos));    return pos;}char CIStreamBuffer::FillBufferNoEOF(char* pos)    THROWS1((CIOException, bad_alloc)){    pos = FillBuffer(pos, true);    if ( pos >= m_DataEndPos )        return 0;    else        return *pos;}void CIStreamBuffer::GetChars(char* buffer, size_t count)    THROWS1((CIOException)){    // cache pos    char* pos = m_CurrentPos;    for ( ;; ) {        size_t c = m_DataEndPos - pos;        if ( c >= count ) {            // all data is already in buffer -> copy it            memcpy(buffer, pos, count);            m_CurrentPos = pos + count;            return;        }        else {            memcpy(buffer, pos, c);            buffer += c;            count -= c;            m_CurrentPos = pos += c;            pos = FillBuffer(pos);        }    }}void CIStreamBuffer::GetChars(size_t count)    THROWS1((CIOException))

⌨️ 快捷键说明

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