Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing sprintf instances with std::ostringstream functionality #35

Closed
wants to merge 7 commits into from
16 changes: 13 additions & 3 deletions src/CartBlock.C
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include <cstring>
#include <stdexcept>
#include <string>
#include <sstream>
#include "TiogaMeshInfo.h"
#include "codetypes.h"
#include "CartBlock.h"
Expand Down Expand Up @@ -686,7 +689,6 @@ void CartBlock::writeCellFile(int bid)
int ibmin,ibmax;
char fname[80];
char qstr[2];
char intstring[12];
char hash,c;
int i,n,j,k,ibindex;
int bodytag;
Expand All @@ -700,8 +702,16 @@ void CartBlock::writeCellFile(int bid)
ibmax=-30000000;
nnodes=(dims[1]+1)*(dims[0]+1)*(dims[2]+1);
ncells=dims[0]*dims[1]*dims[2];
sprintf(intstring,"%d",100000+myid);
sprintf(fname,"cart_cell%s.dat",&(intstring[1]));

std::ostringstream ss;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm we go from 2 lines of code to 8. Kinda too bad. Is there a way to consolidate all this? Make a helper function? Inline some things instead of creating new variables? Or maybe we need to keep sprintf. Or skip the stream thing and just cast straight to string?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is snprintf an option?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced sprintf with snprintf in PR #34 (#34), but I was still getting errors.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm we go from 2 lines of code to 8. Kinda too bad. Is there a way to consolidate all this? Make a helper function? Inline some things instead of creating new variables? Or maybe we need to keep sprintf. Or skip the stream thing and just cast straight to string?

I could write a function that accepts a name root, a number, and a file type, and then returns the full file name. That's probably the way to go to make this tidier.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it seems like a lot of the same things. A function would be nice.

std::ostringstream snum;
std::string strnum;
snum <<100000+myid;
strnum=snum.str();
ss<<"cart_cell"<<strnum.substr(1,5)<<".dat";
std::string buf_str=ss.str();;
std::strcpy(fname,buf_str.c_str());

