(fw,cpn) Support commas in labels

Remove more cpn unused code
This commit is contained in:
dlktdr
2022-09-06 21:04:10 +10:00
committed by Peter Feerick
parent cd50c0a967
commit 4067c6df07
7 changed files with 196 additions and 211 deletions
+107 -65
View File
@@ -323,7 +323,6 @@ LabelsVector ModelMap::getLabels()
* index to it. If label doesn't exist it adds it at the end of the
* list and returns the new index
* Won't allow creation of the special case label "Unlabeled" STR_UNLABELEDMODEL
* All commas(',') are replaced with underscore('_')
*
* @param lbl Adds a label to the list
* @return int -1 on failure, label index on success
@@ -334,9 +333,6 @@ int ModelMap::addLabel(std::string lbl)
if (lbl.size() == 0) return -1;
if (lbl == STR_UNLABELEDMODEL) return -1;
// Remove any commas
std::replace(lbl.begin(), lbl.end(), ',', '_');
// Add a new label if if doesn't already exist in the list
// Returns the index to the label
int ind = getIndexByLabel(lbl);
@@ -364,12 +360,8 @@ bool ModelMap::addLabelToModel(const std::string &lbl, ModelCell *cell,
{
// First check that there aren't too many labels on this model
LabelsVector lbs = getLabelsByModel(cell);
int sz = 0;
for (auto const &it : lbs) {
sz = it.size() + 1; // Label length + ','
}
sz += lbl.size() + 1; // New label + ','
if (sz > LABELS_LENGTH - 1) {
lbs.push_back(lbl);
if(ModelMap::toCSV(lbs).size() > LABELS_LENGTH - 1) {
TRACE("Cannot add the %s label to the model. Too many labels", lbl.c_str());
return true;
}
@@ -409,6 +401,73 @@ void ModelMap::setSortOrder(ModelsSortBy sortby)
}
}
/**
* @brief Convert a vector of labels to a comma separated list
*
* @param labels Vector of the labels
* @return labels list in CSV format
*/
std::string ModelMap::toCSV(const LabelsVector &labels)
{
std::string csv;
bool comma=false;
for(auto lbl: labels) {
if(lbl == "")
continue;
if(comma) {
csv += ',';
}
// Escape out commas
replace_all(lbl, "/", "//");
replace_all(lbl, ",", "/c");
csv += lbl;
comma = true;
}
return csv;
}
/**
* @brief Convert a Comma separated string list to LabelsVector
*
* @param str Comma separated string of labels
* @return vector of all labels
*/
LabelsVector ModelMap::fromCSV(const char *str)
{
LabelsVector lbls;
std::istringstream f(str);
std::string lbl;
while (std::getline(f, lbl, ',')) {
// Un-escape commas
replace_all(lbl, "/c", ",");
replace_all(lbl, "//", "/");
lbls.push_back(lbl);
}
return lbls;
}
/**
* @brief Replace all occurances of <from> to <to> in <str>
*
* @param str String
* @param from String to search
* @param to String to replace
*/
void ModelMap::replace_all(std::string &str,
const std::string &from,
const std::string &to)
{
if(from.empty()) return;
size_t pos = 0;
while((pos = str.find(from, pos)) != std::string::npos) {
str.replace(pos, from.length(), to);
pos += to.length();
}
}
/**
* @brief Removes a label from a model
*
@@ -440,9 +499,8 @@ bool ModelMap::removeLabelFromModel(const std::string &label, ModelCell *cell,
/**
* @brief Removes a label
* @details Remove a label from the list, only if there are no models that have
* the label selected. The label isn't actually removed only set blank
* this is to keep the proper index of the multimap.
* On reboot it will be re-synced again.
* the label selected. The label is set blank, when writing this will be
* skipped. Labels are re-synced
*
* @param label Label to be removed
* @return true Label wasn't found or not empty
@@ -468,6 +526,11 @@ bool ModelMap::removeLabel(
addLabel(STR_FAVORITE_LABEL);
}
// Save and rescan
modelslist.save();
modelslist.clear();
modelslist.load();
return renameFault;
}
@@ -546,8 +609,18 @@ bool ModelMap::renameLabel(
readModelYaml(modcell->modelFilename, (uint8_t *)modeldata,
sizeof(ModelData));
// Make sure there is room to rename
int nlen = strlen(modeldata->header.labels) + to.size() - from.size();
// Make sure there is room to rename, use toCSV to capture escape chars
LabelsVector tmpvect;
tmpvect.push_back(to);
int newlen = ModelMap::toCSV(tmpvect).size();
tmpvect.clear();
tmpvect.push_back(from);
int oldlen = ModelMap::toCSV(tmpvect).size();
// Separate Curent CSV
LabelsVector lbls = ModelMap::fromCSV(modeldata->header.labels);
int nlen = lbls.size() + newlen - oldlen;
if (nlen > LABELS_LENGTH - 1) {
fault = true;
TRACE("Labels: Rename Error! Labels too long on %s - %s",
@@ -555,37 +628,20 @@ bool ModelMap::renameLabel(
continue;
}
// Separate CSV
LabelsVector lbls;
char *cma;
cma = strtok(modeldata->header.labels, ",");
int numTokens = 0;
while (cma != NULL) {
lbls.push_back(cma);
cma = strtok(NULL, ",");
numTokens++;
}
// Replace from->to strings
for (auto &lbl : lbls) {
if (lbl == from) lbl = to;
}
// Remove duplicates
// Remove possible duplicates
std::sort(lbls.begin(), lbls.end());
auto last = std::unique(lbls.begin(), lbls.end());
lbls.erase(last, lbls.end());
lbls.resize(std::distance(lbls.begin(), last));
// Write back
bool comma = false;
modeldata->header.labels[0] = '\0';
for (auto lbl : lbls) {
if (lbl == "") continue;
if (comma) strcat(modeldata->header.labels, ",");
strcat(modeldata->header.labels, lbl.c_str());
comma = true;
}
strncpy(modeldata->header.labels, ModelMap::toCSV(lbls).c_str(), LABELS_LENGTH);
modeldata->header.labels[LABELS_LENGTH-1] = '\0';
char path[256];
getModelPath(path, modcell->modelFilename);
@@ -631,25 +687,20 @@ bool ModelMap::renameLabel(
}
/**
* @brief Returns a comma separated list of the labels
* @brief Returns a comma separated list of the labels, used in model_setup
*
* @param curmod Module
* @return std::string
* @param curmod Model Cell
* @param noresults String to return when no labels found
* @return std::string of all Labels, if no results return
*/
std::string ModelMap::getLabelString(ModelCell *curmod, const char *noresults)
{
std::string allLabels;
int numModels = 0;
for (auto &label : getSelectedLabels(curmod)) {
if (label.second) {
allLabels = allLabels + (numModels != 0 ? "," : "") + label.first;
numModels++;
}
std::string lbls = ModelMap::toCSV(getLabelsByModel(curmod));
if(lbls.size() == 0) {
return noresults;
}
if (numModels == 0) allLabels = noresults;
return allLabels;
return lbls;
}
/**
@@ -781,6 +832,9 @@ void ModelsList::init()
void ModelsList::clear()
{
for(ModelCell *mdl: *this) {
delete(mdl);
}
std::vector<ModelCell *>::clear();
init();
}
@@ -843,11 +897,9 @@ void ModelMap::updateModelCell(ModelCell *cell)
readModelYaml(cell->modelFilename, (uint8_t *)model, sizeof(ModelData));
strcpy(cell->modelName, model->header.name);
strcpy(cell->modelBitmap, model->header.bitmap);
char *cma;
cma = strtok(model->header.labels, ",");
while (cma != NULL) {
modelslabels.addLabelToModel(cma, cell);
cma = strtok(NULL, ",");
LabelsVector labels = ModelMap::fromCSV(model->header.labels);
for(const auto &lbl : labels ) {
modelslabels.addLabelToModel(lbl,cell);
}
// Save Module Data
@@ -1105,17 +1157,7 @@ const char *ModelsList::save(LabelsVector newOrder)
(unsigned int)model->moduleData[i].subType);
}
f_puts(" labels: \"", &file);
LabelsVector labels = modelslabels.getLabelsByModel(model);
bool comma = false;
for (auto const &label : labels) {
if (comma) {
f_puts(",", &file);
}
f_puts(label.c_str(), &file);
comma = true;
}
f_puts("\"\r\n", &file);
f_printf(&file, " labels: \"%s\"\r\n", ModelMap::toCSV(modelslabels.getLabelsByModel(model)).c_str());
#if LEN_BITMAP_NAME > 0
f_puts(" bitmap: \"", &file);
@@ -1202,7 +1244,7 @@ bool ModelsList::readNextLine(char *line, int maxlen)
*
* @param name Model File Name
* @param save True=Update labels.yml right away
* @param copyCell If duplicating, copy the data from this cell. If duplicating, leave null
* @param copyCell If duplicating copy the data from this cell, otherwise leave null
* @return ModelCell* New Model
*/
+7
View File
@@ -30,6 +30,7 @@
#include <map>
#include <set>
#include <string>
#include <sstream>
#include <vector>
#include "sdcard.h"
@@ -158,6 +159,12 @@ class ModelMap : protected std::multimap<uint16_t, ModelCell *>
void setSortOrder(ModelsSortBy sortby);
ModelsSortBy sortOrder() {return _sortOrder;}
static std::string toCSV(const LabelsVector &labels);
static LabelsVector fromCSV(const char *str);
static void replace_all(std::string &str,
const std::string &from,
const std::string &to);
protected:
ModelsSortBy _sortOrder = DEFAULT_MODEL_SORT;
bool _isDirty = true;
+54 -61
View File
@@ -111,7 +111,6 @@ static bool find_node(void* ctx, char* buf, uint8_t len)
TRACE_LABELS_YAML("YAML Found Node. Level = %u, Section= %u, %s", mi->level, mi->section, mi->current_attr);
// Switch between the different maps in labels.yml
if (mi->level == 0) {
if (strcasecmp(mi->current_attr,"labels") == 0) {
@@ -131,7 +130,11 @@ static bool find_node(void* ctx, char* buf, uint8_t len)
bool found=false;
for(auto &filehash : modelslist.fileHashInfo) {
if(filehash.name == mi->current_attr) {
TRACE_LABELS_YAML(" Model %s has a real file, creating a modelcell");
TRACE_LABELS_YAML(" Model %s has a real file, creating a modelcell", filehash.name);
if(filehash.celladded) {
TRACE_LABELS_YAML(" Duplicate found labels.yml model cell %s already added", filehash.name);
break;
}
ModelCell *model = new ModelCell(mi->current_attr);
strcpy(model->modelFinfoHash, filehash.hash);
modelslist.push_back(model);
@@ -172,81 +175,71 @@ static void set_attr(void* ctx, char* buf, uint8_t len)
TRACE_LABELS_YAML("YAML Attr Level %u, Section %u, %s = %s", mi->level, mi->section, mi->current_attr, value);
// Model Section
if(mi->level == 2 && mi->section == labelslist_iter::SEC_Models) {
if(mi->level == 2 && mi->section == labelslist_iter::SEC_Models && mi->curmodel != NULL) {
// File Hash
if(!strcasecmp(mi->current_attr, "hash")) {
if(mi->curmodel != NULL) {
if(!strcmp(mi->curmodel->modelFinfoHash, value)) {
TRACE_LABELS_YAML("FILE HASH MATCHES, No need to scan this model, just load the settings");
mi->modeldatavalid = true;
mi->curmodel->valid_rfData = true;
mi->curmodel->_isDirty = false;
} else {
TRACE_LABELS_YAML("FILE HASH Does not Match, Open model and rebuild modelcell");
mi->modeldatavalid = false;
mi->curmodel->_isDirty = true;
}
if(!strcmp(mi->curmodel->modelFinfoHash, value)) {
TRACE_LABELS_YAML("FILE HASH MATCHES, No need to scan this model, just load the settings");
mi->modeldatavalid = true;
mi->curmodel->valid_rfData = true;
mi->curmodel->_isDirty = false;
} else {
TRACE_LABELS_YAML("FILE HASH Does not Match, Open model and rebuild modelcell");
mi->modeldatavalid = false;
mi->curmodel->_isDirty = true;
}
}
// Model Name
} else if(mi->modeldatavalid && !strcasecmp(mi->current_attr, "name")) {
if(mi->curmodel != NULL) {
// Don't bother filling in values below if hash didn't match
if(mi->modeldatavalid) {
// Model Name
if(!strcasecmp(mi->current_attr, "name")) {
mi->curmodel->setModelName(value);
TRACE_LABELS_YAML(" Set the models name");
}
// Last Opened
} else if(mi->modeldatavalid && !strcasecmp(mi->current_attr, "lastopen")) {
if(mi->curmodel != NULL) {
// Last Opened
} else if(!strcasecmp(mi->current_attr, "lastopen")) {
mi->curmodel->lastOpened = (gtime_t)strtol(value, NULL, 0);
TRACE_LABELS_YAML(" Last Opened %lu", value);
}
// Model Bitmap
#if LEN_BITMAP_NAME > 0
} else if(mi->modeldatavalid && !strcasecmp(mi->current_attr, "bitmap")) {
if(mi->curmodel != NULL) {
// Model Bitmap
#if LEN_BITMAP_NAME > 0
} else if(!strcasecmp(mi->current_attr, "bitmap")) {
// TODO Check if it exists ?
strcpy(mi->curmodel->modelBitmap, value);
TRACE_LABELS_YAML(" Set the models bitmap");
}
#endif
#endif
// Model Labels
} else if(mi->modeldatavalid && !strcasecmp(mi->current_attr, "labels")) {
if(mi->curmodel != NULL) {
char *cma;
cma = strtok(value, ",");
int numTokens = 0;
while(cma != NULL) {
modelslabels.addLabelToModel(cma,mi->curmodel);
TRACE_LABELS_YAML(" Adding the label - %s", cma);
cma = strtok(NULL, ",");
numTokens++;
// Model Labels
} else if(!strcasecmp(mi->current_attr, "labels")) {
LabelsVector labels = ModelMap::fromCSV(value);
for(const auto &lbl : labels ) {
modelslabels.addLabelToModel(lbl,mi->curmodel);
TRACE_LABELS_YAML(" Adding the label - %s", lbl.c_str());
}
}
// RF Module Data
} else {
char cmp[15];
for(int i=0; i < NUM_MODULES; i++) {
snprintf(cmp, sizeof(cmp), MODULE_ID_STR, i);
cmp[sizeof(cmp)-1] = '\0';
if(mi->curmodel != NULL && mi->modeldatavalid && !strcasecmp(mi->current_attr, cmp)) {
mi->curmodel->modelId[i] = strtol(value,NULL,10);
TRACE_LABELS_YAML( " Set the module %d rfId to %s", i, value);
}
snprintf(cmp, sizeof(cmp), MODULE_TYPE_STR, i);
cmp[sizeof(cmp)-1] = '\0';
if(mi->curmodel != NULL && mi->modeldatavalid && !strcasecmp(mi->current_attr, cmp)) {
mi->curmodel->moduleData[i].type = strtol(value,NULL,10);
TRACE_LABELS_YAML(" Set the module %d rfType to %s", i, value);
}
snprintf(cmp, sizeof(cmp), MODULE_RFPROTOCOL_STR, i);
cmp[sizeof(cmp)-1] = '\0';
if(mi->curmodel != NULL && mi->modeldatavalid && !strcasecmp(mi->current_attr, cmp)) {
mi->curmodel->moduleData[i].subType = strtol(value,NULL,10);
TRACE_LABELS_YAML(" Set the module %d rfProtocol to %s", i, value);
// RF Module Data
} else {
char cmp[15];
for(int i=0; i < NUM_MODULES; i++) {
snprintf(cmp, sizeof(cmp), MODULE_ID_STR, i);
cmp[sizeof(cmp)-1] = '\0';
if(!strcasecmp(mi->current_attr, cmp)) {
mi->curmodel->modelId[i] = strtol(value,NULL,10);
TRACE_LABELS_YAML( " Set the module %d rfId to %s", i, value);
}
snprintf(cmp, sizeof(cmp), MODULE_TYPE_STR, i);
cmp[sizeof(cmp)-1] = '\0';
if(!strcasecmp(mi->current_attr, cmp)) {
mi->curmodel->moduleData[i].type = strtol(value,NULL,10);
TRACE_LABELS_YAML(" Set the module %d rfType to %s", i, value);
}
snprintf(cmp, sizeof(cmp), MODULE_RFPROTOCOL_STR, i);
cmp[sizeof(cmp)-1] = '\0';
if(!strcasecmp(mi->current_attr, cmp)) {
mi->curmodel->moduleData[i].subType = strtol(value,NULL,10);
TRACE_LABELS_YAML(" Set the module %d rfProtocol to %s", i, value);
}
}
}
}