merged tag ooo/OOO330_m14
[LibreOffice.git] / cosv / source / storage / mbstream.cxx
blob5559d1f6d59aab105f7c09609ec0ebabff3491f2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 #include <precomp.h>
29 #include <cosv/mbstream.hxx>
31 // NOT FULLY DECLARED SERVICES
32 #include <string.h>
35 namespace csv
40 mbstream::mbstream( uintt i_nSize )
41 : dpOwnedMemorySpace( new char[i_nSize+1] ),
42 nSize( i_nSize ),
43 nCurPosition( 0 )
45 dpOwnedMemorySpace[i_nSize] = '\0';
48 mbstream::~mbstream()
50 delete [] dpOwnedMemorySpace;
53 void
54 mbstream::resize( uintt i_nSize )
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;
63 uintt
64 mbstream::do_read( void * out_pDest,
65 uintt i_nNrofBytes )
67 uintt ret = min( i_nNrofBytes, nSize - nCurPosition );
68 memcpy( out_pDest, dpOwnedMemorySpace, ret );
69 nCurPosition += ret;
70 return ret;
73 bool
74 mbstream::inq_eod() const
76 return nCurPosition == nSize;
79 uintt
80 mbstream::do_write( const void * i_pSrc,
81 uintt i_nNrofBytes )
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;
89 uintt
90 mbstream::do_seek( intt i_nDistance,
91 seek_dir i_eStartPoint )
93 switch ( i_eStartPoint )
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;
108 return position();
111 uintt
112 mbstream::inq_position() const
114 return nCurPosition;
118 } // namespace csv