/* * 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 "SharedStrings.h" #include "../../Common/SimpleTypes_Shared.h" #include "../../XlsbFormat/Biff12_records/BeginSst.h" #include "../../XlsbFormat/Biff12_unions/SHAREDSTRINGS.h" #include "../../XlsbFormat/Biff12_records/SSTItem.h" #include "../../../MsBinaryFile/XlsFile/Format/Logic/GlobalsSubstream.h" #include "../../../MsBinaryFile/XlsFile/Format/Logic/WorkbookStreamObject.h" #include "../../../MsBinaryFile/XlsFile/Format/Logic/Biff_records/SST.h" #include "../../../MsBinaryFile/XlsFile/Format/Logic/Biff_unions/SHAREDSTRINGS.h" #include "../../Binary/XlsbFormat/FileTypes_SpreadsheetBin.h" namespace OOX { namespace Spreadsheet { CSharedStrings::CSharedStrings(OOX::Document* pMain) : OOX::File(pMain), OOX::IFileContainer(pMain) { m_nCount = 0; m_bSpreadsheets = true; CXlsx* xlsx = dynamic_cast(File::m_pMainDocument); if ((xlsx) && (!xlsx->m_pSharedStrings)) xlsx->m_pSharedStrings = this; } CSharedStrings::CSharedStrings(OOX::Document* pMain, const CPath& oRootPath, const CPath& oPath) : OOX::File(pMain), OOX::IFileContainer(pMain) { m_nCount = 0; m_bSpreadsheets = true; CXlsx* xlsx = dynamic_cast(File::m_pMainDocument); if ((xlsx) && (!xlsx->m_pSharedStrings)) xlsx->m_pSharedStrings = this; read( oRootPath, oPath ); } CSharedStrings::~CSharedStrings() { ClearItems(); } void CSharedStrings::readBin(const CPath& oPath) { CXlsb* xlsb = dynamic_cast(File::m_pMainDocument); if (xlsb) { XLSB::SharedStringsStreamPtr sharedStringsStream(new XLSB::SharedStringsStream); xlsb->ReadBin(oPath, sharedStringsStream.get()); if (sharedStringsStream != nullptr) { auto ptr = static_cast(sharedStringsStream->m_SHAREDSTRINGS.get()); if (ptr != nullptr) { ReadAttributes(ptr->m_BrtBeginSst); for(auto &sstItem : ptr->m_arBrtSSTItem) { CSi* pItem = new CSi(); auto ptr = static_cast(sstItem.get()); if(ptr != nullptr) { pItem->fromBin(ptr->richStr); } m_arrItems.push_back(pItem); m_nCount++; } } } //sharedStringsStream.reset(); } } XLS::BaseObjectPtr CSharedStrings::WriteBin() const { XLSB::SharedStringsStreamPtr sharedStringsStream(new XLSB::SharedStringsStream); auto ptr(new XLSB::SHAREDSTRINGS); XLS::BaseObjectPtr objectPtr(ptr); auto atribPtr(new XLSB::BeginSst); ptr->m_BrtBeginSst = XLS::BaseObjectPtr{atribPtr}; if(m_oCount.IsInit()) atribPtr->cstTotal = m_oCount->GetValue(); if(m_oUniqueCount.IsInit()) atribPtr->cstUnique = m_oUniqueCount->GetValue(); for(auto i:m_arrItems) { ptr->m_arBrtSSTItem.push_back(i->toBin()); } return objectPtr; } void CSharedStrings::toXLS(XLS::BaseObjectPtr globalsSubstreamPtr) const { auto castedPtr = static_cast(globalsSubstreamPtr.get()); auto SharedStringsObj = new XLS::SHAREDSTRINGS(XLS::WorkbookStreamObject::DefaultCodePage); auto Sst = new XLS::SST(XLS::WorkbookStreamObject::DefaultCodePage); SharedStringsObj->sstPtr = XLS::BaseObjectPtr(Sst); for(auto i:m_arrItems) { Sst->rgb.push_back(i->toXLS()); } castedPtr->m_SHAREDSTRINGS = XLS::BaseObjectPtr(SharedStringsObj); } void CSharedStrings::read(const CPath& oPath) { //don't use this. use read(const CPath& oRootPath, const CPath& oFilePath) CPath oRootPath; read(oRootPath, oPath); } void CSharedStrings::read(const CPath& oRootPath, const CPath& oPath) { m_oReadPath = oPath; IFileContainer::Read( oRootPath, oPath ); if( m_oReadPath.GetExtention() == _T(".bin")) { readBin(m_oReadPath); return; } XmlUtils::CXmlLiteReader oReader; if ( !oReader.FromFile( m_oReadPath.GetPath() ) ) { //test un-upper(lower)case open - CALACATA GREECE.xlsx if (!m_oReadPath.FileInDirectoryCorrect()) return; if ( !oReader.FromFile( m_oReadPath.GetPath() ) ) return; } if ( !oReader.ReadNextNode() ) return; std::wstring sName = XmlUtils::GetNameNoNS(oReader.GetName()); if ( _T("sst") == sName ) { ReadAttributes( oReader ); m_nCount = 0; if ( !oReader.IsEmptyNode() ) { int nSharedStringsDepth = oReader.GetDepth(); while ( oReader.ReadNextSiblingNode( nSharedStringsDepth ) ) { sName = XmlUtils::GetNameNoNS(oReader.GetName()); if ( _T("si") == sName ) { CSi* pItem = new CSi(); *pItem = oReader; m_arrItems.push_back(pItem ); m_nCount++; } } } } } void CSharedStrings::write(const CPath& oPath, const CPath& oDirectory, CContentTypes& oContent) const { CXlsb* xlsb = dynamic_cast(File::m_pMainDocument); if ((xlsb) && (xlsb->m_bWriteToXlsb)) { XLS::BaseObjectPtr object = WriteBin(); xlsb->WriteBin(oPath, object.get()); } else { NSStringUtils::CStringBuilder writer; writer.WriteString(_T("GetValue()); WritingStringNullableAttrInt(L"uniqueCount", m_oUniqueCount, m_oUniqueCount->GetValue()); writer.WriteString(_T(">")); for(size_t i = 0; i < m_arrItems.size(); i++) { m_arrItems[i]->toXML(writer); } writer.WriteString(_T("")); std::wstring sPath = oPath.GetPath(); NSFile::CFileBinary::SaveToFile(sPath.c_str(), writer.GetData()); } oContent.Registration( type().OverrideType(), oDirectory, oPath.GetFilename() ); } const OOX::FileType CSharedStrings::type() const { CXlsb* xlsb = dynamic_cast(File::m_pMainDocument); if ((xlsb) && (xlsb->m_bWriteToXlsb)) { return OOX::SpreadsheetBin::FileTypes::SharedStringsBin; } return OOX::Spreadsheet::FileTypes::SharedStrings; } const CPath CSharedStrings::DefaultDirectory() const { return type().DefaultDirectory(); } const CPath CSharedStrings::DefaultFileName() const { CXlsb* xlsb = dynamic_cast(File::m_pMainDocument); if ((xlsb) && (xlsb->m_bWriteToXlsb)) { CPath name = type().DefaultFileName(); name.SetExtention(L"bin"); return name; } else { return type().DefaultFileName(); } } const CPath& CSharedStrings::GetReadPath() { return m_oReadPath; } const int CSharedStrings::AddSi(CSi* pSi) { int nIndex = m_nCount++; m_arrItems.push_back(pSi); return nIndex; } void CSharedStrings::ClearItems() { m_nCount = 0; for(size_t i = 0; i < m_arrItems.size(); i++) { if ( m_arrItems[i] )delete m_arrItems[i]; m_arrItems[i] = NULL; } m_arrItems.clear(); } void CSharedStrings::ReadAttributes(XmlUtils::CXmlLiteReader& oReader) { WritingElement_ReadAttributes_Start( oReader ) WritingElement_ReadAttributes_Read_if ( oReader, _T("count"), m_oCount ) WritingElement_ReadAttributes_Read_if ( oReader, _T("uniqueCount"), m_oUniqueCount ) WritingElement_ReadAttributes_End( oReader ) } void CSharedStrings::ReadAttributes(XLS::BaseObjectPtr& obj) { auto ptr = static_cast(obj.get()); if(ptr != nullptr) { m_oCount = ptr->cstTotal; m_oUniqueCount = ptr->cstUnique; } } } //Spreadsheet } // namespace OOX