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 // find (reverse) the first occurrence of '/' in the given string
83 char* res = strrchr(pFilename, '/');
85 // if there is no '/'...
92 int size = res - pFilename + 1;
94 memcpy(path, pFilename, size);
101 void multipr::copyFile(const char* pFilename, const char* pDestDir)
103 const char* srcDir = multipr::getPath(pFilename).c_str();
104 if (strcmp(srcDir, pDestDir) == 0) return;
106 const char* filenameWithoutPath = multipr::getFilenameWithoutPath(pFilename).c_str();
107 char pDstFilename[256];
108 sprintf(pDstFilename, "%s%s", pDestDir, filenameWithoutPath);
110 // open binary source file
111 FILE* src = fopen(pFilename, "rb");
112 if (src == NULL) return;
114 // open (create) binary destination file
115 FILE* dst = fopen(pDstFilename, "wb");
116 if (dst == NULL) return;
118 const int size = 65536; // size of buffer is 64Kb
119 char* buf = new char[size];
121 ret = fread(buf, 1, size, src);
124 fwrite(buf, 1, ret, dst); // write to destination
125 ret = fread(buf, 1, size, src); // read from source
134 void multipr::printArray2D(
135 const med_float* pData,
140 for (int itElt = 0 ; itElt < pNumElt ; itElt++)
142 cout << pPrefix << " " << (itElt + 1) << ": ";
143 for (int itDim = 0 ; itDim < pDimElt ; itDim++)
145 cout << pData[itElt * pDimElt + itDim] << " ";
152 string multipr::realToString(med_float mV)
155 sprintf(str, "%lf", mV);
157 int len = strlen(str);
158 if (str[len - 1] == '.')
167 vector<string> multipr::getListMeshes(const char* pMEDfilename)
169 if (pMEDfilename == NULL) throw multipr::NullArgumentException("", __FILE__, __LINE__);
174 //---------------------------------------------------------------------
175 // Open MED file (READ_ONLY)
176 //---------------------------------------------------------------------
177 med_idt file = MEDouvrir(const_cast<char*>(pMEDfilename), MED_LECTURE); // open MED file for reading
178 if (file <= 0) throw multipr::IOException("MED file not found or not a sequential MED file", __FILE__, __LINE__);
180 //---------------------------------------------------------------------
181 // Read name of meshes
182 //---------------------------------------------------------------------
183 med_int nbMeshes = MEDnMaa(file);
184 if (nbMeshes <= 0) throw multipr::IOException("i/o error while reading number of meshes in MED file", __FILE__, __LINE__);
186 // for each mesh in the file (warning: first mesh is number 1)
187 for (int itMesh = 1 ; itMesh <= nbMeshes ; itMesh++)
189 char meshName[MED_TAILLE_NOM + 1];
192 med_maillage meshType;
193 char meshDesc[MED_TAILLE_DESC + 1];
203 if (ret != 0) throw multipr::IOException("i/o error while reading mesh information in MED file", __FILE__, __LINE__);
205 res.push_back(meshName);
208 //---------------------------------------------------------------------
209 // Close the MED file
210 //---------------------------------------------------------------------
211 ret = MEDfermer(file);
212 if (ret != 0) throw multipr::IOException("i/o error while closing MED file", __FILE__, __LINE__);
218 vector<pair<string,int> > multipr::getListScalarFields(const char* pMEDfilename)
220 if (pMEDfilename == NULL) throw multipr::NullArgumentException("", __FILE__, __LINE__);
222 vector<pair<string, int> > res;
225 //---------------------------------------------------------------------
226 // Open MED file (READ_ONLY)
227 //---------------------------------------------------------------------
228 med_idt file = MEDouvrir(const_cast<char*>(pMEDfilename), MED_LECTURE); // open MED file for reading
229 if (file <= 0) throw multipr::IOException("MED file not found or not a sequential MED file", __FILE__, __LINE__);
231 //---------------------------------------------------------------------
232 // Read number of fields
233 //---------------------------------------------------------------------
234 med_int numFields = MEDnChamp(file, 0);
235 if (numFields <= 0) throw IOException("", __FILE__, __LINE__);
237 //---------------------------------------------------------------------
238 // For each field, read its name
239 //---------------------------------------------------------------------
240 for (int itField = 1 ; itField <= numFields ; itField++)
242 char name[MED_TAILLE_NOM + 1];
245 med_int numComponents = MEDnChamp(file, itField);
247 if (numComponents < 0) throw IOException("", __FILE__, __LINE__);
249 // collect scalar field only (not vectorial fields)
250 if (numComponents != 1)
254 char* strComponent = new char[numComponents * MED_TAILLE_PNOM + 1];
255 char* strUnit = new char[numComponents * MED_TAILLE_PNOM + 1];
257 strComponent[0] = '\0';
260 med_err ret = MEDchampInfo(
269 if (ret != 0) throw IOException("", __FILE__, __LINE__);
272 delete[] strComponent;
274 med_int numTimeStamps = MEDnPasdetemps(
278 (med_geometrie_element) 0);
280 if (numTimeStamps < 0) throw IOException("", __FILE__, __LINE__);
282 if (numTimeStamps == 0)
284 numTimeStamps = MEDnPasdetemps(
290 if (numTimeStamps < 0) throw IOException("", __FILE__, __LINE__);
294 // add the pair(name, #time stamps) to the result
295 res.push_back( make_pair(name, numTimeStamps) );
298 //---------------------------------------------------------------------
299 // Close the MED file
300 //---------------------------------------------------------------------
301 ret = MEDfermer(file);
302 if (ret != 0) throw multipr::IOException("i/o error while closing MED file", __FILE__, __LINE__);
304 // return the list of fields