1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2007 - INRIA
4  * ...
5  *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14  *
15  */
16 /*--------------------------------------------------------------------------*/
17 #include <string.h>
18 #include "filemanager.hxx"
19 extern "C"
20 {
21 #ifndef _MSC_VER
22 #include <stdint.h>
23 #else
24 #define int32_t long
25 #define uint32_t unsigned long
26 #endif
27 #include "mput.h"
28 #include "sciprint.h"
29 #include "islittleendian.h"
30 #include "convert_tools.h"
31 #include "localization.h"
32 #include "configvariable_interface.h"
33 #include "charEncoding.h"
34 }
35 /*--------------------------------------------------------------------------*/
36 /*===============================================
37  * function to write data without type conversion
38  *===============================================*/
39 #define MPUT_CHAR_NC(Type)			\
40   {						\
41     Type *val = (Type *) res ;			\
42     fwrite(val,sizeof(Type),n,fa);		\
43   }
44 /*--------------------------------------------------------------------------*/
45 #define MPUT_NC(Type) {					\
46     Type *val = (Type *) res ;				\
47     if ( swap) {					\
48       unsigned long long temp;				\
49       for ( i=0; i< n; i++) {				\
50 	swap_generic((char *)val,(char *)&temp, sizeof(Type));	\
51         val++;						\
52 	fwrite(&temp,sizeof(Type),1,fa);		\
53       }							\
54     }							\
55     else fwrite(val,sizeof(Type),n,fa);			\
56   }
57 /*--------------------------------------------------------------------------*/
58 #define MPUT_GEN_NC(Type,cf)						\
59   switch ( cf )								\
60     {									\
61     case ' ':								\
62       MPUT_NC(Type); break;						\
63     case 'b':								\
64       swap = (islittleendian()==1) ? 1 : 0;				\
65     MPUT_NC(Type); break;						\
66     case 'l':								\
67       swap = (islittleendian()==1) ? 0 : 1;				\
68     MPUT_NC(Type); break;						\
69     default:								\
70       sciprint(_("%s: Wrong value for input argument #%d (%s): '%s' or '%s' or '%s' expected.\n"),"mput",4,type," ","b","l"); \
71       *ierr=1;return;							\
72     }
73 /*--------------------------------------------------------------------------*/
74 /*================================================
75  * function to write data stored in double
76  *================================================*/
77 /** used for char **/
78 #define MPUT_CHAR(Type) {			\
79     for ( i=0; i< n; i++) {			\
80       Type  val = (char) *res++;		\
81       fwrite(&val,sizeof(Type),1,fa);		\
82     }						\
83   }
84 /*--------------------------------------------------------------------------*/
85 /** write in a machine independant way : i.e data
86     is swaped if necessary to output little-endian
87     data **/
88 
89 #define MPUT(Type) {						\
90     Type val;							\
91     for ( i=0; i< n; i++) {					\
92       val =(Type)res[i];					\
93       if ( swap) {						\
94 	unsigned long long temp;				\
95 	swap_generic((char *)&val,(char *)&temp, sizeof(Type));	\
96 	fwrite(&temp,sizeof(Type),1,fa);			\
97       }								\
98       else fwrite(&val,sizeof(Type),1,fa);			\
99     }								\
100   }
101 
102 /*--------------------------------------------------------------------------*/
103 /** The output mode is controlled by type[1] **/
104 #define MPUT_GEN(Type,cf)						\
105   switch ( cf )								\
106     {									\
107     case ' ':								\
108       MPUT(Type); break;						\
109     case 'b':								\
110       swap = (islittleendian()==1) ? 1 : 0;				\
111     MPUT(Type); break;							\
112     case 'l':								\
113       swap = (islittleendian()==1) ? 0 : 1;				\
114     MPUT(Type); break;							\
115     default:								\
116       if ( getWarningMode() ) sciprint(_("%s: Wrong value for input argument #%d (%s): '%s' or '%s' or '%s' expected.\n"),"mput",4,type," ","b","l"); \
117       *ierr=1;return;							\
118     }
119 /*--------------------------------------------------------------------------*/
mput2(FILE * fa,int swap,double * res,int n,char * type,int * ierr)120 void mput2 (FILE *fa, int swap, double *res, int n, char *type, int *ierr)
121 {
122     char c1, c2;
123     int i;
124     *ierr = 0;
125     c1 = ( strlen(type) > 1) ? type[1] : ' ';
126     c2 = ( strlen(type) > 2) ? type[2] : ' ';
127     switch ( type[0] )
128     {
129         case 'i' :
130             MPUT_GEN(int, c1);
131             break;
132         case 'l' :
133             MPUT_GEN(long long, c1);
134             break;
135         case 's' :
136             MPUT_GEN(short, c1);
137             break;
138         case 'c' :
139             MPUT_CHAR(char) ;
140             break;
141         case 'd' :
142             MPUT_GEN(double, c1);
143             break;
144         case 'f' :
145             MPUT_GEN(float, c1);
146             break;
147         case 'u' :
148             switch ( c1 )
149             {
150                 case 'i' :
151                     MPUT_GEN(unsigned int, c2);
152                     break;
153                 case 'l' :
154                     MPUT_GEN(unsigned long long, c2);
155                     break;
156                 case 's' :
157                     MPUT_GEN(unsigned short, c2);
158                     break;
159                 case ' ' :
160                     MPUT_GEN(unsigned int, ' ');
161                     break;
162                 case 'c' :
163                     MPUT_CHAR(unsigned char);
164                     break;
165                 default :
166                     *ierr = 1;
167                     return ;
168             }
169             break;
170         default :
171             *ierr = 1;
172             break;
173     }
174 }
175 /*--------------------------------------------------------------------------*/
C2F(mput)176 void C2F(mput) (int *fd, double *res, int *n, char *type, int *ierr)
177 {
178     *ierr = 0;
179     if (strlen(type) == 0)
180     {
181         if (getWarningMode())
182         {
183             sciprint(_("%s: Wrong size for input argument #%d ('%s'): Non-empty string expected.\n"), "mput", 4, type);
184         }
185         *ierr = 2;
186         return;
187     }
188 
189     types::File *pFile = FileManager::getFile(*fd);
190     if (pFile && pFile->getFiledesc())
191     {
192         mput2(pFile->getFiledesc(), pFile->getFileSwap(), res, *n, type, ierr);
193         if (*ierr > 0)
194         {
195             if (getWarningMode())
196             {
197                 sciprint(_("%s: Wrong value for input argument #%d ('%s'): Format not recognized.\n"), "mput", 4, type);
198             }
199         }
200     }
201     else
202     {
203         if (getWarningMode())
204         {
205             sciprint(_("%s: No input file associated to logical unit %d.\n"), "mput", *fd);
206         }
207         *ierr = 3;
208     }
209 }
210 /*--------------------------------------------------------------------------*/
211 
212 
213