From 4f961f5d87ba998c19e166de4d077016b0f24e31 Mon Sep 17 00:00:00 2001 From: Kristian Evers Date: Thu, 6 Sep 2018 21:37:37 +0200 Subject: cct: forward comments to output Any text written after the coordinate input will automatically be forwarded to the output stream. Text in columns before the coordinate input is discarded in the output. This works for any combination of -c, -t and -z parameters: $ echo 12 56 100 2018.0 comment comment | cct +proj=merc 1335833.8895 7522963.2411 100.0000 2018.0000 comment commen $ echo text 12 56 100 2018.0 comment | cct -c 2,3,4,5 +proj=merc 1335833.8895 7522963.2411 100.0000 2018.0000 comment $ echo text 12 56 comment | cct -c 2,3 -t0 -z0 +proj=merc 1335833.8895 7522963.2411 0.0000 0.0000 comment $ echo 12 56 comment | cct -t0 -z0 +proj=merc 1335833.8895 7522963.2411 0.0000 0.0000 comment Closes #918 --- src/cct.c | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/cct.c b/src/cct.c index 7a53beeb..7d02eaac 100644 --- a/src/cct.c +++ b/src/cct.c @@ -206,8 +206,12 @@ int main(int argc, char **argv) { PJ_COORD point; PJ_PROJ_INFO info; OPTARGS *o; + char blank_comment[] = ""; + char whitespace[] = " "; + char *comment; + char *comment_delimiter; char *buf; - int nfields = 4, direction = 1, skip_lines = 0, verbose; + int i, nfields = 4, direction = 1, skip_lines = 0, verbose; double fixed_z = HUGE_VAL, fixed_time = HUGE_VAL; int decimals_angles = 10; int decimals_distances = 4; @@ -268,7 +272,6 @@ int main(int argc, char **argv) { int dec = atoi (opt_arg (o, "d")); decimals_angles = dec; decimals_distances = dec; - nfields--; } if (opt_given (o, "s")) { @@ -276,8 +279,13 @@ int main(int argc, char **argv) { } if (opt_given (o, "c")) { + int ncols; + /* reset colum numbers to ease comment output later on */ + for (i=0; i<4; i++) + columns_xyzt[i] = 0; + /* cppcheck-suppress invalidscanf */ - int ncols = sscanf (opt_arg (o, "c"), "%d,%d,%d,%d", columns_xyzt, columns_xyzt+1, columns_xyzt+2, columns_xyzt+3); + ncols = sscanf (opt_arg (o, "c"), "%d,%d,%d,%d", columns_xyzt, columns_xyzt+1, columns_xyzt+2, columns_xyzt+3); if (ncols != nfields) { print (PJ_LOG_ERROR, "%s: Too few input columns given: '%s'\n", o->progname, opt_arg (o, "c")); free (o); @@ -371,14 +379,35 @@ int main(int argc, char **argv) { } proj_errno_restore (P, err); + /* handle comment string */ + comment = column(buf, nfields+1); + if (opt_given(o, "c")) { + /* what number is the last coordinate column in the input data? */ + int colmax = 0; + for (i=0; i<4; i++) + colmax = MAX(colmax, columns_xyzt[i]); + comment = column(buf, colmax+1); + } + comment_delimiter = (comment && *comment) ? whitespace : blank_comment; + /* Time to print the result */ if (proj_angular_output (P, direction)) { point.lpzt.lam = proj_todeg (point.lpzt.lam); point.lpzt.phi = proj_todeg (point.lpzt.phi); - print (PJ_LOG_NONE, "%14.*f %14.*f %12.*f %12.4f\n", decimals_angles, point.xyzt.x, decimals_angles, point.xyzt.y, decimals_distances, point.xyzt.z, point.xyzt.t); + print (PJ_LOG_NONE, "%14.*f %14.*f %12.*f %12.4f%s%s\n", + decimals_angles, point.xyzt.x, + decimals_angles, point.xyzt.y, + decimals_distances, point.xyzt.z, + point.xyzt.t, comment_delimiter, comment + ); } else - print (PJ_LOG_NONE, "%13.*f %13.*f %12.*f %12.4f\n", decimals_distances, point.xyzt.x, decimals_distances, point.xyzt.y, decimals_distances, point.xyzt.z, point.xyzt.t); + print (PJ_LOG_NONE, "%13.*f %13.*f %12.*f %12.4f%s%s\n", + decimals_distances, point.xyzt.x, + decimals_distances, point.xyzt.y, + decimals_distances, point.xyzt.z, + point.xyzt.t, comment_delimiter, comment + ); } if (stdout != fout) -- cgit v1.2.3