mirror of
https://github.com/ONLYOFFICE/core.git
synced 2026-07-11 06:22:44 +08:00
Co-authored-by: Alexander Trofimov <alexander.trofimov@onlyoffice.com> Co-committed-by: Alexander Trofimov <alexander.trofimov@onlyoffice.com>
272 lines
6.4 KiB
C++
272 lines
6.4 KiB
C++
/*
|
|
* Copyright (C) Ascensio System SIA, 2009-2026
|
|
*
|
|
* This program is a free software product. You can redistribute it and/or
|
|
* modify it under the terms of the GNU Affero General Public License (AGPL)
|
|
* version 3 as published by the Free Software Foundation, together with the
|
|
* additional terms provided in the LICENSE file.
|
|
*
|
|
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
|
|
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
|
|
* details, see the GNU AGPL at: https://www.gnu.org/licenses/agpl-3.0.html
|
|
*
|
|
* You can contact Ascensio System SIA by email at info@onlyoffice.com
|
|
* or by postal mail at 20A-6 Ernesta Birznieka-Upisha Street, Riga,
|
|
* LV-1050, Latvia, European Union.
|
|
*
|
|
* The interactive user interfaces in modified versions of the Program
|
|
* are required to display Appropriate Legal Notices in accordance with
|
|
* Section 5 of the GNU AGPL version 3.
|
|
*
|
|
* No trademark rights are granted under this License.
|
|
*
|
|
* All non-code elements of the Product, including illustrations,
|
|
* icon sets, and technical writing content, are licensed under the
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License:
|
|
* https://creativecommons.org/licenses/by-sa/4.0/legalcode
|
|
*
|
|
* This license applies only to such non-code elements and does not
|
|
* modify or replace the licensing terms applicable to the Program's
|
|
* source code, which remains licensed under the GNU Affero General
|
|
* Public License v3.
|
|
*
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
#include "CompoundFile.h"
|
|
#include "CFStream.h"
|
|
|
|
#include <sstream>
|
|
|
|
namespace XLS
|
|
{
|
|
|
|
|
|
CompoundFile::~CompoundFile()
|
|
{
|
|
streams.clear();
|
|
|
|
if (storage_)delete storage_;
|
|
storage_ = NULL;
|
|
}
|
|
|
|
bool CompoundFile::isError()
|
|
{
|
|
if (storage_ != NULL) return false;
|
|
return true;
|
|
}
|
|
|
|
bool CompoundFile::Open(const std::wstring & file_path, const ReadWriteMode mode)
|
|
{
|
|
if (storage_) delete storage_;
|
|
storage_ = NULL;
|
|
|
|
rwMode = mode;
|
|
|
|
unsigned int grfMode = 0;
|
|
|
|
storage_ = new POLE::Storage(file_path.c_str());
|
|
if (storage_ == NULL) return false;
|
|
|
|
switch(rwMode)
|
|
{
|
|
case cf_ReadMode:
|
|
{
|
|
if (storage_->open(false, false) == false)
|
|
{
|
|
delete storage_;
|
|
storage_ = NULL;
|
|
}
|
|
|
|
}break;
|
|
case cf_WriteMode:
|
|
{
|
|
if (storage_->open(true, true) == false)
|
|
{
|
|
delete storage_;
|
|
storage_ = NULL;
|
|
}
|
|
}break;
|
|
}
|
|
|
|
if (storage_ == NULL) return false;
|
|
return true;
|
|
}
|
|
|
|
|
|
CompoundFile::CompoundFile(const std::wstring & file_path, const ReadWriteMode mode)
|
|
{
|
|
storage_ = NULL;
|
|
Open(file_path, mode);
|
|
}
|
|
|
|
void CompoundFile::copy_stream(std::wstring streamNameOpen, std::wstring streamNameCreate, POLE::Storage * storageOut, bool bWithRoot)
|
|
{
|
|
POLE::Stream *stream = new POLE::Stream(storage_, streamNameOpen);
|
|
if (!stream) return;
|
|
|
|
stream->seek(0);
|
|
POLE::uint64 size_stream = stream->size();
|
|
|
|
if (bWithRoot == false)
|
|
{
|
|
int pos = streamNameCreate.find(L"/");
|
|
if (pos >= 0)
|
|
streamNameCreate = streamNameCreate.substr(pos + 1);
|
|
}
|
|
|
|
POLE::Stream *streamNew = new POLE::Stream(storageOut, streamNameCreate, true, size_stream);
|
|
if (!streamNew) return;
|
|
|
|
unsigned char buffer[4096];
|
|
int bytesRead = 0;
|
|
|
|
while(true)
|
|
{
|
|
int bytesToRead = size_stream - bytesRead;
|
|
if (bytesToRead <= 0)
|
|
break;
|
|
if (bytesToRead > 4096)
|
|
bytesToRead = 4096;
|
|
|
|
stream->read(buffer, bytesToRead);
|
|
streamNew->write(buffer, bytesToRead);
|
|
|
|
bytesRead += bytesToRead;
|
|
}
|
|
//unsigned char* data_stream = new unsigned char[size_stream + 64];
|
|
//memset(data_stream, 0, size_stream + 64);
|
|
//if (data_stream)
|
|
//{
|
|
// stream->read(data_stream, size_stream);
|
|
|
|
// streamNew->write(data_stream, size_stream);
|
|
|
|
// delete []data_stream;
|
|
// data_stream = NULL;
|
|
//}
|
|
|
|
streamNew->flush();
|
|
|
|
delete streamNew;
|
|
delete stream;
|
|
}
|
|
|
|
void CompoundFile::copy( int indent, std::wstring path, POLE::Storage * storageOut, bool bWithRoot, bool bSortFiles)
|
|
{
|
|
std::list<std::wstring> entries, entries_files, entries_dir;
|
|
|
|
entries = storage_->entries_with_prefix( path );
|
|
|
|
for( std::list<std::wstring>::iterator it = entries.begin(); it != entries.end(); ++it )
|
|
{
|
|
std::wstring fullname = path + *it;
|
|
|
|
if ((it->at(0) >= 32) && (storage_->isDirectory( fullname ) ))
|
|
{
|
|
entries_dir.push_back(*it);
|
|
}
|
|
else
|
|
{
|
|
entries_files.push_front(*it);
|
|
}
|
|
}
|
|
|
|
for( std::list<std::wstring>::iterator it = entries_dir.begin(); it != entries_dir.end(); ++it )
|
|
{
|
|
std::wstring fullname = path + *it;
|
|
|
|
copy( indent + 1, fullname + L"/", storageOut, bWithRoot, bSortFiles );
|
|
}
|
|
|
|
//entries_files.sort();
|
|
|
|
for( std::list<std::wstring>::iterator it = entries_files.begin(); it != entries_files.end(); ++it )
|
|
{
|
|
std::wstring createName = path + *it;
|
|
std::wstring openName;
|
|
|
|
if (it->at(0) < 32) openName = path + it->substr(1);
|
|
else openName = path + *it;
|
|
|
|
copy_stream(openName, createName, storageOut, bWithRoot);
|
|
}
|
|
}
|
|
CFStreamPtr CompoundFile::getWorkbookStream()
|
|
{
|
|
CFStreamPtr stream = getNamedStream(L"Workbook");
|
|
|
|
if (stream == NULL)
|
|
stream = getNamedStream(L"WORKBOOK"); //6447323.xls
|
|
if (stream == NULL)
|
|
stream = getNamedStream(L"Book");
|
|
if (stream == NULL)
|
|
stream = getNamedStream(L"BOOK");//file(6).xls
|
|
if (stream == NULL)
|
|
stream = getNamedStream(L"book");
|
|
return stream;
|
|
}
|
|
|
|
CFStreamPtr CompoundFile::getNamedStream(const std::wstring& name)
|
|
{
|
|
if(!streams[name])
|
|
{
|
|
POLE::Stream * pStream = openStream(name.c_str());
|
|
if (pStream)
|
|
streams[name].reset(new CFStream(pStream));
|
|
}
|
|
return streams[name];
|
|
}
|
|
|
|
CFStreamPtr CompoundFile::createNamedStream(const std::wstring& name)
|
|
{
|
|
if(!streams[name])
|
|
{
|
|
POLE::Stream * pStream = createStream(name.c_str());
|
|
if (pStream)
|
|
streams[name].reset(new CFStream(pStream));
|
|
}
|
|
return streams[name];
|
|
}
|
|
|
|
|
|
void CompoundFile::closeNamedStream(const std::wstring& name)
|
|
{
|
|
streams[name].reset();
|
|
}
|
|
|
|
|
|
// Opens a stream in the storage (shall be called not more than once per stream)
|
|
POLE::Stream* CompoundFile::openStream(const std::wstring & stream_name)
|
|
{
|
|
if (storage_ == NULL) return NULL;
|
|
|
|
POLE::Stream* pStream = new POLE::Stream(storage_, stream_name);
|
|
if(pStream == NULL)
|
|
{
|
|
return NULL;
|
|
}
|
|
if ((pStream) && (pStream->size() > 0))
|
|
return pStream;
|
|
else return NULL;
|
|
}
|
|
|
|
|
|
// Creates a new stream in the storage
|
|
POLE::Stream* CompoundFile::createStream(const std::wstring & stream_name)
|
|
{
|
|
if (storage_ == NULL) return NULL;
|
|
|
|
POLE::Stream* pStream = new POLE::Stream(storage_, stream_name, true);
|
|
if(pStream == NULL)
|
|
{
|
|
return NULL;
|
|
}
|
|
return pStream;
|
|
}
|
|
|
|
|
|
} // namespace XLS
|
|
|
|
|