if (bid==0)
{
fp=fopen(fname,"w");
Expand Down
74 changes: 57 additions & 17 deletions src/MeshBlock.C
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include <cstring>
#include <stdexcept>
#include <string>
#include <sstream>
#include "codetypes.h"
#include "MeshBlock.h"
#include "tioga.h"
Expand Down Expand Up @@ -643,16 +645,22 @@ void MeshBlock::tagBoundaryFaces(void){
void MeshBlock::writeGridFile(int bid)
{
char fname[80];
char intstring[12];
char hash,c;
int i,n,j;
int bodytag;
FILE *fp;
int ba;
int nvert;

sprintf(intstring,"%d",100000+bid);
sprintf(fname,"part%s.dat",&(intstring[1]));
std::ostringstream ss;
std::ostringstream snum;
std::string strnum;
snum <<100000+bid;
strnum=snum.str();
ss<<"part"<<strnum.substr(1,5)<<".dat";
std::string buf_str=ss.str();;
std::strcpy(fname,buf_str.c_str());

fp=fopen(fname,"w");
fprintf(fp,"TITLE =\"Tioga output\"\n");
fprintf(fp,"VARIABLES=\"X\",\"Y\",\"Z\",\"IBLANK\"\n");
Expand Down Expand Up @@ -727,16 +735,22 @@ void MeshBlock::writeCellFile(int bid)
{
char fname[80];
char qstr[3];
char intstring[12];
char hash,c;
int i,n,j;
int bodytag;
FILE *fp;
int ba;
int nvert;

sprintf(intstring,"%d",100000+bid);
sprintf(fname,"cell%s.dat",&(intstring[1]));
std::ostringstream ss;
std::ostringstream snum;
std::string strnum;
snum <<100000+bid;
strnum=snum.str();
ss<<"cell"<<strnum.substr(1,5)<<".dat";
std::string buf_str=ss.str();;
std::strcpy(fname,buf_str.c_str());

fp=fopen(fname,"w");
fprintf(fp,"TITLE =\"Tioga output\"\n");
fprintf(fp,"VARIABLES=\"X\",\"Y\",\"Z\",\"IBLANK\",\"IBLANK_CELL\" ");
Expand Down Expand Up @@ -813,7 +827,6 @@ void MeshBlock::writeFlowFile(int bid,double *q,int nvar,int type)
{
char fname[80];
char qstr[12];
char intstring[12];
char hash,c;
int i,n,j;
int bodytag;
Expand All @@ -834,15 +847,28 @@ void MeshBlock::writeFlowFile(int bid,double *q,int nvar,int type)
ibl=iblank;
}
//
sprintf(intstring,"%d",100000+bid);
sprintf(fname,"flow%s.tec",&(intstring[1]));
std::ostringstream ss;
std::ostringstream snum;
std::string strnum;
snum <<100000+bid;
strnum=snum.str();
ss<<"flow"<<strnum.substr(1,5)<<".dat";
std::string buf_str=ss.str();;
std::strcpy(fname,buf_str.c_str());

fp=fopen(fname,"w");
fprintf(fp,"TITLE =\"Tioga output\"\n");
fprintf(fp,"VARIABLES=\"X\",\"Y\",\"Z\",\"IBLANK\",\"BTAG\"");
ss.str("");
buf_str.clear();
strnum.clear();
for(i=0;i<nvar;i++)
{
sprintf(qstr,"Q%d",i);
ss<<"Q"<<i;
buf_str=ss.str();
std::strcpy(qstr,buf_str.c_str());
fprintf(fp,",\"%s\"",qstr);
ss.str("");
}
fprintf(fp,"\n");
fprintf(fp,"ZONE T=\"VOL_MIXED\",N=%d E=%d ET=BRICK, F=FEPOINT\n",nnodes,
Expand Down Expand Up @@ -1084,7 +1110,10 @@ void MeshBlock::writeBCnodes(char nodetype2tag,int bodyid){
MPI_Reduce(&nbc,&allnbc,1,MPI_INT,MPI_SUM,0,blockcomm);

static int step = 0;
sprintf(filename,"bcpoints.body%d.%d.tec",bodyid,step);
std::ostringstream ss;
ss<<"bcpoints.body"<<bodyid<<"."<<step<<".tec";
std::string buf_str=ss.str();;
std::strcpy(filename,buf_str.c_str());
step++;

if(blockcomm_id == 0){
Expand Down Expand Up @@ -1778,13 +1807,19 @@ void MeshBlock::getQueryPoints2(OBB *obc,
void MeshBlock::writeOBB(int bid)
{
FILE *fp;
char intstring[12];
char fname[80];
int l,k,j,m,il,ik,ij;
REAL xx[3];

sprintf(intstring,"%d",100000+bid);
sprintf(fname,"box%s.dat",&(intstring[1]));
std::ostringstream ss;
std::ostringstream snum;
std::string strnum;
snum <<100000+bid;
strnum=snum.str();
ss<<"box"<<strnum.substr(1,5)<<".dat";
std::string buf_str=ss.str();;
std::strcpy(fname,buf_str.c_str());

fp=fopen(fname,"w");
fprintf(fp,"TITLE =\"Box file\"\n");
fprintf(fp,"VARIABLES=\"X\",\"Y\",\"Z\"\n");
Expand Down Expand Up @@ -2093,11 +2128,16 @@ void MeshBlock::checkOrphans(void)
}
//fclose(fp);
if (norphan > 0) {
char intstring[12];
char fname[80];
printf("myid/meshtag/norphan/nnodes/nobc=%d %d %d %d %d\n",myid,meshtag,norphan,nnodes,nobc);
sprintf(intstring,"%d",100000+myid);
sprintf(fname,"orphan%s.dat",&(intstring[1]));
std::ostringstream ss;
std::ostringstream snum;
std::string strnum;
snum <<100000+myid;
strnum=snum.str();
ss<<"orphan"<<strnum.substr(1,5)<<".dat";
std::string buf_str=ss.str();;
std::strcpy(fname,buf_str.c_str());
FILE *fp=fopen(fname,"w");
for (int i=0;i<nnodes;i++)
{
Expand Down
14 changes: 12 additions & 2 deletions src/cartOps.C
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include "MeshBlock.h"
#include "tioga_math.h"
#include <assert.h>
#include <cstring>
#include <string>
#include <sstream>
#define ROW 0
#define COLUMN 1

Expand Down Expand Up @@ -162,8 +165,15 @@ void MeshBlock::writeOBB2(OBB * obc, int bid)
int l,k,j,m,il,ik,ij;
REAL xx[3];

sprintf(intstring,"%d",100000+bid);
sprintf(fname,"cbox%s.dat",&(intstring[1]));
std::ostringstream ss;
std::ostringstream snum;
std::string strnum;
snum <<100000+bid;
strnum=snum.str();
ss<<"cbox"<<strnum.substr(1,5)<<".dat";
std::string buf_str=ss.str();;
std::strcpy(fname,buf_str.c_str());

fp=fopen(fname,"w");
fprintf(fp,"TITLE =\"Box file\"\n");
fprintf(fp,"VARIABLES=\"X\",\"Y\",\"Z\"\n");
Expand Down
25 changes: 18 additions & 7 deletions src/holeMap.C
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#include <vector>
#include <array>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>

/* header files */
#include "codetypes.h"
Expand Down Expand Up @@ -55,7 +58,6 @@ void tioga::getHoleMap(void)
int bufferSize;
FILE *fp;
char fname[80];
char intstring[12];
//
// get the local bounding box
//
Expand Down Expand Up @@ -757,8 +759,8 @@ void tioga::getAdaptiveHoleMap(void){
"Mesh Body %d: Levels %d, "
"total octants %lu, total leafs %lu\n",i,
adaptiveHoleMap[i].meta.nlevel,
adaptiveHoleMap[i].meta.elem_count,
adaptiveHoleMap[i].meta.leaf_count);
static_cast<unsigned long>(adaptiveHoleMap[i].meta.elem_count),
static_cast<unsigned long>(adaptiveHoleMap[i].meta.leaf_count));
fflush(stdout);
}
}
Expand All @@ -783,14 +785,20 @@ void tioga::outputHoleMap(void)
int ii,jj,kk,m;
FILE *fp;
double ds[3];
char intstring[12];
char fname[80];

for(i=0;i<nmesh;i++)
if (holeMap[i].existWall)
{
sprintf(intstring,"%d",100000+i+100*myid);
sprintf(fname,"holeMap%s.dat",&(intstring[1]));
std::ostringstream ss;
std::ostringstream snum;
std::string strnum;
snum <<100000+i+100*myid;
strnum=snum.str();
ss<<"holeMap"<<strnum.substr(1,5)<<".dat";
std::string buf_str=ss.str();;
std::strcpy(fname,buf_str.c_str());

fp=fopen(fname,"w");
fprintf(fp,"TITLE =\"Tioga output\"\n");
fprintf(fp,"VARIABLES=\"X\",\"Y\",\"Z\",\"IBLANK\"\n");
Expand Down Expand Up @@ -945,7 +953,10 @@ void tioga::outputAdaptiveHoleMap(void){
ds[1] = meta.extents_hi[1] - meta.extents_lo[1];
ds[2] = meta.extents_hi[2] - meta.extents_lo[2];

sprintf(filename,"AHM.body%d.%d.tec",m,ahm_step++);
std::ostringstream ss;
ss<<"AHM.body"<<m<<"."<<ahm_step<<".tec";
std::string buf_str=ss.str();;
std::strcpy(filename,buf_str.c_str());
writePointsHeaderVolume(filename);

file = fopen(filename, "a");
Expand Down
8 changes: 7 additions & 1 deletion src/tioga.C
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
#include <assert.h>
#include <vector>
#include <iostream>
#include <cstring>
#include <string>
#include <sstream>

using namespace TIOGA;
/**
Expand Down Expand Up @@ -855,7 +858,10 @@ void tioga::dataUpdate(int nvar,int interptype, int at_points)
if (itmp[ib][i]==0 && iorphanPrint) {
if (fp==NULL)
{
sprintf(ofname,"orphan%d.%d.dat",myid,ib);
std::ostringstream ss;
ss<<"orphan"<<myid<<"."<<ib<<".dat";
std::string buf_str=ss.str();;
std::strcpy(ofname,buf_str.c_str());
fp=fopen(ofname,"w");
}
mb->outputOrphan(fp,i);
Expand Down
43 changes: 33 additions & 10 deletions src/tioga_utils.C
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
/* You should have received a copy of the GNU Lesser General Public */
/* License along with this library; if not, write to the Free Software */
/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
#include <cstring>
#include <string>
#include <sstream>
#include "codetypes.h"
#include "tioga_utils.h"
#include "kaiser.h"
Expand Down Expand Up @@ -475,13 +478,19 @@ void transform2OBB(double xv[3],double xc[3],double vec[3][3],double xd[3])
void writebbox(OBB *obb,int bid)
{
FILE *fp;
char intstring[12];
char fname[80];
int l,k,j,m,il,ik,ij;
REAL xx[3];

sprintf(intstring,"%d",100000+bid);
sprintf(fname,"qbox%s.dat",&(intstring[1]));
std::ostringstream ss;
std::ostringstream snum;
std::string strnum;
snum <<100000+bid;
strnum=snum.str();
ss<<"qbox"<<strnum.substr(1,5)<<".dat";
std::string buf_str=ss.str();;
std::strcpy(fname,buf_str.c_str());

fp=fopen(fname,"w");
fprintf(fp,"TITLE =\"Box file\"\n");
fprintf(fp,"VARIABLES=\"X\",\"Y\",\"Z\"\n");
Expand Down Expand Up @@ -518,14 +527,22 @@ void writebboxdiv(OBB *obb,int bid)
int i,j,k,l,m,n;
int iorder[8]={1, 2, 4, 3, 5, 6, 8, 7};
FILE *fp;
char intstring[12];
char fname[80];

for(j=0;j<3;j++) { mapdims[j]=12; mapdx[j]=2*obb->dxc[j]/mapdims[j]; mdx[j]=0.5*mapdx[j];mx0[j]=0;}
ncells=mapdims[2]*mapdims[1]*mapdims[0];
npts=ncells*8;
sprintf(intstring,"%d",100000+bid);
sprintf(fname,"dbox%s.dat",&(intstring[1]));


std::ostringstream ss;
std::ostringstream snum;
std::string strnum;
snum <<100000+bid;
strnum=snum.str();
ss<<"dbox"<<strnum.substr(1,5)<<".dat";
std::string buf_str=ss.str();;
std::strcpy(fname,buf_str.c_str());

fp=fopen(fname,"w");
fprintf(fp,"TITLE =\"Box file\"\n");
fprintf(fp,"VARIABLES=\"X\",\"Y\",\"Z\"\n");
Expand Down Expand Up @@ -559,12 +576,18 @@ void writebboxdiv(OBB *obb,int bid)
void writePoints(double *x,int nsearch,int bid)
{
FILE *fp;
char intstring[12];
char fname[80];
int i;

sprintf(intstring,"%d",100000+bid);
sprintf(fname,"points%s.dat",&(intstring[1]));
std::ostringstream ss;
std::ostringstream snum;
std::string strnum;
snum <<100000+bid;
strnum=snum.str();
ss<<"points"<<strnum.substr(1,5)<<".dat";
std::string buf_str=ss.str();;
std::strcpy(fname,buf_str.c_str());

fp=fopen(fname,"w");
fprintf(fp,"TITLE =\"Box file\"\n");
fprintf(fp,"VARIABLES=\"X\",\"Y\",\"Z\"\n");
Expand Down Expand Up @@ -1087,4 +1110,4 @@ char checkFaceBoundaryNodes(int *nodes,const char *bcnodeflag,

for(v=0;v<numfaceverts;v++) bcFlag &= bcnodeflag[nodes[faceConn[v]-BASE]];
return bcFlag;
}
}
Loading