Localization (fileio)
[scilab.git] / scilab / modules / fileio / sci_gateway / c / sci_msscanf.c
1 /*--------------------------------------------------------------------------*/
2 /* INRIA 2006 */
3 /*--------------------------------------------------------------------------*/
4 #include <stdio.h>
5 #include "machine.h"
6 #include "MALLOC.h"
7 #include "stack-c.h"
8 #include "do_xxprintf.h"
9 #include "do_xxscanf.h"
10 #include "fileio.h"
11 #include "gw_fileio.h"
12 #include "Scierror.h"
13 #include "localization.h"
14
15 /*--------------------------------------------------------------------------*/
16 int int_objsscanf(char *fname,unsigned long fname_len)
17 {
18         static int l1, m1, n1,l2,m2,n2,iarg,maxrow,nrow,rowcount,ncol;
19         int args,retval,retval_s,err,n_count,lw,il1,ild1,skip;
20         int k;
21
22         entry *data;
23         rec_entry buf[MAXSCAN];
24         sfdir  type[MAXSCAN],type_s[MAXSCAN];
25         char* str;
26
27         Nbvars = 0;
28         CheckRhs(2,3);
29
30         if (Rhs==3)
31         {
32                 GetRhsVar(1,MATRIX_OF_INTEGER_DATATYPE,&m1,&n1,&l1);
33                 if (m1*n1!=1)
34                 {
35                         Scierror(999,_("%s: Wrong size for input argument #%d: Scalar expected.\n"),fname,1);
36                         return 0;
37                 }
38
39                 iarg=2;
40                 maxrow=*istk(l1);
41         }
42         else
43         {
44                 iarg=1;
45                 maxrow=1;
46         }
47
48         lw = iarg + Top - Rhs; /* Scilab string vector */
49         if (! C2F(getwsmat)("sscanf",&Top,&lw,&m1,&n1,&il1,&ild1,6L)) return 0;
50         GetRhsVar(iarg+1,STRING_DATATYPE,&m2,&n2,&l2); /* Format */
51         n_count=StringConvert(cstk(l2))+1;  /* conversion */
52
53         if ( (maxrow >= 0) && (maxrow*n_count>m1*n1) )
54         {
55                 Scierror(999,_("%s: An error occurred: %s\n"),fname,_("Not enough entries."));
56                 return 0;
57         }
58
59         k=0;
60         nrow=maxrow;
61         rowcount = -1; /* number-1 of result lines already got */
62         while (1)
63         {
64                 rowcount++;
65                 if ((maxrow >= 0) && (rowcount >= maxrow)) break;
66                 if ( k >= m1*n1 ) break;
67                 skip=*istk(ild1+k)-1;
68                 SciStrtoStr(istk(il1+skip),&n_count,istk(ild1+k),&str);
69                 k +=n_count;
70
71                 args = Rhs; /* args set to Rhs on entry */
72                 err = do_xxscanf("sscanf",(FILE *)0,cstk(l2),&args,str,&retval,buf,type);
73                 FREE(str);
74                 if ( err < 0 )  return 0;
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                                 break;
88
89                         case MEM_LACK:
90                                 Free_Scan(rowcount,ncol,type_s,&data);
91                                 Scierror(999,_("%s: No more memory.\n"),fname);
92                                 return 0;
93                                 break;
94                         }
95
96                         if (err==MISMATCH) break;
97                 }
98         } /* while */
99
100         /* create Scilab variables 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 /*--------------------------------------------------------------------------*/