From d85129e658c74b8cd98b75d5dd94808295da10ad Mon Sep 17 00:00:00 2001 From: Kristian Evers Date: Thu, 6 Sep 2018 21:37:37 +0200 Subject: [PATCH] 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 | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/cct.c b/src/cct.c index 7a53beeb60..85d3dc42da 100644 --- a/src/cct.c +++ b/src/cct.c @@ -206,7 +206,7 @@ int main(int argc, char **argv) { PJ_COORD point; PJ_PROJ_INFO info; OPTARGS *o; - char *buf; + char *buf, *comment; int nfields = 4, direction = 1, skip_lines = 0, verbose; double fixed_z = HUGE_VAL, fixed_time = HUGE_VAL; int decimals_angles = 10; @@ -268,7 +268,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,6 +275,9 @@ int main(int argc, char **argv) { } if (opt_given (o, "c")) { + /* reset colum numbers to ease comment output later on */ + for (int 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); if (ncols != nfields) { @@ -371,14 +373,35 @@ int main(int argc, char **argv) { } proj_errno_restore (P, err); + /* handle comment string */ + if (opt_given(o, "c")) { + /* what number is the last coordinate column in the input data? */ + int colmax = 0; + for (int i=0; i<4; i++) + colmax = MAX(colmax, columns_xyzt[i]); + comment = column(buf, colmax+1); + } else { + comment = column(buf, nfields+1); + } + /* 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\n", + decimals_angles, point.xyzt.x, + decimals_angles, point.xyzt.y, + decimals_distances, point.xyzt.z, + point.xyzt.t, 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\n", + decimals_distances, point.xyzt.x, + decimals_distances, point.xyzt.y, + decimals_distances, point.xyzt.z, + point.xyzt.t, comment + ); } if (stdout != fout)