Merge branch 'master' of scm.dev.nokia.troll.no:qt/oslo-staging-1 into master-integration
[qt-netbsd.git] / tools / qdoc3 / text.cpp
blob7093a433f7a0680fcda250a4c95c5aff4cc64616
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the tools applications of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
43 text.cpp
46 #include <qregexp.h>
47 #include "text.h"
48 #include <stdio.h>
50 QT_BEGIN_NAMESPACE
52 Text::Text()
53 : first(0), last(0)
57 Text::Text(const QString &str)
58 : first(0), last(0)
60 operator<<(str);
63 Text::Text( const Text& text )
64 : first(0), last(0)
66 operator=( text );
69 Text::~Text()
71 clear();
74 Text& Text::operator=( const Text& text )
76 if ( this != &text ) {
77 clear();
78 operator<<( text );
80 return *this;
83 Text& Text::operator<<( Atom::Type atomType )
85 return operator<<( Atom(atomType) );
88 Text& Text::operator<<( const QString& string )
90 return operator<<( Atom(Atom::String, string) );
93 Text& Text::operator<<( const Atom& atom )
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() );
101 return *this;
104 Text& Text::operator<<( const Text& text )
106 const Atom *atom = text.firstAtom();
107 while ( atom != 0 ) {
108 operator<<( *atom );
109 atom = atom->next();
111 return *this;
114 void Text::stripFirstAtom()
116 if ( first != 0 ) {
117 if ( first == last )
118 last = 0;
119 Atom *oldFirst = first;
120 first = first->next();
121 delete oldFirst;
125 void Text::stripLastAtom()
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 );
138 delete oldLast;
142 QString Text::toString() const
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();
151 return str;
154 Text Text::subText( Atom::Type left, Atom::Type right, const Atom *from ) const
156 const Atom *begin = from ? from : firstAtom();
157 const Atom *end;
159 while ( begin != 0 && begin->type() != left )
160 begin = begin->next();
161 if ( begin != 0 )
162 begin = begin->next();
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 );
172 Text Text::sectionHeading(const Atom *sectionLeft)
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();
181 const Atom *end = begin;
182 while ( end != 0 && end->type() != Atom::SectionHeadingRight )
183 end = end->next();
185 if ( end != 0 )
186 return subText( begin, end );
188 return Text();
191 const Atom *Text::sectionHeadingAtom(const Atom *sectionLeft)
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();
200 return begin;
202 return 0;
205 void Text::dump() const
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();
221 Text Text::subText( const Atom *begin, const Atom *end )
223 Text text;
224 if ( begin != 0 ) {
225 while ( begin != end ) {
226 text << *begin;
227 begin = begin->next();
230 return text;
233 void Text::clear()
235 while ( first != 0 ) {
236 Atom *atom = first;
237 first = first->next();
238 delete atom;
240 first = 0;
241 last = 0;
244 int Text::compare(const Text &text1, const Text &text2)
246 if (text1.isEmpty())
247 return text2.isEmpty() ? 0 : -1;
248 if (text2.isEmpty())
249 return 1;
251 const Atom *atom1 = text1.firstAtom();
252 const Atom *atom2 = text2.firstAtom();
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;
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();
270 QT_END_NAMESPACE