Reputation: 43
I am trying to find a more elegant / less memory intensive way to split a char array by separators.
The char array : "192.168.178\nWiFiSSID\nWiFiPassword\n123"
My idiot way of splitting:
void convertPayload()
{
qrData = (char*)mypayload;
String ssidtmp = qrData;
ssidtmp.remove(qrData.indexOf("\n"), qrData.length()+1);
EEPROM.writeString(ssidAddr,ssidtmp);
EEPROM.commit();
String passtmp = qrData;
passtmp.remove(0, passtmp.indexOf("\n")+1);
passtmp.remove(passtmp.indexOf("\n"),passtmp.length()+1);
EEPROM.writeString(passAddr, passtmp);
EEPROM.commit();
String modulenrtmp = qrData;
modulenrtmp.remove(0, modulenrtmp.indexOf("\n") + 1);
modulenrtmp.remove(0, modulenrtmp.indexOf("\n") + 1);
modulenrtmp.remove(modulenrtmp.indexOf("\n") , modulenrtmp.length());
int modNRINT = modulenrtmp.toInt();
EEPROM.write(moduleNraddress, modNRINT);
EEPROM.commit();
String ftptmp = qrData;
ftptmp.remove(0, ftptmp.indexOf("\n") + 1);
ftptmp.remove(0, ftptmp.indexOf("\n") + 1);
ftptmp.remove(0, ftptmp.indexOf("\n") + 1);
ftptmp.remove(ftptmp.indexOf("\n") , ftptmp.length());
EEPROM.writeString(ftpAddr, ftptmp);
EEPROM.commit();
EEPROM.writeBool(configModeAddr, true);
EEPROM.commit();
//indicate QR succesfully read
blinkBurst(2, 300);
ESP.restart();
}
As you can see I'm creating unnecessary Strings. What would be the right way to do it?
Cheers and thanks for your time!
Upvotes: 0
Views: 53
Reputation: 16454
If you can edit mypayload
you can use std::strtok
void convertPayload() {
String ssidtmp = std::strtok(mypayload, "\n");
EEPROM.writeString(ssidAddr,ssidtmp);
EEPROM.commit();
String passtmp = std::strtok(nullptr, "\n");
EEPROM.writeString(passAddr, passtmp);
EEPROM.commit();
String modulenrtmp = std::strtok(nullptr, "\n");
int modNRINT = modulenrtmp.toInt();
EEPROM.write(moduleNraddress, modNRINT);
EEPROM.commit();
String ftptmp = = std::strtok(nullptr, "\n");
EEPROM.writeString(ftpAddr, ftptmp);
EEPROM.commit();
EEPROM.writeBool(configModeAddr, true);
EEPROM.commit();
//indicate QR succesfully read
blinkBurst(2, 300);
ESP.restart();
}
Upvotes: 1