[tests] Use TinyConsole::string instead of any

This commit is contained in:
Francois BIOT
2023-02-20 02:24:39 +01:00
parent c21b7b63fb
commit 775fbc14ee
8 changed files with 92 additions and 79 deletions

View File

@@ -15,6 +15,8 @@
#include <string>
#include <map>
using string = TinyString;
bool echo_on = true;
auto green = TinyConsole::green;
auto red = TinyConsole::red;
@@ -32,13 +34,13 @@ const char* password = "";
struct free_broker
{
public:
free_broker(const char* s, uint16_t p, const char* comment) : url(s), port(p) {}
free_broker(const char* s, uint16_t p, const char* /* comment */) : url(s), port(p) {}
std::string url;
string url;
uint16_t port;
};
const std::map<std::string, free_broker> list =
const std::map<string, free_broker> list =
{
{ "mqtthq", { "public.mqtthq.com" , 8083, "publish/subscribe" }},
{ "hivemq", { "broker.hivemq.com", 1883, "" }}
@@ -73,8 +75,8 @@ void onPublish(const MqttClient* srce, const Topic& topic, const char* payload,
}
}
std::map<std::string, MqttClient*> clients;
std::map<std::string, MqttBroker*> brokers;
std::map<string, MqttClient*> clients;
std::map<string, MqttBroker*> brokers;
void setup()
{
@@ -126,12 +128,12 @@ void setup()
if (Console.isTerm()) onCommand("every 333 view");
}
std::string getword(std::string& str, const char* if_empty=nullptr, char sep=' ');
string getword(string& str, const char* if_empty=nullptr, char sep=' ');
int getint(std::string& str, const int if_empty=0)
int getint(string& str, const int if_empty=0)
{
std::string str2=str;
std::string sword = getword(str);
string str2=str;
string sword = getword(str);
if (sword[0] and isdigit(sword[0]))
{
int ret=atoi(sword.c_str());
@@ -143,11 +145,11 @@ int getint(std::string& str, const int if_empty=0)
return if_empty;
}
std::string getword(std::string& str, const char* if_empty/*=nullptr*/, char sep/*=' '*/)
string getword(string& str, const char* if_empty/*=nullptr*/, char sep/*=' '*/)
{
char quote=(str[0]=='"' or str[0]=='\'' ? str[0] : 0);
if (quote) str.erase(0,1);
std::string sword;
string sword;
while(str.length() and (str[0]!=sep or quote))
{
if (str[0]==quote)
@@ -190,7 +192,7 @@ std::string getword(std::string& str, const char* if_empty/*=nullptr*/, char sep
return sword;
}
bool isaddr(std::string s)
bool isaddr(string s)
{
if (s.length()==0 or s.length()>3) return false;
for(char c: s)
@@ -198,14 +200,14 @@ bool isaddr(std::string s)
return true;
}
std::string getip(std::string& str, const char* if_empty=nullptr, char sep=' ')
string getip(string& str, const char* if_empty=nullptr, char sep=' ')
{
std::string addr=getword(str, if_empty, sep);
std::string ip=addr;
std::vector<std::string> build;
string addr=getword(str, if_empty, sep);
string ip=addr;
std::vector<string> build;
while(ip.length())
{
std::string b=getword(ip,nullptr,'.');
string b=getword(ip,nullptr,'.');
if (isaddr(b) && build.size()<4)
{
build.push_back(b);
@@ -219,9 +221,9 @@ std::string getip(std::string& str, const char* if_empty=nullptr, char sep=' ')
{
std::stringstream b;
b << (int)local[3-build.size()];
build.insert(build.begin(), b.str());
build.insert(build.begin(), b.str().c_str());
}
for(std::string s: build)
for(string s: build)
{
if (addr.length()) addr += '.';
addr += s;
@@ -230,22 +232,22 @@ std::string getip(std::string& str, const char* if_empty=nullptr, char sep=' ')
return addr;
}
std::map<std::string, std::string> vars;
std::map<string, string> vars;
std::set<std::string> commands = {
std::set<string> commands = {
"broker", "blink", "client", "connect",
"create", "delete", "debug", "help", "interval",
"list", "ls", "ip", "off", "on", "set",
"publish", "reset", "subscribe", "unsubscribe", "view", "echo", "every"
};
void convertToCommand(std::string& search)
void convertToCommand(string& search)
{
while(search[0]==' ') search.erase(0,1);
if (search.length()==0) return;
std::string matches;
string matches;
int count=0;
for(std::string cmd: commands)
for(string cmd: commands)
{
if (cmd.substr(0, search.length()) == search)
{
@@ -263,7 +265,7 @@ void convertToCommand(std::string& search)
}
}
void replace(const char* d, std::string& str, std::string srch, std::string to)
void replace(const char* d, string& str, string srch, string to)
{
if (d[0] && d[1])
{
@@ -271,7 +273,7 @@ void replace(const char* d, std::string& str, std::string srch, std::string to)
to=d[0]+to+d[1];
size_t pos = 0;
while((pos=str.find(srch, pos)) != std::string::npos)
while((pos=str.find(srch, pos)) != string::npos)
{
str.erase(pos, srch.length());
str.insert(pos, to);
@@ -280,7 +282,7 @@ void replace(const char* d, std::string& str, std::string srch, std::string to)
}
}
void replaceVars(std::string& cmd)
void replaceVars(string& cmd)
{
cmd = ' '+cmd+' ';
@@ -291,12 +293,12 @@ void replaceVars(std::string& cmd)
replace(" .", cmd, it.first, it.second);
replace(" ", cmd, it.first, it.second);
}
cmd.erase(0, cmd.find_first_not_of(" "));
cmd.erase(cmd.find_last_not_of(" ")+1);
cmd.erase(0, cmd.find_first_not_of(' '));
cmd.erase(cmd.find_last_not_of(' ')+1);
}
bool compare(std::string s, const char* cmd)
bool compare(string s, const char* cmd)
{
uint8_t p=0;
while(s[p++]==*cmd++)
@@ -307,11 +309,11 @@ bool compare(std::string s, const char* cmd)
return false;
}
using ClientFunction = void(*)(std::string& cmd, MqttClient* publish);
using ClientFunction = void(*)(string& cmd, MqttClient* publish);
struct Every
{
std::string cmd;
string cmd;
uint32_t ms;
uint32_t next;
uint32_t underrun=0;
@@ -341,19 +343,19 @@ int16_t blink;
std::vector<Every> everies;
void onCommand(const std::string& command)
void onCommand(const string& command)
{
Console << endl;
std::string cmd=command;
string cmd=command;
if (cmd.substr(0,3)!="set") replaceVars(cmd);
eval(cmd);
Console << endl;
Console.prompt();
}
void clientConnect(MqttClient* client, std::string& cmd)
void clientConnect(MqttClient* client, string& cmd)
{
std::string remote = getword(cmd);
string remote = getword(cmd);
uint16_t port;
auto it=list.find(remote);
if (it != list.end())
@@ -369,18 +371,18 @@ void clientConnect(MqttClient* client, std::string& cmd)
Console << (client->connected() ? "connected." : "not connected") << endl;
}
void eval(std::string& cmd)
void eval(string& cmd)
{
while(cmd.length())
{
MqttError retval = MqttOk;
std::string s;
string s;
MqttBroker* broker = nullptr;
MqttClient* client = nullptr;
// client.function notation
if (cmd.find('.') != std::string::npos &&
if (cmd.find('.') != string::npos &&
cmd.find('.') < cmd.find(' '))
{
s=getword(cmd, nullptr, '.');
@@ -632,7 +634,7 @@ void eval(std::string& cmd)
}
else if (compare(s, "broker"))
{
std::string id=getword(cmd);
string id=getword(cmd);
if (clients.find(id) != clients.end())
{
Console << "A client already have that name" << endl;
@@ -663,7 +665,7 @@ void eval(std::string& cmd)
}
else if (compare(s, "client"))
{
std::string id=getword(cmd);
string id=getword(cmd);
if (brokers.find(id) != brokers.end())
{
Console << "A broker have that name" << endl;
@@ -700,7 +702,7 @@ void eval(std::string& cmd)
}
else if (compare(s, "set"))
{
std::string name(getword(cmd));
string name(getword(cmd));
if (name.length()==0)
{
for(auto it: vars)
@@ -812,7 +814,7 @@ void loop()
if (not every.active) continue;
if (every.ms && every.cmd.length() && ms > every.next)
{
std::string cmd(every.cmd);
string cmd(every.cmd);
eval(cmd);
every.next += every.ms;
if (ms > every.next and ms > every.underrun)
@@ -845,7 +847,6 @@ void loop()
out++;
}
static long count;
#if defined(ESP9266)
MDNS.update();
#endif