Skip to content

Commit

Permalink
[preferences] improving string get and put
Browse files Browse the repository at this point in the history
  • Loading branch information
andreagilardoni authored and pennam committed Nov 27, 2024
1 parent 0f3333b commit 30ba157
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions libraries/Preferences/src/Preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ size_t Preferences::putBool(const char* key, const bool value) {
size_t Preferences::putString(const char* key, const char* value) {
string res = "";
if (key != nullptr && strlen(key) > 0 && value != nullptr && strlen(value) > 0) {
if (modem.write(string(PROMPT(_PREF_PUT)), res, "%s%s,%d,%s\r\n", CMD_WRITE(_PREF_PUT), key, PT_STR, value)) {
return atoi(res.c_str());
modem.write_nowait(string(PROMPT(_PREF_PUT)), res, "%s%s,%d,%d\r\n", CMD_WRITE(_PREF_PUT), key, PT_STR, strlen(value));
if(modem.passthrough((uint8_t *)value, strlen(value))) {
return strlen(value);
}
}
return 0;
Expand Down Expand Up @@ -307,9 +308,11 @@ bool Preferences::getBool(const char* key, const bool defaultValue) {
size_t Preferences::getString(const char* key, char* value, const size_t maxLen) {
string res = "";
if (key != nullptr && strlen(key) > 0 && value != nullptr) {
modem.read_using_size();
if (modem.write(string(PROMPT(_PREF_GET)), res, "%s%s,%d\r\n", CMD_WRITE(_PREF_GET), key, PT_STR)) {
if (res.length() < maxLen) {
if (res.length()+1 < maxLen) { // take into account \0 at the end
strncpy(value, res.c_str(), res.length());
value[res.length()] = '\0';
return res.length();
}
}
Expand All @@ -320,6 +323,7 @@ size_t Preferences::getString(const char* key, char* value, const size_t maxLen)
String Preferences::getString(const char* key, const String defaultValue) {
string res = "";
if (key != nullptr && strlen(key) > 0) {
modem.read_using_size();
if (modem.write(string(PROMPT(_PREF_GET)), res, "%s%s,%d,%s\r\n", CMD_WRITE(_PREF_GET), key, PT_STR, defaultValue.c_str())) {
return String(res.c_str());
}
Expand Down

0 comments on commit 30ba157

Please sign in to comment.