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.1-en.txt
13 /*--------------------------------------------------------------------------*/
19 #include "filesmanagement.h"
21 #include "islittleendian.h"
22 #include "convert_tools.h"
23 #include "localization.h"
24 /*--------------------------------------------------------------------------*/
27 /*--------------------------------------------------------------------------*/
28 /* =================================================
29 * reads data and store them without type conversion
30 * =================================================*/
31 /*--------------------------------------------------------------------------*/
32 #define MGET_CHAR_NC(Type) \
34 Type *val = (Type *) res ; \
35 items=(int)fread(val,sizeof(Type),n,fa); \
38 /*--------------------------------------------------------------------------*/
39 #define MGET_NC(Type) { \
40 Type *val = (Type *) res ; \
43 for ( i=0; i< n; i++) { \
44 unsigned long long tmp; \
45 items+=(int)fread(&tmp,sizeof(Type),1,fa); \
46 swap_generic((char *)&tmp,(char *)val, sizeof(Type)); \
50 else items=(int)fread(val,sizeof(Type),n,fa); \
52 /*--------------------------------------------------------------------------*/
53 #define MGET_GEN_NC(NumType,cf) \
57 MGET_NC(NumType);break; \
59 swap = (islittleendian()==1)? 1:0; \
60 MGET_NC(NumType); break; \
62 swap = (islittleendian()==1) ? 0:1; \
63 MGET_NC(NumType); break; \
65 sciprint(_("%s: Wrong value for input argument #%d: '%s' or '%s' or '%s' expected.\n"),"mget",4," ","b","l"); \
69 /*--------------------------------------------------------------------------*/
70 void C2F(mgetnc) (int *fd, void *res, int *n1, char *type, int *ierr)
73 int i, items, n = *n1;
77 if ((fa = GetFileOpenedInScilab(*fd)) == NULL)
79 sciprint(_("%s: No input file associated to logical unit %d.\n"), "mget", *fd);
83 swap = GetSwapStatus(*fd);
84 /* Safe version for optional type modifiers */
85 c1 = (type[0] && type[1]) ? type[1] : ' ';
86 c2 = (type[0] && type[1] && type[2]) ? type[2] : ' ';
94 MGET_GEN_NC(long long, c1);
98 MGET_GEN_NC(short, c1);
106 MGET_GEN_NC(double, c1);
110 MGET_GEN_NC(float, c1);
117 MGET_GEN_NC(unsigned int, c2);
121 MGET_GEN_NC(unsigned long long, c2);
125 MGET_GEN_NC(unsigned short, c2);
129 MGET_GEN_NC(unsigned int, ' ');
133 MGET_CHAR_NC(unsigned char);
147 *ierr = -(items) - 1;
148 /** sciprint("Read %d out of\n",items,n); **/
153 /*--------------------------------------------------------------------------*/
154 /* =================================================
155 * reads data and store them in double
156 * =================================================*/
158 /* conversion macro */
159 #define CONVGD(Type) \
161 Type *val = (Type *) res ; \
162 for ( i = items-1 ; i >=0 ; i--) \
165 /*--------------------------------------------------------------------------*/
166 #define MGET_GEN(NumType,cf) MGET_GEN_NC(NumType,cf); CONVGD(NumType);
167 #define MGET_CHAR(NumType) MGET_CHAR_NC(NumType); CONVGD(NumType);
168 /*--------------------------------------------------------------------------*/
169 /* reads data and store them in double */
170 void mget2(FILE * fa, int swap2, double *res, int n, char *type, int *ierr)
176 c1 = (strlen(type) > 1) ? type[1] : ' ';
177 c2 = (strlen(type) > 2) ? type[2] : ' ';
185 MGET_GEN(long long, c1);
197 MGET_GEN(double, c1);
208 MGET_GEN(unsigned int, c2);
212 MGET_GEN(unsigned long long, c2);
216 MGET_GEN(unsigned short, c2);
220 MGET_GEN(unsigned int, ' ');
224 MGET_CHAR(unsigned char);
238 *ierr = -(items) - 1;
239 /** sciprint("Read %d out of\n",items,n); **/
244 /*--------------------------------------------------------------------------*/
245 void C2F(mget) (int *fd, double *res, int *n, char *type, int *ierr)
250 nc = (int)strlen(type);
254 sciprint(_("%s: Wrong size for input argument #%d: Non-empty string expected.\n"), "mput", 4, type);
258 fa = GetFileOpenedInScilab(*fd);
261 swap2 = GetSwapStatus(*fd);
262 mget2(fa, swap2, res, *n, type, ierr);
265 sciprint(_("%s: Wrong value for input argument #%d: Format not recognized.\n"), "mget", 4);
270 sciprint(_("%s: No input file associated to logical unit %d.\n"), "mget", *fd);
275 /*--------------------------------------------------------------------------*/