3 // Copyright (C) 2009 Mattia Milleri
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.
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
24 #define VERSION "0.1" // Versione del programma
26 bool DownloadFile( char* url
, char* filename
);
27 bool ExtractFile( char* input
, char* output
);
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() ){
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" );
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
);
58 printf ( "Extraction successful!\n" );
60 printf ( "Extraction failed!\n" );
65 // Scarica file da 'url' salvandolo come 'filename'
66 bool DownloadFile( char* url
, char* filename
) {
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
);
83 // Estrae il file 'output' dall'archivio 'input'
84 bool ExtractFile( char* inputName
, char* outputName
) {
85 FILE *archive
, *output
;
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
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
);
106 BZ2_bzReadClose ( &bzerror
, bz2file
);