/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
// XML-header to query SPELLML support // to handle user words with "Grammar By" model words
constexpr OUStringLiteral SPELLML_SUPPORT = u"";
// User dictionaries can contain optional "title:" tags // to support custom titles with space and other characters. // (old mechanism stores the title of the user dictionary // only in its file name, but special characters are // problem for user dictionaries shipped with LibreOffice). // // The following fake file name extension will be // added to the text of the title: field for correct // text stripping and dictionary saving.
constexpr OUString EXTENSION_FOR_TITLE_TEXT = u"."_ustr;
// lang: title if (getTag(aLine, "title: ", aTagValue))
{
aDicName = OStringToOUString( aTagValue, RTL_TEXTENCODING_UTF8) + // recent title text preparation in GetDicInfoStr() waits for an // extension, so we add it to avoid bad stripping at final dot // of the title text
EXTENSION_FOR_TITLE_TEXT;
}
if (std::string_view(aLine).find("---") != std::string_view::npos) // end of header break;
} if (!bSuccess) return -2;
} else
{
sal_uInt16 nLen;
rStream.Seek (nSniffPos );
rStream.ReadUInt16( nLen ); if (nLen >= MAX_HEADER_LENGTH) return -1;
if( !rMainURL.isEmpty())
{ bool bExists = FileExists( rMainURL ); if( !bExists )
{ // save new dictionaries with in Format 7 (UTF8 plain text)
nDicVersion = DIC_VERSION_7;
//! create physical representation of an **empty** dictionary //! that could be found by the dictionary-list implementation // (Note: empty dictionaries are not just empty files!)
DBG_ASSERT( !bIsReadonly, "DictionaryNeo: dictionaries should be writeable if they are to be saved" ); if (!bIsReadonly)
saveEntries( rMainURL );
bNeedEntries = false;
}
} else
{ // non persistent dictionaries (like IgnoreAllList) should always be writable
bIsReadonly = false;
bNeedEntries = false;
}
}
// counter check that it is safe to set bIsModified to sal_False at // the end of the function
DBG_ASSERT(!bIsModified, "lng : dictionary already modified!");
// function should only be called once in order to load entries from file
bNeedEntries = false;
// remaining lines - stock strings (a [==] b) while (pStream->ReadLine(aLine))
{ if (aLine.isEmpty() || aLine[0] == '#') // skip comments continue;
OUString aText = OStringToOUString(aLine, RTL_TEXTENCODING_UTF8);
uno::Reference< XDictionaryEntry > xEntry = new DicEntry( aText, eDicType == DictionaryType_NEGATIVE );
addEntry_Impl( xEntry, true ); //! don't launch events here
}
}
SAL_WARN_IF(!isSorted(), "linguistic", "dictionary is not sorted");
// since this routine should be called only initially (prior to any // modification to be saved) we reset the bIsModified flag here that // was implicitly set by addEntry_Impl
bIsModified = false;
// Always write as the latest version, i.e. DIC_VERSION_7
rtl_TextEncoding eEnc = RTL_TEXTENCODING_UTF8;
pStream->WriteLine(pVerOOo7);
ErrCode nErr = pStream->GetError(); if (nErr != ERRCODE_NONE) return nErr; /* XXX: the <none> case could be differentiated, is it absence or * undetermined or multiple? Earlier versions did not know about 'und' and
* 'mul' and 'zxx' codes. Sync with ReadDicVersion() */ if (LinguIsUnspecified(nLanguage))
pStream->WriteLine("lang: "); else
{
OString aLine = "lang: " + OUStringToOString(LanguageTag::convertToBcp47(nLanguage), eEnc);
pStream->WriteLine(aLine);
} if (ERRCODE_NONE != (nErr = pStream->GetError())) return nErr; if (eDicType == DictionaryType_POSITIVE)
pStream->WriteLine("type: positive"); else
pStream->WriteLine("type: negative"); if (aDicName.endsWith(EXTENSION_FOR_TITLE_TEXT))
{
pStream->WriteLine(Concat2View("title: " + OUStringToOString( // strip EXTENSION_FOR_TITLE_TEXT
aDicName.subView(0, aDicName.lastIndexOf(EXTENSION_FOR_TITLE_TEXT)), eEnc)));
} if (ERRCODE_NONE != (nErr = pStream->GetError())) return nErr;
pStream->WriteLine("---"); if (ERRCODE_NONE != (nErr = pStream->GetError())) return nErr; for (const Reference<XDictionaryEntry> & aEntrie : aEntries)
{
OString aOutStr = formatForSave(aEntrie, eEnc);
pStream->WriteLine (aOutStr); if (ERRCODE_NONE != (nErr = pStream->GetError())) return nErr;
}
try
{
pStream.reset();
uno::Reference< ucb::XSimpleFileAccess3 > xAccess(ucb::SimpleFileAccess::create(xContext));
Reference<io::XInputStream> xInputStream(xStream, UNO_QUERY_THROW);
uno::Reference<io::XSeekable> xSeek(xInputStream, UNO_QUERY_THROW);
xSeek->seek(0);
xAccess->writeFile(rURL, xInputStream); //If we are migrating from an older version, then on first successful //write, we're now converted to the latest version, i.e. DIC_VERSION_7
nDicVersion = DIC_VERSION_7;
} catch (const uno::Exception &)
{
DBG_ASSERT( false, "failed to write stream" ); return ErrCode(sal_uInt32(-1));
}
int DictionaryNeo::cmpDicEntry(std::u16string_view rWord1,
std::u16string_view rWord2, bool bSimilarOnly)
{ // returns 0 if rWord1 is equal to rWord2 // " a value < 0 if rWord1 is less than rWord2 // " a value > 0 if rWord1 is greater than rWord2
bool DictionaryNeo::seekEntry(std::u16string_view rWord,
sal_Int32 *pPos, bool bSimilarOnly)
{ // look for entry with binary search. // return sal_True if found sal_False else. // if pPos != NULL it will become the position of the found entry, or // if that was not found the position where it has to be inserted // to keep the entries sorted
// look for position to insert entry at // if there is already an entry do not insert the new one
sal_Int32 nPos = 0; if (bAddEntry)
{ constbool bFound = seekEntry( xDicEntry->getDictionaryWord(), &nPos ); if (bFound)
bAddEntry = false;
}
if (bAddEntry)
{
DBG_ASSERT(!bNeedEntries, "lng : entries still not loaded");
// insert new entry at specified position
aEntries.insert(aEntries.begin() + nPos, xDicEntry);
SAL_WARN_IF(!isSorted(), "linguistic", "dictionary entries unsorted");
bIsModified = true;
bRes = true;
if (!bIsLoadEntries)
launchEvent( DictionaryEventFlags::ADD_ENTRY, xDicEntry );
}
}
// add word to the Hunspell dictionary using a sample word for affixation/compounding if (xDicEntry.is() && !xDicEntry->isNegative() && !xDicEntry->getReplacementText().isEmpty()) {
uno::Reference< XLinguServiceManager2 > xLngSvcMgr( GetLngSvcMgr_Impl() );
uno::Reference< XSpellChecker1 > xSpell;
Reference< XSpellAlternatives > xTmpRes;
xSpell.set( xLngSvcMgr->getSpellChecker(), UNO_QUERY );
Sequence< css::beans::PropertyValue > aEmptySeq; if (xSpell.is() && (xSpell->isValid( SPELLML_SUPPORT, static_cast<sal_uInt16>(nLanguage), aEmptySeq )))
{ // "Grammar By" sample word is a Hunspell dictionary word? if (xSpell->isValid( xDicEntry->getReplacementText(), static_cast<sal_uInt16>(nLanguage), aEmptySeq ))
{
xTmpRes = xSpell->spell( "" +
xDicEntry->getDictionaryWord() + "" + xDicEntry->getReplacementText() + "", static_cast<sal_uInt16>(nLanguage), aEmptySeq );
bRes = true;
} else
bRes = false;
}
}
if (!bIsReadonly)
{ if (bNeedEntries)
loadEntries( aMainURL );
sal_Int32 nPos; bool bFound = seekEntry( aWord, &nPos );
DBG_ASSERT(!bFound || nPos < static_cast<sal_Int32>(aEntries.size()), "lng : index out of range");
// remove element if found if (bFound)
{ // entry to be removed
uno::Reference< XDictionaryEntry >
xDicEntry( aEntries[ nPos ] );
DBG_ASSERT(xDicEntry.is(), "lng : dictionary entry is NULL");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.