]> SALOME platform Git repositories - modules/multipr.git/blob - src/MULTIPR/MULTIPR_Utils.cxx
Salome HOME
*** empty log message ***
[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     // find (reverse) the first occurrence of '/' in the given string
83     char* res = strrchr(pFilename, '/');
84     
85     // if there is no '/'...
86     if (res == NULL)
87     {
88         return "";
89     }
90     else
91     {
92         int size = res - pFilename + 1;
93         char path[256];
94         memcpy(path, pFilename, size);
95         path[size] = '\0';
96         return path;
97     }
98 }
99
100
101 void multipr::copyFile(const char* pFilename, const char* pDestDir)
102 {
103     const char* srcDir = multipr::getPath(pFilename).c_str();
104     if (strcmp(srcDir, pDestDir) == 0) return;
105     
106     const char* filenameWithoutPath = multipr::getFilenameWithoutPath(pFilename).c_str();
107     char pDstFilename[256];
108     sprintf(pDstFilename, "%s%s", pDestDir, filenameWithoutPath);
109     
110     // open binary source file
111     FILE* src = fopen(pFilename, "rb");
112     if (src == NULL) return;
113     
114     // open (create) binary destination file
115     FILE* dst = fopen(pDstFilename, "wb");
116     if (dst == NULL) return;
117     
118     const int size = 65536; // size of buffer is 64Kb
119     char* buf = new char[size];
120     int ret;
121     ret = fread(buf, 1, size, src);
122     while (ret != 0)
123     {
124         fwrite(buf, 1, ret, dst); // write to destination
125         ret = fread(buf, 1, size, src); // read from source
126     }
127     
128     delete[] buf;
129     fclose(src);
130     fclose(dst);
131 }
132
133
134 void multipr::printArray2D(
135      const med_float* pData, 
136     const int        pNumElt,
137     const int        pDimElt,
138     const char*      pPrefix)
139 {
140     for (int itElt = 0 ; itElt < pNumElt ; itElt++)
141     {
142         cout << pPrefix << " " << (itElt + 1) << ": ";
143         for (int itDim = 0 ; itDim < pDimElt ; itDim++)
144         {
145             cout << pData[itElt * pDimElt + itDim] << " ";
146         }
147         cout << endl;
148     }
149 }
150
151
152 string multipr::realToString(med_float mV)
153 {
154     char str[32];
155     sprintf(str, "%lf", mV);
156     trim(str, '0');
157     int len = strlen(str);
158     if (str[len - 1] == '.')
159     {
160         str[len] = '0';
161         str[len + 1] = '\0';
162     }
163     return string(str);
164 }
165
166
167 vector<string> multipr::getListMeshes(const char* pMEDfilename)
168 {
169     if (pMEDfilename == NULL) throw multipr::NullArgumentException("", __FILE__, __LINE__);
170     
171     vector<string> res;
172     med_err ret;
173     
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__);
179     
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__);    
185     
186     // for each mesh in the file (warning: first mesh is number 1)
187     for (int itMesh = 1 ; itMesh <= nbMeshes ; itMesh++)
188     {
189         char meshName[MED_TAILLE_NOM + 1];
190         
191         int meshDim;
192         med_maillage meshType;
193         char meshDesc[MED_TAILLE_DESC + 1]; 
194         
195         ret = MEDmaaInfo(
196             file, 
197             itMesh, 
198             meshName, 
199             &meshDim, 
200             &meshType, 
201             meshDesc);
202             
203         if (ret != 0) throw multipr::IOException("i/o error while reading mesh information in MED file", __FILE__, __LINE__);
204         
205         res.push_back(meshName);
206     }
207     
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__);
213     
214     return res;
215 }
216
217
218 vector<pair<string,int> > multipr::getListScalarFields(const char* pMEDfilename)
219 {
220     if (pMEDfilename == NULL) throw multipr::NullArgumentException("", __FILE__, __LINE__);
221     
222     vector<pair<string, int> > res;
223     med_err ret;
224     
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__);
230     
231     //---------------------------------------------------------------------
232     // Read number of fields
233     //---------------------------------------------------------------------
234     med_int numFields = MEDnChamp(file, 0);
235     if (numFields <= 0) throw IOException("", __FILE__, __LINE__);
236     
237     //---------------------------------------------------------------------
238     // For each field, read its name
239     //---------------------------------------------------------------------
240     for (int itField = 1 ; itField <= numFields ; itField++)
241     {
242         char           name[MED_TAILLE_NOM + 1];
243         med_type_champ type;
244         
245         med_int numComponents = MEDnChamp(file, itField);
246     
247         if (numComponents < 0) throw IOException("", __FILE__, __LINE__);
248         
249         // collect scalar field only (not vectorial fields)
250         if (numComponents != 1) 
251             continue;
252         
253         // temporary buffers
254         char* strComponent = new char[numComponents * MED_TAILLE_PNOM + 1];
255         char* strUnit      = new char[numComponents * MED_TAILLE_PNOM + 1];
256         
257         strComponent[0] = '\0';
258         strUnit[0] = '\0';
259         
260         med_err ret = MEDchampInfo(
261             file, 
262             itField, 
263             name, 
264             &(type), 
265             strComponent, 
266             strUnit, 
267             numComponents);
268         
269         if (ret != 0) throw IOException("", __FILE__, __LINE__);
270         
271         delete[] strUnit;
272         delete[] strComponent;
273         
274         med_int numTimeStamps = MEDnPasdetemps(
275             file, 
276             name, 
277             MED_NOEUD, 
278             (med_geometrie_element) 0);
279         
280         if (numTimeStamps < 0) throw IOException("", __FILE__, __LINE__);
281         
282         if (numTimeStamps == 0)
283         {
284             numTimeStamps = MEDnPasdetemps(
285                 file, 
286                 name, 
287                 MED_MAILLE, 
288                 MED_TETRA10);
289         
290             if (numTimeStamps  < 0) throw IOException("", __FILE__, __LINE__);    
291             
292         }
293         
294         // add the pair(name, #time stamps) to the result
295         res.push_back( make_pair(name, numTimeStamps) );
296     }
297     
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__);
303     
304     // return the list of fields
305     return res;
306 }
307
308
309 // EOF