Skip to content

Instantly share code, notes, and snippets.

@syohex
Created February 16, 2011 13:34
Show Gist options
  • Save syohex/829370 to your computer and use it in GitHub Desktop.
Save syohex/829370 to your computer and use it in GitHub Desktop.
saryに表示件数を指定できるオプションを追加
diff --git a/src/sary.c b/src/sary.c
index 22f997d..874359f 100644
--- a/src/sary.c
+++ b/src/sary.c
@@ -112,6 +112,9 @@ static gchar* start_tag = NULL;
static gchar* end_tag = NULL;
static gchar* array_name = NULL;
+#define SEARCH_INFINITE -1
+static SaryInt entry_num = 1000; /* default is 1000 entry */
+
int
main (int argc, char **argv)
{
@@ -253,6 +256,10 @@ grep_normal (SarySearcher *searcher, const gchar *pattern)
sep = separator;
sep2 = separator2;
i++;
+
+ if (entry_num != SEARCH_INFINITE && i >= entry_num) {
+ break;
+ }
}
if (i > 1) {
if (sep2) g_print("%s", sep2);
@@ -289,10 +296,11 @@ sort_lexicographical (SarySearcher *searcher)
}
-static const char *short_options = "a:ce:hils:vA:B:C::p";
+static const char *short_options = "a:cn:e:hils:vA:B:C::p";
static struct option long_options[] = {
{ "array", required_argument, NULL, 'a' },
{ "count", no_argument, NULL, 'c' },
+ { "num", required_argument, NULL, 'n' },
{ "end", required_argument, NULL, 'e' },
{ "help", no_argument, NULL, 'h' },
{ "ignore-case", no_argument, NULL, 'i' },
@@ -312,6 +320,7 @@ show_help (void)
g_print("\
Usage: sary [OPTION]... PATTERN FILE\n\
-c, --count only print the number of occurrences\n\
+ -n, --num=NUM print NUM entry (default 1000), -1 is no limitation\n\
-i, --ignore-case ignore case distinctions\n\
-l, --lexicographical sort in lexicographical order\n\
-A, --after-context=NUM print NUM lines of trailing context\n\
@@ -368,6 +377,11 @@ parse_options (int argc, char **argv)
case 'c':
grep_mode = "count";
break;
+ case 'n':
+ if (ck_atoi(optarg, &entry_num)) {
+ g_printerr("sary: invalid entry number argument\n");
+ exit(EXIT_FAILURE);
+ }
case 'i':
icase_p = 1;
search = sary_searcher_icase_search;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment