reading info from SD crossing sector boundaries.

I am using the MMC/Microfat libraries to read sectors from the SD card.
Only reading, no writing required.
I read a SD card sector into a 512 byte RAM buffer.
Then I need to find a string in this buffer.
All works fine while I am within 1 sector.
Loading a next sector is no problem.

Now I need to expand the code to be able to find a string that crosses the sector boundary (1 part of the string in this sector, other part of the string in the next sector)
I do not have enough RAM space left on the 328 to define additional 512 byte RAM buffers to load more sectors simultaneously.

Does anybody know some good example code how to deal with these sector boundary crossings ?

Thanks in advance

How long are the strings you are looking for?

If the string is length n, can you determine if some of the last n characters matches part of the string, then read the next sector, and see if the rest of the string is at the beginning of the sector.

This is fairly easy if the strings are short, but more difficult if the string is long or random length.

Where does the data on the SD card come from? Where do the strings to search for come from?

The file is generated on a PC.
Just 1 file on SD card formatted as FAT16.
This is a comma separated text file generated in Excel.
Basically a large lookup table with 1000 lines.
Looks like:

The Stringx that I need to look for on the SD card is 4 to 10 charachters.
Then I need to read the following Datax.
This is 1 to 5 characters.

I have all of this working within 1 sector.
What I am looking for is some example code or coding hints to deal with situation where the sector boundary lies somewhere in the string(s) "Stringx,Datax," and I have to reload the next sector into RAM in the middle of this Stringx evaluation process.
Such that I can scan through all of the file (all sectors).



I would write a function. The function would do this (pseudocode):

byte sector_buffer[512];
int sector_idx = -1;
boolean got_string;

got_string = false
while (not got_string)
  if (sector_idx == -1)
    read next sector into sector_buffer
    sector_idx = 0
  if (sector_buffer[sector_idx] is end of string)
     got_string = true
    put sector_buffer[sector_idx] into string
    if (sector_idx >= 512)
      sector_idx = -1



If the maximum string length is 10 characters, and the maximum data length is 5 characters, that's a total of 15 characters. Counting the carriage return as 2 characters (CR and LF), that's 17 characters.

There are then 3 possibilities to consider. The string may be in one sector with the data in another. The string may start in one sector and end in the next. The string may be in one sector, and the data may span sectors.

In a worst case scenario, the string, the data and 1 of the two characters that make up the CR/LF combo may be in one sector, with the last character in the next sector.

You could save the last 16 characters of one sector, before reading the next sector in the front half of a 33 character array. Read the next sector.

Copy the first 16 characters of the new sector into the end of the 33 character array.

See if the string of interest, and it's data is in that array. If so, you've found the string and data. Use it.

If not, begin scanning again from the start of the 512 character sector data. At most, you will scan 32 characters extra per sector.