Unverified Commit 25bd585c authored by Alexandre Trevizoli's avatar Alexandre Trevizoli Committed by GitHub

Corrections of Stream.Find, FindUntil and added FindMulti - like AVR-Core Libraries (#3442)

* Corrections of Find, FindUntil and FindMulti

Find has some bug that is not working with Ethernet.find() so, I copied code from Stream.h and Stream.cpp in AVR-CORE library and now it's working perfectly.
I don't know where was the error, but an Ethernet.find compiled to MEGA2560 was working but not working when compiled to esp32, after corrections of code (copy of AVR-Core libraries) it's working perfect.
So probably has some error on original ESP32-Core library.

Below is part of code that was working with MEGA2560 and not with ESP32 libraries.
client.find never return TRUE with ESP32 original library and with AVR it's works.

boolean esp32_fw_update(EthernetClient &client, DecodedHeader &header, const String &field_filename, const String &field_crc) {

  char bound[header.boundary.length()+3];
  char term[]="\r\n";
  
  strcpy(bound,header.boundary.c_str());
  strcat(bound,term);
  while (client.find(bound)) { 
    String line=client.readStringUntil('\r');

* Update Stream.h

* Update Stream.cpp
Co-authored-by: default avatarMe No Dev <me-no-dev@users.noreply.github.com>
parent d79a1f3d
...@@ -89,7 +89,7 @@ unsigned long Stream::getTimeout(void) { ...@@ -89,7 +89,7 @@ unsigned long Stream::getTimeout(void) {
// find returns true if the target string is found // find returns true if the target string is found
bool Stream::find(const char *target) bool Stream::find(const char *target)
{ {
return findUntil(target, (char*) ""); return findUntil(target, strlen(target), NULL, 0);
} }
// reads data from the stream until the target string of given length is found // reads data from the stream until the target string of given length is found
...@@ -110,35 +110,78 @@ bool Stream::findUntil(const char *target, const char *terminator) ...@@ -110,35 +110,78 @@ bool Stream::findUntil(const char *target, const char *terminator)
// returns true if target string is found, false if terminated or timed out // returns true if target string is found, false if terminated or timed out
bool Stream::findUntil(const char *target, size_t targetLen, const char *terminator, size_t termLen) bool Stream::findUntil(const char *target, size_t targetLen, const char *terminator, size_t termLen)
{ {
size_t index = 0; // maximum target string length is 64k bytes! if (terminator == NULL) {
size_t termIndex = 0; MultiTarget t[1] = {{target, targetLen, 0}};
int c; return findMulti(t, 1) == 0 ? true : false;
} else {
MultiTarget t[2] = {{target, targetLen, 0}, {terminator, termLen, 0}};
return findMulti(t, 2) == 0 ? true : false;
}
}
if(*target == 0) { int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {
return true; // return true if target is a null string // any zero length target string automatically matches and would make
// a mess of the rest of the algorithm.
for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
if (t->len <= 0)
return t - targets;
} }
while((c = timedRead()) > 0) {
if(c != target[index]) { while (1) {
index = 0; // reset index if any char does not match int c = timedRead();
if (c < 0)
return -1;
for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
// the simple case is if we match, deal with that first.
if (c == t->str[t->index]) {
if (++t->index == t->len)
return t - targets;
else
continue;
} }
if(c == target[index]) { // if not we need to walk back and see if we could have matched further
//////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1); // down the stream (ie '1112' doesn't match the first position in '11112'
if(++index >= targetLen) { // return true if all chars in the target match // but it will match the second position so we can't just reset the current
return true; // index to 0 when we find a mismatch.
if (t->index == 0)
continue;
int origIndex = t->index;
do {
--t->index;
// first check if current char works against the new current index
if (c != t->str[t->index])
continue;
// if it's the only char then we're good, nothing more to check
if (t->index == 0) {
t->index++;
break;
} }
// otherwise we need to check the rest of the found string
int diff = origIndex - t->index;
size_t i;
for (i = 0; i < t->index; ++i) {
if (t->str[i] != t->str[i + diff])
break;
} }
if(termLen > 0 && c == terminator[termIndex]) { // if we successfully got through the previous loop then our current
if(++termIndex >= termLen) { // index is good.
return false; // return false if terminate string found before target string if (i == t->index) {
t->index++;
break;
} }
} else {
termIndex = 0; // otherwise we just try the next index
} while (t->index);
} }
} }
return false; // unreachable
return -1;
} }
// returns the first valid (long) integer value from the current position. // returns the first valid (long) integer value from the current position.
......
...@@ -60,6 +60,7 @@ public: ...@@ -60,6 +60,7 @@ public:
void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second void setTimeout(unsigned long timeout); // sets maximum milliseconds to wait for stream data, default is 1 second
unsigned long getTimeout(void); unsigned long getTimeout(void);
bool find(const char *target); // reads data from the stream until the target string is found bool find(const char *target); // reads data from the stream until the target string is found
bool find(uint8_t *target) bool find(uint8_t *target)
{ {
...@@ -123,6 +124,17 @@ protected: ...@@ -123,6 +124,17 @@ protected:
// this allows format characters (typically commas) in values to be ignored // this allows format characters (typically commas) in values to be ignored
float parseFloat(char skipChar); // as above but the given skipChar is ignored float parseFloat(char skipChar); // as above but the given skipChar is ignored
struct MultiTarget {
const char *str; // string you're searching for
size_t len; // length of string you're searching for
size_t index; // index used by the search routine.
};
// This allows you to search for an arbitrary number of strings.
// Returns index of the target that is found first or -1 if timeout occurs.
int findMulti(struct MultiTarget *targets, int tCount);
}; };
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment