]> SALOME platform Git repositories - modules/multipr.git/blob - src/MULTIPR/MULTIPR_Utils.cxx
Salome HOME
a5f9f0c50b33799120fbfeef93e5624c2f47bff0
[modules/multipr.git] / src / MULTIPR / MULTIPR_Utils.cxx
1 // Project MULTIPR, IOLS WP1.2.1 - EDF/CS
2 // Partitioning/decimation module for the SALOME v3.2 platform
3
4 /**
5  * \file    MULTIPR_Utils.cxx
6  *
7  * \brief   see MULTIPR_Utils.hxx
8  *
9  * \author  Olivier LE ROUX - CS, Virtual Reality Dpt
10  * 
11  * \date    01/2007
12  */
13
14 //*****************************************************************************
15 // Includes section
16 //*****************************************************************************
17
18 #include "MULTIPR_Utils.hxx"
19 #include "MULTIPR_Exceptions.hxx"
20
21 #include <iostream>
22
23 using namespace std;
24
25
26 //*****************************************************************************
27 // Implementation
28 //*****************************************************************************
29
30 void multipr::trim(char* pStr, char pChar)
31 {
32         int len = strlen(pStr) - 1;
33         int p = len;
34         while (pStr[p] == pChar)
35         {
36                 p--;
37         }
38         if (p != len)
39         {
40                 pStr[p + 1] = '\0';
41         }
42 }
43
44
45 bool multipr::startsWith(const char* pStr, const char* pStrPrefix)
46 {
47         int i = 0;
48         while ((pStr[i] != '\0') && (pStrPrefix[i] != '\0') && (pStr[i] == pStrPrefix[i]))
49         {
50                 i++;
51         }
52         return (pStrPrefix[i] == '\0');
53 }
54
55
56 string multipr::removeExtension(const char* pFilename, const char* pExtension)
57 {
58         string strPrefix(pFilename); 
59         strPrefix.erase(strPrefix.rfind(pExtension), strlen(pExtension));
60         
61         return strPrefix;
62 }
63
64
65 string multipr::getFilenameWithoutPath(const char* pFilename)
66 {
67         char* res = strrchr(pFilename, '/');
68         if (res == NULL)
69         {
70                 return pFilename;
71         }
72         else
73         {
74                 char* name = res + 1;
75                 return name;
76         }
77 }
78
79
80 string multipr::getPath(const char* pFilename)
81 {
82         char* res = strrchr(pFilename, '/');
83         if (res == NULL)
84         {
85                 return "";
86         }
87         else
88         {
89                 int size = res - pFilename + 1;
90                 char path[256];
91                 memcpy(path, pFilename, size);
92                 path[size] = '\0';
93                 return path;
94         }
95 }
96
97
98 void multipr::copyFile(const char* pFilename, const char* pDestDir)
99 {
100         const char* srcDir = multipr::getPath(pFilename).c_str();
101         if (strcmp(srcDir, pDestDir) == 0) return;
102         
103         const char* filenameWithoutPath = multipr::getFilenameWithoutPath(pFilename).c_str();
104         char pDstFilename[256];
105         sprintf(pDstFilename, "%s%s", pDestDir, filenameWithoutPath);
106         
107         FILE* src = fopen(pFilename, "rb");
108         if (src == NULL) return;
109         FILE* dst = fopen(pDstFilename, "wb");
110         if (dst == NULL) return;
111         
112         const int size = 65536;
113         char* buf = new char[size];
114         int ret;
115         ret = fread(buf, 1, size, src);
116         while (ret != 0)
117         {
118                 fwrite(buf, 1, ret, dst);
119                 ret = fread(buf, 1, size, src);
120         }
121         
122         delete[] buf;
123         fclose(src);
124         fclose(dst);
125 }
126
127
128 void multipr::printArray2D(
129         const med_float* pData, 
130         const int        pNumElt,
131         const int        pDimElt,
132         const char*      pPrefix)
133 {
134         for (int itElt = 0 ; itElt < pNumElt ; itElt++)
135         {
136                 cout << pPrefix << " " << (itElt + 1) << ": ";
137                 for (int itDim = 0 ; itDim < pDimElt ; itDim++)
138                 {
139                         cout << pData[itElt * pDimElt + itDim] << " ";
140                 }
141                 cout << endl;
142         }
143 }
144
145
146 string multipr::realToString(med_float mV)
147 {
148         char str[32];
149         sprintf(str, "%lf", mV);
150         trim(str, '0');
151         int len = strlen(str);
152         if (str[len - 1] == '.')
153         {
154                 str[len] = '0';
155                 str[len + 1] = '\0';
156         }
157         return string(str);
158 }
159
160
161 vector<string> multipr::getListMeshes(const char* pMEDfilename)
162 {
163         if (pMEDfilename == NULL) throw multipr::NullArgumentException("", __FILE__, __LINE__);
164         
165         vector<string> res;
166         med_err ret;
167         
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__);
173         
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__);      
179         
180         // for each mesh in the file (warning: first mesh is number 1)
181         for (int itMesh = 1 ; itMesh <= nbMeshes ; itMesh++)
182         {
183                 char meshName[MED_TAILLE_NOM + 1];
184                 
185                 int meshDim;
186                 med_maillage meshType;
187                 char meshDesc[MED_TAILLE_DESC + 1]; 
188                 
189                 ret = MEDmaaInfo(
190                         file, 
191                         itMesh, 
192                         meshName, 
193                         &meshDim, 
194                         &meshType, 
195                         meshDesc);
196                         
197                 if (ret != 0) throw multipr::IOException("i/o error while reading mesh information in MED file", __FILE__, __LINE__);
198                 
199                 res.push_back(meshName);
200         }
201         
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__);
207         
208         return res;
209 }
210
211
212 vector<pair<string,int> > multipr::getListScalarFields(const char* pMEDfilename)
213 {
214         if (pMEDfilename == NULL) throw multipr::NullArgumentException("", __FILE__, __LINE__);
215         
216         vector<pair<string, int> > res;
217         med_err ret;
218         
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__);
224         
225         //---------------------------------------------------------------------
226         // Read number of fields
227         //---------------------------------------------------------------------
228         med_int numFields = MEDnChamp(file, 0);
229         if (numFields <= 0) throw IOException("", __FILE__, __LINE__);
230         
231         //---------------------------------------------------------------------
232         // For each field, read its name
233         //---------------------------------------------------------------------
234         for (int itField = 1 ; itField <= numFields ; itField++)
235         {
236                 char           name[MED_TAILLE_NOM + 1];
237                 med_type_champ type;
238                 
239                 med_int numComponents = MEDnChamp(file, itField);
240         
241                 if (numComponents < 0) throw IOException("", __FILE__, __LINE__);
242                 
243                 // collect scalar field only (not vectorial fields)
244                 if (numComponents != 1) 
245                         continue;
246                 
247                 char* strComponent = new char[numComponents * MED_TAILLE_PNOM + 1];
248                 char* strUnit      = new char[numComponents * MED_TAILLE_PNOM + 1];
249                 
250                 strComponent[0] = '\0';
251                 strUnit[0] = '\0';
252                 
253                 med_err ret = MEDchampInfo(
254                         file, 
255                         itField, 
256                         name, 
257                         &(type), 
258                         strComponent, 
259                         strUnit, 
260                         numComponents);
261                 
262                 if (ret != 0) throw IOException("", __FILE__, __LINE__);
263                 
264                 delete[] strUnit;
265                 delete[] strComponent;
266                 
267                 med_int numTimeStep = MEDnPasdetemps(
268                         file, 
269                         name, 
270                         MED_NOEUD, 
271                         (med_geometrie_element) 0);
272                 
273                 if (numTimeStep < 0) throw IOException("", __FILE__, __LINE__);
274                 
275                 if (numTimeStep == 0)
276                 {
277                         numTimeStep = MEDnPasdetemps(
278                                 file, 
279                                 name, 
280                                 MED_MAILLE, 
281                                 MED_TETRA10);
282                 
283                         if (numTimeStep  < 0) throw IOException("", __FILE__, __LINE__);        
284                         
285                 }
286                 
287                 res.push_back(make_pair(name, numTimeStep));
288         }
289         
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__);
295         
296         return res;
297 }
298
299
300 // EOF