1*59617ebcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*59617ebcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*59617ebcSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*59617ebcSAndrew Rist * distributed with this work for additional information
6*59617ebcSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*59617ebcSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*59617ebcSAndrew Rist * "License"); you may not use this file except in compliance
9*59617ebcSAndrew Rist * with the License. You may obtain a copy of the License at
10*59617ebcSAndrew Rist *
11*59617ebcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*59617ebcSAndrew Rist *
13*59617ebcSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*59617ebcSAndrew Rist * software distributed under the License is distributed on an
15*59617ebcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*59617ebcSAndrew Rist * KIND, either express or implied. See the License for the
17*59617ebcSAndrew Rist * specific language governing permissions and limitations
18*59617ebcSAndrew Rist * under the License.
19*59617ebcSAndrew Rist *
20*59617ebcSAndrew Rist *************************************************************/
21*59617ebcSAndrew Rist
22*59617ebcSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir #include <precomp.h>
25cdf0e10cSrcweir #include <cosv/mbstream.hxx>
26cdf0e10cSrcweir
27cdf0e10cSrcweir // NOT FULLY DECLARED SERVICES
28cdf0e10cSrcweir #include <string.h>
29cdf0e10cSrcweir
30cdf0e10cSrcweir
31cdf0e10cSrcweir namespace csv
32cdf0e10cSrcweir {
33cdf0e10cSrcweir
34cdf0e10cSrcweir
35cdf0e10cSrcweir
mbstream(uintt i_nSize)36cdf0e10cSrcweir mbstream::mbstream( uintt i_nSize )
37cdf0e10cSrcweir : dpOwnedMemorySpace( new char[i_nSize+1] ),
38cdf0e10cSrcweir nSize( i_nSize ),
39cdf0e10cSrcweir nCurPosition( 0 )
40cdf0e10cSrcweir {
41cdf0e10cSrcweir dpOwnedMemorySpace[i_nSize] = '\0';
42cdf0e10cSrcweir }
43cdf0e10cSrcweir
~mbstream()44cdf0e10cSrcweir mbstream::~mbstream()
45cdf0e10cSrcweir {
46cdf0e10cSrcweir delete [] dpOwnedMemorySpace;
47cdf0e10cSrcweir }
48cdf0e10cSrcweir
49cdf0e10cSrcweir void
resize(uintt i_nSize)50cdf0e10cSrcweir mbstream::resize( uintt i_nSize )
51cdf0e10cSrcweir {
52cdf0e10cSrcweir DYN char * pNew = new char[i_nSize];
53cdf0e10cSrcweir memcpy( pNew, dpOwnedMemorySpace, min(i_nSize,nSize) );
54cdf0e10cSrcweir delete [] dpOwnedMemorySpace;
55cdf0e10cSrcweir dpOwnedMemorySpace = pNew;
56cdf0e10cSrcweir nSize = i_nSize;
57cdf0e10cSrcweir }
58cdf0e10cSrcweir
59cdf0e10cSrcweir uintt
do_read(void * out_pDest,uintt i_nNrofBytes)60cdf0e10cSrcweir mbstream::do_read( void * out_pDest,
61cdf0e10cSrcweir uintt i_nNrofBytes )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir uintt ret = min( i_nNrofBytes, nSize - nCurPosition );
64cdf0e10cSrcweir memcpy( out_pDest, dpOwnedMemorySpace, ret );
65cdf0e10cSrcweir nCurPosition += ret;
66cdf0e10cSrcweir return ret;
67cdf0e10cSrcweir }
68cdf0e10cSrcweir
69cdf0e10cSrcweir bool
inq_eod() const70cdf0e10cSrcweir mbstream::inq_eod() const
71cdf0e10cSrcweir {
72cdf0e10cSrcweir return nCurPosition == nSize;
73cdf0e10cSrcweir }
74cdf0e10cSrcweir
75cdf0e10cSrcweir uintt
do_write(const void * i_pSrc,uintt i_nNrofBytes)76cdf0e10cSrcweir mbstream::do_write( const void * i_pSrc,
77cdf0e10cSrcweir uintt i_nNrofBytes )
78cdf0e10cSrcweir {
79cdf0e10cSrcweir resize( max( 3 * (nSize+1) / 2, nCurPosition + i_nNrofBytes) );
80cdf0e10cSrcweir memcpy( dpOwnedMemorySpace+nCurPosition, i_pSrc, i_nNrofBytes );
81cdf0e10cSrcweir nCurPosition += i_nNrofBytes;
82cdf0e10cSrcweir return i_nNrofBytes;
83cdf0e10cSrcweir }
84cdf0e10cSrcweir
85cdf0e10cSrcweir uintt
do_seek(intt i_nDistance,seek_dir i_eStartPoint)86cdf0e10cSrcweir mbstream::do_seek( intt i_nDistance,
87cdf0e10cSrcweir seek_dir i_eStartPoint )
88cdf0e10cSrcweir {
89cdf0e10cSrcweir switch ( i_eStartPoint )
90cdf0e10cSrcweir {
91cdf0e10cSrcweir case beg: if ( uintt(i_nDistance) < nSize )
92cdf0e10cSrcweir nCurPosition = uintt(i_nDistance);
93cdf0e10cSrcweir break;
94cdf0e10cSrcweir case cur: if ( i_nDistance < 0
95cdf0e10cSrcweir ? uintt(-i_nDistance) <= nCurPosition
96cdf0e10cSrcweir : uintt(i_nDistance) + nCurPosition < nSize )
97cdf0e10cSrcweir nCurPosition = uintt( intt(nCurPosition) + i_nDistance );
98cdf0e10cSrcweir break;
99cdf0e10cSrcweir case end: if ( i_nDistance < 0
100cdf0e10cSrcweir AND uintt(-i_nDistance) < nSize - 1 )
101cdf0e10cSrcweir nCurPosition = uintt( intt(nSize) - 1 + i_nDistance );
102cdf0e10cSrcweir break;
103cdf0e10cSrcweir }
104cdf0e10cSrcweir return position();
105cdf0e10cSrcweir }
106cdf0e10cSrcweir
107cdf0e10cSrcweir uintt
inq_position() const108cdf0e10cSrcweir mbstream::inq_position() const
109cdf0e10cSrcweir {
110cdf0e10cSrcweir return nCurPosition;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir
113cdf0e10cSrcweir
114cdf0e10cSrcweir } // namespace csv
115cdf0e10cSrcweir
116