source: trunk/tools/qdoc3/text.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 6.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the tools applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*
43 text.cpp
44*/
45
46#include <qregexp.h>
47#include "text.h"
48#include <stdio.h>
49
50QT_BEGIN_NAMESPACE
51
52Text::Text()
53 : first(0), last(0)
54{
55}
56
57Text::Text(const QString &str)
58 : first(0), last(0)
59{
60 operator<<(str);
61}
62
63Text::Text( const Text& text )
64 : first(0), last(0)
65{
66 operator=( text );
67}
68
69Text::~Text()
70{
71 clear();
72}
73
74Text& Text::operator=( const Text& text )
75{
76 if ( this != &text ) {
77 clear();
78 operator<<( text );
79 }
80 return *this;
81}
82
83Text& Text::operator<<( Atom::Type atomType )
84{
85 return operator<<( Atom(atomType) );
86}
87
88Text& Text::operator<<( const QString& string )
89{
90 return operator<<( Atom(Atom::String, string) );
91}
92
93Text& Text::operator<<( const Atom& atom )
94{
95 if ( first == 0 ) {
96 first = new Atom( atom.type(), atom.string() );
97 last = first;
98 } else {
99 last = new Atom( last, atom.type(), atom.string() );
100 }
101 return *this;
102}
103
104Text& Text::operator<<( const Text& text )
105{
106 const Atom *atom = text.firstAtom();
107 while ( atom != 0 ) {
108 operator<<( *atom );
109 atom = atom->next();
110 }
111 return *this;
112}
113
114void Text::stripFirstAtom()
115{
116 if ( first != 0 ) {
117 if ( first == last )
118 last = 0;
119 Atom *oldFirst = first;
120 first = first->next();
121 delete oldFirst;
122 }
123}
124
125void Text::stripLastAtom()
126{
127 if ( last != 0 ) {
128 Atom *oldLast = last;
129 if ( first == last ) {
130 first = 0;
131 last = 0;
132 } else {
133 last = first;
134 while ( last->next() != oldLast )
135 last = last->next();
136 last->setNext( 0 );
137 }
138 delete oldLast;
139 }
140}
141
142QString Text::toString() const
143{
144 QString str;
145 const Atom *atom = firstAtom();
146 while ( atom != 0 ) {
147 if ( atom->type() == Atom::String || atom->type() == Atom::AutoLink )
148 str += atom->string();
149 atom = atom->next();
150 }
151 return str;
152}
153
154Text Text::subText( Atom::Type left, Atom::Type right, const Atom *from ) const
155{
156 const Atom *begin = from ? from : firstAtom();
157 const Atom *end;
158
159 while ( begin != 0 && begin->type() != left )
160 begin = begin->next();
161 if ( begin != 0 )
162 begin = begin->next();
163
164 end = begin;
165 while ( end != 0 && end->type() != right )
166 end = end->next();
167 if ( end == 0 )
168 begin = 0;
169 return subText( begin, end );
170}
171
172Text Text::sectionHeading(const Atom *sectionLeft)
173{
174 if ( sectionLeft != 0 ) {
175 const Atom *begin = sectionLeft;
176 while ( begin != 0 && begin->type() != Atom::SectionHeadingLeft )
177 begin = begin->next();
178 if ( begin != 0 )
179 begin = begin->next();
180
181 const Atom *end = begin;
182 while ( end != 0 && end->type() != Atom::SectionHeadingRight )
183 end = end->next();
184
185 if ( end != 0 )
186 return subText( begin, end );
187 }
188 return Text();
189}
190
191const Atom *Text::sectionHeadingAtom(const Atom *sectionLeft)
192{
193 if ( sectionLeft != 0 ) {
194 const Atom *begin = sectionLeft;
195 while ( begin != 0 && begin->type() != Atom::SectionHeadingLeft )
196 begin = begin->next();
197 if ( begin != 0 )
198 begin = begin->next();
199
200 return begin;
201 }
202 return 0;
203}
204
205void Text::dump() const
206{
207 const Atom *atom = firstAtom();
208 while ( atom != 0 ) {
209 QString str = atom->string();
210 str.replace( "\\", "\\\\" );
211 str.replace( "\"", "\\\"" );
212 str.replace( "\n", "\\n" );
213 str.replace( QRegExp("[^\x20-\x7e]"), "?" );
214 if ( !str.isEmpty() )
215 str = " \"" + str + "\"";
216 fprintf(stderr, " %-15s%s\n", atom->typeString().toLatin1().data(), str.toLatin1().data() );
217 atom = atom->next();
218 }
219}
220
221Text Text::subText( const Atom *begin, const Atom *end )
222{
223 Text text;
224 if ( begin != 0 ) {
225 while ( begin != end ) {
226 text << *begin;
227 begin = begin->next();
228 }
229 }
230 return text;
231}
232
233void Text::clear()
234{
235 while ( first != 0 ) {
236 Atom *atom = first;
237 first = first->next();
238 delete atom;
239 }
240 first = 0;
241 last = 0;
242}
243
244int Text::compare(const Text &text1, const Text &text2)
245{
246 if (text1.isEmpty())
247 return text2.isEmpty() ? 0 : -1;
248 if (text2.isEmpty())
249 return 1;
250
251 const Atom *atom1 = text1.firstAtom();
252 const Atom *atom2 = text2.firstAtom();
253
254 for (;;) {
255 if (atom1->type() != atom2->type())
256 return (int)atom1->type() - (int)atom2->type();
257 int cmp = QString::compare(atom1->string(), atom2->string());
258 if (cmp != 0)
259 return cmp;
260
261 if (atom1 == text1.lastAtom())
262 return atom2 == text2.lastAtom() ? 0 : -1;
263 if (atom2 == text2.lastAtom())
264 return 1;
265 atom1 = atom1->next();
266 atom2 = atom2->next();
267 }
268}
269
270QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.