File: sscanf.sci

package info (click to toggle)
scilab 5.2.2-9
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 334,832 kB
  • ctags: 52,586
  • sloc: xml: 526,945; ansic: 223,590; fortran: 163,080; java: 56,934; cpp: 33,840; tcl: 27,936; sh: 20,397; makefile: 9,908; ml: 9,451; perl: 1,323; cs: 614; lisp: 30
file content (388 lines) | stat: -rw-r--r-- 7,162 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) XXXX-2008 - INRIA
// Copyright (C) 2008 - DIGITEO - Allan CORNET
// 
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at    
// http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt


function varargout = sscanf(buf,frmt)
	
	// sscanf - Emulator of C language sscanf
	
	[lhs,rhs]  = argn(0);
	MAXLHS = 50;
	if lhs > MAXLHS then
	  error(999, msprintf(gettext("%s: Wrong number of output argument(s).\n"),"sscanf"));
	end
	
	hexdigits  = [string(0:9),'a','b','c','d','e','f','A','B','C','D','E','F']
	hexvalues  = [0:15,10,11,12,13,14,15];
	kbuf       = 1;
	
	sbuf       = length(buf);
	nv         = lhs;
	
	if type(buf)<>10 then
		error(999, msprintf(gettext("%s: Wrong type for input argument #%d: A String expected.\n"),"sscanf",1));
	end
	
	if size(buf,"*")<>1 then
		error(999, msprintf(gettext("%s: Wrong size for input argument #%d: A String expected.\n"),"sscanf",1));
	end
	
	if type(frmt)<>10 then
		error(999, msprintf(gettext("%s: Wrong type for input argument #%d: A String expected.\n"),"sscanf",2));
	end
	
	if size(frmt,"*")<>1 then
		error(999, msprintf(gettext("%s: Wrong size for input argument #%d: A String expected.\n"),"sscanf",2));
	end
	
	lb          = 1;
	lfmt        = length(frmt);
	il          = 0;
	count       = 0; // argument counter
	
	while il<=lfmt do
		
		[str,flags,width,typemod,conv,err]=next_scanf_format()
		
		if err==1 then
			error(msprintf(gettext("%s: Incorrect format.\n")),"sscanf");
		end
		
		str = strcat(str);
		ns  = length(str);
		
		if part(buf,lb:lb+ns-1)<>str then
			error(msprintf(gettext("%s: Invalid conversion.\n")),"sscanf");
		end
		
		lb=lb+ns
		
		if conv<>[] then
			[v,nc,err] = next_scanf_value(part(buf,lb:sbuf),str,flags,width,typemod,conv)
			if err==1 then
				error(msprintf(gettext("%s: end of input reached before final conversion.\n")),"sscanf");
			elseif err==2 then
				error(msprintf(gettext("%s: Invalid conversion.\n")),"sscanf");
			end
			if v<>[] then
				count=count+1;
				execstr("v"+string(count)+"=v");
			end
			lb = lb+nc-1;
		end
	end
	
	for k=count+1:lhs
		execstr("v"+string(k)+"=[]");
	end
	
	v = 'v';
	args = strcat( v(ones(count,1)) + string(1:count)',',');
	execstr('varargout = list(' + args + ');');
		
endfunction


// =============================================================================
// next_scanf_value
// =============================================================================

function [v,lb,err]=next_scanf_value(buf,str,flags,width,typemod,conv)

	intf   = ['d','i','o','u','x','X']
	floatf = ['f','e','E','g','G']
	strf   = ['c','s']
	lbuf   = length(buf);
	
	if width==[] | width==0 then
		width=length(buf);
	end
	
	err = 0;
	cc  = convstr(conv,'l');
	lb  = 1;
	x   = emptystr();
	
	if cc<>"c" then //skip initial blanks
		while %t do
			ch = part(buf,lb);
			if ch==' '&lb<=lbuf then
				lb=lb+1
			elseif ch=='\'&part(buf,lb+1)=='n' then
				lb=lb+2
			else
				break;
			end
		end
		if lb>lbuf then
			err=1
			v=[];
			return;
		end
	end
	
	if cc=="d"|cc=="i"|cc=="u" then
		ll = 0;
		ch = part(buf,lb);
		
		if ch=="+" | ch=="-" then
			if cc=='u'&ch=='-' then
				err = 2;
				v   = [];
				return;
			else
				x   = x+ch;
				lb  = lb+1;
				ll  = ll+1;
			end
		end
		
		while isdigit(part(buf,lb))&ll<width&lb<=lbuf do
			x  = x+part(buf,lb);
			lb = lb+1;
			ll = ll+1;
		end
		
		if length(x)>0 then
			v = evstr(x);
		else
			v = [];
		end
	
	
	elseif cc=="o" then
		
		ll =0;
		v  =0
		
		while isdigit(part(buf,lb))&ll<width&lb<=lbuf do
			v=8*v+evstr(part(buf,lb))
			lb=lb+1
			ll=ll+1
		end
	
	
	
	elseif cc=="x" then
	
		ll = 0;
		v  = 0;
		c  = part(buf,lb);
		k  = find(c==hexdigits);
		
		while k<>[] & ll<width & lb<=lbuf do
			v   = 16*v+hexvalues(k);
			lb  = lb+1;
			ll  = ll+1;
			c   = part(buf,lb);
			k   = find(c==hexdigits);
		end
	
	
	elseif cc=="f"|cc=="e"|cc=="g" then
	
		ll = 0;
		ch = part(buf,lb)
		
		if ch=="+"|ch=="-" then
			x  = x+ch;
			lb = lb+1;
			ll = ll+1;
		end
		
		if part(buf,lb:min(lb+2,lb+width-ll))=='INF' then
			v  = %inf;
			lb = lb+3;
		
		elseif part(buf,lb:min(lb+2,lb+width-ll))=='NaN'
			v  = %nan;
			lb = lb+3;
		
		else
			
			while isdigit(part(buf,lb))&ll<width&lb<=lbuf do
				x  = x  + part(buf,lb);
				lb = lb + 1;
				ll = ll + 1;
			end
			
			ch = part(buf,lb);
			
			if ch=="." & ll<width & lb<=lbuf then
				x  = x  + ".";
				lb = lb + 1;
				ll = ll + 1;
				
				while isdigit(part(buf,lb))&ll<width&lb<=lbuf do
					x  = x  + part(buf,lb);
					lb = lb + 1;
					ll = ll + 1;
				end
			
			end
			
			ch=part(buf,lb)
			
			if (ch=='e'|ch=='E')&ll<width&lb<=lbuf then
				
				x  = x  + 'e';
				lb = lb + 1;
				ll = ll + 1;
				ch = part(buf,lb);
				
				if (ch=='+'|ch=='-') & ll<width & lb<=lbuf then
					x  = x+ch;
					lb = lb+1;
					ll = ll+1;
				end
				
				while isdigit(part(buf,lb))&ll<width&lb<=lbuf do
					x  = x  + part(buf,lb);
					lb = lb + 1;
					ll = ll + 1;
				end
				
			end
			
			if length(x)>0 then
				v=evstr(x)
			else
				v=[]
			end
		end
	
	elseif cc=="c" then
		
		if lb>lbuf then
			err=1
			return
		end
		
		v=part(buf,lb);
	
	elseif cc=="s" then
		ll=0
		ch=part(buf,lb)
		while ch<>' '&lb<=lbuf&ll<width do
			x  = x+ch;
			lb = lb+1;
			ll = ll+1;
			ch = part(buf,lb);
		end
		
		v = x;
	end

	if flags=='*' then
		v=[];
	end

endfunction


// =============================================================================
// next_scanf_format
// =============================================================================

function [str,flags,width,typemod,conv,err]=next_scanf_format()

	//Scan frmt for % escapes and print out the arguments.
	err     = 0;
	str     = emptystr();
	kstr    = 1;
	width   = [];
	prec    = [];
	flags   = [];
	typemod = [];
	conv    = [];
	il      = il+1;
	
	if il>lfmt then
		[il,count] = resume(il,count);
	end
	
	c = part(frmt,il);
	
	while c<>"%" then
		if c=='\' then
			if part(frmt,il+1)=='n' then
				str  = [str;emptystr()];
				kstr = kstr+1
			end
			il=il+1;
		else
			str(kstr)=str(kstr)+c
		end
		
		il=il+1
		
		if il>lfmt then break, end
		
		c=part(frmt,il);
	end
	
	if il>lfmt then
		[il,count] = resume(il,count);
	end
	
	if part(frmt,il+1)=='%' then 
		str(kstr)  = str(kstr)+'%';
		il         = il+1;
		[il,count] = resume(il,count);
	end
	
	//beginning of a format
	
	//get flags
	flags = [];
	il    = il+1;
	c     = part(frmt,il);
	
	if c=="*" then
		flags = "*";
		il    = il+1;
		c     = part(frmt,il)
	end
	
	width = [];
	
	if isdigit(c) then
		// get width
		width = 0;
		while isdigit(c) do
			width = 10*width+evstr(c);
			il    = il+1;
			if il>lfmt then
				err=1;
				return;
			end
			c = part(frmt,il)
		end
	end
	
	// get type modifier
	typemod = [];
	if c=='l'| c=='L'|c=='h' then
		typemod = c;
		il      = il+1;
		
		if il>lfmt then
			err=1;
			return;
		end
		
		c = part(frmt,il);
	end
	
	//get conversion
	conv       = c;
	[il,count] = resume(il,count);
	
endfunction