Skip to content

Commit

Permalink
v.to.db: add h-flag to write header (#3401)
Browse files Browse the repository at this point in the history
* add h-flag

* Update parse.c

* Update report.c

* Update global.h

* Remove whitespaces

---------

Co-authored-by: Huidae Cho <grass4u@gmail.com>
  • Loading branch information
ninsbl and HuidaeCho committed Feb 16, 2024
1 parent 25bc70d commit 3f03909
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
7 changes: 4 additions & 3 deletions vector/v.to.db/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ struct options {
char *qcol;
int type;
int option;
int print; /* print only */
int sql; /* print only sql statements */
int total; /* print totals */
int print; /* print only */
int print_header; /* print header for print and total */
int sql; /* print only sql statements */
int total; /* print totals */
int units;
int qfield; /* query field */
char *fs;
Expand Down
10 changes: 9 additions & 1 deletion vector/v.to.db/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int parse_command_line(int argc, char *argv[])
struct Option *fs;
} parms;
struct {
struct Flag *p, *s, *t;
struct Flag *h, *p, *s, *t;
} flags;
char *desc;

Expand Down Expand Up @@ -122,6 +122,11 @@ int parse_command_line(int argc, char *argv[])
flags.p->guisection = _("Print");
flags.p->suppress_required = YES;

flags.h = G_define_flag();
flags.h->key = 'h';
flags.h->description = _("Print header");
flags.h->guisection = _("Print");

flags.s = G_define_flag();
flags.s->key = 's';
flags.s->description = _("Only print SQL statements");
Expand All @@ -134,6 +139,8 @@ int parse_command_line(int argc, char *argv[])
flags.t->guisection = _("Print");
flags.t->suppress_required = YES;

G_option_requires(flags.h, flags.p, flags.t, NULL);

if (G_parser(argc, argv))
exit(EXIT_FAILURE);

Expand All @@ -146,6 +153,7 @@ int parse_command_line(int argc, char *argv[])
parms.option->key, parms.option->description);

options.print = flags.p->answer;
options.print_header = flags.h->answer;
options.sql = flags.s->answer;
options.total = flags.t->answer;

Expand Down
30 changes: 15 additions & 15 deletions vector/v.to.db/report.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

int report(void)
{
int i;
int i, print_header = G_verbose() > G_verbose_min() || options.print_header;
char left[20], right[20];

if (!options.print &&
Expand All @@ -17,15 +17,15 @@ int report(void)

switch (options.option) {
case O_CAT:
if (G_verbose() > G_verbose_min())
if (print_header)
fprintf(stdout, "cat\n");
for (i = 0; i < vstat.rcat; i++)
fprintf(stdout, "%d\n", Values[i].cat);
break;

case O_COUNT:
if (options.print) {
if (G_verbose() > G_verbose_min())
if (print_header)
fprintf(stdout, "cat%scount\n", options.fs);
for (i = 0; i < vstat.rcat; i++)
fprintf(stdout, "%d%s%d\n", Values[i].cat, options.fs,
Expand All @@ -43,7 +43,7 @@ int report(void)

case O_AREA:
if (options.print) {
if (G_verbose() > G_verbose_min())
if (print_header)
fprintf(stdout, "cat%sarea\n", options.fs);
for (i = 0; i < vstat.rcat; i++)
fprintf(stdout, "%d%s%.15g\n", Values[i].cat, options.fs,
Expand All @@ -62,7 +62,7 @@ int report(void)
case O_COMPACT:
/* perimeter / perimeter of equivalent circle
* perimeter of equivalent circle: 2.0 * sqrt(M_PI * area) */
if (G_verbose() > G_verbose_min())
if (print_header)
fprintf(stdout, "cat%scompact\n", options.fs);
for (i = 0; i < vstat.rcat; i++) {
Values[i].d1 = Values[i].d2 / (2.0 * sqrt(M_PI * Values[i].d1));
Expand All @@ -82,7 +82,7 @@ int report(void)
*
* avoid division by zero:
* 2.0 * log(1 + perimeter) / log(1 + area) */
if (G_verbose() > G_verbose_min())
if (print_header)
fprintf(stdout, "cat%sfd\n", options.fs);
for (i = 0; i < vstat.rcat; i++) {
if (Values[i].d1 == 1) /* log(1) == 0 */
Expand All @@ -94,15 +94,15 @@ int report(void)
break;

case O_PERIMETER:
if (G_verbose() > G_verbose_min())
if (print_header)
fprintf(stdout, "cat%sperimeter\n", options.fs);
for (i = 0; i < vstat.rcat; i++)
fprintf(stdout, "%d%s%.15g\n", Values[i].cat, options.fs,
Values[i].d1);
break;

case O_BBOX:
if (G_verbose() > G_verbose_min())
if (print_header)
fprintf(stdout, "cat%sN%sS%sE%sW\n", options.fs, options.fs,
options.fs, options.fs);
for (i = 0; i < vstat.rcat; i++) {
Expand All @@ -114,7 +114,7 @@ int report(void)

case O_LENGTH:
if (options.print) {
if (G_verbose() > G_verbose_min())
if (print_header)
fprintf(stdout, "cat%slength\n", options.fs);
for (i = 0; i < vstat.rcat; i++)
fprintf(stdout, "%d%s%.15g\n", Values[i].cat, options.fs,
Expand All @@ -130,15 +130,15 @@ int report(void)
}
break;
case O_SLOPE:
if (G_verbose() > G_verbose_min())
if (print_header)
fprintf(stdout, "cat%sslope\n", options.fs);
for (i = 0; i < vstat.rcat; i++)
fprintf(stdout, "%d%s%.15g\n", Values[i].cat, options.fs,
Values[i].d1);

break;
case O_SINUOUS:
if (G_verbose() > G_verbose_min())
if (print_header)
fprintf(stdout, "cat%ssinuous\n", options.fs);
for (i = 0; i < vstat.rcat; i++)
fprintf(stdout, "%d%s%.15g\n", Values[i].cat, options.fs,
Expand All @@ -147,7 +147,7 @@ int report(void)
case O_COOR:
case O_START:
case O_END:
if (G_verbose() > G_verbose_min())
if (print_header)
fprintf(stdout, "cat%sx%sy%sz\n", options.fs, options.fs,
options.fs);
for (i = 0; i < vstat.rcat; i++) {
Expand All @@ -159,7 +159,7 @@ int report(void)
break;

case O_SIDES:
if (G_verbose() > G_verbose_min())
if (print_header)
fprintf(stdout, "cat%sleft%sright\n", options.fs, options.fs);
for (i = 0; i < vstat.rcat; i++) {
if (Values[i].count1 == 1) {
Expand Down Expand Up @@ -197,7 +197,7 @@ int report(void)
break;

case O_QUERY:
if (G_verbose() > G_verbose_min())
if (print_header)
fprintf(stdout, "cat%squery\n", options.fs);
for (i = 0; i < vstat.rcat; i++) {
if (Values[i].null) {
Expand All @@ -222,7 +222,7 @@ int report(void)
}
break;
case O_AZIMUTH:
if (G_verbose() > G_verbose_min())
if (print_header)
fprintf(stdout, "cat%sazimuth\n", options.fs);
for (i = 0; i < vstat.rcat; i++)
fprintf(stdout, "%d%s%.15g\n", Values[i].cat, options.fs,
Expand Down

0 comments on commit 3f03909

Please sign in to comment.