1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include <precomp.h>
29 #include <cosv/mbstream.hxx>
30 
31 // NOT FULLY DECLARED SERVICES
32 #include <string.h>
33 
34 
35 namespace csv
36 {
37 
38 
39 
40 mbstream::mbstream( uintt i_nSize )
41 	:	dpOwnedMemorySpace( new char[i_nSize+1] ),
42 		nSize( i_nSize ),
43 		nCurPosition( 0 )
44 {
45 	dpOwnedMemorySpace[i_nSize] = '\0';
46 }
47 
48 mbstream::~mbstream()
49 {
50 	delete [] dpOwnedMemorySpace;
51 }
52 
53 void
54 mbstream::resize( uintt  i_nSize )
55 {
56 	DYN char * pNew = new char[i_nSize];
57 	memcpy( pNew, dpOwnedMemorySpace, min(i_nSize,nSize) );
58 	delete [] dpOwnedMemorySpace;
59 	dpOwnedMemorySpace = pNew;
60 	nSize = i_nSize;
61 }
62 
63 uintt
64 mbstream::do_read( void *	       out_pDest,
65 				   uintt           i_nNrofBytes )
66 {
67 	uintt  ret = min( i_nNrofBytes, nSize - nCurPosition );
68 	memcpy( out_pDest, dpOwnedMemorySpace, ret );
69 	nCurPosition += ret;
70 	return ret;
71 }
72 
73 bool
74 mbstream::inq_eod() const
75 {
76  	return nCurPosition == nSize;
77 }
78 
79 uintt
80 mbstream::do_write( const void *   	i_pSrc,
81 					uintt           i_nNrofBytes )
82 {
83 	resize( max( 3 * (nSize+1) / 2, nCurPosition + i_nNrofBytes) );
84 	memcpy( dpOwnedMemorySpace+nCurPosition, i_pSrc, i_nNrofBytes );
85 	nCurPosition += i_nNrofBytes;
86 	return i_nNrofBytes;
87 }
88 
89 uintt
90 mbstream::do_seek( intt 	i_nDistance,
91                    seek_dir i_eStartPoint )
92 {
93     switch ( i_eStartPoint )
94     {
95      	case beg:       if ( uintt(i_nDistance) < nSize )
96                             nCurPosition = uintt(i_nDistance);
97                         break;
98      	case cur:       if ( i_nDistance < 0
99                                 ?   uintt(-i_nDistance) <= nCurPosition
100                                 :   uintt(i_nDistance) + nCurPosition < nSize )
101                             nCurPosition = uintt( intt(nCurPosition) + i_nDistance );
102                         break;
103      	case end:       if ( i_nDistance < 0
104                              AND uintt(-i_nDistance) < nSize - 1 )
105                             nCurPosition = uintt( intt(nSize) - 1 + i_nDistance );
106                         break;
107     }
108 	return position();
109 }
110 
111 uintt
112 mbstream::inq_position() const
113 {
114     return nCurPosition;
115 }
116 
117 
118 }   // namespace csv
119 
120