Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

Argument lookup method for ESP8266WebServer

Open odilonafonso opened this issue 10 years ago • 2 comments

Basic Infos

Hardware

Hardware: ESP8266 General Core Version: 2.0.0+

Description

Another (faster) way to get a ESP8266WebServer arg by name

ESP8266WebServer.cpp

// new method...
int ESP8266WebServer::hasArg(const char* name) {
  for (int i = 0; i < _currentArgCount; ++i) {
    if (_currentArgs[i].key == name)
      return i;
  }
  return -1;
}

ESP8266WebServer.h

....
  bool hasArg(const char* name);  // check if argument exists
  // new prototype..
  int hasArg(const char* name);   // check if argument exists and return index or -1 if arg not exists
  void collectHeaders(const char* headerKeys[], const size_t headerKeysCount); // set the request headers to collect
....

sketch (sample)

.....
ESP8266WebServer esp8266WebServer(port);
int iArg;
String argName;
....
if ( (iArg = esp8266WebServer.has("argName")) >= 0)
   argName = esp8266WebServer.arg(iArg);
....

odilonafonso avatar Apr 07 '16 03:04 odilonafonso

C++ doesn't allow you to have two function overloads which only differ by their return type:

  bool hasArg(const char* name);  // check if argument exists
  int hasArg(const char* name);   // check if argument exists and return index or -1 if arg not exists

I think better naming for the function which you are proposing would be findArg.

igrr avatar Apr 09 '16 20:04 igrr

Thanks Ivan ! Now I'll test this new method

odilonafonso avatar Apr 10 '16 20:04 odilonafonso