commit 3c8ee4956b0ec61260b10328e5411d4559e301c9
parent 8eb91720318adc1a4f331b22936b3461917af203
Author: Robin <kroekerrobin@gmail.com>
Date: Thu, 17 Aug 2023 08:02:01 +0200
Add --limit argument
Diffstat:
3 files changed, 17 insertions(+), 1 deletion(-)
diff --git a/htex.c b/htex.c
@@ -149,12 +149,14 @@ int main(int argc, char *argv[])
bool isExcept = false;
char *text = NULL;
char *searchPattern = NULL;
+ int limit = -1;
static struct option long_options[] = {
{ "output", required_argument, 0, 'o' },
{ "except", no_argument, 0, 'e' },
+ { "limit", required_argument, 0, 'l' },
{ 0, 0, 0, 0 }
};
- while ((o = getopt_long(argc, argv, "o:e", long_options, &option_index)) != -1) {
+ while ((o = getopt_long(argc, argv, "o:el:", long_options, &option_index)) != -1) {
switch(o) {
case 'o':
output = realloc(output, (strlen(optarg)+1) * sizeof(char));
@@ -163,6 +165,9 @@ int main(int argc, char *argv[])
case 'e':
isExcept = true;
break;
+ case 'l':
+ limit = atoi(optarg);
+ break;
}
}
enum output_type out = parseOutputArg(output);
@@ -172,6 +177,12 @@ int main(int argc, char *argv[])
free(output);
return -1;
}
+ if (limit == 0)
+ {
+ fprintf(stderr, "Provide a valid limit value.\n");
+ free(output);
+ return -1;
+ }
if (argc == optind)
{
fprintf(stderr, "Provide a search pattern!\n");
@@ -210,6 +221,7 @@ int main(int argc, char *argv[])
struct find_opts *options = parseFilterOpts(searchPattern);
options->out = out;
options->isExcept = isExcept;
+ options->limit = limit;
filterHtml(text, options);
free(output);
freeOpts(options);
diff --git a/html.c b/html.c
@@ -592,6 +592,8 @@ void freeTagList(struct tag_list *t)
void findTag(struct tag *tag, struct find_opts *opt, struct tag_list *foundTags)
{
+ if (opt->limit > 0 && foundTags->len == opt->limit)
+ return;
bool matchesTag = false;
bool matchesAttrKey = false;
bool matchesAttrValue = false;
@@ -776,6 +778,7 @@ void filterHtml(char *text, struct find_opts *opts)
}
else
findTag(rootTag, opts, foundTags);
+ // printf("len: %ld\n", foundTags->len);
printResult(text, rootTag, opts, foundTags);
freeTag(rootTag);
freeTagList(tagList);
diff --git a/html.h b/html.h
@@ -35,6 +35,7 @@ struct find_opts
char *key;
enum output_type out;
bool isExcept;
+ int limit;
};
struct attr