File: sscanf.sci

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (258 lines) | stat: -rw-r--r-- 5,121 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
function [v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,..
v17,v18,v19,v20,v21,v22,v23,v24,v25,v26,v27,v28,v29,v30]=sscanf(buf,frmt)
// sscanf - Emulator of C language sscanf
// Copyright INRIA
//!
[lhs,rhs]=argn(0)
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(frmt)<>10 then
  error('argument must be a character string')
end
if prod(size(frmt))<>1 then
  error('argument must be a character string')
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('incorrect format')
  end
  str=strcat(str)
  ns=length(str)
  if part(buf,lb:lb+ns-1)<>str then
    error('scanf : invalid conversion')
  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('scanf: end of input reached before final conversion')
    elseif err==2 then
      error('scanf: invalid conversion')
    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  

endfunction
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
    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
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
function ok=isdigit(c)
ok=(c=='0'|c=='1'|c=='2'|c=='3'|c=='4'|c=='5'|c=='6'|c=='7'|c=='8'|c=='9')
endfunction