Skip to content

Commit

Permalink
WIP: Removed last use of sprintf, but it required changing a public API
Browse files Browse the repository at this point in the history
  • Loading branch information
seanm committed May 1, 2023
1 parent 578180f commit c02e58c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
3 changes: 2 additions & 1 deletion include/nctime.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ MSC_NCTIME_EXTRA extern void Cde2h(double etime, CdTimeType timeType, long baseY
MSC_NCTIME_EXTRA extern int cdParseRelunits(cdCalenType timetype, char* relunits, cdUnitTime* unit, cdCompTime* base_comptime);
MSC_NCTIME_EXTRA extern int cdSetErrOpts(int opts);
#else
extern void cdRel2Iso(cdCalenType timetype, char* relunits, int separator, double reltime, char* chartime);
extern void cdRel2IsoNew(cdCalenType timetype, char* relunits, int separator, double reltime, char* chartime, size_t chartime_size);
extern void cdRel2Iso(cdCalenType timetype, char* relunits, int separator, double reltime, char* chartime); // deprecated
extern void cdChar2Comp(cdCalenType timetype, char* chartime, cdCompTime* comptime);
extern void Cdh2e(CdTime *htime, double *etime);
extern void Cde2h(double etime, CdTimeType timeType, long baseYear, CdTime *htime);
Expand Down
32 changes: 19 additions & 13 deletions libdispatch/nctime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ cdRel2Comp(cdCalenType timetype, char* relunits, double reltime, cdCompTime* com

/* rkr: output as ISO 8601 strings */
static void
cdComp2Iso(cdCalenType timetype, int separator, cdCompTime comptime, char* time)
cdComp2Iso(cdCalenType timetype, int separator, cdCompTime comptime, char* time, size_t time_size)
{
double dtmp, sec;
int ihr, imin, isec;
Expand Down Expand Up @@ -1121,47 +1121,47 @@ cdComp2Iso(cdCalenType timetype, int separator, cdCompTime comptime, char* time)
if(timetype & cdStandardCal){
switch (nskip) {
case 0: /* sec != 0 && (int)sec != sec */
sprintf(time,"%4.4ld-%2.2hd-%2.2hd%c%2.2d:%2.2d:%lf",
snprintf(time,time_size,"%4.4ld-%2.2hd-%2.2hd%c%2.2d:%2.2d:%lf",
comptime.year,comptime.month,comptime.day,separator,ihr,imin,sec);
break;
case 1:
sprintf(time,"%4.4ld-%2.2hd-%2.2hd%c%2.2d:%2.2d:%2.2d",
snprintf(time,time_size,"%4.4ld-%2.2hd-%2.2hd%c%2.2d:%2.2d:%2.2d",
comptime.year,comptime.month,comptime.day,separator,ihr,imin,isec);
break;
case 2:
sprintf(time,"%4.4ld-%2.2hd-%2.2hd%c%2.2d:%2.2d",
snprintf(time,time_size,"%4.4ld-%2.2hd-%2.2hd%c%2.2d:%2.2d",
comptime.year,comptime.month,comptime.day,separator,ihr,imin);
break;
case 3:
sprintf(time,"%4.4ld-%2.2hd-%2.2hd%c%2.2d",
snprintf(time,time_size,"%4.4ld-%2.2hd-%2.2hd%c%2.2d",
comptime.year,comptime.month,comptime.day,separator,ihr);
break;
case 4:
sprintf(time,"%4.4ld-%2.2hd-%2.2hd",
snprintf(time,time_size,"%4.4ld-%2.2hd-%2.2hd",
comptime.year,comptime.month,comptime.day);
break;
}
}
else { /* Climatological */
switch (nskip) {
case 0: /* sec != 0 && (int)sec != sec */
sprintf(time,"%2.2hd-%2.2hd%c%2.2d:%2.2d:%lf",
snprintf(time,time_size,"%2.2hd-%2.2hd%c%2.2d:%2.2d:%lf",
comptime.month,comptime.day,separator,ihr,imin,sec);
break;
case 1:
sprintf(time,"%2.2hd-%2.2hd%c%2.2d:%2.2d:%2.2d",
snprintf(time,time_size,"%2.2hd-%2.2hd%c%2.2d:%2.2d:%2.2d",
comptime.month,comptime.day,separator,ihr,imin,isec);
break;
case 2:
sprintf(time,"%2.2hd-%2.2hd%c%2.2d:%2.2d",
snprintf(time,time_size,"%2.2hd-%2.2hd%c%2.2d:%2.2d",
comptime.month,comptime.day,separator,ihr,imin);
break;
case 3:
sprintf(time,"%2.2hd-%2.2hd%c%2.2d",
snprintf(time,time_size,"%2.2hd-%2.2hd%c%2.2d",
comptime.month,comptime.day,separator,ihr);
break;
case 4:
sprintf(time,"%2.2hd-%2.2hd",
snprintf(time,time_size,"%2.2hd-%2.2hd",
comptime.month,comptime.day);
break;
}
Expand All @@ -1171,16 +1171,22 @@ cdComp2Iso(cdCalenType timetype, int separator, cdCompTime comptime, char* time)

/* rkr: added for output closer to ISO 8601 */
void
cdRel2Iso(cdCalenType timetype, char* relunits, int separator, double reltime, char* chartime)
cdRel2IsoNew(cdCalenType timetype, char* relunits, int separator, double reltime, char* chartime, size_t chartime_size)
{
cdCompTime comptime;

cdRel2Comp(timetype, relunits, reltime, &comptime);
cdComp2Iso(timetype, separator, comptime, chartime);
cdComp2Iso(timetype, separator, comptime, chartime, chartime_size);

return;
}

void
cdRel2Iso(cdCalenType timetype, char* relunits, int separator, double reltime, char* chartime)
{
cdRel2IsoNew(timetype, relunits, separator, reltime, chartime, SIZE_MAX);
}

int
cdSetErrOpts(int opts)
{
Expand Down
2 changes: 1 addition & 1 deletion ncdump/dumplib.c
Original file line number Diff line number Diff line change
Expand Up @@ -1272,7 +1272,7 @@ nctime_val_tostring(const ncvar_t *varp, safebuf_t *sfbf, const void *valp) {
oldopts = cdSetErrOpts(0);
newopts = oldopts | CU_VERBOSE;
cdSetErrOpts(newopts);
cdRel2Iso(varp->timeinfo->calendar, varp->timeinfo->units, separator, vv, &sout[1], sizeof(sout) - 1);
cdRel2IsoNew(varp->timeinfo->calendar, varp->timeinfo->units, separator, vv, &sout[1], sizeof(sout) - 1);
cdSetErrOpts(oldopts);
res = strlen(sout);
sout[res++] = '"';
Expand Down

0 comments on commit c02e58c

Please sign in to comment.