2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2007 - INRIA
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution. The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
13 /*--------------------------------------------------------------------------*/
19 #include "filesmanagement.h"
21 #include "islittleendian.h"
22 #include "convert_tools.h"
23 #include "localization.h"
24 #include "warningmode.h"
25 /*--------------------------------------------------------------------------*/
26 extern int swap; /* defined in mget.c */
27 /*--------------------------------------------------------------------------*/
29 /*===============================================
30 * function to write data without type conversion
31 *===============================================*/
32 #define MPUT_CHAR_NC(Type) \
34 Type *val = (Type *) res ; \
35 fwrite(val,sizeof(Type),n,fa); \
37 /*--------------------------------------------------------------------------*/
38 #define MPUT_NC(Type) { \
39 Type *val = (Type *) res ; \
41 unsigned long long temp; \
42 for ( i=0; i< n; i++) { \
43 swap_generic((char *)val,(char *)&temp, sizeof(Type)); \
45 fwrite(&temp,sizeof(Type),1,fa); \
48 else fwrite(val,sizeof(Type),n,fa); \
50 /*--------------------------------------------------------------------------*/
51 #define MPUT_GEN_NC(Type,cf) \
55 MPUT_NC(Type); break; \
57 swap = (islittleendian()==1) ? 1 : 0; \
58 MPUT_NC(Type); break; \
60 swap = (islittleendian()==1) ? 0 : 1; \
61 MPUT_NC(Type); break; \
63 sciprint(_("%s: Wrong value for input argument #%d (%s): '%s' or '%s' or '%s' expected.\n"),"mput",4,type," ","b","l"); \
66 /*--------------------------------------------------------------------------*/
67 /* write data without convertion (res is supposed to have type type) */
68 void C2F(mputnc) (int *fd, void * res, int *n1, char *type, int *ierr)
75 if ((fa = GetFileOpenedInScilab(*fd)) == NULL)
77 if ( getWarningMode() )
79 sciprint(_("%s: No input file associated to logical unit %d.\n"), "mput", *fd);
85 c1 = ( strlen(type) > 1) ? type[1] : ' ';
86 c2 = ( strlen(type) > 2) ? type[2] : ' ';
90 MPUT_GEN_NC(long, c1);
93 MPUT_GEN_NC(long long, c1);
96 MPUT_GEN_NC(short, c1);
102 MPUT_GEN_NC(double, c1);
105 MPUT_GEN_NC(float, c1);
111 MPUT_GEN_NC(unsigned long, c2);
114 MPUT_GEN_NC(unsigned long long, c2);
117 MPUT_GEN_NC(unsigned short, c2);
120 MPUT_GEN_NC(unsigned int, ' ');
123 MPUT_CHAR_NC(unsigned char);
135 /*--------------------------------------------------------------------------*/
136 /*================================================
137 * function to write data stored in double
138 *================================================*/
139 /** used for char **/
140 #define MPUT_CHAR(Type) { \
141 for ( i=0; i< n; i++) { \
142 Type val = (char) *res++; \
143 fwrite(&val,sizeof(Type),1,fa); \
146 /*--------------------------------------------------------------------------*/
147 /** write in a machine independant way : i.e data
148 is swaped if necessary to output little-endian
151 #define MPUT(Type) { \
153 for ( i=0; i< n; i++) { \
156 unsigned long long temp; \
157 swap_generic((char *)&val,(char *)&temp, sizeof(Type)); \
158 fwrite(&temp,sizeof(Type),1,fa); \
160 else fwrite(&val,sizeof(Type),1,fa); \
164 /*--------------------------------------------------------------------------*/
165 /** The output mode is controlled by type[1] **/
166 #define MPUT_GEN(Type,cf) \
172 swap = (islittleendian()==1) ? 1 : 0; \
175 swap = (islittleendian()==1) ? 0 : 1; \
178 if ( getWarningMode() ) sciprint(_("%s: Wrong value for input argument #%d (%s): '%s' or '%s' or '%s' expected.\n"),"mput",4,type," ","b","l"); \
181 /*--------------------------------------------------------------------------*/
182 void mput2 (FILE *fa, int swap2, double *res, int n, char *type, int *ierr)
187 c1 = ( strlen(type) > 1) ? type[1] : ' ';
188 c2 = ( strlen(type) > 2) ? type[2] : ' ';
195 MPUT_GEN(long long, c1);
204 MPUT_GEN(double, c1);
213 MPUT_GEN(unsigned long, c2);
216 MPUT_GEN(unsigned long long, c2);
219 MPUT_GEN(unsigned short, c2);
222 MPUT_GEN(unsigned int, ' ');
225 MPUT_CHAR(unsigned char);
237 /*--------------------------------------------------------------------------*/
238 void C2F(mput) (int *fd, double *res, int *n, char *type, int *ierr)
240 int nc = 0, swap2 = 0;
243 if ((nc = (int)strlen(type)) == 0)
245 if ( getWarningMode() )
247 sciprint(_("%s: Wrong size for input argument #%d ('%s'): Non-empty string expected.\n"), "mput", 4, type);
253 if ( *fd == -1 && GetFileOpenedInScilab(*fd) == NULL )
255 sciprint(_("%s: No File opened in Scilab.\n") , "mput" ) ;
260 if ((fa = GetFileOpenedInScilab(*fd)) != NULL)
262 swap2 = GetSwapStatus(*fd);
263 mput2(fa, swap2, res, *n, type, ierr);
266 if ( getWarningMode() )
268 sciprint(_("%s: Wrong value for input argument #%d ('%s'): Format not recognized.\n"), "mput", 4, type);
274 if ( getWarningMode() )
276 sciprint(_("%s: Error while opening, reading or writing '%s'.\n"), "mput", GetFileNameOpenedInScilab(*fd));
281 /*--------------------------------------------------------------------------*/