1 // Project MULTIPR, IOLS WP1.2.1 - EDF/CS
2 // Partitioning/decimation module for the SALOME v3.2 platform
5 * \file MULTIPR_Utils.cxx
7 * \brief see MULTIPR_Utils.hxx
9 * \author Olivier LE ROUX - CS, Virtual Reality Dpt
14 //*****************************************************************************
16 //*****************************************************************************
18 #include "MULTIPR_Utils.hxx"
19 #include "MULTIPR_Exceptions.hxx"
26 //*****************************************************************************
28 //*****************************************************************************
30 void multipr::trim(char* pStr, char pChar)
32 int len = strlen(pStr) - 1;
34 while (pStr[p] == pChar)
45 bool multipr::startsWith(const char* pStr, const char* pStrPrefix)
48 while ((pStr[i] != '\0') && (pStrPrefix[i] != '\0') && (pStr[i] == pStrPrefix[i]))
52 return (pStrPrefix[i] == '\0');
56 string multipr::removeExtension(const char* pFilename, const char* pExtension)
58 string strPrefix(pFilename);
59 strPrefix.erase(strPrefix.rfind(pExtension), strlen(pExtension));
65 string multipr::getFilenameWithoutPath(const char* pFilename)
67 char* res = strrchr(pFilename, '/');
80 string multipr::getPath(const char* pFilename)
82 char* res = strrchr(pFilename, '/');
89 int size = res - pFilename + 1;
91 memcpy(path, pFilename, size);
98 void multipr::copyFile(const char* pFilename, const char* pDestDir)
100 const char* srcDir = multipr::getPath(pFilename).c_str();
101 if (strcmp(srcDir, pDestDir) == 0) return;
103 const char* filenameWithoutPath = multipr::getFilenameWithoutPath(pFilename).c_str();
104 char pDstFilename[256];
105 sprintf(pDstFilename, "%s%s", pDestDir, filenameWithoutPath);
107 FILE* src = fopen(pFilename, "rb");
108 if (src == NULL) return;
109 FILE* dst = fopen(pDstFilename, "wb");
110 if (dst == NULL) return;
112 const int size = 65536;
113 char* buf = new char[size];
115 ret = fread(buf, 1, size, src);
118 fwrite(buf, 1, ret, dst);
119 ret = fread(buf, 1, size, src);
128 void multipr::printArray2D(
129 const med_float* pData,
134 for (int itElt = 0 ; itElt < pNumElt ; itElt++)
136 cout << pPrefix << " " << (itElt + 1) << ": ";
137 for (int itDim = 0 ; itDim < pDimElt ; itDim++)
139 cout << pData[itElt * pDimElt + itDim] << " ";
146 string multipr::realToString(med_float mV)
149 sprintf(str, "%lf", mV);
151 int len = strlen(str);
152 if (str[len - 1] == '.')
161 vector<string> multipr::getListMeshes(const char* pMEDfilename)
163 if (pMEDfilename == NULL) throw multipr::NullArgumentException("", __FILE__, __LINE__);
168 //---------------------------------------------------------------------
169 // Open MED file (READ_ONLY)
170 //---------------------------------------------------------------------
171 med_idt file = MEDouvrir(const_cast<char*>(pMEDfilename), MED_LECTURE); // open MED file for reading
172 if (file <= 0) throw multipr::IOException("MED file not found or not a sequential MED file", __FILE__, __LINE__);
174 //---------------------------------------------------------------------
175 // Read name of meshes
176 //---------------------------------------------------------------------
177 med_int nbMeshes = MEDnMaa(file);
178 if (nbMeshes <= 0) throw multipr::IOException("i/o error while reading number of meshes in MED file", __FILE__, __LINE__);
180 // for each mesh in the file (warning: first mesh is number 1)
181 for (int itMesh = 1 ; itMesh <= nbMeshes ; itMesh++)
183 char meshName[MED_TAILLE_NOM + 1];
186 med_maillage meshType;
187 char meshDesc[MED_TAILLE_DESC + 1];
197 if (ret != 0) throw multipr::IOException("i/o error while reading mesh information in MED file", __FILE__, __LINE__);
199 res.push_back(meshName);
202 //---------------------------------------------------------------------
203 // Close the MED file
204 //---------------------------------------------------------------------
205 ret = MEDfermer(file);
206 if (ret != 0) throw multipr::IOException("i/o error while closing MED file", __FILE__, __LINE__);
212 vector<pair<string,int> > multipr::getListScalarFields(const char* pMEDfilename)
214 if (pMEDfilename == NULL) throw multipr::NullArgumentException("", __FILE__, __LINE__);
216 vector<pair<string, int> > res;
219 //---------------------------------------------------------------------
220 // Open MED file (READ_ONLY)
221 //---------------------------------------------------------------------
222 med_idt file = MEDouvrir(const_cast<char*>(pMEDfilename), MED_LECTURE); // open MED file for reading
223 if (file <= 0) throw multipr::IOException("MED file not found or not a sequential MED file", __FILE__, __LINE__);
225 //---------------------------------------------------------------------
226 // Read number of fields
227 //---------------------------------------------------------------------
228 med_int numFields = MEDnChamp(file, 0);
229 if (numFields <= 0) throw IOException("", __FILE__, __LINE__);
231 //---------------------------------------------------------------------
232 // For each field, read its name
233 //---------------------------------------------------------------------
234 for (int itField = 1 ; itField <= numFields ; itField++)
236 char name[MED_TAILLE_NOM + 1];
239 med_int numComponents = MEDnChamp(file, itField);
241 if (numComponents < 0) throw IOException("", __FILE__, __LINE__);
243 // collect scalar field only (not vectorial fields)
244 if (numComponents != 1)
247 char* strComponent = new char[numComponents * MED_TAILLE_PNOM + 1];
248 char* strUnit = new char[numComponents * MED_TAILLE_PNOM + 1];
250 strComponent[0] = '\0';
253 med_err ret = MEDchampInfo(
262 if (ret != 0) throw IOException("", __FILE__, __LINE__);
265 delete[] strComponent;
267 med_int numTimeStep = MEDnPasdetemps(
271 (med_geometrie_element) 0);
273 if (numTimeStep < 0) throw IOException("", __FILE__, __LINE__);
275 if (numTimeStep == 0)
277 numTimeStep = MEDnPasdetemps(
283 if (numTimeStep < 0) throw IOException("", __FILE__, __LINE__);
287 res.push_back(make_pair(name, numTimeStep));
290 //---------------------------------------------------------------------
291 // Close the MED file
292 //---------------------------------------------------------------------
293 ret = MEDfermer(file);
294 if (ret != 0) throw multipr::IOException("i/o error while closing MED file", __FILE__, __LINE__);