int GmfOpenMesh(const char *FilNam, int mod, ...) { int i, KwdCod, res, *PtrVer, *PtrDim, MshIdx=0; char str[ GmfStrSiz ]; va_list par; GmfMshSct *msh; if(!IniFlg) { for(i=0;i<MaxMsh;i++) MshTab[i] = NULL; IniFlg = 1; } /*---------------------*/ /* MESH STRUCTURE INIT */ /*---------------------*/ for(i=1;i<MaxMsh;i++) if(!MshTab[i]) { MshIdx = i; break; } if( !MshIdx || !(msh = (GmfMshSct *) calloc(1, sizeof(GmfMshSct))) ) return(0); /* Copy the FilNam into the structure */ if(strlen(FilNam) + 7 >= GmfStrSiz) return(0); strcpy(msh->FilNam, FilNam); /* Store the opening mod (read or write) and guess the filetype (binary or ascii) depending on the extension */ msh->mod = mod; if(strstr(msh->FilNam, ".meshb")) msh->typ |= (Bin | MshFil); else if(strstr(msh->FilNam, ".mesh")) msh->typ |= (Asc | MshFil); else if(strstr(msh->FilNam, ".solb")) msh->typ |= (Bin | SolFil); else if(strstr(msh->FilNam, ".sol")) msh->typ |= (Asc | SolFil); else return(0); /* Open the file in the required mod and initialyse the mesh structure */ if(msh->mod == GmfRead) { /*-----------------------*/ /* OPEN FILE FOR READING */ /*-----------------------*/ va_start(par, mod); PtrVer = va_arg(par, int *); PtrDim = va_arg(par, int *); va_end(par); /* Create the name string and open the file */ if(!(msh->hdl = fopen(msh->FilNam, "rb"))) return(0); /* Read the endian coding tag, the mesh version and the mesh dimension (mandatory kwd) */ if(msh->typ & Bin) { fread((unsigned char *)&msh->cod, WrdSiz, 1, msh->hdl); if( (msh->cod != 1) && (msh->cod != 16777216) ) return(0); ScaWrd(msh, (unsigned char *)&msh->ver); ScaWrd(msh, (unsigned char *)&KwdCod); if(KwdCod != GmfDimension) return(0); ScaWrd(msh, (unsigned char *)&KwdCod); ScaWrd(msh, (unsigned char *)&msh->dim); } else { do { res = fscanf(msh->hdl, "%s", str); }while( (res != EOF) && strcmp(str, "MeshVersionFormatted") ); if(res == EOF) return(0); fscanf(msh->hdl, "%d", &msh->ver); do { res = fscanf(msh->hdl, "%s", str); }while( (res != EOF) && strcmp(str, "Dimension") ); if(res == EOF) return(0); fscanf(msh->hdl, "%d", &msh->dim); } if( (msh->dim != 2) && (msh->dim != 3) ) return(0); (*PtrVer) = msh->ver; (*PtrDim) = msh->dim; /*------------*/ /* KW READING */ /*------------*/ /* Read the list of kw present in the file */ if(!ScaKwdTab(msh)) return(0); MshTab[ MshIdx ] = msh; return(MshIdx); }
int GmfOpenMesh(const char *FilNam, int mod, ...) { int i, KwdCod, res, *PtrVer, *PtrDim, MshIdx=0; char str[ GmfStrSiz ]; va_list VarArg; GmfMshSct *msh; if(!GmfIniFlg) { for(i=0;i<=MaxMsh;i++) GmfMshTab[i] = NULL; GmfIniFlg = 1; } /*---------------------*/ /* MESH STRUCTURE INIT */ /*---------------------*/ for(i=1;i<=MaxMsh;i++) if(!GmfMshTab[i]) { MshIdx = i; break; } if( !MshIdx || !(msh = calloc(1, sizeof(GmfMshSct))) ) return(0); /* Save the current stack environment for longjmp */ if(setjmp(GmfEnv) != 0) { if(msh->hdl != NULL) fclose(msh->hdl); free(msh); return(0); } /* Copy the FilNam into the structure */ if(strlen(FilNam) + 7 >= GmfStrSiz) longjmp(GmfEnv, -1); strcpy(msh->FilNam, FilNam); /* Store the opening mod (read or write) and guess the filetype (binary or ascii) depending on the extension */ msh->mod = mod; msh->buf = (void *)msh->DblBuf; msh->FltBuf = (void *)msh->DblBuf; msh->IntBuf = (void *)msh->DblBuf; if(strstr(msh->FilNam, ".meshb")) msh->typ |= (Bin | MshFil); else if(strstr(msh->FilNam, ".mesh")) msh->typ |= (Asc | MshFil); else if(strstr(msh->FilNam, ".solb")) msh->typ |= (Bin | SolFil); else if(strstr(msh->FilNam, ".sol")) msh->typ |= (Asc | SolFil); else longjmp(GmfEnv, -1); /* Open the file in the required mod and initialyse the mesh structure */ if(msh->mod == GmfRead) { /*-----------------------*/ /* OPEN FILE FOR READING */ /*-----------------------*/ va_start(VarArg, mod); PtrVer = va_arg(VarArg, int *); PtrDim = va_arg(VarArg, int *); va_end(VarArg); /* Create the name string and open the file */ if(!(msh->hdl = fopen(msh->FilNam, "rb"))) longjmp(GmfEnv, -1); /* Read the endian coding tag, the mesh version and the mesh dimension (mandatory kwd) */ if(msh->typ & Bin) { if( fread(&msh->cod, WrdSiz, 1, msh->hdl) != 1 ) longjmp(GmfEnv, -1); if( (msh->cod != 1) && (msh->cod != 16777216) ) longjmp(GmfEnv, -1); ScaWrd(msh, (unsigned char *)&msh->ver); if( (msh->ver < 1) || (msh->ver > 4) ) longjmp(GmfEnv, -1); if( (msh->ver >= 3) && (sizeof(long) == 4) ) longjmp(GmfEnv, -1); ScaWrd(msh, (unsigned char *)&KwdCod); if(KwdCod != GmfDimension) longjmp(GmfEnv, -1); GetPos(msh); ScaWrd(msh, (unsigned char *)&msh->dim); } else { do { res = fscanf(msh->hdl, "%s", str); }while( (res != EOF) && strcmp(str, "MeshVersionFormatted") ); if(res == EOF) longjmp(GmfEnv, -1); safe_fscanf(msh->hdl, "%d", &msh->ver); if( (msh->ver < 1) || (msh->ver > 4) ) longjmp(GmfEnv, -1); do { res = fscanf(msh->hdl, "%s", str); }while( (res != EOF) && strcmp(str, "Dimension") ); if(res == EOF) longjmp(GmfEnv, -1); safe_fscanf(msh->hdl, "%d", &msh->dim); } if( (msh->dim != 2) && (msh->dim != 3) ) longjmp(GmfEnv, -1); (*PtrVer) = msh->ver; (*PtrDim) = msh->dim; /*------------*/ /* KW READING */ /*------------*/ /* Read the list of kw present in the file */ if(!ScaKwdTab(msh)) return(0); GmfMshTab[ MshIdx ] = msh; return(MshIdx); }