[Scilab-users] Regex problem

Samuel Gougeon sgougeon at free.fr
Sun Aug 28 06:20:29 CEST 2016


Hello,
Here is another (fast) implementation. You get directly all the binary 
blocs of a file in an output list.
Cheers
Samuel
--------------------

function [res, bytes]=getBinaryBlocs(data, startstr, endstr) fid = 
mopen(data, 'rb') data = mgeti(1e6, 'uc', fid) mclose(fid) bytes = data 
s = data<32 | data>127 data(s) = ascii(".") [s, e] = regexp(ascii(data), 
"/"+startstr+".*?"+endstr+"/") res = list() for i = 1:length(s) res(i) = 
bytes((s(i)+length(ascii(startstr))):(e(i)-length(ascii(endstr)))) end 
endfunction bb = getBinaryBlocs('Fails.raw', "#data#", "#EOC#");

--------------------
Le 27/08/2016 08:10, Gerhard Kreuzer a écrit :
>
> Hi Jan,
>
> get it.
>
> Yesterday I faild until I see that the file wasn’t accessible, for 
> what reason ever. I don’t get an error message, but after mgetstr I 
> don’t have any data read. After rebooting the machine, this problem 
> dissappears.
>
> Here the working version:
>
> /// Copyright (C) 2016 - Liftoff - Gerhard Kreuzer msc./
>
> ////
>
> /// Date of creation: 26.08.2016/
>
> ////
>
> /// Just for tests/
>
> clear;
>
> fnRawData='Works.raw';
>
> fnRawData='Fails.raw'/// Comment out to see working file which 
> contains only dummy data/
>
> /// HEUREKA, both files working now/
>
> fdRawData= mopen(fnRawData,'rb');
>
> /// Find start of binary data by searching the first non ascii value/
>
> /// No problem if the exact end is missed, regex will do the rest/
>
> if~meof(fdRawData) then c = mgeti(1, 'uc', fdRawData); end
>
> while( c < 128 & c >= 32 ) & ~meof(fdRawData)
>
> c = mgeti(1, 'uc', fdRawData);
>
> end
>
> metaDataEnd= mtell(fdRawData) /// Note down this position/
>
> mseek(0,fdRawData, 'set'); /// Rewind 
> file                                  ///
>
> strData= mgetstr(metaDataEnd - 1, fdRawData); /// Read whole meta data 
> block, only ASCII values go here/
>
> [dummy,dataStart,dataMatch,data]=regexp(strData,'/#data#/');/// 
> Analyze meta data, we just seek for the begin of the binary data here/
>
> mseek(dataStart,fdRawData, 'set'); /// Set file pointer to correct 
> position/
>
> sset= mgeti(7, 'il', fdRawData); /// Read first sample set, just for 
> test./
>
> mclose(fdRawData);/// Ich habe fertig/
>
> I am sure ther could be done some optimization, but ist a good start now.
>
> Thanks a lot.
>
> With best regards
>
> Gerhard
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20160828/a71fbfc5/attachment.htm>


More information about the users mailing list