Skip to content

Commit

Permalink
Began removing sprintf...
Browse files Browse the repository at this point in the history
  • Loading branch information
RoryBarnes committed Apr 24, 2024
1 parent 4423e7a commit 6cb2379
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/atmesc.c
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ void InitializeOptionsAtmEsc(OPTIONS *options, fnReadOption fnRead[]) {
options[OPT_ATMXABSEFFH2OMODEL].iType = 3;
options[OPT_ATMXABSEFFH2OMODEL].bMultiFile = 1;
fnRead[OPT_ATMXABSEFFH2OMODEL] = &ReadAtmXAbsEffH2OModel;
sprintf(options[OPT_ATMXABSEFFH2OMODEL].cLongDescr,
fvFormattedString(&options[OPT_ATMXABSEFFH2OMODEL].cLongDescr,
"If BOLMONT16 is selected, then the value of %s will follow the "
"model of\n"
"Bolmont et al. (2017, MNRAS, 464, 3728). NONE will not change the "
Expand Down
27 changes: 27 additions & 0 deletions src/control.c
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,33 @@ void fprintd(FILE *fp, double x, int iExp, int iDig) {
}
}

void fvFormattedString(char **sString,const char* sFormattedString, ...) {
va_list vaArgs;
va_start(vaArgs, sFormattedString);

va_list vaArgsCopy;
va_copy(vaArgsCopy, vaArgs);

// Determine the length of the formatted string
int iString = vsnprintf(NULL, 0, sFormattedString, vaArgs);

// Allocate memory for the string
*sString = (char*)malloc((iString + 1) * sizeof(char));
if (*sString == NULL) {
va_end(vaArgs);
va_end(vaArgsCopy);
fprintf(stderr,"ERROR: Unable to create formatted string in functionfvWriteString.\n");
exit(EXIT_EXE);
}

// Fill the string with the formatted data using the copied va_list
vsnprintf(*sString, iString + 1, sFormattedString, vaArgsCopy);

va_end(vaArgs); // Clean up the original va_list
va_end(vaArgsCopy); // Clean up the copied va_list
}


/*
* Unit Conversions
*/
Expand Down
1 change: 1 addition & 0 deletions src/control.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void Help(OPTIONS *, OUTPUT *, char[], int);
void LineExit(char[], int);
char *sLower(char[]);
void fprintd(FILE *, double, int, int);
void fvFormattedString(char**,const char*, ...);

double fdUnitsLength(int);
double fdUnitsTime(int);
Expand Down
1 change: 1 addition & 0 deletions src/vplanet.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
Expand Down

0 comments on commit 6cb2379

Please sign in to comment.