merge the formfield patch from ooo-build
[ooovba.git] / extensions / source / oooimprovement / logpacker.cxx
blob2c37ee1ed3c84704ece27aa0eb1d5ea3a6e7a5fc
1 /*************************************************************************
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * Copyright 2008 by Sun Microsystems, Inc.
6 * OpenOffice.org - a multi-platform office productivity suite
8 * $RCSfile: logpacker.cxx,v $
10 * $Revision: 1.1 $
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.
28 ************************************************************************/
30 // MARKER(update_precomp.py): autogen include statement, do not remove
31 #include "precompiled_extensions.hxx"
33 #include "logpacker.hxx"
34 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
35 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
36 #include <com/sun/star/embed/XStorage.hpp>
37 #include <com/sun/star/embed/XTransactedObject.hpp>
38 #include <com/sun/star/embed/ElementModes.hpp>
39 #include <rtl/ustrbuf.hxx>
42 using namespace com::sun::star::embed;
43 using namespace com::sun::star::io;
44 using namespace com::sun::star::lang;
45 using namespace com::sun::star::uno;
46 using ::com::sun::star::ucb::XSimpleFileAccess;
47 using ::rtl::OUString;
48 using ::rtl::OUStringBuffer;
51 namespace
53 static const OUString getZipfileurl(const OUString& csvfileurl)
55 OUStringBuffer buf(csvfileurl);
56 buf.setLength(csvfileurl.getLength()-3);
57 buf.appendAscii("zip");
58 return buf.makeStringAndClear();
61 static sal_Int32 countLines(const Sequence<sal_Int8>& data)
63 sal_Int32 result = 0;
64 for(sal_Int32 idx = data.getLength()-1; idx>=0; --idx)
65 if(data[idx]==0x0a) result++;
66 return result;
70 namespace oooimprovement
72 LogPacker::LogPacker(const Reference<XMultiServiceFactory>& sf)
73 : m_ServiceFactory(sf)
76 sal_Int32 LogPacker::pack(const OUString& fileurl)
78 Reference<XSimpleFileAccess> file_access(
79 m_ServiceFactory->createInstance(OUString::createFromAscii("com.sun.star.ucb.SimpleFileAccess")),
80 UNO_QUERY_THROW);
82 Reference<XStorage> storage;
84 Reference<XSingleServiceFactory> storage_factory(
85 m_ServiceFactory->createInstance(OUString::createFromAscii("com.sun.star.embed.StorageFactory")),
86 UNO_QUERY_THROW);
87 Sequence<Any> storage_init_args(2);
88 storage_init_args[0] = Any(getZipfileurl(fileurl));
89 storage_init_args[1] = Any(ElementModes::WRITE);
90 storage = Reference<XStorage>(
91 storage_factory->createInstanceWithArguments(storage_init_args),
92 UNO_QUERY_THROW);
95 Reference<XOutputStream> zipped_stream = storage->openStreamElement(
96 OUString::createFromAscii("logdata.csv"),
97 ElementModes::WRITE)->getOutputStream();
98 Reference<XInputStream> unzipped_stream = file_access->openFileRead(fileurl);
99 const sal_Int32 bufsize = 1024;
100 sal_Int32 read_bytes;
101 sal_Int32 logged_events = -1; // ignore header row
102 Sequence<sal_Int8> buf(bufsize);
105 read_bytes = unzipped_stream->readBytes(buf, bufsize);
106 buf.realloc(read_bytes);
107 logged_events += countLines(buf);
108 zipped_stream->writeBytes(buf);
109 } while(read_bytes == bufsize);
110 unzipped_stream->closeInput();
111 zipped_stream->flush();
112 zipped_stream->closeOutput();
113 Reference<XTransactedObject>(storage, UNO_QUERY_THROW)->commit();
114 file_access->kill(fileurl);
115 return logged_events;