Some fix for scrolling with lasso.
[tangerine.git] / workbench / c / Unpack / package.c
blobf3c80b59931d0419caeefd686095631748530640
1 /*
2 Copyright © 2003, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <exec/memory.h>
7 #include <dos/dos.h>
9 #include <proto/exec.h>
10 #include <proto/dos.h>
11 #include <aros/macros.h>
13 #include "support.h"
14 #include "modes.h"
15 #include "bzip2.h"
16 #include "file.h"
17 #include "package.h"
18 #include "gui.h"
20 #define PKG_BUFFER_SIZE (32*1024) /* 32kiB */
22 /** Low-level functions *****************************************************/
24 APTR PKG_Open( CONST_STRPTR filename, LONG mode )
26 if( mode != MODE_READ && mode != MODE_WRITE ) return NULL;
28 return BZ2_Open( filename, mode );
31 void PKG_Close( APTR pkg )
33 if( pkg != NULL ) BZ2_Close( pkg );
36 LONG PKG_Read( APTR pkg, APTR buffer, LONG length )
38 return BZ2_Read( pkg, buffer, length );
41 /** High-level functions ****************************************************/
43 UBYTE /* version */ PKG_ReadHeader( APTR pkg )
45 UBYTE data[4] = { 0, 0, 0, 0 };
46 /*LONG actual = */ PKG_Read( pkg, data, 4 );
48 /* FIXME: error handling? naaaah... :S */
50 return data[3];
53 LONG /* error */ PKG_ExtractFile( APTR pkg )
55 LONG pathLength, dataLength, rc, result;
56 STRPTR path = NULL;
57 APTR buffer = NULL;
58 BPTR output = NULL;
60 /* Read the path length */
61 rc = PKG_Read( pkg, &pathLength, sizeof( pathLength ) );
62 pathLength = AROS_BE2LONG(pathLength);
64 if( rc == -1 ) { result = -1; goto cleanup; }
65 if( rc == 0 ) { result = 0; goto cleanup; }
67 /* Read the path */
68 path = AllocMem( pathLength + 1, MEMF_ANY );
69 if( path == NULL ) { result = -1; goto cleanup; }
70 rc = PKG_Read( pkg, path, pathLength + 1 );
71 if( rc == -1 || rc == 0) { result = -1; goto cleanup; }
73 /* Read the data lendth */
74 rc = PKG_Read( pkg, &dataLength, sizeof( dataLength ) );
75 dataLength = AROS_BE2LONG(dataLength);
77 if( rc == -1 || rc == 0 ) { result = -1; goto cleanup; }
79 //Printf( "Extracting %s (%ld bytes)...\n", path, dataLength );
81 /* Make sure the destination directory exists */
82 if( !MakeDirs( path ) ) { Printf("E:makedirs\n"); result = -1; goto cleanup; }
84 /* Read and write the data in pieces */
85 buffer = AllocMem( PKG_BUFFER_SIZE, MEMF_ANY );
86 if( buffer == NULL ) { Printf("E:mem\n"); result = -1; goto cleanup; }
87 output = Open( path, MODE_NEWFILE );
88 if( output == NULL ) { Printf("E:create\n"); result = -1; goto cleanup; }
91 LONG total = 0;
93 while( total < dataLength )
95 LONG length = 0;
97 if( dataLength - total >= PKG_BUFFER_SIZE )
99 length = PKG_BUFFER_SIZE;
101 else
103 length = dataLength - total;
106 rc = PKG_Read( pkg, buffer, length );
107 if( rc == -1 || rc == 0 ) { Printf("E:read\n"); result = -1; goto cleanup; }
109 rc = FILE_Write( output, buffer, length );
110 if( rc == -1 ) { Printf("E:write\n"); result = -1; goto cleanup; }
112 total += length;
116 result = 1;
118 cleanup:
119 if( path != NULL ) FreeMem( path, pathLength + 1 );
120 if( buffer != NULL ) FreeMem( buffer, PKG_BUFFER_SIZE );
121 if( output != NULL ) Close( output );
123 return result;
126 LONG /* error */ PKG_ExtractEverything( APTR pkg )
128 LONG result;
130 PKG_ReadHeader( pkg );
132 result = PKG_ExtractFile( pkg );
133 while( result != -1 && result != 0 )
135 result = PKG_ExtractFile( pkg );
138 return result;