Usa libbz2 per decomprimere il file, senza bisogno di chiamare bunzip.
[gutenget.git] / src / gutenget.cpp
blob876a994ca56f9893b9d1899251327b5fabddccb1
1 // gutenget.cpp
2 //
3 // Copyright (C) 2009 Mattia Milleri
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 3 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, see <http://www.gnu.org/licenses/>.
18 #include <fstream> // Gestione file
19 #include <stdio.h> // Per il remove()
20 #include <curl/curl.h> // Per il download
21 #include <bzlib.h> // Per i file compressi
22 using namespace std;
24 #define VERSION "0.1" // Versione del programma
26 bool DownloadFile( char* url, char* filename );
27 bool ExtractFile( char* input, char* output );
29 int main() {
30 char url[100], filename[100]; // url del file da scaricare e nome del file
31 char archive[100], data[100]; // nome del file compresso e estratto
32 bool downloadSuccess, extractSuccess;
34 printf( "gutenget %s\n", VERSION );
35 // Controlla se c'è ancora il file catalog.rdf
36 ifstream catalog( "catalog.rdf", ios::in );
37 if ( catalog.is_open() ){
38 catalog.close();
39 // Se il file è presente lo elimina
40 remove( "catalog.rdf" );
42 // Scarica il catalogo dal sito
43 //system("wget http://www.gutenberg.org/feeds/catalog.rdf.bz2");
44 strcpy( url, "http://www.gutenberg.org/feeds/catalog.rdf.bz2" );
45 strcpy( filename, "catalog.rdf.bz2" );
46 downloadSuccess = DownloadFile( url, filename );
47 if ( downloadSuccess )
48 printf( "Download successful!\n" );
49 else
50 printf( "Download failed!\n" );
52 // Scompatta l'archivio del catalogo
53 //system("bunzip2 -vv catalog.rdf.bz2");
54 strcpy( archive, "catalog.rdf.bz2" );
55 strcpy( data, "catalog.rdf" );
56 extractSuccess = ExtractFile( archive, data );
57 if ( extractSuccess )
58 printf ( "Extraction successful!\n" );
59 else
60 printf ( "Extraction failed!\n" );
62 return 0;
65 // Scarica file da 'url' salvandolo come 'filename'
66 bool DownloadFile( char* url, char* filename ) {
67 CURL *curl;
68 FILE *data;
69 curl = curl_easy_init();
71 data = fopen( filename, "w" );
73 curl_easy_setopt( curl, CURLOPT_URL, url );
74 curl_easy_setopt( curl, CURLOPT_WRITEDATA, data );
75 curl_easy_perform( curl );
77 curl_easy_cleanup( curl );
78 fclose( data );
80 return true;
83 // Estrae il file 'output' dall'archivio 'input'
84 bool ExtractFile( char* inputName, char* outputName ) {
85 FILE *archive, *output;
86 BZFILE *bz2file;
87 int nBuf;
88 char buf[ 10 ];
89 int bzerror;
91 archive = fopen( inputName, "r" );
92 output = fopen( outputName, "w" );
93 bz2file = BZ2_bzReadOpen( &bzerror, archive, 0, 0, NULL, 0 );
95 // Finchè non da errore, continua a leggere dal file
96 bzerror = BZ_OK;
97 while ( bzerror == BZ_OK ) {
98 nBuf = BZ2_bzRead ( &bzerror, bz2file, buf, 10 );
99 if ( bzerror == BZ_OK || bzerror == BZ_STREAM_END ) {
100 fprintf( output, buf );
101 } else {
102 return false;
106 BZ2_bzReadClose ( &bzerror, bz2file );
107 fclose( archive );
108 fclose( output );
110 return true;