test_osm3.c shows how to intentionally abort the parser.Here is a typical usage example, parsing an OSM XML file (.osm):
./test_osm3 test.osm 10
node#1
node#2
node#3
node#4
node#5
node#6
node#7
node#8
node#9
node#10
PARSING ABORTED
Here is another example, this time parsing a .pbf (Protocol Buffer) OSM file:
./test_osm3 test.osm 5
node#1
node#2
node#3
node#4
node#5
PARSING ABORTED
#include <stdio.h>
#include <stdlib.h>
struct osm_helper
{
int read_count;
int stop_limit;
};
static int
eval_abort (struct osm_helper *helper)
{
if (helper->read_count > helper->stop_limit)
return 1;
return 0;
}
static int
parse_node (
const void *user_data,
const readosm_node * node)
{
struct osm_helper *helper = (struct osm_helper *) user_data;
if (node != NULL)
node = NULL;
helper->read_count++;
if (eval_abort (helper))
printf ("Node#%d\n", helper->read_count);
}
static int
parse_way (
const void *user_data,
const readosm_way * way)
{
struct osm_helper *helper = (struct osm_helper *) user_data;
if (way != NULL)
way = NULL;
helper->read_count++;
if (eval_abort (helper))
printf ("Way#%d\n", helper->read_count);
}
static int
{
struct osm_helper *helper = (struct osm_helper *) user_data;
if (relation != NULL)
relation = NULL;
helper->read_count++;
if (eval_abort (helper))
printf ("Relation#%d\n", helper->read_count);
}
int
main (int argc, char *argv[])
{
const void *osm_handle;
int ret;
struct osm_helper helper;
helper.read_count = 0;
helper.stop_limit = 0;
if (argc != 3)
{
fprintf (stderr, "usage: test_osm3 path-to-OSM limit\n");
return -1;
}
helper.stop_limit = atoi (argv[2]);
{
fprintf (stderr, "OPEN error: %d\n", ret);
goto stop;
}
ret =
parse_relation);
{
fprintf (stderr, "PARSE error: %d\n", ret);
goto stop;
}
stop:
return 0;
}