bump product version to 4.2.0.1
[LibreOffice.git] / sot / source / sdstor / stgole.cxx
blob1825b357615cae08a07afbc684058444a3823912
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "rtl/string.h"
21 #include "stgole.hxx"
22 #include "sot/storinfo.hxx"
24 #ifdef _MSC_VER
25 #pragma warning(disable: 4342)
26 #endif
27 ///////////////////////// class StgInternalStream ////////////////////////
29 StgInternalStream::StgInternalStream( BaseStorage& rStg, const OUString& rName, bool bWr )
31 bIsWritable = true;
32 sal_uInt16 nMode = bWr
33 ? STREAM_WRITE | STREAM_SHARE_DENYALL
34 : STREAM_READ | STREAM_SHARE_DENYWRITE | STREAM_NOCREATE;
35 pStrm = rStg.OpenStream( rName, nMode );
37 // set the error code right here in the stream
38 SetError( rStg.GetError() );
39 SetBufferSize( 1024 );
42 StgInternalStream::~StgInternalStream()
44 delete pStrm;
47 sal_uLong StgInternalStream::GetData( void* pData, sal_uLong nSize )
49 if( pStrm )
51 nSize = pStrm->Read( pData, nSize );
52 SetError( pStrm->GetError() );
53 return nSize;
55 else
56 return 0;
59 sal_uLong StgInternalStream::PutData( const void* pData, sal_uLong nSize )
61 if( pStrm )
63 nSize = pStrm->Write( pData, nSize );
64 SetError( pStrm->GetError() );
65 return nSize;
67 else
68 return 0;
71 sal_uLong StgInternalStream::SeekPos( sal_uLong nPos )
73 return pStrm ? pStrm->Seek( nPos ) : 0;
76 void StgInternalStream::FlushData()
78 if( pStrm )
80 pStrm->Flush();
81 SetError( pStrm->GetError() );
85 void StgInternalStream::Commit()
87 Flush();
88 pStrm->Commit();
91 ///////////////////////// class StgCompObjStream /////////////////////////
93 StgCompObjStream::StgCompObjStream( BaseStorage& rStg, bool bWr )
94 : StgInternalStream( rStg, OUString("\1CompObj"), bWr )
96 memset( &aClsId, 0, sizeof( ClsId ) );
97 nCbFormat = 0;
100 bool StgCompObjStream::Load()
102 memset( &aClsId, 0, sizeof( ClsId ) );
103 nCbFormat = 0;
104 aUserName = "";
105 if( GetError() != SVSTREAM_OK )
106 return false;
107 Seek( 8L ); // skip the first part
108 sal_Int32 nMarker = 0;
109 *this >> nMarker;
110 if( nMarker == -1L )
112 *this >> aClsId;
113 sal_Int32 nLen1 = 0;
114 *this >> nLen1;
115 if ( nLen1 > 0 )
117 // higher bits are ignored
118 sal_uLong nStrLen = ::std::min( nLen1, (sal_Int32)0xFFFE );
120 sal_Char* p = new sal_Char[ nStrLen+1 ];
121 p[nStrLen] = 0;
122 if( Read( p, nStrLen ) == nStrLen )
124 //The encoding here is "ANSI", which is pretty useless seeing as
125 //the actual codepage used doesn't seem to be specified/stored
126 //anywhere :-(. Might as well pick 1252 and be consistent on
127 //all platforms and envs
128 //http://www.openoffice.org/nonav/issues/showattachment.cgi/68668/Orginal%20Document.doc
129 //for a good edge-case example
130 aUserName = nStrLen ? OUString( p, nStrLen, RTL_TEXTENCODING_MS_1252 ) : OUString();
131 nCbFormat = ReadClipboardFormat( *this );
133 else
134 SetError( SVSTREAM_GENERALERROR );
135 delete [] p;
138 return GetError() == SVSTREAM_OK;
141 bool StgCompObjStream::Store()
143 if( GetError() != SVSTREAM_OK )
144 return false;
145 Seek( 0L );
146 OString aAsciiUserName(OUStringToOString(aUserName, RTL_TEXTENCODING_MS_1252));
147 *this << (sal_Int16) 1 // Version?
148 << (sal_Int16) -2 // 0xFFFE = Byte Order Indicator
149 << (sal_Int32) 0x0A03 // Windows 3.10
150 << (sal_Int32) -1L
151 << aClsId // Class ID
152 << (sal_Int32) (aAsciiUserName.getLength() + 1)
153 << (const char *)aAsciiUserName.getStr()
154 << (sal_uInt8) 0; // string terminator
155 WriteClipboardFormat( *this, nCbFormat );
156 *this << (sal_Int32) 0; // terminator
157 Commit();
158 return GetError() == SVSTREAM_OK;
161 /////////////////////////// class StgOleStream ///////////////////////////
163 StgOleStream::StgOleStream( BaseStorage& rStg, bool bWr )
164 : StgInternalStream( rStg, OUString("\1Ole"), bWr )
166 nFlags = 0;
169 bool StgOleStream::Load()
171 nFlags = 0;
172 if( GetError() != SVSTREAM_OK )
173 return false;
175 sal_Int32 version = 0;
176 Seek( 0L );
177 *this >> version >> nFlags;
178 return GetError() == SVSTREAM_OK;
181 bool StgOleStream::Store()
183 if( GetError() != SVSTREAM_OK )
184 return false;
186 Seek( 0L );
187 *this << (sal_Int32) 0x02000001 // OLE version, format
188 << (sal_Int32) nFlags // Object flags
189 << (sal_Int32) 0 // Update Options
190 << (sal_Int32) 0 // reserved
191 << (sal_Int32) 0; // Moniker 1
192 Commit();
193 return GetError() == SVSTREAM_OK;
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */