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