37 #include <strings.hrc>
43 #include <osl/diagnose.h>
45 #define ASC_BUFFLEN 4096
52 std::unique_ptr<SwPaM> pPam;
54 std::unique_ptr<char[]> pArr;
56 std::unique_ptr<SfxItemSet> pItemSet;
64 SwASCIIParser(
const SwASCIIParser&) =
delete;
65 SwASCIIParser& operator=(
const SwASCIIParser&) =
delete;
81 OSL_ENSURE(
false,
"ASCII read without a stream" );
85 std::unique_ptr<SwASCIIParser> xParser(
new SwASCIIParser( rDoc, rPam, *
m_pStream,
87 ErrCode nRet = xParser->CallParser();
98 , bNewDoc(bReadNewDoc)
103 pItemSet = std::make_unique<SfxItemSet>( rDoc.GetAttrPool(),
109 if( rOpt.GetLanguage() )
112 pItemSet->Put( aLang );
113 aLang.SetWhich(RES_CHRATR_CJK_LANGUAGE);
114 pItemSet->Put( aLang );
116 pItemSet->Put( aLang );
118 if( rOpt.GetFontName().isEmpty() )
122 if( rDoc.getIDocumentDeviceAccess().getPrinter(
false ) )
123 aTextFont = rDoc.getIDocumentDeviceAccess().getPrinter(
false )->GetFontMetric( aTextFont );
124 SvxFontItem aFont( aTextFont.GetFamilyType(), aTextFont.GetFamilyName(),
125 OUString(), aTextFont.GetPitch(), aTextFont.GetCharSet(),
RES_CHRATR_FONT );
126 pItemSet->Put( aFont );
127 aFont.SetWhich(RES_CHRATR_CJK_FONT);
128 pItemSet->Put( aFont );
129 aFont.SetWhich(RES_CHRATR_CTL_FONT);
130 pItemSet->Put( aFont );
134 ErrCode SwASCIIParser::CallParser()
137 nFileSize = rInput.TellEnd();
138 rInput.Seek(STREAM_SEEK_TO_BEGIN);
141 ::StartProgress( STR_STATSTR_W4WREAD, 0, nFileSize, rDoc.GetDocShell() );
143 std::unique_ptr<SwPaM> pInsPam;
144 sal_Int32 nSttContent = 0;
148 pInsPam.reset(
new SwPaM( rTmp, rTmp, 0, -1 ));
149 nSttContent = pPam->GetPoint()->nContent.
GetIndex();
160 rDoc.SetTextFormatColl(*pPam, pColl);
168 if( !( SvtScriptType::LATIN & nScript ))
170 pItemSet->ClearItem( RES_CHRATR_FONT );
171 pItemSet->ClearItem( RES_CHRATR_LANGUAGE );
173 if( !( SvtScriptType::ASIAN & nScript ))
175 pItemSet->ClearItem( RES_CHRATR_CJK_FONT );
176 pItemSet->ClearItem( RES_CHRATR_CJK_LANGUAGE );
178 if( !( SvtScriptType::COMPLEX & nScript ))
180 pItemSet->ClearItem( RES_CHRATR_CTL_FONT );
183 if( pItemSet->Count() )
200 sal_uInt16 aWhichIds[4] =
205 sal_uInt16 *pWhichIds = aWhichIds;
209 if (SfxItemState::SET == pItemSet->GetItemState(*pWhichIds,
213 pItemSet->ClearItem( *pWhichIds );
218 if (pItemSet->Count())
219 rDoc.SetDefault(*pItemSet);
224 *pInsPam->GetMark() = *pPam->GetPoint();
225 ++pInsPam->GetPoint()->nNode;
226 pInsPam->GetPoint()->nContent.Assign(
227 pInsPam->GetContentNode(), nSttContent );
230 OSL_ENSURE(
false,
"Have to change - hard attr. to para. style" );
231 rDoc.getIDocumentContentOperations().InsertItemSet( *pInsPam, *pItemSet );
243 ErrCode SwASCIIParser::ReadChars()
245 sal_Unicode *pStt =
nullptr, *pEnd =
nullptr, *pLastStt =
nullptr;
248 bool bSwapUnicode =
false;
252 if (nFileSize >= 2 &&
258 sal_Size nLen, nOrig;
259 nOrig = nLen = rInput.ReadBytes(pArr.get(),
ASC_BUFFLEN);
260 rtl_TextEncoding eCharSet;
267 OSL_ENSURE(bRet,
"Autodetect of text import without nag dialog must have failed");
268 if (bRet && eCharSet != RTL_TEXTENCODING_DONTKNOW)
279 rtl_TextToUnicodeConverter hConverter=
nullptr;
280 rtl_TextToUnicodeContext hContext=
nullptr;
281 rtl_TextEncoding currentCharSet = pUseMe->
GetCharSet();
282 if (RTL_TEXTENCODING_UCS2 != currentCharSet)
284 if( currentCharSet == RTL_TEXTENCODING_DONTKNOW )
285 currentCharSet = RTL_TEXTENCODING_ASCII_US;
286 hConverter = rtl_createTextToUnicodeConverter( currentCharSet );
287 OSL_ENSURE( hConverter,
"no string convert available" );
289 return ErrCode(ErrCodeArea::Sw, ErrCodeClass::Read, 0);
290 bSwapUnicode =
false;
291 hContext = rtl_createTextToUnicodeContext( hConverter );
293 else if (pUseMe != &aEmpty)
295 rInput.StartReadingUnicodeText( currentCharSet );
296 bSwapUnicode = rInput.IsEndianSwap();
299 std::unique_ptr<sal_Unicode[]> aWork;
300 sal_Size nArrOffset = 0;
305 if( pLastStt != pStt )
310 if( ERRCODE_NONE != rInput.GetError() || 0 == (lGCount =
311 rInput.ReadBytes( pArr.get() + nArrOffset,
321 lGCount += nArrOffset;
326 sal_Size nNewLen = lGCount, nCntBytes;
331 nNewLen = rtl_convertTextToUnicode( hConverter, hContext,
332 pArr.get(), lGCount, pBuf, nNewLen,
334 RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_DEFAULT |
335 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_DEFAULT |
336 RTL_TEXTTOUNICODE_FLAGS_INVALID_DEFAULT |
337 RTL_TEXTTOUNICODE_FLAGS_GLOBAL_SIGNATURE
341 nArrOffset = lGCount - nCntBytes;
342 if( 0 != nArrOffset )
343 memmove( pArr.get(), pArr.get() + nCntBytes, nArrOffset );
345 pStt = pLastStt = aWork.get();
346 pEnd = pStt + nNewLen;
350 pStt = pLastStt =
reinterpret_cast<sal_Unicode*
>(pArr.get());
351 auto nChars = lGCount / 2;
352 pEnd = pStt + nChars;
356 char* pF = pArr.get(), *pN = pArr.get() + 1;
357 for (sal_Size n = 0;
n < nChars; ++
n, pF += 2, pN += 2)
373 if( 0x0a == *pStt && 0x0d == cLastCR )
378 if( !rInput.eof() || !(pEnd == pStt ||
379 ( !*pEnd && pEnd == pStt+1 ) ) )
380 rDoc.getIDocumentContentOperations().SplitNode( *pPam->GetPoint(), false );
384 bool bIns =
true, bSplitNode =
false;
395 if( !rInput.eof() || pEnd != pStt )
406 bool bChkSplit =
true;
414 else if( 0x0a == *pStt )
419 if( bChkSplit && ( !rInput.eof() || pEnd != pStt ))
432 rDoc.getIDocumentContentOperations().SplitNode( *pPam->GetPoint(), false );
433 rDoc.getIDocumentContentOperations().InsertPoolItem(
442 if( nReadCnt == nFileSize && pStt+1 == pEnd )
465 rDoc.getIDocumentContentOperations().SplitNode( *pPam->GetPoint(), false );
473 else if( bSplitNode )
478 rDoc.getIDocumentContentOperations().AppendTextNode( *pPam->GetPoint() );
480 rDoc.getIDocumentContentOperations().SplitNode( *pPam->GetPoint(), false );
488 rtl_destroyTextToUnicodeContext( hConverter, hContext );
489 rtl_destroyTextToUnicodeConverter( hConverter );
494 void SwASCIIParser::InsertText(
const OUString& rStr )
496 rDoc.getIDocumentContentOperations().InsertString( *pPam, rStr );
498 if( pItemSet &&
g_pBreakIt && nScript != ( SvtScriptType::LATIN |
499 SvtScriptType::ASIAN |
500 SvtScriptType::COMPLEX ) )
rtl_TextEncoding GetCharSet() const
Represents the style of a paragraph.
constexpr TypedWhichId< SvxFontItem > RES_CHRATR_CTL_FONT(27)
const OUString & GetFontName() const
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)
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...
constexpr TypedWhichId< SvxFontItem > RES_CHRATR_FONT(7)
void StartProgress(const char *pMessResId, tools::Long nStartValue, tools::Long nEndValue, SwDocShell *pDocShell)
constexpr TypedWhichId< SvxLanguageItem > RES_CHRATR_CTL_LANGUAGE(29)
sal_uLong GetIndex() const
const SwAsciiOptions & GetASCIIOpts() const
void SetCharSet(rtl_TextEncoding nVal)
PaM is Point and Mark: a selection of the document model.
const SwPosition * GetPoint() const
virtual ErrCode Read(SwDoc &, const OUString &rBaseURL, SwPaM &, const OUString &) override
static bool IsDetectableText(const char *pBuf, sal_uLong &rLen, rtl_TextEncoding *pCharSet, bool *pSwap, LineEnd *pLineEnd)
Marks a node in the document model.
#define ERRCODE_IO_BROKENPACKAGE
SwgReaderOption m_aOption
void SetProgressState(tools::Long nPosition, SwDocShell const *pDocShell)
constexpr TypedWhichId< SvxFontItem > RES_CHRATR_CJK_FONT(22)
#define ERR_SWG_READ_ERROR
LineEnd GetParaFlags() const
constexpr TypedWhichId< SvxLanguageItem > RES_CHRATR_CJK_LANGUAGE(24)