7ac7999738c874db3fc38b5efc77729fc0432a52
[scilab.git] / scilab / modules / fileio / sci_gateway / c / sci_mfscanf.c
1 /*--------------------------------------------------------------------------*/
2 /* INRIA 2006 */
3 /*--------------------------------------------------------------------------*/
4 #include <stdio.h>
5 #include "machine.h"
6 #include "stack-c.h"
7 #include "do_xxprintf.h"
8 #include "do_xxscanf.h"
9 #include "fileio.h"
10 #include "gw_fileio.h"
11 #include "filesmanagement.h"
12 #include "Scierror.h"
13 #include "localization.h"
14 /*--------------------------------------------------------------------------*/
15 int int_objfscanf(char *fname,unsigned long fname_len)
16 {
17         static int l1, m1, n1,l2,m2,n2,iarg,maxrow,nrow,rowcount,ncol;
18         FILE  *f;
19         int args,retval,err;
20         int retval_s=0;
21         entry *data=NULL;
22         long int pos;
23         int param1=0;
24
25         rec_entry buf[MAXSCAN];
26         sfdir  type[MAXSCAN],type_s[MAXSCAN];
27
28         Nbvars = 0;
29         CheckRhs(2,3);
30
31         if (Rhs==3)
32         {
33                 GetRhsVar(1,MATRIX_OF_INTEGER_DATATYPE,&m1,&n1,&l1);
34                 if (m1*n1 != 1 )
35                 {
36                         Scierror(999,_("%s: Wrong size for first input argument: Scalar expected.\n"),fname);
37                         return 0;
38                 }
39                 iarg=2;
40                 maxrow=*istk(l1);
41         }
42         else
43         {
44                 iarg=1;
45                 maxrow=1;
46         }
47
48         GetRhsVar(iarg,MATRIX_OF_INTEGER_DATATYPE,&m1,&n1,&l1);
49         GetRhsVar(iarg+1,STRING_DATATYPE,&m2,&n2,&l2);/* format */
50
51         param1=*istk(l1);
52         StringConvert(cstk(l2));  /* conversion */
53         if ((f= GetFileOpenedInScilab(param1)) == (FILE *)0)
54         {
55                 Scierror(999,_("%s: Wrong file descriptor: %d.\n"),fname,*istk(l1));
56                 return 0;
57         }
58
59         nrow=maxrow;
60         rowcount = -1;
61
62         while (1)
63         {
64                 rowcount++;
65                 if ((maxrow >= 0) && (rowcount >= maxrow)) break;
66                 args = Rhs; /* args set to Rhs on entry */
67                 pos=ftell(f);
68                 if ( do_xxscanf("fscanf",f,cstk(l2),&args,(char *)0,&retval,buf,type) < 0 )  return 0;
69                 if ( retval == EOF)
70                 {
71                         /*
72                         Scierror(999,"Error: in %s: end of file reached\n",fname);
73                         */
74                         break;
75                 }
76                 if ((err=Store_Scan(&nrow,&ncol,type_s,type,&retval,&retval_s,buf,&data,rowcount,args)) <0 )
77                 {
78                         switch (err)
79                         {
80                         case MISMATCH:
81                                 if (maxrow>=0)
82                                 {
83                                         Free_Scan(rowcount,ncol,type_s,&data);
84                                         Scierror(999,_("%s: Data mismatch.\n"),fname);
85                                         return 0;
86                                 }
87                                 fseek(f,pos,SEEK_SET);
88                                 break;
89
90                         case MEM_LACK:
91                                 Free_Scan(rowcount,ncol,type_s,&data);
92                                 Scierror(999,_("%s: No more memory.\n"),fname);
93                                 return 0;
94                                 break;
95                         }
96                         if (err==MISMATCH) break;
97                 }
98         } /* while */
99
100         /* create Scilab variable with each column of data */
101         err=Sci_Store(rowcount,ncol,data,type_s,retval_s);
102         Free_Scan(rowcount,ncol,type_s,&data);
103         if (err==MEM_LACK) { Scierror(999,_("%s: No more memory.\n"),fname);}
104         return 0;
105 }
106 /*--------------------------------------------------------------------------*/