1// ============================================================================= 2// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab 3// Copyright (C) ????-2008 - INRIA 4// Copyright (C) ????-2008 - ENPC 5// 6// This file is distributed under the same license as the Scilab package. 7// ============================================================================= 8// <-- CLI SHELL MODE --> 9function [n,a,b,c,d,e]=Fscanf(str ,f) 10 fd = mopen(TMPDIR + '/fscanf.rep', 'w'); 11 mputstr(str, fd); 12 mclose(fd); 13 fd = mopen(TMPDIR + '/fscanf.rep', 'r'); 14 [n,a,b,c,d,e] = mfscanf(fd, f); 15 mclose(fd); 16endfunction 17//--------------test format %i 18[n,a]=Fscanf('123','%i'); 19if n<>1|a<>123 then bugmes();quit;end 20[n,a]=Fscanf(' 123','%i'); 21if n<>1|a<>123 then bugmes();quit;end 22[n,a]=Fscanf('123','%2i'); 23if n<>1|a<>12 then bugmes();quit;end 24[n,a]=Fscanf('123','%5i'); 25//--------------test format %li 26[n,a]=Fscanf('123','%li'); 27if n<>1|a<>123 then bugmes();quit;end 28[n,a]=Fscanf(' 123','%li'); 29if n<>1|a<>123 then bugmes();quit;end 30[n,a]=Fscanf('123','%2li'); 31if n<>1|a<>12 then bugmes();quit;end 32[n,a]=Fscanf('123','%5li'); 33//--------------test format %hi 34[n,a]=Fscanf('123','%hi'); 35if n<>1|a<>123 then bugmes();quit;end 36[n,a]=Fscanf(' 123','%hi'); 37if n<>1|a<>123 then bugmes();quit;end 38[n,a]=Fscanf('123','%2hi'); 39if n<>1|a<>12 then bugmes();quit;end 40[n,a]=Fscanf('123','%5hi'); 41//--------------test format %d 42[n,a]=Fscanf('123','%d'); 43if n<>1|a<>123 then bugmes();quit;end 44[n,a]=Fscanf(' 123','%d'); 45if n<>1|a<>123 then bugmes();quit;end 46[n,a]=Fscanf('123','%2d'); 47if n<>1|a<>12 then bugmes();quit;end 48[n,a]=Fscanf('123','%5d'); 49//--------------test format %ld 50[n,a]=Fscanf('123','%ld'); 51if n<>1|a<>123 then bugmes();quit;end 52[n,a]=Fscanf(' 123','%ld'); 53if n<>1|a<>123 then bugmes();quit;end 54[n,a]=Fscanf('123','%2ld'); 55if n<>1|a<>12 then bugmes();quit;end 56[n,a]=Fscanf('123','%5ld'); 57//--------------test format %hd 58[n,a]=Fscanf('123','%hd'); 59if n<>1|a<>123 then bugmes();quit;end 60[n,a]=Fscanf(' 123','%hd'); 61if n<>1|a<>123 then bugmes();quit;end 62[n,a]=Fscanf('123','%2hd'); 63if n<>1|a<>12 then bugmes();quit;end 64[n,a]=Fscanf('123','%5hd'); 65//------------- test format %n 66// Note that %n returned values are not counted in n 67s1='123 45.67 pipo';s2=' foo'; 68[n,a,b,c,d,e]=Fscanf(s1+s2,'%d%lf%s%n%s'); 69if n<>4|a<>123|b<>45.67|c<>'pipo'|d<>length(s1)|e<>'foo' then bugmes();quit;end 70//------------- test format %u 71[n,a]=Fscanf('+123','%u'); 72if n<>1|a<>123 then bugmes();quit;end 73[n,a]=Fscanf(' 123','%2u'); 74if n<>1|a<>12 then bugmes();quit;end 75[n,a]=Fscanf('+123','%5u'); 76if n<>1|a<>123 then bugmes();quit;end 77//------------- test format %lu 78[n,a]=Fscanf('+123','%lu'); 79if n<>1|a<>123 then bugmes();quit;end 80[n,a]=Fscanf(' 123','%2lu'); 81if n<>1|a<>12 then bugmes();quit;end 82[n,a]=Fscanf('+123','%5lu'); 83if n<>1|a<>123 then bugmes();quit;end 84//------------- test format %hu 85[n,a]=Fscanf('+123','%hu'); 86if n<>1|a<>123 then bugmes();quit;end 87[n,a]=Fscanf(' 123','%2hu'); 88if n<>1|a<>12 then bugmes();quit;end 89[n,a]=Fscanf('+123','%5hu'); 90if n<>1|a<>123 then bugmes();quit;end 91//------------- test format %o 92[n,a]=Fscanf('123','%o'); 93if n<>1|a<>83 then bugmes();quit;end 94[n,a]=Fscanf(' 123','%o'); 95if n<>1|a<>83 then bugmes();quit;end 96[n,a]=Fscanf('123','%2o'); 97if n<>1|a<>10 then bugmes();quit;end 98[n,a]=Fscanf('123','%5o'); 99if n<>1|a<>83 then bugmes();quit;end 100//------------- test format %x 101rep=10*16^2+11*16+12; 102[n,a]=Fscanf('0xabc','%x'); 103if n<>1|a<>rep then bugmes();quit;end 104[n,a]=Fscanf(' 0xabc','%x'); 105if n<>1|a<>rep then bugmes();quit;end 106[n,a]=Fscanf('abc','%2x'); 107if n<>1|a<>10*16+11 then bugmes();quit;end 108[n,a]=Fscanf('0xabc','%5x'); 109if n<>1|a<>rep then bugmes();quit;end 110//------------- test format %e 111[n,a]=Fscanf('123.45','%e'); 112if n<>1|norm(a-123.45)>1.e-2 then bugmes();quit;end 113[n,a]=Fscanf(' 123.45','%e'); 114if n<>1|norm(a-123.45)>1.e-2 then bugmes();quit;end 115[n,a]=Fscanf('123.45','%2e'); 116if n<>1| norm(a-12)>0.1 then bugmes();quit;end 117[n,a]=Fscanf('123.45','%5e'); 118if n<>1|norm(a-123.4)>1.e-1 then bugmes();quit;end 119//------------- test format %le 120[n,a]=Fscanf('123.45','%le'); 121if n<>1|a<>123.45 then bugmes();quit;end 122[n,a]=Fscanf(' 123.45','%le'); 123if n<>1|a<>123.45 then bugmes();quit;end 124[n,a]=Fscanf('123.45','%2le'); 125if n<>1|a<>12 then bugmes();quit;end 126[n,a]=Fscanf('123.45','%5le'); 127if n<>1|a<>123.4 then bugmes();quit;end 128//------------- test format %f 129[n,a]=Fscanf('123.45','%f'); 130if n<>1|norm(a-123.45)>1.e-2 then bugmes();quit;end 131[n,a]=Fscanf(' 123.45','%f'); 132if n<>1|norm(a-123.45)>1.e-2 then bugmes();quit;end 133[n,a]=Fscanf('123.45','%2f'); 134if n<>1| norm(a-12)>0.1 then bugmes();quit;end 135[n,a]=Fscanf('123.45','%5f'); 136if n<>1|norm(a-123.4)>1.e-1 then bugmes();quit;end 137//------------- test format %lf 138[n,a]=Fscanf('123.45','%lf'); 139if n<>1|a<>123.45 then bugmes();quit;end 140[n,a]=Fscanf(' 123.45','%lf'); 141if n<>1|a<>123.45 then bugmes();quit;end 142[n,a]=Fscanf('123.45','%2lf'); 143if n<>1|a<>12 then bugmes();quit;end 144[n,a]=Fscanf('123.45','%5lf'); 145if n<>1|a<>123.4 then bugmes();quit;end 146//------------- test format %s 147[n,a]=Fscanf('123','%s'); 148if n<>1|a<>'123' then bugmes();quit;end 149[n,a]=Fscanf(' 123','%s'); 150if n<>1|a<>'123' then bugmes();quit;end 151[n,a]=Fscanf('123','%2s'); 152if n<>1|a<>'12' then bugmes();quit;end 153[n,a]=Fscanf('123','%5s'); 154if n<>1|a<>'123' then bugmes();quit;end 155//------------- test format %c 156//XXXX : attention a finir .... il faut ? scaner \n comme 157//un seul caractere 158[n,a,b,c,d]=Fscanf(' 12\n','%c%c%c%c'); 159if n<>4|a<>' '|b<>'1'|c<>'2' then bugmes();quit;end 160//------------- test format [ 161[n,a,b]=Fscanf('012345abczoo','%[0-9abc]%s'); 162if n<>2|a<>'012345abc'|b<>'zoo' then bugmes();quit;end 163//------------- test format [ 164[n,a,b]=Fscanf('012345abczoo','%[^c]c%s'); 165if n<>2|a<>'012345ab'|b<>'zoo' then bugmes();quit;end 166//------------- test ignoring arguments 167[n,a,b]=Fscanf('123 4 pipo poo','%*s%s%*s%s'); 168if n<>2|a<>'4'|b<>'poo' then bugmes();quit;end 169//------------- test [ * ^ 170[n,a]=Fscanf('123 4 pipo poo','%[^\n]\n'); 171if n<>1|a<>'123 4 pipo poo' then bugmes();quit;end 172[n,a]=Fscanf('123 4 pipo Xpoo','%*[^X]X%s'); 173if n<>1|a<>'poo' then bugmes();quit;end 174//------------- test composed directives 175[n,a]=Fscanf('123 4','123%le'); 176if n<>1|a<>4 then bugmes();quit;end 177[n,a,b,c]=Fscanf('xxxxx 4 test 23.45','xxxxx%d%s%le'); 178if n<>3|a<>4|b<>'test'|c<>23.45 then bugmes();quit;end 179[n,a,b]=Fscanf('123 456','%le%le'); 180if n<>2|a<>123|b<>456 then bugmes();quit;end 181//------------- test mismatch 182[n,a]=Fscanf('123 poo','123%le'); 183if n<>0 then bugmes();quit;end 184//------------- test end-of-file 185[n,a]=Fscanf('123','123%le'); 186if n<>-1 then bugmes();quit;end 187//------------- test with matrix scan 188n=5; 189A=int(10*rand(n,n)); 190A1=strcat(string(A),' ','c'); 191tmpf=TMPDIR+'/fscanf.rep'; 192mputl(A1,tmpf); 193F='%d';F=strcat(F(ones(1,n)),' '); 194// all lines read as int we scan a 5x5 matrix 195fd=mopen(tmpf,'r');A2=mfscanf(-1,fd,F);mclose(fd); 196if norm(A2-A) > %eps then bugmes();quit;end 197// read just 2 lines 198fd=mopen(tmpf,'r');A2=mfscanf(2,fd,F);mclose(fd); 199if norm(A2-A(1:2,:)) > %eps then bugmes();quit;end 200// explicit columns we scan five columns 201fd=mopen(tmpf,'r');[n,a,b,c,d,e]=mfscanf(-1,fd,F);mclose(fd); 202if n<>5 then bugmes();quit;end 203if norm([a,b,c,d,e]-A) > %eps then bugmes();quit;end 204// all lines read as int but we scan only 2 columns using %*[^\n] to ignore the rest 205fd=mopen(tmpf,'r');A2=mfscanf(-1,fd,'%d%d%*[^\n]\n');mclose(fd); 206if norm(A2-A(:,1:2)) > %eps then bugmes();quit;end 207// all lines read as string 208F='%s';F=strcat(F(ones(1,n)),' '); 209fd=mopen(tmpf,'r');A2=mfscanf(-1,fd,F);mclose(fd); 210if A2<>string(A) then bugmes();quit;end 211// read just 2 lines 212fd=mopen(tmpf,'r');A2=mfscanf(2,fd,F);mclose(fd); 213if A2<>string(A(1:2,:)) then bugmes();quit;end 214// mixed types read column 1 and 2 as string and others as int 215Fs='%s';Fs=strcat(Fs(ones(1,2)),' '); 216Fd='%d';Fd=strcat(Fd(ones(1,n-2)),' '); 217fd=mopen(tmpf,'r');[n,a,b,c,d,e]=mfscanf(-1,fd,Fs+' '+Fd);mclose(fd); 218if n<>5 then bugmes();quit;end 219if norm([evstr(a), evstr(b),c,d,e]-A) > %eps then bugmes();quit;end 220// same example but returned values are compacted in L 221fd=mopen(tmpf,'r');L=mfscanf(-1,fd,Fs+' '+Fd);mclose(fd); 222if length(L)<>3 then bugmes();quit;end 223if norm(getfield(3,L)-A(:,3:n)) > %eps then bugmes();quit;end 224if getfield(2,L)<>string(A(:,1:2)) then bugmes();quit;end 225