Skip to content

Commit

Permalink
Remove remaining sprintf() uses, and deprecate proj_rtodms() by newly…
Browse files Browse the repository at this point in the history
… added proj_rtodms2() (#3431)

Fixes #3243
  • Loading branch information
rouault committed Nov 7, 2022
1 parent e3eee3b commit 5b9303d
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 33 deletions.
22 changes: 22 additions & 0 deletions docs/source/development/reference/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,30 @@ Various
Convert radians to string representation of degrees, minutes and seconds.
.. deprecated:: 9.2
Use :cpp:func:`proj_rtodms2` instead.
:param s: Buffer that holds the output string
:type s: `char *`
:param r: Value to convert to dms-representation
:type r: `double`
:param pos: Character denoting positive direction, typically `'N'` or `'E'`.
:type pos: `int`
:param neg: Character denoting negative direction, typically `'S'` or `'W'`.
:type neg: `int`
:returns: `char*` Pointer to output buffer (same as :c:data:`s`)
.. c:function:: char *proj_rtodms2(char *s, size_t sizeof_s, double r, int pos, int neg)
.. versionadded:: 9.2.0
Convert radians to string representation of degrees, minutes and seconds.
:param s: Buffer that holds the output string
:type s: `char *`
:param sizeof_s: Size of s buffer
:type sizeof_s: `size_t`
:param r: Value to convert to dms-representation
:type r: `double`
:param pos: Character denoting positive direction, typically `'N'` or `'E'`.
Expand Down
3 changes: 2 additions & 1 deletion scripts/reference_exported_symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,7 @@ proj_prime_meridian_get_parameters
proj_query_geodetic_crs_from_datum
proj_roundtrip
proj_rtodms
proj_rtodms2
proj_string_destroy
proj_string_list_destroy
proj_suggests_code_for
Expand All @@ -1066,5 +1067,5 @@ proj_unit_list_destroy
proj_uom_get_info_from_database
proj_xy_dist
proj_xyz_dist
rtodms(char*, double, int, int)
rtodms(char*, unsigned long, double, int, int)
set_rtodms(int, int)
9 changes: 8 additions & 1 deletion src/4D_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,14 @@ double proj_dmstor(const char *is, char **rs) {
}

char* proj_rtodms(char *s, double r, int pos, int neg) {
return rtodms(s, r, pos, neg);
// 40 is the size used for the buffer in proj.cpp
size_t arbitrary_size = 40;
return rtodms(s, arbitrary_size, r, pos, neg);
}

char * proj_rtodms2(char *s, size_t sizeof_s, double r, int pos, int neg)
{
return rtodms(s, sizeof_s, r, pos, neg);
}

/*************************************************************************************/
Expand Down
16 changes: 8 additions & 8 deletions src/apps/cs2cs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,22 +199,22 @@ static void process(FILE *fid)

if (destIsLatLong) {
if (reverseout) {
fputs(rtodms(pline, data.v, 'E', 'W'), stdout);
fputs(rtodms(pline, sizeof(pline), data.v, 'E', 'W'), stdout);
putchar('\t');
fputs(rtodms(pline, data.u, 'N', 'S'), stdout);
fputs(rtodms(pline, sizeof(pline), data.u, 'N', 'S'), stdout);
} else {
fputs(rtodms(pline, data.u, 'N', 'S'), stdout);
fputs(rtodms(pline, sizeof(pline), data.u, 'N', 'S'), stdout);
putchar('\t');
fputs(rtodms(pline, data.v, 'E', 'W'), stdout);
fputs(rtodms(pline, sizeof(pline), data.v, 'E', 'W'), stdout);
}
} else if (reverseout) {
fputs(rtodms(pline, data.v, 'N', 'S'), stdout);
fputs(rtodms(pline, sizeof(pline), data.v, 'N', 'S'), stdout);
putchar('\t');
fputs(rtodms(pline, data.u, 'E', 'W'), stdout);
fputs(rtodms(pline, sizeof(pline), data.u, 'E', 'W'), stdout);
} else {
fputs(rtodms(pline, data.u, 'E', 'W'), stdout);
fputs(rtodms(pline, sizeof(pline), data.u, 'E', 'W'), stdout);
putchar('\t');
fputs(rtodms(pline, data.v, 'N', 'S'), stdout);
fputs(rtodms(pline, sizeof(pline), data.v, 'N', 'S'), stdout);
}

} else { /* x-y or decimal degree ascii output */
Expand Down
14 changes: 7 additions & 7 deletions src/apps/geod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ printLL(double p, double l) {
(void)printf(oform, p * RAD_TO_DEG); TAB;
(void)printf(oform, l * RAD_TO_DEG);
} else {
(void)fputs(rtodms(pline, p, 'N', 'S'),stdout); TAB;
(void)fputs(rtodms(pline, l, 'E', 'W'),stdout);
(void)fputs(rtodms(pline, sizeof(pline), p, 'N', 'S'),stdout); TAB;
(void)fputs(rtodms(pline, sizeof(pline), l, 'E', 'W'),stdout);
}
}
static void
Expand Down Expand Up @@ -102,8 +102,8 @@ process(FILE *fid) {
(void)printf(oform, al21 * RAD_TO_DEG); TAB;
(void)printf(osform, geod_S * fr_meter);
} else {
(void)fputs(rtodms(pline, al12, 0, 0), stdout); TAB;
(void)fputs(rtodms(pline, al21, 0, 0), stdout); TAB;
(void)fputs(rtodms(pline, sizeof(pline), al12, 0, 0), stdout); TAB;
(void)fputs(rtodms(pline, sizeof(pline), al21, 0, 0), stdout); TAB;
(void)printf(osform, geod_S * fr_meter);
}
} else if (inverse)
Expand All @@ -112,16 +112,16 @@ process(FILE *fid) {
(void)printf(oform, al21 * RAD_TO_DEG); TAB;
(void)printf(osform, geod_S * fr_meter);
} else {
(void)fputs(rtodms(pline, al12, 0, 0), stdout); TAB;
(void)fputs(rtodms(pline, al21, 0, 0), stdout); TAB;
(void)fputs(rtodms(pline, sizeof(pline), al12, 0, 0), stdout); TAB;
(void)fputs(rtodms(pline, sizeof(pline), al21, 0, 0), stdout); TAB;
(void)printf(osform, geod_S * fr_meter);
}
else {
printLL(phi2, lam2); TAB;
if (oform)
(void)printf(oform, al21 * RAD_TO_DEG);
else
(void)fputs(rtodms(pline, al21, 0, 0), stdout);
(void)fputs(rtodms(pline, sizeof(pline), al21, 0, 0), stdout);
}
(void)fputs(s, stdout);
fflush(stdout);
Expand Down
14 changes: 7 additions & 7 deletions src/apps/proj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ static void process(FILE *fid) {
(void)fputs(oterr, stdout);
else if (inverse && !oform) { /*ascii DMS output */
if (reverseout) {
(void)fputs(rtodms(pline, data.uv.v, 'N', 'S'), stdout);
(void)fputs(rtodms(pline, sizeof(pline), data.uv.v, 'N', 'S'), stdout);
putchar('\t');
(void)fputs(rtodms(pline, data.uv.u, 'E', 'W'), stdout);
(void)fputs(rtodms(pline, sizeof(pline), data.uv.u, 'E', 'W'), stdout);
} else {
(void)fputs(rtodms(pline, data.uv.u, 'E', 'W'), stdout);
(void)fputs(rtodms(pline, sizeof(pline), data.uv.u, 'E', 'W'), stdout);
putchar('\t');
(void)fputs(rtodms(pline, data.uv.v, 'N', 'S'), stdout);
(void)fputs(rtodms(pline, sizeof(pline), data.uv.v, 'N', 'S'), stdout);
}
} else { /* x-y or decimal degree ascii output, scale if warranted by output units */
if (inverse) {
Expand Down Expand Up @@ -269,10 +269,10 @@ static void vprocess(FILE *fid) {
(void)fputs(s, stdout);

(void)fputs("Longitude: ", stdout);
(void)fputs(proj_rtodms(pline, dat_ll.lam, 'E', 'W'), stdout);
(void)fputs(proj_rtodms2(pline, sizeof(pline), dat_ll.lam, 'E', 'W'), stdout);
(void)printf(" [ %.11g ]\n", dat_ll.lam * RAD_TO_DEG);
(void)fputs("Latitude: ", stdout);
(void)fputs(proj_rtodms(pline, dat_ll.phi, 'N', 'S'), stdout);
(void)fputs(proj_rtodms2(pline, sizeof(pline), dat_ll.phi, 'N', 'S'), stdout);
(void)printf(" [ %.11g ]\n", dat_ll.phi * RAD_TO_DEG);
(void)fputs("Easting (x): ", stdout);
(void)printf(oform, dat_xy.x); putchar('\n');
Expand All @@ -284,7 +284,7 @@ static void vprocess(FILE *fid) {
(void)printf("Angular distortion (w): %.3f\n", facs.angular_distortion * RAD_TO_DEG);
(void)printf("Meridian/Parallel angle: %.5f\n", facs.meridian_parallel_angle * RAD_TO_DEG);
(void)printf("Convergence : ");
(void)fputs(proj_rtodms(pline, facs.meridian_convergence, 0, 0), stdout);
(void)fputs(proj_rtodms2(pline, sizeof(pline), facs.meridian_convergence, 0, 0), stdout);
(void)printf(" [ %.8f ]\n", facs.meridian_convergence * RAD_TO_DEG);
(void)printf("Max-min (Tissot axis a-b) scale error: %.5f %.5f\n\n", facs.tissot_semimajor, facs.tissot_semiminor);

Expand Down
3 changes: 2 additions & 1 deletion src/proj.h
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,8 @@ double PROJ_DLL proj_torad (double angle_in_degrees);
double PROJ_DLL proj_todeg (double angle_in_radians);

double PROJ_DLL proj_dmstor(const char *is, char **rs);
char PROJ_DLL * proj_rtodms(char *s, double r, int pos, int neg);
PROJ_DEPRECATED(char PROJ_DLL * proj_rtodms(char *s, double r, int pos, int neg), "Deprecated by proj_rtodms2");
char PROJ_DLL * proj_rtodms2(char *s, size_t sizeof_s, double r, int pos, int neg);

void PROJ_DLL proj_cleanup(void);

Expand Down
2 changes: 1 addition & 1 deletion src/proj_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ PJ *pj_projection_specific_setup_##name (PJ *P)
double PROJ_DLL dmstor(const char *, char **);
double dmstor_ctx(PJ_CONTEXT *ctx, const char *, char **);
void PROJ_DLL set_rtodms(int, int);
char PROJ_DLL *rtodms(char *, double, int, int);
char PROJ_DLL *rtodms(char *, size_t, double, int, int);
double PROJ_DLL adjlon(double);
double aacos(PJ_CONTEXT *,double);
double aasin(PJ_CONTEXT *,double);
Expand Down
10 changes: 5 additions & 5 deletions src/rtodms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ set_rtodms(int fract, int con_w) {
}
}
char *
rtodms(char *s, double r, int pos, int neg) {
rtodms(char *s, size_t sizeof_s, double r, int pos, int neg) {
int deg, min, sign;
char *ss = s;
double sec;
Expand All @@ -60,13 +60,13 @@ rtodms(char *s, double r, int pos, int neg) {
deg = (int)r;

if (dolong)
(void)sprintf(ss,format,deg,min,sec,sign);
(void)snprintf(ss,sizeof_s,format,deg,min,sec,sign);
else if (sec != 0.0) {
char *p, *q;
/* double prime + pos/neg suffix (if included) + NUL */
size_t suffix_len = sign ? 3 : 2;

(void)sprintf(ss,format,deg,min,sec,sign);
(void)snprintf(ss,sizeof_s,format,deg,min,sec,sign);
/* Replace potential decimal comma by decimal point for non C locale */
for( p = ss; *p != '\0'; ++p ) {
if( *p == ',' ) {
Expand All @@ -80,8 +80,8 @@ rtodms(char *s, double r, int pos, int neg) {
if (++q != p)
(void)memmove(p, q, suffix_len);
} else if (min)
(void)sprintf(ss,"%dd%d'%c",deg,min,sign);
(void)snprintf(ss,sizeof_s,"%dd%d'%c",deg,min,sign);
else
(void)sprintf(ss,"%dd%c",deg, sign);
(void)snprintf(ss,sizeof_s,"%dd%c",deg, sign);
return s;
}
4 changes: 2 additions & 2 deletions test/unit/gie_self_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,12 +440,12 @@ TEST(gie, info_functions) {
ASSERT_EQ(std::string(init_info.name), "epsg");

/* test proj_rtodms() and proj_dmstor() */
ASSERT_EQ(std::string("180dN"), proj_rtodms(buf, M_PI, 'N', 'S'));
ASSERT_EQ(std::string("180dN"), proj_rtodms2(buf, sizeof(buf), M_PI, 'N', 'S'));

ASSERT_EQ(proj_dmstor(&buf[0], NULL), M_PI);

ASSERT_EQ(std::string("114d35'29.612\"S"),
proj_rtodms(buf, -2.0, 'N', 'S'));
proj_rtodms2(buf, sizeof(buf), -2.0, 'N', 'S'));

/* we can't expect perfect numerical accuracy so testing with a tolerance */
ASSERT_NEAR(-2.0, proj_dmstor(&buf[0], NULL), 1e-7);
Expand Down

0 comments on commit 5b9303d

Please sign in to comment.