1 /*  $Id: asnio.cpp 103491 2007-05-04 17:18:18Z kazimird $
2 * ===========================================================================
3 *
4 *                            PUBLIC DOMAIN NOTICE
5 *               National Center for Biotechnology Information
6 *
7 *  This software/database is a "United States Government Work" under the
8 *  terms of the United States Copyright Act.  It was written as part of
9 *  the author's official duties as a United States Government employee and
10 *  thus cannot be copyrighted.  This software/database is freely available
11 *  to the public for use. The National Library of Medicine and the U.S.
12 *  Government have not placed any restriction on its use or reproduction.
13 *
14 *  Although all reasonable efforts have been taken to ensure the accuracy
15 *  and reliability of the software and data, the NLM and the U.S.
16 *  Government do not and cannot warrant the performance or results that
17 *  may be obtained by using this software or data. The NLM and the U.S.
18 *  Government disclaim all warranties, express or implied, including
19 *  warranties of performance, merchantability or fitness for any particular
20 *  purpose.
21 *
22 *  Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Author: Eugene Vasilchenko
27 *
28 * File Description:
29 *   !!! PUT YOUR DESCRIPTION HERE !!!
30 */
31 
32 #include <ncbi_pch.hpp>
33 #include <corelib/ncbistd.hpp>
34 #include <ctools/asn/asnio.hpp>
35 #include <algorithm>
36 
37 
38 BEGIN_NCBI_SCOPE
39 
40 
41 /////////////////////////////////////////////////////////////////////////////
42 //  AsnMemoryRead::
43 //
44 
AsnMemoryRead(Uint2 mode,const string & str)45 AsnMemoryRead::AsnMemoryRead(Uint2 mode, const string& str)
46     : m_Source(str), m_Data(str.c_str()), m_Size(str.size()), m_mode(mode)
47 {
48     Init();
49 }
50 
AsnMemoryRead(Uint2 mode,const char * data,size_t size)51 AsnMemoryRead::AsnMemoryRead(Uint2 mode, const char* data, size_t size)
52     : m_Data(data), m_Size(size), m_mode(mode)
53 {
54     Init();
55 }
56 
~AsnMemoryRead(void)57 AsnMemoryRead::~AsnMemoryRead(void)
58 {
59     AsnIoClose(m_In);
60 }
61 
Read(char * buffer,size_t size)62 size_t AsnMemoryRead::Read(char* buffer, size_t size)
63 {
64     size_t count = min(size, m_Size - m_Ptr);
65     memcpy(buffer, m_Data + m_Ptr, count);
66     m_Ptr += count;
67     return count;
68 }
69 
ReadAsn(Pointer data,CharPtr buffer,Uint2 size)70 static Int2 LIBCALLBACK ReadAsn(Pointer data, CharPtr buffer, Uint2 size)
71 {
72     if ( !data || !buffer )
73         return -1;
74 
75     return Int2(static_cast<AsnMemoryRead*>(data)->Read(buffer, size));
76 }
77 
Init(void)78 void AsnMemoryRead::Init(void)
79 {
80     m_Ptr = 0;
81     m_In = AsnIoNew(m_mode | ASNIO_IN, 0, this,
82 		    reinterpret_cast<IoFuncType>(ReadAsn), 0);
83 }
84 
85 
86 
87 /////////////////////////////////////////////////////////////////////////////
88 //  AsnMemoryWrite::
89 //
90 
WriteAsn(Pointer data,CharPtr buffer,Uint2 size)91 static Int2 LIBCALLBACK WriteAsn(Pointer data, CharPtr buffer, Uint2 size)
92 {
93     if ( !data || !buffer )
94         return -1;
95 
96     return Int2(static_cast<AsnMemoryWrite*>(data)->Write(buffer, size));
97 }
98 
AsnMemoryWrite(Uint2 mode)99 AsnMemoryWrite::AsnMemoryWrite(Uint2 mode)
100     : m_Data(new char[512]), m_Size(512), m_Ptr(0)
101 {
102     m_Out = AsnIoNew(mode | ASNIO_OUT, 0, this,
103 		     0, reinterpret_cast<IoFuncType>(WriteAsn));
104 }
105 
~AsnMemoryWrite(void)106 AsnMemoryWrite::~AsnMemoryWrite(void)
107 {
108     AsnIoClose(m_Out);
109     delete[] m_Data;
110 }
111 
Write(const char * buffer,size_t size)112 size_t AsnMemoryWrite::Write(const char* buffer, size_t size)
113 {
114     if ( m_Size - m_Ptr < size ) { // not enough space
115         // new buffer
116         char* data = new char[m_Size *= 2];
117         if ( m_Ptr ) // copy old data
118             memcpy(data, m_Data, m_Ptr);
119         // delete old buffer
120         delete[] m_Data;
121         // set new buffer
122         m_Data = data;
123     }
124     // append data
125     memcpy(m_Data + m_Ptr, buffer, size);
126     // increase size
127     m_Ptr += size;
128     return size;
129 }
130 
flush(void) const131 void AsnMemoryWrite::flush(void) const
132 {
133     AsnIoFlush(m_Out);
134 }
135 
136 END_NCBI_SCOPE
137