1 /* AbiWord
2  * Copyright (C) 1998 AbiSource, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA.
18  */
19 
20 
21 #include "pf_Frag_Text.h"
22 #include "pt_PieceTable.h"
23 #include "px_ChangeRecord.h"
24 #include "px_CR_Span.h"
25 #include "pd_Iterator.h"
26 #include "ut_debugmsg.h"
27 
pf_Frag_Text(pt_PieceTable * pPT,PT_BufIndex bufIndex,UT_uint32 length,PT_AttrPropIndex indexAP,fd_Field * pField)28 pf_Frag_Text::pf_Frag_Text(pt_PieceTable * pPT,
29 						   PT_BufIndex bufIndex,
30 						   UT_uint32 length,
31 						   PT_AttrPropIndex indexAP,
32                            fd_Field * pField)
33 	: pf_Frag(pPT,pf_Frag::PFT_Text,length)
34 {
35 	m_bufIndex = bufIndex;
36     m_indexAP = indexAP;
37     m_pField = pField;
38 }
39 
~pf_Frag_Text()40 pf_Frag_Text::~pf_Frag_Text()
41 {
42 }
43 
_isContentEqual(const pf_Frag & f2) const44 bool pf_Frag_Text::_isContentEqual(const pf_Frag & f2) const
45 {
46 	if(!pf_Frag::_isContentEqual(f2))
47 		return false;
48 
49 	// NB: this we are asked to strictly compare 2 frags
50 	if(getLength() != f2.getLength())
51 		return false;
52 
53 	pf_Frag * pf2 = const_cast<pf_Frag *>(&f2);
54 
55 	PD_DocIterator t1(* (m_pPieceTable->getDocument()), getPos());
56 	PD_DocIterator t2(* (pf2->getPieceTable()->getDocument()), f2.getPos());
57 
58 	UT_uint32 iLen = UT_MIN(getLength(), f2.getLength());
59 	UT_uint32 i = 0;
60 
61 	while(i < iLen && t1.getStatus() == UTIter_OK && t2.getStatus() == UTIter_OK)
62 	{
63 		if(t1.getChar() != t2.getChar())
64 			return false;
65 
66 		++i;
67 		++t1;
68 		++t2;
69 	}
70 
71 	return true;
72 }
73 
74 
75 
createSpecialChangeRecord(PX_ChangeRecord ** ppcr,PT_DocPosition dpos,PT_BlockOffset blockOffset) const76 bool pf_Frag_Text::createSpecialChangeRecord(PX_ChangeRecord ** ppcr,
77 												PT_DocPosition dpos,
78 												PT_BlockOffset blockOffset) const
79 {
80 	UT_return_val_if_fail (ppcr,false);
81 
82 	PX_ChangeRecord * pcr
83 		= new PX_ChangeRecord_Span(PX_ChangeRecord::PXT_InsertSpan,
84 								   dpos, m_indexAP,
85 								   m_bufIndex,m_length,blockOffset,m_pField);
86 	if (!pcr)
87 		return false;
88 
89 	*ppcr = pcr;
90 	return true;
91 }
92 
createSpecialChangeRecord(PX_ChangeRecord ** ppcr,PT_DocPosition dpos,PT_BlockOffset blockOffset,PT_BlockOffset startFragOffset,PT_BlockOffset endFragOffset) const93 bool pf_Frag_Text::createSpecialChangeRecord(PX_ChangeRecord ** ppcr,
94 												PT_DocPosition dpos,
95 												PT_BlockOffset blockOffset,
96 												PT_BlockOffset startFragOffset,
97 												PT_BlockOffset endFragOffset) const
98 {
99 	UT_return_val_if_fail (ppcr,false);
100 	UT_return_val_if_fail (endFragOffset <= m_length,false);
101 	UT_return_val_if_fail (startFragOffset < endFragOffset,false);
102 
103 	PX_ChangeRecord * pcr
104 		= new PX_ChangeRecord_Span(PX_ChangeRecord::PXT_InsertSpan,
105 								   dpos+startFragOffset,
106 								   m_indexAP,
107 								   m_bufIndex+startFragOffset,
108 								   (endFragOffset-startFragOffset),
109 								   blockOffset+startFragOffset,m_pField);
110 	if (!pcr)
111 		return false;
112 
113 	*ppcr = pcr;
114 	return true;
115 }
116 
changeLength(UT_uint32 newLength)117 void pf_Frag_Text::changeLength(UT_uint32 newLength)
118 {
119 	UT_ASSERT_HARMLESS(newLength > 0);
120 	UT_sint32 delta = static_cast<UT_sint32>(newLength) -  static_cast<UT_sint32>(m_length);
121 	m_length = newLength;
122 	lengthChanged(delta);
123 }
124 
adjustOffsetLength(PT_BufIndex bi,UT_uint32 newLength)125 void pf_Frag_Text::adjustOffsetLength(PT_BufIndex bi, UT_uint32 newLength)
126 {
127 	m_bufIndex = bi;
128 	UT_sint32 delta = static_cast<UT_sint32>(newLength) -  static_cast<UT_sint32>(m_length);
129 	m_length = newLength;
130 	lengthChanged(delta);
131 }
132 
setField(fd_Field * pField)133 void pf_Frag_Text::setField(fd_Field * pField)
134 {
135     m_pField = pField;
136 }
137 
toString() const138 std::string pf_Frag_Text::toString() const
139 {
140     PT_BufIndex startidx = getBufIndex();
141     const UT_UCSChar* ucsstart = m_pPieceTable->getPointer( startidx );
142 	UT_UTF8String ustr( ucsstart, getLength() );
143     return ustr.utf8_str();
144 }
145