SVN_SILENT made messages (.desktop file)
[kdeadmin.git] / kdat / File.cpp
blobb5157a1440e3d8b5136e74eb9d3990f5cd11de94
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>
20 #include <values.h>
22 #include "File.h"
23 //Added by qt3to4:
24 #include <Q3PtrList>
26 File::File( File* parent, int size, int mtime, int startRecord, int endRecord, const QString & name )
27 : _stubbed( FALSE ),
28 _name( name ),
29 _parent( parent )
31 assert( endRecord >= startRecord );
33 _union._data._size = size;
34 _union._data._mtime = mtime;
35 _union._data._startRecord = startRecord;
36 _union._data._endRecord = endRecord;
39 File::File( File* parent, FILE* fptr, int offset )
40 : _stubbed( TRUE ),
41 _parent( parent )
43 _union._stub._fptr = fptr;
44 _union._stub._offset = offset;
47 File::~File()
49 while ( _children.first() ) {
50 delete _children.first();
51 _children.removeFirst();
55 void File::read( int version )
57 if ( !_stubbed ) {
58 return;
61 _stubbed = FALSE;
63 FILE* fptr = _union._stub._fptr;
65 fseek( fptr, _union._stub._offset, SEEK_SET );
67 // File name (4 bytes + n chars).
68 int ival;
69 fread( &ival, sizeof( ival ), 1, fptr );
71 char * buf = new char[ival+1];
72 buf[ival] = '\0';
73 fread( buf, sizeof( char ), ival, fptr );
74 _name = buf;
76 delete [] buf;
78 // File size (4 bytes).
79 fread( &ival, sizeof( ival ), 1, fptr );
80 _union._data._size = ival;
82 // File modification time (4 bytes).
83 fread( &ival, sizeof( ival ), 1, fptr );
84 _union._data._mtime = ival;
86 // Start record number.
87 fread( &ival, sizeof( ival ), 1, fptr );
88 _union._data._startRecord = ival;
90 // End record number.
91 fread( &ival, sizeof( ival ), 1, fptr );
92 _union._data._endRecord = ival;
94 //%%% This is a kludge to cope with some screwed up tape indexes.
95 //%%% Hopefully the file with the zero end record is *always* at
96 //%%% the end of the archive.
97 if ( ( _union._data._endRecord <= 0 ) && ( _union._data._startRecord != _union._data._endRecord ) ) {
98 _union._data._endRecord = MAXINT;
101 if ( version > 3 ) {
102 fread( &ival, sizeof( ival ), 1, fptr );
103 int rc = ival;
104 int start = 0;
105 int end = 0;
106 for ( int ii = 0; ii < rc; ii++ ) {
107 fread( &ival, sizeof( ival ), 1, fptr );
108 start = ival;
109 fread( &ival, sizeof( ival ), 1, fptr );
110 end = ival;
111 _ranges.addRange( start, end );
115 //===== Read files =====
116 fread( &ival, sizeof( ival ), 1, fptr );
117 for ( int count = ival; count > 0; count-- ) {
118 fread( &ival, sizeof( ival ), 1, fptr );
119 addChild( new File( this, fptr, ival ) );
123 void File::readAll( int version )
125 read( version );
127 Q3PtrListIterator<File> i( getChildren() );
128 for ( ; i.current(); ++i ) {
129 i.current()->readAll( version );
133 void File::write( FILE* fptr )
135 int zero = 0;
137 // File name (4 bytes + n chars).
138 int ival = getName().length();
139 fwrite( &ival, sizeof( ival ), 1, fptr );
140 fwrite( getName().ascii(), sizeof( char ), ival, fptr );
142 // File size (4 bytes).
143 ival = getSize();
144 fwrite( &ival, sizeof( ival ), 1, fptr );
146 // File modification time (4 bytes).
147 ival = getMTime();
148 fwrite( &ival, sizeof( ival ), 1, fptr );
150 // Start record number.
151 ival = getStartRecord();
152 fwrite( &ival, sizeof( ival ), 1, fptr );
154 // End record number.
155 ival = getEndRecord();
156 fwrite( &ival, sizeof( ival ), 1, fptr );
158 // Child range list.
159 ival = _ranges.getRanges().count();
160 fwrite( &ival, sizeof( ival ), 1, fptr );
161 Q3PtrListIterator<Range> it( _ranges.getRanges() );
162 for ( ; it.current(); ++it ) {
163 ival = it.current()->getStart();
164 fwrite( &ival, sizeof( ival ), 1, fptr );
165 ival = it.current()->getEnd();
166 fwrite( &ival, sizeof( ival ), 1, fptr );
169 // Number of immediate children (4 bytes).
170 ival = getChildren().count();
171 fwrite( &ival, sizeof( ival ), 1, fptr );
173 // Fill in file offsets later...
174 int fileTable = ftell( fptr );
175 for ( ; ival > 0; ival-- ) {
176 fwrite( &zero, sizeof( zero ), 1, fptr );
179 //===== Write files =====
180 ival = _children.count();
181 fwrite( &ival, sizeof( ival ), 1, fptr );
183 Q3PtrListIterator<File> i( _children );
184 int count = 0;
185 for ( ; i.current(); ++i, count++ ) {
186 // Fill in the file offset.
187 int here = ftell( fptr );
188 fseek( fptr, fileTable + 4*count, SEEK_SET );
189 fwrite( &here, sizeof( here ), 1, fptr );
190 fseek( fptr, here, SEEK_SET );
192 i.current()->write( fptr );
196 bool File::isDirectory()
198 read();
200 return _name[ _name.length() - 1 ] == '/';
203 int File::getSize()
205 read();
207 return _union._data._size;
210 int File::getMTime()
212 read();
214 return _union._data._mtime;
217 int File::getStartRecord()
219 read();
221 return _union._data._startRecord;
224 int File::getEndRecord()
226 read();
228 return _union._data._endRecord;
231 QString File::getName()
233 read();
235 return _name;
238 QString File::getFullPathName()
240 QString tmp = _name.copy();
241 for ( File* parent = getParent(); parent; parent = parent->getParent() ) {
242 tmp.prepend( parent->getName() );
245 return tmp;
248 File* File::getParent()
250 return _parent;
253 const Q3PtrList<File>& File::getChildren()
255 read();
257 return _children;
260 const Q3PtrList<Range>& File::getRanges()
262 read();
264 return _ranges.getRanges();
267 void File::addChild( File* file )
269 read();
271 _children.append( file );
274 void File::calcRanges()
276 assert( !_stubbed );
278 _ranges.clear();
279 _ranges.addRange( getStartRecord(), getEndRecord() );
281 Q3PtrListIterator<File> it( getChildren() );
282 for ( ; it.current(); ++it ) {
283 it.current()->calcRanges();
284 Q3PtrListIterator<Range> it2( it.current()->getRanges() );
285 for ( ; it2.current(); ++it2 ) {
286 _ranges.addRange( it2.current()->getStart(), it2.current()->getEnd() );