NCBI C++ ToolKit
asnio.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: asnio.cpp 33815 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 
39 
40 
41 /////////////////////////////////////////////////////////////////////////////
42 // AsnMemoryRead::
43 //
44 
46  : m_Source(str), m_Data(str.c_str()), m_Size(str.size()), m_mode(mode)
47 {
48  Init();
49 }
50 
52  : m_Data(data), m_Size(size), m_mode(mode)
53 {
54  Init();
55 }
56 
58 {
59  AsnIoClose(m_In);
60 }
61 
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 
71 {
72  if ( !data || !buffer )
73  return -1;
74 
75  return Int2(static_cast<AsnMemoryRead*>(data)->Read(buffer, size));
76 }
77 
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 
92 {
93  if ( !data || !buffer )
94  return -1;
95 
96  return Int2(static_cast<AsnMemoryWrite*>(data)->Write(buffer, size));
97 }
98 
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 
107 {
108  AsnIoClose(m_Out);
109  delete[] m_Data;
110 }
111 
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 
131 void AsnMemoryWrite::flush(void) const
132 {
133  AsnIoFlush(m_Out);
134 }
135 
static Int2 LIBCALLBACK WriteAsn(Pointer data, CharPtr buffer, Uint2 size)
Definition: asnio.cpp:91
static Int2 LIBCALLBACK ReadAsn(Pointer data, CharPtr buffer, Uint2 size)
Definition: asnio.cpp:70
virtual ~AsnMemoryRead(void)
Definition: asnio.cpp:57
const char * m_Data
Definition: asnio.hpp:59
void Init(void)
Definition: asnio.cpp:78
int m_mode
Definition: asnio.hpp:62
AsnIoPtr m_In
Definition: asnio.hpp:65
size_t m_Size
Definition: asnio.hpp:60
AsnMemoryRead(Uint2 mode, const string &str)
Definition: asnio.cpp:45
size_t m_Ptr
Definition: asnio.hpp:61
size_t Read(char *buffer, size_t size)
Definition: asnio.cpp:62
size_t m_Size
Definition: asnio.hpp:94
AsnMemoryWrite(Uint2 mode)
Definition: asnio.cpp:99
virtual ~AsnMemoryWrite(void)
Definition: asnio.cpp:106
void flush(void) const
Definition: asnio.cpp:131
AsnIoPtr m_Out
Definition: asnio.hpp:98
size_t Write(const char *buffer, size_t size)
Definition: asnio.cpp:112
char * m_Data
Definition: asnio.hpp:93
size_t m_Ptr
Definition: asnio.hpp:95
Include a standard set of the NCBI C++ Toolkit most basic headers.
static const char * str(char *buf, int n)
Definition: stats.c:84
char data[12]
Definition: iconv.c:80
#define CharPtr
Definition: ncbistd.hpp:125
#define LIBCALLBACK
Definition: ncbistd.hpp:302
#define Pointer
Definition: ncbistd.hpp:114
void Read(CObjectIStream &in, TObjectPtr object, const CTypeRef &type)
Definition: serial.cpp:60
void Write(CObjectOStream &out, TConstObjectPtr object, const CTypeRef &type)
Definition: serial.cpp:55
int16_t Int2
2-byte (16-bit) signed integer
Definition: ncbitype.h:100
uint16_t Uint2
2-byte (16-bit) unsigned integer
Definition: ncbitype.h:101
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
mdb_mode_t mode
Definition: lmdb++.h:38
const struct ncbi::grid::netcache::search::fields::SIZE size
T min(T x_, T y_)
#define count
static uint8_t * buffer
Definition: pcre2test.c:1016
Modified on Fri Sep 20 14:57:14 2024 by modify_doxy.py rev. 669887