Skip to content

Commit

Permalink
Merge pull request #271 from mpotse/develop
Browse files Browse the repository at this point in the history
use fgets() results when reading .mesh files, plus language fixes
  • Loading branch information
Algiane committed May 17, 2024
2 parents 184b00b + 1ec5494 commit a6a9584
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
16 changes: 10 additions & 6 deletions src/mmg2d/inout_2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ int MMG2D_loadMesh(MMG5_pMesh mesh,const char *filename) {
strcpy(chaine,"D");
while(fscanf(inm,"%127s",&chaine[0])!=EOF && strncmp(chaine,"End",strlen("End")) ) {
if ( chaine[0] == '#' ) {
fgets(strskip,MMG5_FILESTR_LGTH,inm);
while(1){ // skip until end of line or file
char *s = fgets(strskip,MMG5_FILESTR_LGTH,inm);
if(!s) break; // nothing could be read
if(s[strlen(s)-1]=='\n') break; // end of line
}
continue;
}

Expand Down Expand Up @@ -640,7 +644,7 @@ int MMG2D_loadGenericMesh(MMG5_pMesh mesh, MMG5_pSol met, MMG5_pSol sol, const c
ier = MMG2D_loadMesh(mesh,tmp);
if ( ier < 1 ) { break; }

/* Facultative metric */
/* Optional metric */
if ( sol ) {
MMG5_SAFE_MALLOC(soltmp,strlen(solnameptr)+1,char,return -1);
strcpy(soltmp,solnameptr);
Expand Down Expand Up @@ -960,7 +964,7 @@ int MMG2D_loadSol(MMG5_pMesh mesh,MMG5_pSol sol,const char *filename) {
fseek(inm,posnp,SEEK_SET);

if ( sol->ver == 1 ) {
/* Simple precision */
/* Single precision */
for (k=1; k<=sol->np; k++) {
if ( MMG2D_readFloatSol(sol,inm,bin,iswp,k) < 0 ) return -1;
}
Expand Down Expand Up @@ -1041,7 +1045,7 @@ int MMG2D_loadAllSols(MMG5_pMesh mesh,MMG5_pSol *sol, const char *filename) {
for ( j=0; j<nsols; ++j) {
psl = *sol+j;

/* Give an arbitrary name to the solution because the Medit format has non
/* Give an arbitrary name to the solution because the Medit format has no
* name field */
sprintf(data,"sol_%d",j);
if ( !MMG2D_Set_inputSolName(mesh,psl,data) ) {
Expand All @@ -1059,7 +1063,7 @@ int MMG2D_loadAllSols(MMG5_pMesh mesh,MMG5_pSol *sol, const char *filename) {
return -1;
}
psl->dim = 2;
/* For binary file, we read the verson inside the file */
/* For binary files, we read the verson inside the file */
if ( ver ) psl->ver = ver;
}
MMG5_SAFE_FREE(type);
Expand All @@ -1069,7 +1073,7 @@ int MMG2D_loadAllSols(MMG5_pMesh mesh,MMG5_pSol *sol, const char *filename) {
fseek(inm,posnp,SEEK_SET);

if ( (*sol)[0].ver == 1 ) {
/* Simple precision */
/* Single precision */
for (k=1; k<=mesh->np; k++) {
for ( j=0; j<nsols; ++j ) {
psl = *sol+j;
Expand Down
30 changes: 17 additions & 13 deletions src/mmg3d/inout_3d.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
* \param modeASCII mode in which to open an ascii file ("r","r+","w","w+",...)
* \param modeASCII mode in which to open an ascii file ("r","r+","w","w+",...)
*
* \return 0 if fail to open file, -1 for other errors, 1 if success.
* \return 0 if the file could not be opened, -1 for other errors, 1 if success.
*
* Try to open a Medit file at asked mode (read only, write, etc) and store if
* Try to open a Medit file in the requested mode (read only, write, etc) and store if
* file is binary (depending on the extension).
*
*/
Expand Down Expand Up @@ -145,7 +145,11 @@ int MMG3D_loadMesh_opened(MMG5_pMesh mesh,FILE *inm,int bin) {
strcpy(chaine,"D");
while(fscanf(inm,"%127s",&chaine[0])!=EOF && strncmp(chaine,"End",strlen("End")) ) {
if ( chaine[0] == '#' ) {
fgets(strskip,MMG5_FILESTR_LGTH,inm);
while(1){ // skip until end of line or file
char *s = fgets(strskip,MMG5_FILESTR_LGTH,inm);
if(!s) break; // nothing could be read
if(s[strlen(s)-1]=='\n') break; // end of line
}
continue;
}

Expand Down Expand Up @@ -807,7 +811,7 @@ int MMG3D_loadMesh_opened(MMG5_pMesh mesh,FILE *inm,int bin) {
ppt->tag &= ~MG_NUL;
}

/* Possibly switch 2 vertices number so that each tet is positively oriented */
/* Possibly switch 2 vertex numbers so that each tet is positively oriented */
if ( MMG5_orvol(mesh->point,pt->v) < 0.0 ) {
/* mesh->xt temporary used to count reoriented tetra */
mesh->xt++;
Expand Down Expand Up @@ -1233,7 +1237,7 @@ int MMG3D_loadGenericMesh(MMG5_pMesh mesh, MMG5_pSol met, MMG5_pSol sol, const c
ier = MMG3D_loadMesh(mesh,tmp);
if ( ier < 1 ) { break; }

/* Facultative metric */
/* Optional metric */
if ( sol ) {
MMG5_SAFE_MALLOC(soltmp,strlen(solnameptr)+1,char,return -1);
strcpy(soltmp,solnameptr);
Expand Down Expand Up @@ -1286,7 +1290,7 @@ int MMG3D_saveMesh(MMG5_pMesh mesh, const char *filename) {
return 0;
}

/*entete fichier*/
/* file header */
binch=0; bpos=10;
if(!bin) {
strcpy(&chaine[0],"MeshVersionFormatted 2\n");
Expand Down Expand Up @@ -2041,7 +2045,7 @@ int MMG3D_saveMesh(MMG5_pMesh mesh, const char *filename) {
}
}

/*fin fichier*/
/* end of file */
if(!bin) {
strcpy(&chaine[0],"\n\nEnd\n");
fprintf(inm,"%s",chaine);
Expand Down Expand Up @@ -2173,13 +2177,13 @@ int MMG3D_loadSol(MMG5_pMesh mesh,MMG5_pSol met, const char *filename) {
return ier;
}

/* Allocate and store the header informations for each solution */
/* Allocate and store the header information for each solution */
if ( !MMG3D_Set_solSize(mesh,met,MMG5_Vertex,mesh->np,type[0]) ) {
fclose(inm);
MMG5_SAFE_FREE(type);
return -1;
}
/* For binary file, we read the verson inside the file */
/* For binary files, we read the verson inside the file */
if ( ver ) met->ver = ver;

MMG5_SAFE_FREE(type);
Expand All @@ -2189,7 +2193,7 @@ int MMG3D_loadSol(MMG5_pMesh mesh,MMG5_pSol met, const char *filename) {
fseek(inm,posnp,SEEK_SET);

if ( met->ver == 1 ) {
/* Simple precision */
/* Single precision */
for (k=1; k<=mesh->np; k++) {
if ( MMG5_readFloatSol3D(met,inm,bin,iswp,k) < 0 ) return -1;
}
Expand Down Expand Up @@ -2254,7 +2258,7 @@ int MMG3D_loadAllSols(MMG5_pMesh mesh,MMG5_pSol *sol, const char *filename) {
for ( j=0; j<nsols; ++j ) {
psl = *sol + j;

/* Give an arbitrary name to the solution because the Medit format has non
/* Give an arbitrary name to the solution because the Medit format has no
* name field */
sprintf(data,"sol_%" MMG5_PRId "",j);
if ( !MMG3D_Set_inputSolName(mesh,psl,data) ) {
Expand All @@ -2272,7 +2276,7 @@ int MMG3D_loadAllSols(MMG5_pMesh mesh,MMG5_pSol *sol, const char *filename) {
return -1;
}

/* For binary file, we read the verson inside the file */
/* For binary file, we read the version inside the file */
if ( ver ) psl->ver = ver;
}
MMG5_SAFE_FREE(type);
Expand All @@ -2282,7 +2286,7 @@ int MMG3D_loadAllSols(MMG5_pMesh mesh,MMG5_pSol *sol, const char *filename) {
fseek(inm,posnp,SEEK_SET);

if ( (*sol)[0].ver == 1 ) {
/* Simple precision */
/* Single precision */
for (k=1; k<=mesh->np; k++) {
for ( j=0; j<nsols; ++j ) {
psl = *sol + j;
Expand Down
26 changes: 15 additions & 11 deletions src/mmgs/inout_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ int MMGS_loadMesh(MMG5_pMesh mesh, const char *filename) {
strcpy(chaine,"D");
while(fscanf(inm,"%127s",&chaine[0])!=EOF && strncmp(chaine,"End",strlen("End")) ) {
if ( chaine[0] == '#' ) {
fgets(strskip,MMG5_FILESTR_LGTH,inm);
while(1){ // skip until end of line or file
char *s = fgets(strskip,MMG5_FILESTR_LGTH,inm);
if(!s) break; // nothing could be read
if(s[strlen(s)-1]=='\n') break; // end of line
}
continue;
}
if(!strncmp(chaine,"MeshVersionFormatted",strlen("MeshVersionFormatted"))) {
Expand Down Expand Up @@ -393,7 +397,7 @@ int MMGS_loadMesh(MMG5_pMesh mesh, const char *filename) {
}
}

/* read quads: automatic conversion into tria */
/* read quads: automatic conversion to triangles */
if ( nq > 0 ) {
rewind(inm);
fseek(inm,posnq,SEEK_SET);
Expand Down Expand Up @@ -888,7 +892,7 @@ int MMGS_saveMesh(MMG5_pMesh mesh, const char* filename) {
fprintf(stdout," %%%% %s OPENED\n",data);
MMG5_SAFE_FREE(data);

/*entete fichier*/
/* file header */
if(!bin) {
strcpy(&chaine[0],"MeshVersionFormatted 2\n");
fprintf(inm,"%s",chaine);
Expand Down Expand Up @@ -1281,7 +1285,7 @@ int MMGS_saveMesh(MMG5_pMesh mesh, const char* filename) {
if ( nn+ng )
fprintf(stdout," NUMBER OF NORMALS %8" MMG5_PRId " TANGENTS %6" MMG5_PRId "\n",nn,ng);
}
/*fin fichier*/
/* end of file */
if(!bin) {
strcpy(&chaine[0],"\n\nEnd\n");
fprintf(inm,"%s",chaine);
Expand Down Expand Up @@ -1334,7 +1338,7 @@ int MMGS_loadSol(MMG5_pMesh mesh,MMG5_pSol met,const char* filename) {
return -1;
}

/* #MMG5_loadSolHeader function reads only solution at vertices so we don't
/* #MMG5_loadSolHeader function reads only solutions at vertices so we don't
have to check the entites on which the metric applies */
int entities = MMG5_Vertex;
ier = MMG5_chkMetricType(mesh,type,&entities,inm);
Expand All @@ -1343,7 +1347,7 @@ int MMGS_loadSol(MMG5_pMesh mesh,MMG5_pSol met,const char* filename) {
return ier;
}

/* Allocate and store the header informations for each solution */
/* Allocate and store the header information for each solution */
if ( !MMGS_Set_solSize(mesh,met,MMG5_Vertex,mesh->np,type[0]) ) {
fclose(inm);
MMG5_SAFE_FREE(type);
Expand Down Expand Up @@ -1424,7 +1428,7 @@ int MMGS_loadAllSols(MMG5_pMesh mesh,MMG5_pSol *sol, const char *filename) {
for ( j=0; j<nsols; ++j ) {
psl = *sol + j;

/* Give an arbitrary name to the solution because the Medit format has non
/* Give an arbitrary name to the solution because the Medit format has no
* name field */
sprintf(data,"sol_%" MMG5_PRId "",j);
if ( !MMGS_Set_inputSolName(mesh,psl,data) ) {
Expand All @@ -1435,13 +1439,13 @@ int MMGS_loadAllSols(MMG5_pMesh mesh,MMG5_pSol *sol, const char *filename) {
}
}

/* Allocate and store the header informations for each solution */
/* Allocate and store the header information for each solution */
if ( !MMGS_Set_solSize(mesh,psl,MMG5_Vertex,mesh->np,type[j]) ) {
MMG5_SAFE_FREE(type);
fclose(inm);
return -1;
}
/* For binary file, we read the verson inside the file */
/* For binary files, we read the verson inside the file */
if ( ver ) psl->ver = ver;
}
MMG5_SAFE_FREE(type);
Expand All @@ -1451,7 +1455,7 @@ int MMGS_loadAllSols(MMG5_pMesh mesh,MMG5_pSol *sol, const char *filename) {
fseek(inm,posnp,SEEK_SET);

if ( (*sol)[0].ver == 1 ) {
/* Simple precision */
/* Single precision */
for (k=1; k<=mesh->np; k++) {
for ( j=0; j<nsols; ++j ) {
psl = *sol + j;
Expand Down Expand Up @@ -1502,7 +1506,7 @@ int MMGS_saveSol(MMG5_pMesh mesh,MMG5_pSol met, const char *filename) {
fprintf(inm,"\n");
}

/*fin fichier*/
/* end of file */
if(!bin) {
fprintf(inm,"\n\nEnd\n");
} else {
Expand Down

0 comments on commit a6a9584

Please sign in to comment.