summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJo-Philipp Wich <jow@openwrt.org>2014-06-19 13:51:28 +0200
committerJo-Philipp Wich <jow@openwrt.org>2014-06-19 14:00:21 +0200
commitcdc760c58077f44fc40adbbe41e1556a67c1b9a9 (patch)
tree21ddc60f1e8ffdc644536ab2bfa73ee048d938f8
parent6f49eeec51e1697496d97479d41ff31cb4563270 (diff)
downloadjsonpath-cdc760c58077f44fc40adbbe41e1556a67c1b9a9.tar.gz
cli: implement limit flag
The new "-l" flag allows limiting the number of results getting printed. This is useful for expressions which match many items when only the first is desired. Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
-rw-r--r--main.c31
1 files changed, 23 insertions, 8 deletions
diff --git a/main.c b/main.c
index 2da2acf..0f1a39b 100644
--- a/main.c
+++ b/main.c
@@ -126,7 +126,8 @@ print_separator(const char *sep, int *sc, int sl)
}
static void
-export_value(struct list_head *matches, const char *prefix, const char *sep)
+export_value(struct list_head *matches, const char *prefix, const char *sep,
+ int limit)
{
int n, len;
int sc = 0, sl = strlen(sep);
@@ -141,6 +142,9 @@ export_value(struct list_head *matches, const char *prefix, const char *sep)
list_for_each_entry(item, matches, list)
{
+ if (limit-- <= 0)
+ break;
+
switch (json_object_get_type(item->jsobj))
{
case json_type_object:
@@ -195,6 +199,9 @@ export_value(struct list_head *matches, const char *prefix, const char *sep)
{
list_for_each_entry(item, matches, list)
{
+ if (limit-- <= 0)
+ break;
+
switch (json_object_get_type(item->jsobj))
{
case json_type_object:
@@ -217,7 +224,7 @@ export_value(struct list_head *matches, const char *prefix, const char *sep)
}
static void
-export_type(struct list_head *matches, const char *prefix)
+export_type(struct list_head *matches, const char *prefix, int limit)
{
bool first = true;
struct match_item *item;
@@ -242,6 +249,9 @@ export_type(struct list_head *matches, const char *prefix)
if (!first)
printf("\\ ");
+ if (limit-- <= 0)
+ break;
+
printf("%s", types[json_object_get_type(item->jsobj)]);
first = false;
}
@@ -317,7 +327,8 @@ print_error(struct jp_state *state, char *expr)
}
static bool
-filter_json(int opt, struct json_object *jsobj, char *expr, const char *sep)
+filter_json(int opt, struct json_object *jsobj, char *expr, const char *sep,
+ int limit)
{
struct jp_state *state;
const char *prefix = NULL;
@@ -346,11 +357,11 @@ filter_json(int opt, struct json_object *jsobj, char *expr, const char *sep)
switch (opt)
{
case 't':
- export_type(&matches, prefix);
+ export_type(&matches, prefix, limit);
break;
default:
- export_value(&matches, prefix, sep);
+ export_value(&matches, prefix, sep, limit);
break;
}
@@ -366,12 +377,12 @@ out:
int main(int argc, char **argv)
{
- int opt, rv = 0;
+ int opt, rv = 0, limit = 0x7FFFFFFF;
FILE *input = stdin;
struct json_object *jsobj = NULL;
const char *jserr = NULL, *source = NULL, *separator = " ";
- while ((opt = getopt(argc, argv, "i:s:e:t:F:q")) != -1)
+ while ((opt = getopt(argc, argv, "i:s:e:t:F:l:q")) != -1)
{
switch (opt)
{
@@ -398,6 +409,10 @@ int main(int argc, char **argv)
separator = optarg;
break;
+ case 'l':
+ limit = atoi(optarg);
+ break;
+
case 't':
case 'e':
if (!jsobj)
@@ -414,7 +429,7 @@ int main(int argc, char **argv)
}
}
- if (!filter_json(opt, jsobj, optarg, separator))
+ if (!filter_json(opt, jsobj, optarg, separator, limit))
rv = 1;
break;