1
/*********************************************************************
2
* This Software is ( Copyright ENPC 1998 ) *
3
* Jean-Philippe Chancelier Enpc/Cergrene *
4
* See also the copyright below for do_printf *
5
* interface and implementation of xxprintf and xxscanf functions *
8
* Modified May 2000 by S. Steer for vectorization of *
10
*********************************************************************/
21
#include <ctype.h> /* isdigit */
22
#include "../graphics/Math.h"
23
#include "../stack-c.h"
24
#include "../sun/Sun.h"
26
extern char * SciGetLine __PARAMS((char *));
27
extern FILE *GetFile __PARAMS((int *));
28
extern void C2F(zzledt1) __PARAMS((char *buffer, int *buf_size, int *len_line, int *eof, long int dummy1));
29
extern int C2F(xscion) __PARAMS((int *));
30
extern int sciprint2 __PARAMS((int i,char *fmt, ...));
31
static int do_printf __PARAMS((char *fname,FILE * fp, char *format,int n_args,
32
int arg_cnt,int lcount,char **strv));
34
/*if maxscan is increased don't forget to chage the (*printer)(......)
35
in do_scanf procedure */
40
long unsigned int lui;
41
short unsigned int sui;
53
typedef enum {SF_C,SF_S,SF_LUI,SF_SUI,SF_UI,SF_LI,SF_SI,SF_I,SF_LF,SF_F} sfdir;
54
static int Store_Scan __PARAMS((int *nrow,int *ncol,sfdir *type_s,sfdir *type,
55
int *retval, int*retval_s, rec_entry *buf,
56
entry **data,int rowcount,int n));
57
static void Free_Scan __PARAMS((int rowcount,int ncol,sfdir *type_s,entry **data));
58
static int Sci_Store __PARAMS((int nrow,int ncol,entry* data,sfdir *type,int retval));
60
int SciStrtoStr __PARAMS((int *Scistring,int *nstring,int *ptrstrings,char **strh));
62
static int do_scanf __PARAMS((char *fname, FILE *fp, char *format,int *nargs,
63
char *strv,int *retval,rec_entry *buf,sfdir *type));
70
#define NOT_ENOUGH_ARGS -5
72
static int GetString __PARAMS((char *fname,int *first,int *arg,int narg,
73
int *ir,int ic,char **sval) );
74
static int GetScalarInt __PARAMS((char *fname,int *first,int *arg,int narg,
75
int *ir,int ic,int *ival));
76
static int GetScalarDouble __PARAMS((char *fname,int *first,int *arg,int narg,
77
int *ir,int ic,double *dval));
78
static int StringConvert __PARAMS((char *str));
79
static int ReadLine __PARAMS((FILE *fd));
80
int NumTokens __PARAMS((char *str));
82
/*********************************************************************
83
* Scilab printf function OK
84
*********************************************************************/
86
int int_objprintf(fname)
89
static int l1, m1, n1, lcount, rval, k, typ, mx, mk, nk;
95
Scierror(999,"Error:\tRhs must be > 0\r\n");
98
GetRhsVar(1,"c",&m1,&n1,&l1);
102
GetMatrixdims(2,&mx,&nk);
103
for (k=3;k<=Rhs;k++) {
104
GetMatrixdims(k,&mk,&nk);
110
rval=do_printf("printf",stdout,cstk(l1),Rhs,1,lcount,(char **)0);
114
if ((rval = do_printf("printf",stdout,cstk(l1),Rhs,1,lcount,(char **)0)) < 0) break;
116
if (lcount>mx) break;
118
if (rval == RET_BUG) return 0;
119
LhsVar(1)=0; /** No return value **/
124
/*********************************************************************
125
* Scilab fprintf function OK
126
*********************************************************************/
128
int int_objfprintf(fname)
132
static int l1, m1, n1,l2,m2,n2,lcount,rval, mx, mk, nk, k;
138
Scierror(999,"Error:\tRhs must be >= 2\r\n");
141
GetRhsVar(1,"i",&m1,&n1,&l1); /* file id */
142
GetRhsVar(2,"c",&m2,&n2,&l2); /* format */
143
if ((f= GetFile(istk(l1))) == (FILE *)0)
145
Scierror(999,"fprintf:\t wrong file descriptor %d\r\n",*istk(l1));
150
GetMatrixdims(3,&mx,&nk);
151
for (k=4;k<=Rhs;k++) {
152
GetMatrixdims(k,&mk,&nk);
158
rval=do_printf("fprintf",f,cstk(l2),Rhs,2,lcount,(char **)0);
162
if ((rval=do_printf("fprintf",f,cstk(l2),Rhs,2,lcount,(char **)0)) < 0) break;
164
if (lcount>mx) break;
166
if (rval == RET_BUG) return 0;
167
LhsVar(1)=0; /** No return value **/
172
/*********************************************************************
173
* Scilab sprintf function OK
174
*********************************************************************/
176
int int_objsprintf(fname)
180
static int l1, m1, n1,n2,lcount,rval,blk=200;
184
int n,nmax,cat_to_last,ll;
190
Scierror(999,"Error:\tRhs must be > 0\r\n");
193
GetRhsVar(1,"c",&m1,&n1,&l1);
194
n=0; /* output line counter */
202
if ((rval=do_printf("sprintf",(FILE *) 0,cstk(l1),Rhs,1,lcount,
203
(char **) &lstr)) < 0) break;
207
while (*str != '\0') {
208
if (strncmp(str,"\\n",2) ==0) {
210
if (! cat_to_last) { /*add a new line */
213
if ( (strs = (char **) realloc(strs,nmax*sizeof(char **))) == NULL) goto mem;
215
if ((strs[n]=malloc((k+1))) == NULL) goto mem;
216
strncpy(strs[n],str1, k);
220
else { /* cat to previous line */
221
ll=strlen(strs[n-1]);
222
if ((strs[n-1]=realloc(strs[n-1],(k+1+ll))) == NULL) goto mem;
223
strncpy(&(strs[n-1][ll]),str1, k);
224
strs[n-1][k+ll]='\0';
236
if ((! cat_to_last) || (n == 0)) { /*add a new line */
239
if ( (strs = (char **) realloc(strs,nmax*sizeof(char **))) == NULL) goto mem;
241
if ((strs[n]=malloc((k+1))) == NULL) goto mem;
242
strncpy(strs[n],str1, k);
247
else { /* cat to previous line */
248
ll=strlen(strs[n-1]);
249
if ((strs[n-1]=realloc(strs[n-1],(k+1+ll))) == NULL) goto mem;
250
strncpy(&(strs[n-1][ll]),str1, k);
251
strs[n-1][k+ll]='\0';
254
if (strncmp(str-2,"\\n",2) !=0) cat_to_last=1;
258
if (rval == RET_BUG) return 0;
259
/** Create a Scilab String : lstr must not be freed **/
261
CreateVarFromPtr( 2, "S", &n, &n2, strs);
262
for (k=0;k<n;k++) free(strs[k]);
268
Scierror(999,"sprintf: cannot allocate cannot allocate more memory \r\n");
272
/*********************************************************************
273
* Scilab scanf function
274
*********************************************************************/
278
int int_objscanf(fname)
281
static char String[MAXSTR];
282
static int l1, m1, n1, len= MAXSTR-1,iarg,maxrow,nrow,rowcount,ncol;
283
int args,retval,retval_s,lline,status,iflag,err,n_count;
285
rec_entry buf[MAXSCAN];
286
sfdir type[MAXSCAN],type_s[MAXSCAN];
291
GetRhsVar(1,"i",&m1,&n1,&l1);
293
Scierror(999,"Error: in scanf: incorrect first argument\r\n");
304
GetRhsVar(iarg,"c",&m1,&n1,&l1); /** format **/
305
n_count=StringConvert(cstk(l1))+1; /* conversion */
306
/** Read a line with Scilab read function **/
309
Scierror(999,"Error: in scanf: format cannot include \\n \r\n");
314
rowcount = -1; /* number-1 of result lines already got */
317
if ((maxrow >= 0) && (rowcount >= maxrow)) break;
320
C2F(zzledt)(String,&len,&lline,&status,strlen(String));
322
C2F(zzledt1)(String,&len,&lline,&status,strlen(String));
325
Scierror(999,"Error: in scanf\r\n");
328
if (lline == 0) {String[0] = ' ';lline=1;}
329
/** use the scaned line as input **/
330
args = Rhs; /* args set to Rhs on entry */
331
if (do_scanf("scanf",(FILE *) 0,cstk(l1),&args,String,&retval,buf,type) < 0) return 0;
333
if ((err=Store_Scan(&nrow,&ncol,type_s,type,&retval,&retval_s,
334
buf,&data,rowcount,args)) <0 ) {
338
Free_Scan(rowcount,ncol,type_s,&data);
339
Scierror(999,"Error: in fscanf: data mismatch\r\n");
344
Free_Scan(rowcount,ncol,type_s,&data);
345
Scierror(999,"Error: in scanf: cannot allocate more memory \r\n");
349
if (err ==MISMATCH) break;
353
/* create Scilab variables with each column of data */
354
err=Sci_Store(rowcount,ncol,data,type_s,retval_s);
355
if (err==MEM_LACK) { Scierror(999,"Error: in sscanf: cannot allocate more memory \r\n");}
359
/*********************************************************************
360
* Scilab sscanf function
361
*********************************************************************/
363
int int_objsscanf(fname)
366
static int l1, m1, n1,l2,m2,n2,iarg,maxrow,nrow,rowcount,ncol;
367
int args,retval,retval_s,err,n_count,lw,il1,ild1,skip;
371
rec_entry buf[MAXSCAN];
372
sfdir type[MAXSCAN],type_s[MAXSCAN];
378
GetRhsVar(1,"i",&m1,&n1,&l1);
380
Scierror(999,"Error: in sscanf: incorrect first argument\r\n");
391
lw = iarg + Top - Rhs; /* Scilab string vector */
392
if (! C2F(getwsmat)("sscanf",&Top,&lw,&m1,&n1,&il1,&ild1,6L)) return 0;
393
GetRhsVar(iarg+1,"c",&m2,&n2,&l2); /* Format */
394
n_count=StringConvert(cstk(l2))+1; /* conversion */
396
if (maxrow*n_count>m1*n1) {
397
Scierror(999,"Error: in sscanf: not enough entries in str\r\n");
403
rowcount = -1; /* number-1 of result lines already got */
406
if ((maxrow >= 0) && (rowcount >= maxrow)) break;
408
skip=*istk(ild1+k)-1;
409
SciStrtoStr(istk(il1+skip),&n_count,istk(ild1+k),&str);
412
args = Rhs; /* args set to Rhs on entry */
413
err = do_scanf("sscanf",(FILE *)0,cstk(l2),&args,str,&retval,buf,type);
415
if ( err < 0) return 0;
416
if ((err=Store_Scan(&nrow,&ncol,type_s,type,&retval,&retval_s,
417
buf,&data,rowcount,args)) <0 ) {
421
Free_Scan(rowcount,ncol,type_s,&data);
422
Scierror(999,"Error: in sscanf: data mismatch\r\n");
427
Free_Scan(rowcount,ncol,type_s,&data);
428
Scierror(999,"Error: in sscanf: cannot allocate more memory \r\n");
432
if (err==MISMATCH) break;
435
/* create Scilab variables with each column of data */
436
err=Sci_Store(rowcount,ncol,data,type_s,retval_s);
437
if (err==MEM_LACK) { Scierror(999,"Error: in sscanf: cannot allocate more memory \r\n");}
441
/*********************************************************************
442
* Scilab fscanf function
443
*********************************************************************/
445
int int_objfscanf(fname)
448
static int l1, m1, n1,l2,m2,n2,iarg,maxrow,nrow,rowcount,ncol;
450
int args,retval,retval_s,err;
454
rec_entry buf[MAXSCAN];
455
sfdir type[MAXSCAN],type_s[MAXSCAN];
461
GetRhsVar(1,"i",&m1,&n1,&l1);
463
Scierror(999,"Error: in fscanf: incorrect first argument\r\n");
474
GetRhsVar(iarg,"i",&m1,&n1,&l1);
475
GetRhsVar(iarg+1,"c",&m2,&n2,&l2);/* format */
477
StringConvert(cstk(l2)); /* conversion */
478
if ((f= GetFile(istk(l1))) == (FILE *)0)
480
Scierror(999,"fprintf:\t wrong file descriptor %d\r\n",*istk(l1));
487
if ((maxrow >= 0) && (rowcount >= maxrow)) break;
488
args = Rhs; /* args set to Rhs on entry */
490
if ( do_scanf("fscanf",f,cstk(l2),&args,(char *)0,&retval,buf,type) < 0 ) return 0;
491
if ((err=Store_Scan(&nrow,&ncol,type_s,type,&retval,&retval_s,
492
buf,&data,rowcount,args)) <0 ) {
496
Free_Scan(rowcount,ncol,type_s,&data);
497
Scierror(999,"Error: in fscanf: data mismatch\r\n");
500
fseek(f,pos,SEEK_SET);
503
Free_Scan(rowcount,ncol,type_s,&data);
504
Scierror(999,"Error: in fscanf: cannot allocate more memory \r\n");
508
if (err==MISMATCH) break;
513
/* create Scilab variable with each column of data */
514
err=Sci_Store(rowcount,ncol,data,type_s,retval_s);
515
if (err==MEM_LACK) { Scierror(999,"Error: in sscanf: cannot allocate more memory \r\n");}
520
/*********************************************************************
522
*********************************************************************/
524
int int_objnumTokens(fname)
527
static int l1,m1,n1,l2,un=1;
530
GetRhsVar(1,"c",&m1,&n1,&l1);
531
StringConvert(cstk(l1)); /* conversion */
532
CreateVar(2, "d", &un, &un, &l2);
533
*stk(l2) = (double) NumTokens(cstk(l1));
540
/*********************************************************************
541
* Scilab fprintfMat function
542
*********************************************************************/
544
int int_objfprintfMat(fname)
547
int l1, m1, n1,l2,m2,n2,m3,n3,l3,i,j;
553
GetRhsVar(1,"c",&m1,&n1,&l1);/* file name */
554
GetRhsVar(2,"d",&m2,&n2,&l2);/* data */
557
GetRhsVar(3,"c",&m3,&n3,&l3);/* format */
558
StringConvert(cstk(l3)); /* conversion */
565
if (( f = fopen(cstk(l1),"w")) == (FILE *)0)
567
Scierror(999,"Error: in function %s, cannot open file %s\r\n",
571
for (i = 0 ; i < m2 ; i++ )
573
for ( j = 0 ; j < n2 ; j++)
575
fprintf(f,Format,*stk(l2+i + m2*j));
581
LhsVar(1)=0 ; /** no return value **/
586
/*********************************************************************
587
* Scilab fscanMat function
588
*********************************************************************/
589
#define INFOSIZE 1024
590
static char Info[INFOSIZE];
592
int int_objfscanfMat(fname)
596
static int l1, m1, n1,l2,m2,n2;
597
int i,j,rows,cols,lres,n;
602
CheckRhs(1,1); /** just 1 <<pour l''instant>> **/
604
GetRhsVar(1,"c",&m1,&n1,&l1);/* file name */
607
GetRhsVar(2,"c",&m2,&n2,&l2);/* format */
608
StringConvert(cstk(l2)); /* conversion */
615
if (( f = fopen(cstk(l1),"r")) == (FILE *)0)
617
Scierror(999,"Error: in function %s, cannot open file %s\r\n",
621
/*** first pass to get colums and rows ***/
622
strcpy(Info,"--------");
624
while ( sscanf(Info,"%lf",&x) <= 0 && n != EOF )
625
{ n=ReadLine(f); vl++;}
628
Scierror(999,"Error: in function %s, cannot read data in file %s\r\n",
633
cols = NumTokens(Info);
638
if ( n == EOF || n == 0 ) break;
639
if ( sscanf(Info,"%lf",&x) <= 0) break;
642
if ( cols == 0 || rows == 0) rows=cols=0;
643
CreateVar(Rhs+1, "d", &rows, &cols, &lres);
644
/** second pass to read data **/
646
/** skip non numeric lines **/
647
for ( i = 0 ; i < vl ; i++) ReadLine(f);
648
for (i=0; i < rows ;i++)
649
for (j=0;j < cols;j++)
652
fscanf(f,"%lf",&xloc);
653
*stk(lres+i+rows*j)=xloc;
661
static int ReadLine_Old(fd)
665
n= fscanf(fd,"%[^\n]%*c",Info);
666
if ( n==0) n=fscanf(fd,"%*c");
670
static int ReadLine(fd)
679
sciprint("Error Info buffer is too small (too many columns in your file ?)\r\n");
680
return EOF ;/** A changer XXXX : pour retourner un autre message **/
683
if ( c == '\n') { Info[n] = '\0' ; return 1;}
684
else if ( c == EOF ) return EOF;
691
/***************************************
692
* Test de TestNumTokens
693
***************************************/
695
static void TestNumTokens()
697
char buf[30], format[20];
698
strcpy(format,"%d Tokens in <%s>\n");
699
strcpy(buf,"un deux trois");fprintf(stderr,format,NumTokens(buf),buf);
700
strcpy(buf,"un"); fprintf(stderr,format,NumTokens(buf),buf);
701
strcpy(buf,"un deux trois "); fprintf(stderr,format,NumTokens(buf),buf);
702
strcpy(buf,"un\tdeux\ttrois\n"); fprintf(stderr,format,NumTokens(buf),buf);
703
fprintf(stderr,format, NumTokens((char *) 0) , ((char *) 0));
704
strcpy(buf,"un\t"); fprintf(stderr,format,NumTokens(buf),buf);
705
strcpy(buf," \t\nun"); fprintf(stderr,format,NumTokens(buf),buf);
706
strcpy(buf,"1.0 1.0");fprintf(stderr,format,NumTokens(buf),buf);
710
int NumTokens(string)
715
int lnchar=0,ntok=-1;
716
int length = strlen(string)+1;
719
/** Counting leading white spaces **/
720
sscanf(string,"%*[ \t\n]%n",&lnchar);
721
while ( n != 0 && n != EOF && lnchar <= length )
723
int nchar1=0,nchar2=0;
725
n= sscanf(&(string[lnchar]),
726
"%[^ \n\t]%n%*[ \t\n]%n",buf,&nchar1,&nchar2);
727
lnchar += (nchar2 <= nchar1) ? nchar1 : nchar2 ;
735
/***************************************************************
736
* Emulation of Ansi C XXscanf functions
737
* The number of scaned object is hardwired (MAXSCAN)
738
* and scaned strings (%s,%c %[) are limited to MAX_STR characters
740
* XXXX Could be changed to eliminate the MAXSCAN limitation
742
****************************************************************/
749
typedef int (*PRINTER) __PARAMS((FILE *, char *,...));
750
typedef int (*FLUSH) __PARAMS((FILE *));
758
static int do_scanf (fname,fp,format,nargs,strv,retval,buf,type)
769
char sformat[MAX_STR];
770
void *ptrtab[MAXSCAN];
772
int n_directive_count=0;
777
int l_flag=0, h_flag=0,width_flag,width_val,ign_flag,str_width_flag=0;
778
int num_conversion = -1; /* for error messages and counting arguments*/
781
PRINTER printer; /* pts at fprintf() or sprintf() */
782
if (fp == (FILE *) 0)
784
/* doing sscanf or scanf */
786
printer = (PRINTER) sscanf;
791
target = (char *) fp;
792
printer = (PRINTER) fscanf;
798
/* Traverse format string, doing scanf(). */
803
while (*q != '%' && *q != '\0' ) q++;
804
if ( *q == '%' && *(q+1) == '%' )
807
while (*q != '%' && *q != '\0' ) q++;
814
q++; /** q point to character following % **/
818
* We have found a conversion specifier, figure it out,
819
* then scan the data asociated with it.
823
/* mark the '%' with p1 */
827
/* check for width field */
829
while ( isdigit(((int)*q)) ) q++;
837
sscanf(p1+1,"%d",&width_val);
841
/* check for ignore argument */
847
/* Ignore the argument in the input args */
848
/*num_conversion = Max(num_conversion-1,0);*/
854
/* check for %l or %h */
869
/* directive points to the scan directive */
873
if ( directive == '[' )
876
/** we must find the closing bracket **/
877
while ( *q1 != '\0' && *q1 != ']') q1++;
880
Scierror(998,"Error:\tscanf, unclosed [ directive\r\n");
883
if ( q1 == q +1 || strncmp(q,"[^]",3)==0 )
886
while ( *q1 != '\0' && *q1 != ']') q1++;
889
Scierror(998,"Error:\tscanf unclosed [ directive\r\n");
897
/** accumulate characters in the format up to next % directive **/
899
while ( *q != '\0' && *q != '%' ) q++;
900
if ( *q == '%' && *(q+1) == '%' )
903
while (*q != '%' && *q != '\0' ) q++;
909
/** if (debug) Sciprintf("Now directive [%s],%c\r\n",p,directive); **/
914
if ( num_conversion > MAXSCAN )
916
Scierror(998,"Error:\tscanf too many (%d > %d) conversion required\r\n",
917
num_conversion,MAXSCAN);
923
if (width_flag == 0 ) str_width_flag = 1;
924
if (width_flag == 1 && width_val > MAX_STR-1 )
926
Scierror(998,"Error:\tscanf, width field %d is too long (> %d) for %%[ directive\r\n",
927
width_val,MAX_STR-1);
930
if ((buf[num_conversion].c=malloc(MAX_STR))==NULL) return MEM_LACK;
931
ptrtab[num_conversion] = buf[num_conversion].c;
932
type[num_conversion] = SF_S;
935
if (l_flag + h_flag) {
936
Scierror(998,"Error:\tscanf: bad conversion\r\n");
939
if (width_flag == 0 ) str_width_flag = 1;
940
if (width_flag == 1 && width_val > MAX_STR-1 )
942
Scierror(998,"Error:\tscanf, width field %d is too long (< %d) for %%s directive\r\n",
943
width_val,MAX_STR-1);
946
if ((buf[num_conversion].c=malloc(MAX_STR))==NULL) return MEM_LACK;
947
ptrtab[num_conversion] = buf[num_conversion].c;
948
type[num_conversion] = SF_S;
951
if (l_flag + h_flag) {
952
Scierror(998,"Error:\tscanf: bad conversion\r\n");
955
if ( width_flag == 1 )
956
nc[num_conversion ] = width_val;
958
nc[num_conversion ] = 1;
959
if (width_flag == 1 && width_val > MAX_STR-1 )
961
Scierror(998,"Error:\tscanf width field %d is too long (< %d) for %%c directive\r\n",
962
width_val,MAX_STR-1);
965
if ((buf[num_conversion].c=malloc(MAX_STR))==NULL) return MEM_LACK;
966
ptrtab[num_conversion] = buf[num_conversion].c;
967
type[num_conversion] = SF_C;
975
ptrtab[num_conversion] = &buf[num_conversion].lui;
976
type[num_conversion] = SF_LUI;
980
ptrtab[num_conversion] = &buf[num_conversion].sui;
981
type[num_conversion] = SF_SUI;
985
ptrtab[num_conversion] = &buf[num_conversion].ui;
986
type[num_conversion] = SF_UI;
990
ptrtab[num_conversion] = &buf[num_conversion].li;
991
type[num_conversion] = SF_LI;
994
/** count the n directives since they are not counted by retval **/
1000
ptrtab[num_conversion] = &buf[num_conversion].li;
1001
type[num_conversion] = SF_LI;
1005
ptrtab[num_conversion] = &buf[num_conversion].si;
1006
type[num_conversion] = SF_SI;
1010
ptrtab[num_conversion] = &buf[num_conversion].i;
1011
type[num_conversion] = SF_I;
1021
Scierror(998,"Error:\tscanf: bad conversion\r\n");
1026
ptrtab[num_conversion] = &buf[num_conversion].lf;
1027
type[num_conversion] = SF_LF;
1031
ptrtab[num_conversion] = &buf[num_conversion].f;
1032
type[num_conversion] = SF_F;
1036
Scierror(998,"Error:\tscanf: bad conversion\r\n");
1042
/** we replace %s and %[ directive with a max length field **/
1044
if ( str_width_flag == 1)
1048
char *slast = sformat + MAX_STR-1 -4;
1049
while ( *f1 != '\0' )
1054
if ( *(f1-1) == '%' && ( *(f1) == 's' || *(f1) == '['))
1056
n=sprintf(f2,"%d",MAX_STR-1);
1062
Scierror(998,"Error:\tscanf, format is too long (> %d) \r\n",MAX_STR-1);
1071
/** Calling scanf function :
1072
Only num_conversion +1 qrgument are used
1073
the last arguments transmited points to nothing
1074
but this is not a problem since they won't be used ***/
1077
*retval = (*printer) ((VPTR) target,format,ptrtab[0],ptrtab[1],ptrtab[2],
1078
ptrtab[3],ptrtab[4],ptrtab[5],ptrtab[6],ptrtab[7],
1079
ptrtab[8],ptrtab[9],ptrtab[10],ptrtab[11],ptrtab[12],
1080
ptrtab[13],ptrtab[14],ptrtab[15],ptrtab[16],ptrtab[17],
1081
ptrtab[18],ptrtab[19],ptrtab[20],ptrtab[21],ptrtab[22],
1082
ptrtab[23],ptrtab[24],ptrtab[25],ptrtab[26],ptrtab[27],
1083
ptrtab[28],ptrtab[29]);
1084
/** *nargs counts the number of corectly scaned arguments **/
1085
*nargs = Min(num_conversion+1,Max(*retval+n_directive_count,0));
1087
for ( i=1 ; i <= *nargs ; i++)
1088
if ( type[i-1] == SF_C ) {
1089
sval=(char *) ptrtab[i-1];
1096
/***************************************************************
1098
do_printf: code extraced from RLab and hacked for Scilab
1099
by Jean-Philippe Chancelier 1998.
1101
Copyright (C) 1995 Ian R. Searle
1102
This program is free software; you can redistribute it and/or modify
1103
it under the terms of the GNU General Public License as published by
1104
the Free Software Foundation; either version 2 of the License, or
1105
(at your option) any later version.
1107
This program is distributed in the hope that it will be useful,
1108
but WITHOUT ANY WARRANTY; without even the implied warranty of
1109
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1110
GNU General Public License for more details.
1112
You should have received a copy of the GNU General Public License
1113
along with this program; if not, write to the Free Software
1114
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
1115
*************************************************************** */
1118
/*---------- types and defs for doing printf ------------*/
1121
#define PF_D 2 /* int conversion */
1122
#define PF_LD 3 /* long int */
1123
#define PF_F 4 /* float conversion */
1125
/* for switch on number of '*' and type */
1127
#define AST(num,type) (5*(num)+(type))
1129
/* Buffer for printf **/
1131
#define MAX_SPRINTF_SIZE 4096
1132
static char sprintf_buff[MAX_SPRINTF_SIZE];
1133
static char *sprintf_limit = sprintf_buff + MAX_SPRINTF_SIZE;
1135
static int do_printf (fname,fp,format,nargs,argcnt,lcount,strv)
1139
int nargs,argcnt,lcount;
1148
int l_flag, h_flag; /* seen %ld or %hd */
1154
int num_conversion = 0; /* for error messages */
1155
int pf_type = 0; /* conversion type */
1156
PRINTER printer; /* pts at fprintf() or sprintf() */
1160
int retval; /* Attempt to return C-printf() return-val */
1167
if (fp == (FILE *) 0)
1170
target = sprintf_buff;
1172
printer = (PRINTER) sprintf;
1174
else if ( fp == stdout )
1176
/** doing printf **/
1177
target = (char *) 0; /* unused */
1179
printer = (PRINTER) sciprint2;
1184
target = (char *) fp; /* will never change */
1186
printer = (PRINTER) fprintf;
1189
/* Traverse format string, doing printf(). */
1192
if (fp) /* printf */
1193
/** XXXX on pourrait couper en deux pour separer fp==stdout et fp == file **/
1212
(*printer) ((VPTR) target, "\r");
1218
(*printer) ((VPTR) target, "\r");
1219
(*printer) ((VPTR) target, "\n");
1224
(*printer) ((VPTR) target, "\t");
1229
(*printer) ((VPTR) target, "\\");
1234
/** putc (*q, fp); **/
1235
(*printer) ((VPTR) target, "%c",*q);
1241
/** putc (*q, fp); **/
1242
(*printer) ((VPTR) target, "%c",*q);
1256
if (target > sprintf_limit) /* damaged */
1258
Scierror(998,"Error:\tsprintf problem, buffer too small\r\n");
1265
*strv = sprintf_buff;
1279
if (*++q == '%') /* %% */
1283
/** putc (*q, fp); **/
1284
(*printer) ((VPTR) target, "%c",*q);
1296
* We have found a conversion specifier, figure it out,
1297
* then print the data associated with it.
1301
/* mark the '%' with p */
1305
while (*q == '-' || *q == '+' || *q == ' ' ||
1306
*q == '#' || *q == '0')
1309
ast_cnt = 0; /* asterisk count */
1312
/* Use current arg as field width spec */
1313
rval=GetScalarInt(fname,&previous_t,&arg_cnt,nargs,&ccount,lcount,&m1);
1315
if (rval== NOT_ENOUGH_ARGS) goto bad;
1319
ast[ast_cnt++] = m1;
1324
while ( isdigit(((int)*q))) q++;
1327
if (*q == '.') /* have precision */
1332
/* Use current arg as precision spec */
1333
rval=GetScalarInt(fname,&previous_t,&arg_cnt,nargs,&ccount,lcount,&m1);
1335
if (rval== NOT_ENOUGH_ARGS) goto bad;
1338
ast[ast_cnt++] = m1;
1342
while ( isdigit(((int)*q)) ) q++;
1346
l_flag = h_flag = 0;
1359
/* Set pf_type and load val */
1363
if (l_flag + h_flag)
1364
Scierror(998,"Warning:\tprintf: bad conversion l or h flag mixed with s directive\r\n");
1365
rval=GetString(fname,&previous_t,&arg_cnt,nargs,&ccount,lcount,&sval);
1367
if (rval== NOT_ENOUGH_ARGS) goto bad;
1373
if (l_flag + h_flag)
1374
Scierror(998,"Warning:\tprintf: bad conversion l or h flag mixed with c directive\r\n");
1375
rval=GetString(fname,&previous_t,&arg_cnt,nargs,&ccount,lcount,&sval);
1377
if (rval== NOT_ENOUGH_ARGS) goto bad;
1383
rval=GetScalarDouble(fname,&previous_t,&arg_cnt,nargs,&ccount,lcount,&dval);
1385
if (rval== NOT_ENOUGH_ARGS) goto bad;
1392
Scierror(998,"Error:\tprintf: \"o\" format not allowed\r\n");
1397
rval=GetScalarDouble(fname,&previous_t,&arg_cnt,nargs,&ccount,lcount,&dval);
1399
if (rval== NOT_ENOUGH_ARGS) goto bad;
1406
rval=GetScalarDouble(fname,&previous_t,&arg_cnt,nargs,&ccount,lcount,&dval);
1408
if (rval== NOT_ENOUGH_ARGS) goto bad;
1416
/* use strod() here */
1417
rval=GetScalarDouble(fname,&previous_t,&arg_cnt,nargs,&ccount,lcount,&dval);
1419
if (rval== NOT_ENOUGH_ARGS) goto bad;
1422
pf_type = l_flag ? PF_LD : PF_D;
1430
if (h_flag + l_flag)
1432
Scierror(998,"Error:\tprintf: bad conversion\r\n");
1435
/* use strod() here */
1436
rval=GetScalarDouble(fname,&previous_t,&arg_cnt,nargs,&ccount,lcount,&dval);
1438
if (rval== NOT_ENOUGH_ARGS) goto bad;
1445
Scierror(998,"Error:\tprintf: bad conversion\r\n");
1452
/* ready to call printf() */
1454
* target: The output file (or variable for sprintf())
1455
* p: the beginning of the format
1456
* ast: array with asterisk values
1458
switch (AST (ast_cnt, pf_type))
1461
retval += (*printer) ((VPTR) target, p, sval[0]);
1466
retval += (*printer) ((VPTR) target, p, ast[0], sval[0]);
1471
retval += (*printer) ((VPTR) target, p, ast[0], ast[1],sval[0]);
1476
retval += (*printer) ((VPTR) target, p, sval);
1481
retval += (*printer) ((VPTR) target, p, ast[0], sval);
1486
retval += (*printer) ((VPTR) target, p, ast[0], ast[1], sval);
1491
retval += (*printer) ((VPTR) target, p, (int) dval);
1495
retval += (*printer) ((VPTR) target, p, ast[0], (int) dval);
1499
retval += (*printer) ((VPTR) target, p, ast[0], ast[1], (int) dval);
1502
case AST (0, PF_LD):
1503
retval += (*printer) ((VPTR) target, p, (long int) dval);
1506
case AST (1, PF_LD):
1507
retval += (*printer) ((VPTR) target, p, ast[0], (long int) dval);
1510
case AST (2, PF_LD):
1511
retval += (*printer) ((VPTR) target, p, ast[0], ast[1], (long int) dval);
1515
retval += (*printer) ((VPTR) target, p, dval);
1519
retval += (*printer) ((VPTR) target, p, ast[0], dval);
1523
retval += (*printer) ((VPTR) target, p, ast[0], ast[1], dval);
1526
if (fp == (FILE *) 0)
1533
(*printer) ((VPTR) target, "\n");
1534
(*flush) ((FILE *) target);
1535
Scierror(998,"Error:\tprintf: not enough arguments\r\n");
1543
/****************************************************
1545
****************************************************/
1547
static int GetString(fname,previous_t,arg,narg,ic,ir,sval)
1549
int *previous_t,*arg,narg,*ic,ir;
1551
int mx,nx,il,ild,lw,k,one=1;
1554
if (*previous_t != 2) {
1555
*arg = *arg+1;*ic=1;
1558
lw = *arg + Top - Rhs;
1560
if (! C2F(getwsmat)(fname,&Top,&lw,&mx,&nx,&il,&ild, strlen(fname))) return RET_BUG;
1564
if (*arg>narg ) return NOT_ENOUGH_ARGS;
1566
lw = *arg + Top - Rhs;
1567
if (! C2F(getwsmat)(fname,&Top,&lw,&mx,&nx,&il,&ild, strlen(fname))) return RET_BUG;
1570
if (ir>mx) return RET_END;
1572
if (SciStrtoStr(istk(il-1+*istk(ild+k)),&one,istk(ild+k),&p) < 0) return MEM_LACK;
1578
/** changes `\``n` --> `\n` idem for \t and \r **/
1580
static int StringConvert(str)
1593
case 'n' : *str1 = '\n' ; str1++; str += 2;count++;break;
1594
case 't' : *str1 = '\t' ; str1++; str += 2;break;
1595
case 'r' : *str1 = '\r' ; str1++; str += 2;break;
1596
default : *str1 = *str; str1++; str++;break;
1601
*str1 = *str; str1++; str++;
1607
static int GetScalarInt(fname,previous_t,arg,narg,ic,ir,ival)
1609
int *previous_t,*arg,narg,*ic,ir,*ival;
1613
if (*previous_t != 1) {
1618
if (! C2F(getrhsvar)(arg,"i",&mx,&nx,&lx,1L))
1621
if ( (*ic>nx) || (*previous_t != 1)) {
1623
if (*arg > narg ) return NOT_ENOUGH_ARGS;
1625
if (! C2F(getrhsvar)(arg,"i",&mx,&nx,&lx,1L))
1629
if (ir>mx) return RET_END;
1630
*ival = *(istk(lx+ir-1+mx*(*ic-1)));
1635
static int GetScalarDouble(fname,previous_t,arg,narg,ic,ir,dval)
1637
int *previous_t,*arg,narg,*ic,ir;
1642
if (*previous_t != 1) {
1647
if (! C2F(getrhsvar)(arg,"d",&mx,&nx,&lx,1L))
1652
if (*arg > narg ) return NOT_ENOUGH_ARGS;
1654
if (! C2F(getrhsvar)(arg,"d",&mx,&nx,&lx,1L))
1658
if (ir>mx) return RET_END;
1659
*dval = *(stk(lx+ir-1+mx*(*ic-1)));
1664
static int Sci_Store(nrow,ncol,data,type,retval_s)
1665
int nrow,ncol,retval_s;
1669
int cur_i,i,j,i1,one=1,zero=0,k,l,iarg,colcount;
1673
/* create Scilab variable with each column of data */
1674
if (ncol+Rhs > intersiz ){
1675
Scierror(998,"Error:\ttoo many directive in scanf\r\n");
1679
CreateVar(1, "d", &one, &one, &l);
1680
*stk(l) = (double) retval_s;
1683
if (ncol==0) goto Complete;
1685
for ( i=0 ; i < ncol ; i++) {
1686
if ( (type[i] == SF_C) || (type[i] == SF_S) ) {
1687
if( (temp = (char **) malloc(nrow*ncol*sizeof(char **)))==NULL) return MEM_LACK;
1689
for (j=0;j<nrow;j++) temp[k++]=data[i+ncol*j].s;
1690
CreateVarFromPtr(iarg+i, "S", &nrow, &one, temp);
1692
for (j=0;j<nrow;j++) free(data[i+ncol*j].s);
1695
CreateVar(iarg+i, "d", &nrow, &one, &l);
1696
for ( j=0 ; j < nrow ; j++)
1697
*stk(l+j)= data[i+ncol*j].d;
1700
LhsVar(iarg+i)=iarg+i;
1703
/** we must complete the returned arguments up to Lhs **/
1705
for ( i = ncol+2; i <= Lhs ; i++)
1707
CreateVar(i,"d",&zero,&zero,&l);
1712
char *ltype="cblock";
1713
int multi=0,endblk,ii;
1717
for (i=0;i<ncol;i++)
1718
if (type[i] != cur_type) {
1724
CreateVarFromPtr(1, "c", &one, &i, <ype);
1730
endblk=(type[i] != cur_type);
1736
CreateVar(++iarg, "d", &zero, &zero, &l);}
1737
else if ( (cur_type == SF_C) || (cur_type == SF_S) ) {
1738
if( (temp = (char **) malloc(nrow*colcount*sizeof(char **)))==NULL) return MEM_LACK;
1740
for (i1=cur_i;i1<i;i1++)
1741
for (j=0;j<nrow;j++) temp[k++]=data[i1+ncol*j].s;
1742
CreateVarFromPtr(++iarg, "S", &nrow, &colcount,temp);
1744
for (i1=cur_i;i1<i;i1++)
1745
for (j=0;j<nrow;j++) free(data[i1+ncol*j].s);
1748
CreateVar(++iarg, "d", &nrow, &colcount, &l);
1750
for (i1=cur_i;i1<i;i1++) {
1751
for ( j=0 ; j < nrow ; j++)
1752
*stk(l+j+nrow*ii)= data[i1+ncol*j].d;
1764
C2F(mkmlistfromvars)(&i,&iarg);
1769
CreateVar(1, "d", &zero, &zero, &l);}
1770
else if ( (cur_type == SF_C) || (cur_type == SF_S) ) {
1771
if( (temp = (char **) malloc(nrow*ncol*sizeof(char **)))==NULL) return MEM_LACK;
1773
for (i1=0;i1<ncol;i1++)
1774
for (j=0;j<nrow;j++) temp[k++]=data[i1+ncol*j].s;
1775
CreateVarFromPtr(1, "S", &nrow, &ncol, temp);
1777
for (i1=0;i1<ncol;i1++)
1778
for (j=0;j<nrow;j++) free(data[i1+ncol*j].s);
1781
CreateVar(1, "d", &nrow, &ncol, &l);
1783
for (i1=0;i1<ncol;i1++) {
1784
for ( j=0 ; j < nrow ; j++)
1785
*stk(l+j+nrow*ii)= data[i1+ncol*j].d;
1796
/* ************************************************************************
1797
* Store data scanned by a single call to do_scan in line rowcount of data
1799
************************************************************************/
1801
static int Store_Scan(nrow,ncol,type_s,type,retval,retval_s,buf,data,rowcount,n)
1803
int *ncol, *nrow, *retval, *retval_s;
1805
sfdir *type_s,*type;
1811
int blk=20; /* block size for memory allocation */
1816
for ( i=0 ; i < MAXSCAN ; i++) type_s[i]=SF_F; /* initialisation */
1824
if ( (*data = (entry *) malloc(nc*nr*sizeof(entry)))==NULL) {
1828
for ( i=0 ; i < nc ; i++) type_s[i]=type[i];
1832
/* check if number of data read match with previous number */
1833
if ( (n !=nc ) || (*retval_s != *retval) ){
1837
/* check if types of data read match with previous types */
1838
for ( i=0 ; i < nc ; i++)
1839
if (type[i] != type_s[i]) {
1844
/* check for memory and realloc if necessary*/
1845
if (rowcount>= nr) {
1848
if ( (*data = (entry *) realloc(*data,nc*nr*sizeof(entry)))==NULL) {
1855
/* store values scanned in a new row */
1856
for ( i=0 ; i < nc ; i++)
1858
switch ( type_s[i] )
1862
Data[i+nc*rowcount].s=buf[i].c;
1865
Data[i+nc*rowcount].d=(double)buf[i].lui;
1868
Data[i+nc*rowcount].d=(double)buf[i].sui;
1871
Data[i+nc*rowcount].d=(double)buf[i].ui;
1874
Data[i+nc*rowcount].d=(double)buf[i].li;
1877
Data[i+nc*rowcount].d=(double)buf[i].si;
1880
Data[i+nc*rowcount].d=(double)buf[i].i;
1883
Data[i+nc*rowcount].d=buf[i].lf;
1886
Data[i+nc*rowcount].d=(double)buf[i].f;
1892
/* free allocated strings in scan buffer */
1893
for ( j=0 ; j < MAXSCAN ; j++)
1894
if ( (type_s[j] == SF_C) || (type_s[j] == SF_S)) free(buf[j].c);
1902
static void Free_Scan(nrow,ncol,type_s,data)
1912
for ( j=0 ; j < ncol ; j++)
1913
if ( (type_s[j] == SF_C) || (type_s[j] == SF_S) )
1914
/* free allocated strings in scan data area */
1915
for ( i=0 ; i < nrow ; i++) {
1916
free(Data[j+ncol*i].s);
1919
/* free scaned data area */
1920
if (ncol>0) free(Data);
1923
/********************************************************
1924
* Converts a Scilab array of String coded as integer array
1925
* into a regular string.
1926
* entries of the original array are catenated, separated by
1928
********************************************************/
1930
int SciStrtoStr(Scistring,nstring,ptrstrings,strh)
1931
int *Scistring,*nstring,*ptrstrings;
1935
int li,ni,*SciS,i,job=1;
1938
ni=ptrstrings[*nstring] - li + *nstring +1;
1939
p=(char *) malloc(ni);
1940
if (p ==NULL) return MEM_LACK;
1943
for ( i=1 ; i<*nstring+1 ; i++)
1945
ni=ptrstrings[i]-li;
1947
F2C(cvstr)(&ni,SciS,s,&job,(long int)ni);