merge the formfield patch from ooo-build
[ooovba.git] / configmgr / source / backend / binarywriter.cxx
blobfafe25f3f1ffc05765baa5f0e069612da7a4f244
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: binarywriter.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_configmgr.hxx"
34 #include "binarywriter.hxx"
35 #include "valuenode.hxx"
36 #include "filehelper.hxx"
37 #include <com/sun/star/uno/Type.hxx>
38 #include <com/sun/star/uno/Any.hxx>
39 #include "oslstream.hxx"
40 #include <com/sun/star/io/IOException.hpp>
41 #include <com/sun/star/io/XOutputStream.hpp>
42 #include <com/sun/star/io/XActiveDataSource.hpp>
43 #include <com/sun/star/io/XDataOutputStream.hpp>
44 #include "typeconverter.hxx"
45 #include "binarytype.hxx"
46 #include "simpletypehelper.hxx"
48 #define ASCII(x) rtl::OUString::createFromAscii(x)
49 namespace configmgr
51 // -----------------------------------------------------------------------------
52 namespace backend
54 namespace uno = com::sun::star::uno;
55 namespace io = com::sun::star::io;
57 BinaryWriter::BinaryWriter(rtl::OUString const &_aFileURL, uno::Reference<lang::XMultiServiceFactory> const& _xServiceProvider)
58 : m_aFileURL(_aFileURL)
59 , m_xServiceProvider(_xServiceProvider)
60 , m_xDataOutputStream()
63 bool BinaryWriter::open() SAL_THROW( (io::IOException, uno::RuntimeException) )
65 if (m_aFileURL.getLength() == 0)
66 return false;
68 OSL_ENSURE(!m_xDataOutputStream.is(), "Binary Writer: already open");
69 if ( m_xDataOutputStream.is())
70 return false;
72 if (FileHelper::fileExists(m_aFileURL))
74 if (osl::File::RC errorCode = osl::File::remove(m_aFileURL))
76 // creating the file will fail later
77 OSL_TRACE("Binary Cache: Cannot remove existing file [%d]",int(errorCode));
80 else
82 //create missing directories
83 rtl::OUString parentDirectory = FileHelper::getParentDir(m_aFileURL) ;
85 if (osl::File::RC errorCode = FileHelper::mkdirs(parentDirectory))
87 // creating the file will fail later
88 OSL_TRACE("Binary Cache: Cannot create package cache directory [%d]",int(errorCode));
92 uno::Reference<io::XOutputStream> xOutput = new BufferedFileOutputStream(m_aFileURL, true, 1024);
94 uno::Reference< io::XActiveDataSource > xFormattingStream(
95 m_xServiceProvider->createInstance(ASCII("com.sun.star.io.DataOutputStream")),
96 uno::UNO_QUERY_THROW);
98 xFormattingStream->setOutputStream(xOutput);
100 m_xDataOutputStream.set(xFormattingStream, uno::UNO_QUERY_THROW);
102 OSL_ASSERT(m_xDataOutputStream.is());
103 return m_xDataOutputStream.is();
106 void BinaryWriter::close() SAL_THROW( (io::IOException, uno::RuntimeException) )
108 if (m_xDataOutputStream.is())
109 m_xDataOutputStream->closeOutput();
111 m_xDataOutputStream.clear();
114 BinaryWriter::~BinaryWriter()
116 try
118 close();
120 catch (uno::Exception& e)
122 (void)e;
123 OSL_ENSURE(false, rtl::OUStringToOString(e.Message,RTL_TEXTENCODING_ASCII_US).getStr());
127 // -----------------------------------------------------------------------------
128 // -----------------------------------------------------------------------------
129 void BinaryWriter::write(sal_Bool _aValue)
130 SAL_THROW( (io::IOException, uno::RuntimeException) )
132 // write one byte
133 m_xDataOutputStream->writeBoolean(_aValue);
135 void BinaryWriter::write(sal_Int8 _aValue)
136 SAL_THROW( (io::IOException, uno::RuntimeException) )
138 // write one byte
139 m_xDataOutputStream->writeByte(_aValue);
141 void BinaryWriter::write(sal_Int16 _aValue)
142 SAL_THROW( (io::IOException, uno::RuntimeException) )
144 // write two bytes
145 m_xDataOutputStream->writeShort(_aValue);
147 void BinaryWriter::write(sal_Int32 _aValue)
148 SAL_THROW( (io::IOException, uno::RuntimeException) )
150 // write four byte
151 m_xDataOutputStream->writeLong(_aValue);
153 void BinaryWriter::write(sal_Int64 _aValue)
154 SAL_THROW( (io::IOException, uno::RuntimeException) )
156 // write eight byte
157 m_xDataOutputStream->writeHyper(_aValue);
159 void BinaryWriter::write(double _aValue)
160 SAL_THROW( (io::IOException, uno::RuntimeException) )
162 // write eight byte
163 m_xDataOutputStream->writeDouble(_aValue);
166 // -----------------------------------------------------------------------------
167 bool isAsciiEncoding(rtl::OUString const& _aStr)
169 const sal_Unicode *pStr = _aStr.getStr();
170 sal_Int32 nLen = _aStr.getLength();
171 while (nLen--)
173 if (*pStr++ > 127)
174 return false;
176 return true;
179 // -----------------------------------------------------------------------------
180 void BinaryWriter::write(rtl::OUString const& _aStr)
181 SAL_THROW( (io::IOException, uno::RuntimeException) )
183 // @@@ OBinaryBaseReader_Impl::readUTF() @@@
185 rtl::OString aUTF;
186 // to fasten the conversion for ascii data, we mask the length
187 bool bIsAscii = isAsciiEncoding(_aStr);
188 if (bIsAscii)
189 rtl_uString2String (
190 &(aUTF.pData), _aStr.getStr(), _aStr.getLength(),
191 RTL_TEXTENCODING_ASCII_US, OUSTRING_TO_OSTRING_CVTFLAGS);
192 else
193 rtl_uString2String (
194 &(aUTF.pData), _aStr.getStr(), _aStr.getLength(),
195 RTL_TEXTENCODING_UTF8, OUSTRING_TO_OSTRING_CVTFLAGS);
197 sal_Int32 nLength = aUTF.getLength();
198 uno::Sequence<sal_Int8> aData (nLength);
199 memcpy (aData.getArray(), aUTF.getStr(), nLength);
201 OSL_ENSURE((nLength & binary::STR_ASCII_MASK) == 0,"String too long");
202 if (bIsAscii)
204 nLength |= binary::STR_ASCII_MASK;
205 OSL_ASSERT((nLength & binary::STR_ASCII_MASK) == binary::STR_ASCII_MASK);
206 OSL_ASSERT(sal_Int32(nLength & ~binary::STR_ASCII_MASK) == aData.getLength());
208 m_xDataOutputStream->writeLong (nLength);
210 m_xDataOutputStream->writeBytes (aData);
213 // -----------------------------------------------------------------------------
215 // -----------------------------------------------------------------------------
217 // -----------------------------------------------------------------------------