1 // KDat - a tar-based DAT archiver
2 // Copyright (C) 1998-2000 Sean Vyain, svyain@mail.tds.net
3 // Copyright (C) 2001-2002 Lawrence Widman, kdat@cardiothink.com
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 Range::Range( int start
, int end
)
41 void Range::setStart( int start
)
46 void Range::setEnd( int end
)
51 RangeList::RangeList()
55 RangeList::~RangeList()
57 while ( _ranges
.first() ) {
58 delete _ranges
.first();
59 _ranges
.removeFirst();
63 const Q3PtrList
<Range
>& RangeList::getRanges() const
68 void RangeList::addRange( int start
, int end
)
70 assert( end
>= start
);
76 // Remove all of the ranges that are totally contained by the new range.
77 for ( _ranges
.first(); _ranges
.current(); ) {
78 if ( ( _ranges
.current()->getStart() >= start
) && ( _ranges
.current()->getEnd() <= end
) ) {
80 } else if ( ( start
>= _ranges
.current()->getStart() ) && ( end
<= _ranges
.current()->getEnd() ) ) {
81 // The new range is totally contained in the current range.
88 // Find the correct insertion point for the new range.
90 for ( _ranges
.first(); _ranges
.current(); _ranges
.next(), idx
++ ) {
91 if ( _ranges
.current()->getStart() > start
) {
96 if ( !_ranges
.current() ) {
97 // Append new range to end of list.
98 if ( ( _ranges
.last() ) && ( _ranges
.last()->getEnd() >= start
) ) {
99 // Ranges overlap, merge them.
100 _ranges
.last()->setEnd( end
);
102 // Append a new range.
103 _ranges
.append( new Range( start
, end
) );
106 // Insert new range before current range.
107 if ( _ranges
.current()->getStart() <= end
) {
108 // Ranges overlap, merge them.
109 _ranges
.current()->setStart( start
);
110 end
= _ranges
.current()->getEnd();
112 // Check previous range for overlap.
113 if ( ( _ranges
.prev() ) && ( _ranges
.current()->getEnd() >= start
) ) {
114 // New range overlapped both the before and after ranges.
115 _ranges
.current()->setEnd( end
);
120 // Check previous range for overlap.
121 if ( ( _ranges
.prev() ) && ( _ranges
.current()->getEnd() >= start
) ) {
122 // New range overlapped the before range.
123 _ranges
.current()->setEnd( end
);
125 _ranges
.insert( idx
, new Range( start
, end
) );
131 void RangeList::clear()