SVN_SILENT made messages (.desktop file)
[kdeadmin.git] / kdat / Range.cpp
blob416912e5d5dba7bdcd78ce5b80dddca573045f91
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
4 //
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.
9 //
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
19 #include <assert.h>
21 #include "Range.h"
22 //Added by qt3to4:
23 #include <Q3PtrList>
25 Range::Range( int start, int end )
26 : _start( start ),
27 _end( end )
31 int Range::getStart()
33 return _start;
36 int Range::getEnd()
38 return _end;
41 void Range::setStart( int start )
43 _start = start;
46 void Range::setEnd( int end )
48 _end = 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
65 return _ranges;
68 void RangeList::addRange( int start, int end )
70 assert( end >= start );
72 if ( start == end ) {
73 return;
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 ) ) {
79 _ranges.remove();
80 } else if ( ( start >= _ranges.current()->getStart() ) && ( end <= _ranges.current()->getEnd() ) ) {
81 // The new range is totally contained in the current range.
82 return;
83 } else {
84 _ranges.next();
88 // Find the correct insertion point for the new range.
89 int idx = 0;
90 for ( _ranges.first(); _ranges.current(); _ranges.next(), idx++ ) {
91 if ( _ranges.current()->getStart() > start ) {
92 break;
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 );
101 } else {
102 // Append a new range.
103 _ranges.append( new Range( start, end ) );
105 } else {
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 );
116 _ranges.next();
117 _ranges.remove();
119 } else {
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 );
124 } else {
125 _ranges.insert( idx, new Range( start, end ) );
131 void RangeList::clear()
133 _ranges.clear();