Skip to content

Commit

Permalink
beecut: added --newline to print each element on a seperate line
Browse files Browse the repository at this point in the history
  • Loading branch information
mariux committed Jul 7, 2011
1 parent 253b7df commit 8d3a16c
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/beecut/beecut.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#define OPT_SHORT 's'
#define OPT_PREPEND 'p'
#define OPT_APPEND 'a'
#define OPT_NEWLINE 'n'
#define OPT_VERSION 128
#define OPT_HELP 129

Expand All @@ -49,7 +50,8 @@ void print_full_usage(void)

printf(" -d | --delimeter <char> specify the delimeter character\n\n");

printf(" -s | --short output short elements\n\n");
printf(" -s | --short output short elements\n");
printf(" -n | --newline output each element on a seperate line\n\n");

printf(" -p | --prepend <string> prepend <string> to each output element\n");
printf(" -a | --append <string> append <string> to each output element\n\n");
Expand All @@ -61,17 +63,20 @@ void print_full_usage(void)
printf(" beecut -s -d '-' a-b-c will print 'a-b-c a b c'\n\n");
}

void cut_and_print(char *string, char delimeter, char opt_short, char *prefix, char *suffix)
void cut_and_print(char *string, char delimeter, char opt_short, char opt_newline, char *prefix, char *suffix)
{
char *p, *s;
char nl;

nl = opt_newline ? '\n' : ' ';

p = s = string;

printf("%s%s", prefix, string);

while((p=strchr(p, delimeter))) {
if(s < p) /* only print space if we have something to print */
printf("%s %s", suffix, prefix);
printf("%s%c%s", suffix, nl, prefix);

while(s < p)
putchar(*(s++));
Expand All @@ -81,7 +86,7 @@ void cut_and_print(char *string, char delimeter, char opt_short, char *prefix, c
s = (opt_short) ? p : string;
}

printf("%s %s%s%s\n", suffix, prefix, s, suffix);
printf("%s%c%s%s%s\n", suffix, nl, prefix, s, suffix);
}

int main(int argc, char *argv[])
Expand All @@ -91,7 +96,8 @@ int main(int argc, char *argv[])

char delimeter = '.';

char opt_short = 0;
char opt_short = 0;
char opt_newline = 0;

char *opt_prepend = "";
char *opt_append = opt_prepend;
Expand All @@ -103,14 +109,15 @@ int main(int argc, char *argv[])
{"append", required_argument, 0, OPT_APPEND},

{"short", no_argument, 0, OPT_SHORT},
{"newline", no_argument, 0, OPT_NEWLINE},

{"version", no_argument, 0, OPT_VERSION},
{"help", no_argument, 0, OPT_HELP},

{0, 0, 0, 0}
};

while ((c = getopt_long_only(argc, argv, "p:a:d:s", long_options, &option_index)) != -1) {
while ((c = getopt_long_only(argc, argv, "p:a:d:sn", long_options, &option_index)) != -1) {

switch (c) {
case OPT_DELIMETER:
Expand All @@ -133,6 +140,10 @@ int main(int argc, char *argv[])
opt_short = 1;
break;

case OPT_NEWLINE:
opt_newline = 1;
break;

case OPT_HELP:
printf("\n");
print_version();
Expand All @@ -152,8 +163,8 @@ int main(int argc, char *argv[])
}

while(optind < argc)
cut_and_print(argv[optind++], delimeter, opt_short, opt_prepend, opt_append);

cut_and_print(argv[optind++], delimeter, opt_short,
opt_newline, opt_prepend, opt_append);

return(0);
}

0 comments on commit 8d3a16c

Please sign in to comment.