projects
/
oweals
/
jsonpath.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
cli: implement limit flag
[oweals/jsonpath.git]
/
main.c
diff --git
a/main.c
b/main.c
index 2da2acf2944d469a39b4ea29a99b3b18469e58c8..0f1a39b30c507e5a775e6dfa538e74716645e165 100644
(file)
--- a/
main.c
+++ b/
main.c
@@
-126,7
+126,8
@@
print_separator(const char *sep, int *sc, int sl)
}
static void
}
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);
{
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)
{
list_for_each_entry(item, matches, list)
{
+ if (limit-- <= 0)
+ break;
+
switch (json_object_get_type(item->jsobj))
{
case json_type_object:
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)
{
{
list_for_each_entry(item, matches, list)
{
+ if (limit-- <= 0)
+ break;
+
switch (json_object_get_type(item->jsobj))
{
case json_type_object:
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
}
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;
{
bool first = true;
struct match_item *item;
@@
-242,6
+249,9
@@
export_type(struct list_head *matches, const char *prefix)
if (!first)
printf("\\ ");
if (!first)
printf("\\ ");
+ if (limit-- <= 0)
+ break;
+
printf("%s", types[json_object_get_type(item->jsobj)]);
first = false;
}
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
}
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;
{
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':
switch (opt)
{
case 't':
- export_type(&matches, prefix);
+ export_type(&matches, prefix
, limit
);
break;
default:
break;
default:
- export_value(&matches, prefix, sep);
+ export_value(&matches, prefix, sep
, limit
);
break;
}
break;
}
@@
-366,12
+377,12
@@
out:
int main(int argc, char **argv)
{
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 = " ";
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)
{
{
switch (opt)
{
@@
-398,6
+409,10
@@
int main(int argc, char **argv)
separator = optarg;
break;
separator = optarg;
break;
+ case 'l':
+ limit = atoi(optarg);
+ break;
+
case 't':
case 'e':
if (!jsobj)
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;
rv = 1;
break;