I have a long string in program memory containing subsequences separated by delimiters.
For example
F("AAA:BBB:CCC")
I don't want to pull the entire string into RAM as it's potentially more than would fit.
Instead I want to pull back "AAA", "BBB","CCC" in turn in an iterator so I can process them indvidually.
I considered strtok_P however it seems to search within a RAM string for a substring that's in PROGMEM - this is the wrong way round for me. Additionally, strtok modifies the first arg which also won't work.
I imagine I'm looking for a function that behaves a bit like strtok but copies the tokens into a buffer that I provide.
My delimiter is just a single char at the moment so something like the api below would work.
In order to avoid having to mod the source string I'm proposing to pass in a working buffer.
char buf[100+1];
int tokenise(F("AAA:BBB:CCC"), ':', buf, 100); // pass NULL as the first arg in subsequent calls
- Fills the given buffer with a token if there was one and returns the number of chars read in the token.
- If no token was found then return 0
- If the token is longer than the buffer (eg 100) then fill the buffer with whatever fits and return the length of the complete token (eg more than 100), in this case the rest of the token is ignored, and the next call will start at the next token.
I tried to work out how to read the progmem string byte by byte using pgm_read_byte() but it was beyond me as a beginner.