Salome HOME
update with the version in the OCC branch OCC_development_generic_2006.
[modules/med.git] / src / MEDWrapper / V2_1 / Core / MEDattrNumEcrire.cxx
1 /*************************************************************************
2 * COPYRIGHT (C) 1999 - 2002  EDF R&D
3 * THIS LIBRARY IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY
4 * IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE 
5 * AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; 
6 * EITHER VERSION 2.1 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
7 *  
8 * THIS LIBRARY IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT
9 * WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU
11 * LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
12 *
13 * YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE
14 * ALONG WITH THIS LIBRARY; IF NOT, WRITE TO THE FREE SOFTWARE FOUNDATION,
15 * INC., 59 TEMPLE PLACE, SUITE 330, BOSTON, MA 02111-1307 USA
16 *
17 *************************************************************************/
18
19
20 #include "med.hxx"
21 #include "med_outils.hxx"
22
23 /*
24  * - Nom de la fonction : _MEDattrNumEcrire
25  * - Description : ecriture d'un attribut entier
26  * - Parametres :
27  *     - pere (IN) : l'ID de l'objet HDF pere ou placer l'attribut
28  *     - type (IN) : le type du champ {MED_REEL64,MED_INT}
29  *     - nom  (IN) : le nom de l'attribut
30  *     - val  (IN) : la valeur de l'attribut
31  * - Resultat : 0 en cas de succes, -1 sinon
32  */
33
34 namespace med_2_1{
35
36 med_err 
37 _MEDattrNumEcrire(med_idt pere,med_type_champ type,char *nom,unsigned char *val, 
38                   med_mode_acces mode)
39 {
40   med_idt aid,attr;
41   med_err ret;
42   int type_hdf;
43
44   switch(type)
45     {
46     case MED_REEL64 :
47       /* 1) IA32 is LE but due to an (?HDF convertion BUG?) when using H5T_NATIVE_DOUBLE/MED_REEL64? under PCLINUX
48          the file read under SGI is incorrect
49          2) Compaq OSF/1 is LE, since we force SGI64,SUN4SOL2,HP to write double in LE even if they are BE, mips OSF/1 must be BE
50          REM  : Be careful of compatibility between MED files when changing this (med2.2)                    */
51 #if defined(PCLINUX) || defined(OSF1)
52       type_hdf = H5T_IEEE_F64BE;
53 #else 
54       type_hdf = H5T_IEEE_F64LE;
55 #endif
56       break;
57       
58     case MED_INT :
59 #if defined(IRIX64) || defined(OSF1)
60       type_hdf = H5T_NATIVE_LONG; 
61 #elif defined(PCLINUX)
62       /* This explicit convertion avoid a core dump between in HDF&ASTER when reading on SGI
63          a file written under a PCLINUX system (in founction H5Tconvert),
64          we don't know yet if it is an HDF bug or an ASTER one */
65       /* The problem seems to be in convertion process between INT32LE->INT32BE ? */
66       type_hdf = H5T_STD_I32BE;
67       if ((H5Tconvert(H5T_NATIVE_INT,H5T_STD_I32BE,1,(void *)val,NULL,NULL)) < 0) 
68           return -1;
69 #else
70       type_hdf = H5T_NATIVE_INT;
71 #endif
72       break;
73
74     default :
75       return -1;
76     }
77
78   if ((aid = H5Screate(H5S_SCALAR)) < 0)
79     return -1;
80
81   if ( ((attr = H5Aopen_name(pere,nom)) > 0) && (mode != MED_REMP) )
82     return -1;
83   else
84     if ( attr < 0)
85       if ((attr = H5Acreate(pere,nom,type_hdf,aid,H5P_DEFAULT)) < 0) return -1;  
86
87   if ((ret = H5Awrite(attr,type_hdf,val)) < 0)
88     return -1;
89
90
91   if ((ret = H5Sclose(aid)) < 0)
92     return -1;
93   if ((ret = H5Aclose(attr)) < 0)
94     return -1;
95
96 #if defined(PCLINUX)
97   /* This explicit convertion cancel the previous on which avoid a mysterious bug between HDF&ASTER when reading
98      a file written under a PCLINUX system, we don't know yet if it is an HDF bug or an ASTER one */  
99   if (type == MED_INT) 
100     if ((H5Tconvert(H5T_STD_I32BE,H5T_NATIVE_INT,1,(void *)val,NULL,NULL)) < 0) 
101       return -1;
102 #endif
103
104   return 0;
105 }
106
107 }