How to? read while ignoring whitespace

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

samintz

Scott Mintz
May 20, 2008
1,204
11
Solon, OH, USA
#1
I have a file with contents similar to the following:
Code:
8e 00 01 00 00 00 00 00 00 00 00 00 00  
   00 7f 00 01 00 69 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 7f 00
   51 75 69 63 6b 2d 44 69 72 74 79 20 54 65 73 74 20 66 6f 72 20 4d 6f 74 69
I want to read the individual hex bytes in a continuous fashion and ignore the end of line characters and leading whitespace. Is there an easy way to do that?
To be more specific, the data forms records. The first column is 12 bytes. The second is 2, and so on. I want to just read the file in that way - i.e. read 12, read 2, and so on. So using the above as an example, I want to read "8e 00 01 00 00 00 00 00 00 00 00 00" with the 12 byte read and "00 00" on the 2 byte read, and "7f 00" and the next read, etc. That second read spans EOL.
Any easy ideas?
-Scott
 

samintz

Scott Mintz
May 20, 2008
1,204
11
Solon, OH, USA
#4
Steve, @fileread with a number reads literally, that many bytes. It doesn't do what I need. I ended up coding a solution that worked out pretty well in the end.

I created a subroutine named ReadMore:
Code:
:readmore
set line=%@inc[line]
set l=%@fileread[%qf]
set cw=0
iff "%l"=="**EOF**" then
    set qf=%@fileclose[%qf]
    quit
endiff
return
And I then created helper routines for reading 16 bit words, 32 bit words, structures, and strings:
Code:
:ReadBytes [howmany]
set str=
do i=1 to %howmany
    if %cw == %@words[%l] gosub ReadMore
    echos %@word[%cw,%l] ``
    set str=%[str]%@char[0x%@word[%cw,%l]]``
    set cw=%@inc[cw]
enddo
echo.
return
:ReadWord   
if %cw == %@words[%l] gosub readmore
set wrd=%@word[%cw,%l]
set cw=%@inc[cw]
if %cw == %@words[%l] gosub readmore
set wrd=%@word[%cw,%l]%wrd
set cw=%@inc[cw]
return
:String
gosub ReadWord
echos %wrd ``
gosub ReadBytes %@convert[16,10,%wrd]
if not '%str'=='' echo %str
return
Then I just had a loop that iterated through the structure gosub-ing each data type.
The big issue I was trying to track down was where in this 100K file was the data corrupted? After running this routine I was able to track and find exactly where, which gave me a clue as to why it was happening.
I was very excited. (But I'm easily impressed).