1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) XXXX-2008 - INRIA
3 // Copyright (C) 2008 - DIGITEO - Allan CORNET
5 // This file must be used under the terms of the CeCILL.
6 // This source file is licensed as described in the file COPYING, which
7 // you should have received as part of this distribution. The terms
8 // are also available at
9 // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
12 function varargout = sscanf(buf, frmt)
14 // sscanf - Emulator of C language sscanf
16 warnobsolete("msscanf","5.5.0");
21 error(999, msprintf(gettext("%s: Wrong number of output argument(s).\n"),"sscanf"));x
24 hexdigits = [string(0:9),'a','b','c','d','e','f','A','B','C','D','E','F']
25 hexvalues = [0:15,10,11,12,13,14,15];
32 error(999, msprintf(gettext("%s: Wrong type for input argument #%d: A String expected.\n"),"sscanf",1));
35 if size(buf,"*")<>1 then
36 error(999, msprintf(gettext("%s: Wrong size for input argument #%d: A String expected.\n"),"sscanf",1));
39 if type(frmt)<>10 then
40 error(999, msprintf(gettext("%s: Wrong type for input argument #%d: A String expected.\n"),"sscanf",2));
43 if size(frmt,"*")<>1 then
44 error(999, msprintf(gettext("%s: Wrong size for input argument #%d: A String expected.\n"),"sscanf",2));
50 count = 0; // argument counter
54 [str, flags, width, typemod, scanfconversiontype, err] = next_scanf_format();
57 error(msprintf(gettext("%s: Incorrect format.\n")),"sscanf");
63 if part(buf,lb:lb+ns-1)<>str then
64 error(msprintf(gettext("%s: Invalid conversion.\n")),"sscanf");
69 if scanfconversiontype<>[] then
70 [v,nc,err] = next_scanf_value(part(buf,lb:sbuf),str,flags,width,typemod,scanfconversiontype)
72 error(msprintf(gettext("%s: end of input reached before final conversion.\n")),"sscanf");
74 error(msprintf(gettext("%s: Invalid conversion.\n")),"sscanf");
78 execstr("v"+string(count)+"=v");
85 execstr("v"+string(k)+"=[]");
89 args = strcat( v(ones(count,1)) + string(1:count)',',');
90 execstr('varargout = list(' + args + ');');
95 // =============================================================================
97 // =============================================================================
99 function [v, lb, err] = next_scanf_value(buf, str, flags, width, typemod, scanfconversiontype)
101 intf = ['d','i','o','u','x','X']
102 floatf = ['f','e','E','g','G']
106 if width==[] | width==0 then
111 cc = convstr(scanfconversiontype,'l');
115 if cc<>"c" then //skip initial blanks
118 if ch==' '&lb<=lbuf then
120 elseif ch=='\'&part(buf,lb+1)=='n' then
133 if cc=="d"|cc=="i"|cc=="u" then
137 if ch=="+" | ch=="-" then
138 if cc=='u'&ch=='-' then
149 while isdigit(part(buf,lb))&ll<width&lb<=lbuf do
167 while isdigit(part(buf,lb))&ll<width&lb<=lbuf do
168 v=8*v+evstr(part(buf,lb))
180 k = find(c==hexdigits);
182 while k<>[] & ll<width & lb<=lbuf do
183 v = 16*v+hexvalues(k);
187 k = find(c==hexdigits);
191 elseif cc=="f"|cc=="e"|cc=="g" then
196 if ch=="+"|ch=="-" then
202 if part(buf,lb:min(lb+2,lb+width-ll))=='INF' then
206 elseif part(buf,lb:min(lb+2,lb+width-ll))=='NaN'
212 while isdigit(part(buf,lb))&ll<width&lb<=lbuf do
213 x = x + part(buf,lb);
220 if ch=="." & ll<width & lb<=lbuf then
225 while isdigit(part(buf,lb))&ll<width&lb<=lbuf do
226 x = x + part(buf,lb);
235 if (ch=='e'|ch=='E')&ll<width&lb<=lbuf then
242 if (ch=='+'|ch=='-') & ll<width & lb<=lbuf then
248 while isdigit(part(buf,lb))&ll<width&lb<=lbuf do
249 x = x + part(buf,lb);
275 while ch<>' '&lb<=lbuf&ll<width do
292 // =============================================================================
294 // =============================================================================
296 function [str, flags, width, typemod, scanfconversiontype, err] = next_scanf_format()
298 //Scan frmt for % escapes and print out the arguments.
306 scanfconversiontype = [];
310 [il,count] = resume(il,count);
317 if part(frmt,il+1)=='n' then
318 str = [str;emptystr()];
323 str(kstr)=str(kstr)+c
328 if il>lfmt then break, end
334 [il,count] = resume(il,count);
337 if part(frmt,il+1)=='%' then
338 str(kstr) = str(kstr)+'%';
340 [il,count] = resume(il,count);
343 //beginning of a format
362 width = 10*width+evstr(c);
374 if c=='l'| c=='L'|c=='h' then
387 scanfconversiontype = c;
388 [il, count] = resume(il, count);