26#include <sfx2/sfxsids.hrc>
45#include <osl/diagnose.h>
47#define ASC_BUFFLEN 4096
54 std::optional<SwPaM> m_oPam;
56 std::unique_ptr<char[]> m_pArr;
59 std::optional<SfxItemSet> m_oItemSet;
67 SwASCIIParser(
const SwASCIIParser&) =
delete;
68 SwASCIIParser& operator=(
const SwASCIIParser&) =
delete;
75 const SwAsciiOptions& GetUsedAsciiOptions()
const {
return m_usedAsciiOptions; }
85 OSL_ENSURE(
false,
"ASCII read without a stream" );
91 SwASCIIParser aParser( rDoc, rPam, *
m_pStream,
93 nRet = aParser.CallParser();
95 OUString optionsString;
96 aParser.GetUsedAsciiOptions().WriteUserData(optionsString);
106SwASCIIParser::SwASCIIParser(
SwDoc& rD,
const SwPaM& rCursor,
SvStream& rIn,
bool bReadNewDoc,
111 , m_usedAsciiOptions(rOpts)
114 , m_bNewDoc(bReadNewDoc)
116 m_oPam.emplace(*rCursor.
GetPoint());
125 if (m_rOpt.GetLanguage())
128 m_oItemSet->Put(aLang);
130 m_oItemSet->Put(aLang);
132 m_oItemSet->Put(aLang);
134 if (m_rOpt.GetFontName().isEmpty())
140 SvxFontItem aFont( aTextFont.GetFamilyType(), aTextFont.GetFamilyName(),
141 OUString(), aTextFont.GetPitch(), aTextFont.GetCharSet(),
RES_CHRATR_FONT );
142 m_oItemSet->Put(aFont);
144 m_oItemSet->Put(aFont);
146 m_oItemSet->Put(aFont);
150ErrCode SwASCIIParser::CallParser()
152 m_rInput.ResetError();
153 m_nFileSize = m_rInput.TellEnd();
154 m_rInput.Seek(STREAM_SEEK_TO_BEGIN);
155 m_rInput.ResetError();
159 std::optional<SwPaM> pInsPam;
160 sal_Int32 nSttContent = 0;
163 const SwNode& rTmp = m_oPam->GetPoint()->GetNode();
165 nSttContent = m_oPam->GetPoint()->GetContentIndex();
186 if (!(SvtScriptType::LATIN & m_nScript))
191 if (!(SvtScriptType::ASIAN & m_nScript))
196 if (!(SvtScriptType::COMPLEX & m_nScript))
201 if (m_oItemSet->Count())
218 sal_uInt16 aWhichIds[4] =
223 sal_uInt16 *pWhichIds = aWhichIds;
227 if (SfxItemState::SET
228 == m_oItemSet->GetItemState(*pWhichIds,
false, &pItem))
231 m_oItemSet->ClearItem(*pWhichIds);
236 if (m_oItemSet->Count())
242 *pInsPam->GetMark() = *m_oPam->GetPoint();
243 pInsPam->GetPoint()->Assign(pInsPam->GetPoint()->GetNode(),
SwNodeOffset(1),
247 OSL_ENSURE(
false,
"Have to change - hard attr. to para. style" );
260ErrCode SwASCIIParser::ReadChars()
262 sal_Unicode *pStt =
nullptr, *pEnd =
nullptr, *pLastStt =
nullptr;
265 bool bSwapUnicode =
false;
269 if (m_nFileSize >= 2 && aEmpty.
GetFontName() == m_rOpt.GetFontName()
274 sal_Size nLen, nOrig;
275 nOrig = nLen = m_rInput.ReadBytes(m_pArr.get(),
ASC_BUFFLEN);
276 rtl_TextEncoding eCharSet;
281 &bSwapUnicode, &eLineEnd, &bHasBom);
285 OSL_ENSURE(bRet,
"Autodetect of text import without nag dialog must have failed");
286 if (bRet && eCharSet != RTL_TEXTENCODING_DONTKNOW)
297 m_usedAsciiOptions = *pUseMe;
299 rtl_TextToUnicodeConverter hConverter=
nullptr;
300 rtl_TextToUnicodeContext hContext=
nullptr;
301 rtl_TextEncoding currentCharSet = pUseMe->
GetCharSet();
302 if (RTL_TEXTENCODING_UCS2 != currentCharSet)
304 if( currentCharSet == RTL_TEXTENCODING_DONTKNOW )
305 currentCharSet = RTL_TEXTENCODING_ASCII_US;
306 hConverter = rtl_createTextToUnicodeConverter( currentCharSet );
307 OSL_ENSURE( hConverter,
"no string convert available" );
309 return ErrCode(ErrCodeArea::Sw, ErrCodeClass::Read, 0);
310 bSwapUnicode =
false;
311 hContext = rtl_createTextToUnicodeContext( hConverter );
313 else if (pUseMe != &aEmpty)
315 m_rInput.StartReadingUnicodeText(currentCharSet);
316 bSwapUnicode = m_rInput.IsEndianSwap();
319 std::unique_ptr<sal_Unicode[]> aWork;
320 sal_Size nArrOffset = 0;
325 if( pLastStt != pStt )
330 if (ERRCODE_NONE != m_rInput.GetError()
332 == (lGCount = m_rInput.ReadBytes(m_pArr.get() + nArrOffset,
342 lGCount += nArrOffset;
347 sal_Size nNewLen = lGCount, nCntBytes;
352 nNewLen = rtl_convertTextToUnicode(hConverter, hContext, m_pArr.get(), lGCount,
354 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_DEFAULT
355 | RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT
356 | RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT
357 | RTL_TEXTTOUNICODE_FLAGS_GLOBAL_SIGNATURE),
359 nArrOffset = lGCount - nCntBytes;
360 if( 0 != nArrOffset )
361 memmove(m_pArr.get(), m_pArr.get() + nCntBytes, nArrOffset);
363 pStt = pLastStt = aWork.get();
364 pEnd = pStt + nNewLen;
368 pStt = pLastStt =
reinterpret_cast<sal_Unicode*
>(m_pArr.get());
369 auto nChars = lGCount / 2;
370 pEnd = pStt + nChars;
374 char *pF = m_pArr.get(), *pN = m_pArr.get() + 1;
375 for (sal_Size n = 0;
n < nChars; ++
n, pF += 2, pN += 2)
391 if( 0x0a == *pStt && 0x0d == cLastCR )
396 if (!m_rInput.eof() || !(pEnd == pStt || (!*pEnd && pEnd == pStt + 1)))
401 bool bIns =
true, bSplitNode =
false;
412 if (!m_rInput.eof() || pEnd != pStt)
423 bool bChkSplit =
true;
431 else if( 0x0a == *pStt )
436 if (bChkSplit && (!m_rInput.eof() || pEnd != pStt))
460 if (nReadCnt == m_nFileSize && pStt + 1 == pEnd)
491 else if( bSplitNode )
506 rtl_destroyTextToUnicodeContext( hConverter, hContext );
507 rtl_destroyTextToUnicodeConverter( hConverter );
512void SwASCIIParser::InsertText(
const OUString& rStr )
517 && m_nScript != (SvtScriptType::LATIN | SvtScriptType::ASIAN | SvtScriptType::COMPLEX))
virtual ErrCode Read(SwDoc &, const OUString &rBaseURL, SwPaM &, const OUString &) override
virtual bool AppendTextNode(SwPosition &rPos)=0
virtual bool SplitNode(const SwPosition &rPos, bool bChkTableStart)=0
Split a node at rPos (implemented only for TextNode).
virtual void InsertItemSet(const SwPaM &rRg, const SfxItemSet &, const SetAttrMode nFlags=SetAttrMode::DEFAULT, SwRootFrame const *pLayout=nullptr)=0
virtual bool InsertPoolItem(const SwPaM &rRg, const SfxPoolItem &, const SetAttrMode nFlags=SetAttrMode::DEFAULT, SwRootFrame const *pLayout=nullptr, SwTextAttr **ppNewTextAttr=nullptr)=0
Insert an attribute.
virtual bool InsertString(const SwPaM &rRg, const OUString &, const SwInsertFlags nInsertMode=SwInsertFlags::EMPTYEXPAND)=0
Insert string into existing text node at position rRg.Point().
virtual SfxPrinter * getPrinter(bool bCreate) const =0
Return the printer set at the document.
virtual SwTextFormatColl * GetTextCollFromPool(sal_uInt16 nId, bool bRegardLanguage=true)=0
Return "Auto-Collection with ID.
SwgReaderOption m_aOption
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
SfxItemSet & GetItemSet() const
rtl_TextEncoding GetCharSet() const
LanguageType GetLanguage() const
LineEnd GetParaFlags() const
void SetIncludeBOM(bool bVal)
void SetParaFlags(LineEnd eVal)
const OUString & GetFontName() const
void SetCharSet(rtl_TextEncoding nVal)
SvtScriptType GetAllScriptsOfText(const OUString &rText) const
IDocumentDeviceAccess const & getIDocumentDeviceAccess() const
void SetDefault(const SfxPoolItem &)
Set attribute as new default attribute in current document.
IDocumentContentOperations const & getIDocumentContentOperations() const
IDocumentStylePoolAccess const & getIDocumentStylePoolAccess() const
const SwAttrPool & GetAttrPool() const
bool SetTextFormatColl(const SwPaM &rRg, SwTextFormatColl *pFormat, const bool bReset=true, const bool bResetListAttrs=false, SwRootFrame const *pLayout=nullptr)
Add 4th optional parameter <bResetListAttrs>.
SwDocShell * GetDocShell()
static bool IsDetectableText(const char *pBuf, sal_uLong &rLen, rtl_TextEncoding *pCharSet, bool *pSwap, LineEnd *pLineEnd, bool *pBom)
Base class of the Writer document model elements.
PaM is Point and Mark: a selection of the document model.
const SwPosition * GetPoint() const
Represents the style of a paragraph.
virtual bool SetFormatAttr(const SfxPoolItem &rAttr) override
Override to recognize changes on the <SwNumRuleItem> and register/unregister the paragragh style at t...
const SwAsciiOptions & GetASCIIOpts() const
#define ERRCODE_IO_BROKENPACKAGE
constexpr TypedWhichId< SvxFontItem > RES_CHRATR_CJK_FONT(22)
constexpr TypedWhichId< SvxLanguageItem > RES_CHRATR_LANGUAGE(10)
constexpr TypedWhichId< SvxLanguageItem > RES_CHRATR_CTL_LANGUAGE(29)
constexpr TypedWhichId< SvxFontItem > RES_CHRATR_CTL_FONT(27)
constexpr TypedWhichId< SvxLanguageItem > RES_CHRATR_CJK_LANGUAGE(24)
constexpr TypedWhichId< SvxFormatBreakItem > RES_BREAK(100)
constexpr TypedWhichId< SvxFontItem > RES_CHRATR_FONT(7)
void StartProgress(TranslateId pMessResId, tools::Long nStartValue, tools::Long nEndValue, SwDocShell *pDocShell)
void EndProgress(SwDocShell const *pDocShell)
void SetProgressState(tools::Long nPosition, SwDocShell const *pDocShell)
static constexpr auto Items
o3tl::strong_int< sal_Int32, struct Tag_SwNodeOffset > SwNodeOffset
@ RES_POOLCOLL_STANDARD
Standard.
#define ERR_SWG_READ_ERROR