26 #include <sfx2/sfxsids.hrc>
39 #include <strings.hrc>
45 #include <osl/diagnose.h>
47 #define ASC_BUFFLEN 4096
54 std::unique_ptr<SwPaM> m_pPam;
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" );
89 std::unique_ptr<SwASCIIParser> xParser(
new SwASCIIParser( rDoc, rPam, *
m_pStream,
91 ErrCode nRet = xParser->CallParser();
93 OUString optionsString;
94 xParser->GetUsedAsciiOptions().WriteUserData(optionsString);
105 SwASCIIParser::SwASCIIParser(
SwDoc& rD,
const SwPaM& rCursor,
SvStream& rIn,
bool bReadNewDoc,
110 , m_usedAsciiOptions(rOpts)
113 , m_bNewDoc(bReadNewDoc)
124 if (m_rOpt.GetLanguage())
127 m_oItemSet->Put(aLang);
128 aLang.SetWhich(RES_CHRATR_CJK_LANGUAGE);
129 m_oItemSet->Put(aLang);
131 m_oItemSet->Put(aLang);
133 if (m_rOpt.GetFontName().isEmpty())
139 SvxFontItem aFont( aTextFont.GetFamilyType(), aTextFont.GetFamilyName(),
140 OUString(), aTextFont.GetPitch(), aTextFont.GetCharSet(),
RES_CHRATR_FONT );
141 m_oItemSet->Put(aFont);
142 aFont.SetWhich(RES_CHRATR_CJK_FONT);
143 m_oItemSet->Put(aFont);
144 aFont.SetWhich(RES_CHRATR_CTL_FONT);
145 m_oItemSet->Put(aFont);
149 ErrCode SwASCIIParser::CallParser()
151 m_rInput.ResetError();
152 m_nFileSize = m_rInput.TellEnd();
153 m_rInput.Seek(STREAM_SEEK_TO_BEGIN);
154 m_rInput.ResetError();
158 std::optional<SwPaM> pInsPam;
159 sal_Int32 nSttContent = 0;
162 const SwNodeIndex& rTmp = m_pPam->GetPoint()->nNode;
164 nSttContent = m_pPam->GetPoint()->nContent.
GetIndex();
185 if (!(SvtScriptType::LATIN & m_nScript))
187 m_oItemSet->ClearItem(RES_CHRATR_FONT);
188 m_oItemSet->ClearItem(RES_CHRATR_LANGUAGE);
190 if (!(SvtScriptType::ASIAN & m_nScript))
192 m_oItemSet->ClearItem(RES_CHRATR_CJK_FONT);
193 m_oItemSet->ClearItem(RES_CHRATR_CJK_LANGUAGE);
195 if (!(SvtScriptType::COMPLEX & m_nScript))
197 m_oItemSet->ClearItem(RES_CHRATR_CTL_FONT);
200 if (m_oItemSet->Count())
217 sal_uInt16 aWhichIds[4] =
222 sal_uInt16 *pWhichIds = aWhichIds;
226 if (SfxItemState::SET
227 == m_oItemSet->GetItemState(*pWhichIds,
false, &pItem))
230 m_oItemSet->ClearItem(*pWhichIds);
235 if (m_oItemSet->Count())
241 *pInsPam->GetMark() = *m_pPam->GetPoint();
242 ++pInsPam->GetPoint()->nNode;
243 pInsPam->GetPoint()->nContent.Assign(
244 pInsPam->GetContentNode(), nSttContent );
247 OSL_ENSURE(
false,
"Have to change - hard attr. to para. style" );
260 ErrCode 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 );
512 void SwASCIIParser::InsertText(
const OUString& rStr )
517 && m_nScript != (SvtScriptType::LATIN | SvtScriptType::ASIAN | SvtScriptType::COMPLEX))
rtl_TextEncoding GetCharSet() const
static bool IsDetectableText(const char *pBuf, sal_uLong &rLen, rtl_TextEncoding *pCharSet, bool *pSwap, LineEnd *pLineEnd, bool *pBom)
Represents the style of a paragraph.
constexpr TypedWhichId< SvxFontItem > RES_CHRATR_CTL_FONT(27)
static constexpr auto Items
SwDocShell * GetDocShell()
IDocumentDeviceAccess const & getIDocumentDeviceAccess() const
const OUString & GetFontName() const
virtual void InsertItemSet(const SwPaM &rRg, const SfxItemSet &, const SetAttrMode nFlags=SetAttrMode::DEFAULT, SwRootFrame const *pLayout=nullptr)=0
constexpr TypedWhichId< SvxLanguageItem > RES_CHRATR_LANGUAGE(10)
LanguageType GetLanguage() const
SvtScriptType GetAllScriptsOfText(const OUString &rText) const
constexpr TypedWhichId< SvxFormatBreakItem > RES_BREAK(94)
void EndProgress(SwDocShell const *pDocShell)
virtual bool InsertPoolItem(const SwPaM &rRg, const SfxPoolItem &, const SetAttrMode nFlags=SetAttrMode::DEFAULT, SwRootFrame const *pLayout=nullptr, SwTextAttr **ppNewTextAttr=nullptr)=0
Insert an attribute.
void SetParaFlags(LineEnd eVal)
virtual bool SetFormatAttr(const SfxPoolItem &rAttr) override
Override to recognize changes on the and register/unregister the paragragh style at t...
void StartProgress(TranslateId pMessResId, tools::Long nStartValue, tools::Long nEndValue, SwDocShell *pDocShell)
IDocumentContentOperations const & getIDocumentContentOperations() const
constexpr TypedWhichId< SvxFontItem > RES_CHRATR_FONT(7)
bool SetTextFormatColl(const SwPaM &rRg, SwTextFormatColl *pFormat, const bool bReset=true, const bool bResetListAttrs=false, SwRootFrame const *pLayout=nullptr)
Add 4th optional parameter .
constexpr TypedWhichId< SvxLanguageItem > RES_CHRATR_CTL_LANGUAGE(29)
IDocumentStylePoolAccess const & getIDocumentStylePoolAccess() const
const SwAsciiOptions & GetASCIIOpts() const
SwNodeOffset GetIndex() const
void SetCharSet(rtl_TextEncoding nVal)
PaM is Point and Mark: a selection of the document model.
virtual bool InsertString(const SwPaM &rRg, const OUString &, const SwInsertFlags nInsertMode=SwInsertFlags::EMPTYEXPAND)=0
Insert string into existing text node at position rRg.Point().
const SwPosition * GetPoint() const
virtual ErrCode Read(SwDoc &, const OUString &rBaseURL, SwPaM &, const OUString &) override
SfxItemSet * GetItemSet() const
Marks a node in the document model.
virtual SfxPrinter * getPrinter(bool bCreate) const =0
Return the printer set at the document.
#define ERRCODE_IO_BROKENPACKAGE
void SetDefault(const SfxPoolItem &)
Set attribute as new default attribute in current document.
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
SwgReaderOption m_aOption
void SetProgressState(tools::Long nPosition, SwDocShell const *pDocShell)
virtual SwTextFormatColl * GetTextCollFromPool(sal_uInt16 nId, bool bRegardLanguage=true)=0
Return "Auto-Collection with ID.
constexpr TypedWhichId< SvxFontItem > RES_CHRATR_CJK_FONT(22)
o3tl::strong_int< sal_Int32, struct Tag_SwNodeOffset > SwNodeOffset
virtual bool AppendTextNode(SwPosition &rPos)=0
void SetIncludeBOM(bool bVal)
virtual bool SplitNode(const SwPosition &rPos, bool bChkTableStart)=0
Split a node at rPos (implemented only for TextNode).
#define ERR_SWG_READ_ERROR
LineEnd GetParaFlags() const
constexpr TypedWhichId< SvxLanguageItem > RES_CHRATR_CJK_LANGUAGE(24)
const SwAttrPool & GetAttrPool() const