@@ -53,6 +53,7 @@ typedef struct dive_data_t {
5353 dc_buffer_t * * fingerprint ;
5454 unsigned int number ;
5555 dctool_output_t * output ;
56+ unsigned int limit ;
5657} dive_data_t ;
5758
5859static int
@@ -96,6 +97,11 @@ dive_cb (const unsigned char *data, unsigned int size, const unsigned char *fing
9697
9798cleanup :
9899 dc_parser_destroy (parser );
100+
101+ if (divedata -> limit > 0 && divedata -> number >= divedata -> limit ) {
102+ return 0 ;
103+ }
104+
99105 return 1 ;
100106}
101107
@@ -146,7 +152,7 @@ event_cb (dc_device_t *device, dc_event_type_t event, const void *data, void *us
146152}
147153
148154static dc_status_t
149- download (dc_context_t * context , dc_descriptor_t * descriptor , dc_transport_t transport , const char * devname , const char * cachedir , dc_buffer_t * fingerprint , dctool_output_t * output )
155+ download (dc_context_t * context , dc_descriptor_t * descriptor , dc_transport_t transport , const char * devname , const char * cachedir , dc_buffer_t * fingerprint , dctool_output_t * output , unsigned int limit )
150156{
151157 dc_status_t rc = DC_STATUS_SUCCESS ;
152158 dc_iostream_t * iostream = NULL ;
@@ -214,6 +220,7 @@ download (dc_context_t *context, dc_descriptor_t *descriptor, dc_transport_t tra
214220 divedata .fingerprint = & ofingerprint ;
215221 divedata .number = 0 ;
216222 divedata .output = output ;
223+ divedata .limit = limit ;
217224
218225 // Download the dives.
219226 message ("Downloading the dives.\n" );
@@ -260,10 +267,11 @@ dctool_download_run (int argc, char *argv[], dc_context_t *context, dc_descripto
260267 const char * filename = NULL ;
261268 const char * cachedir = NULL ;
262269 const char * format = "xml" ;
270+ unsigned int limit = 0 ;
263271
264272 // Parse the command-line options.
265273 int opt = 0 ;
266- const char * optstring = "ht:o:p:c:f:u:" ;
274+ const char * optstring = "ht:o:p:c:f:u:l: " ;
267275#ifdef HAVE_GETOPT_LONG
268276 struct option options [] = {
269277 {"help" , no_argument , 0 , 'h' },
@@ -273,6 +281,7 @@ dctool_download_run (int argc, char *argv[], dc_context_t *context, dc_descripto
273281 {"cache" , required_argument , 0 , 'c' },
274282 {"format" , required_argument , 0 , 'f' },
275283 {"units" , required_argument , 0 , 'u' },
284+ {"limit" , required_argument , 0 , 'l' },
276285 {0 , 0 , 0 , 0 }
277286 };
278287 while ((opt = getopt_long (argc , argv , optstring , options , NULL )) != -1 ) {
@@ -304,6 +313,9 @@ dctool_download_run (int argc, char *argv[], dc_context_t *context, dc_descripto
304313 if (strcmp (optarg , "imperial" ) == 0 )
305314 units = DCTOOL_UNITS_IMPERIAL ;
306315 break ;
316+ case 'l' :
317+ limit = strtoul (optarg , NULL , 0 );
318+ break ;
307319 default :
308320 return EXIT_FAILURE ;
309321 }
@@ -345,7 +357,7 @@ dctool_download_run (int argc, char *argv[], dc_context_t *context, dc_descripto
345357 }
346358
347359 // Download the dives.
348- status = download (context , descriptor , transport , argv [0 ], cachedir , fingerprint , output );
360+ status = download (context , descriptor , transport , argv [0 ], cachedir , fingerprint , output , limit );
349361 if (status != DC_STATUS_SUCCESS ) {
350362 message ("ERROR: %s\n" , dctool_errmsg (status ));
351363 exitcode = EXIT_FAILURE ;
@@ -375,6 +387,7 @@ const dctool_command_t dctool_download = {
375387 " -c, --cache <directory> Cache directory\n"
376388 " -f, --format <format> Output format\n"
377389 " -u, --units <units> Set units (metric or imperial)\n"
390+ " -l, --limit <number> Maximum number of dives to download\n"
378391#else
379392 " -h Show help message\n"
380393 " -t <transport> Transport type\n"
@@ -383,6 +396,7 @@ const dctool_command_t dctool_download = {
383396 " -c <directory> Cache directory\n"
384397 " -f <format> Output format\n"
385398 " -u <units> Set units (metric or imperial)\n"
399+ " -l <limit> Maximum number of dives to download\n"
386400#endif
387401 "\n"
388402 "Supported output formats:\n"
0 commit comments