Reworked the dictionary algorithm and Added the deleteParameter method
This commit is contained in:
parent
de462a4de3
commit
933b055ec3
@ -1,8 +1,8 @@
|
||||
#include "Dictionary.h"
|
||||
|
||||
Dictionary::Dictionary() :_parameter(NULL), _value(NULL), _next(NULL), _head(NULL)
|
||||
Dictionary::Dictionary() :_parameter(NULL), _value(NULL), _next(NULL), _head(this)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
Dictionary::Dictionary(const char *parameter, const char *value) : Dictionary()
|
||||
@ -22,7 +22,7 @@ Dictionary::Dictionary(Dictionary const& dictionaryToCopy) //Copy constructor ne
|
||||
|
||||
_parameter = (char *) malloc((strlen(dictionaryToCopy._parameter) * sizeof(char)) + 1); //+1 for the string terminating character
|
||||
_value = (char *) malloc((strlen(dictionaryToCopy._value) * sizeof(char)) + 1);
|
||||
|
||||
|
||||
strcpy(_parameter, dictionaryToCopy._parameter);
|
||||
strcpy(_value, dictionaryToCopy._value);
|
||||
}
|
||||
@ -30,9 +30,9 @@ Dictionary::Dictionary(Dictionary const& dictionaryToCopy) //Copy constructor ne
|
||||
Dictionary::~Dictionary()
|
||||
{
|
||||
free(_parameter);
|
||||
_parameter = NULL;
|
||||
//_parameter = NULL; Useless, just my c habits
|
||||
free(_value);
|
||||
_value = NULL;
|
||||
//_value = NULL; Useless, just my c habits
|
||||
}
|
||||
|
||||
boolean Dictionary::addParameter(const char *parameter, const char *value)
|
||||
@ -43,9 +43,9 @@ boolean Dictionary::addParameter(const char *parameter, const char *value)
|
||||
|
||||
Dictionary Dictionary::get(const char *parameter)
|
||||
{
|
||||
if(isListEmpty(_head))return Dictionary();
|
||||
if(isListEmpty(_head->_next))return Dictionary();
|
||||
|
||||
Dictionary *cursor = _head;
|
||||
Dictionary *cursor = _head->_next;
|
||||
|
||||
while(!isListEmpty(cursor))
|
||||
{
|
||||
@ -59,9 +59,9 @@ Dictionary Dictionary::get(const char *parameter)
|
||||
|
||||
Dictionary Dictionary::operator()(const char *parameter)
|
||||
{
|
||||
if(isListEmpty(_head))return Dictionary();
|
||||
if(isListEmpty(_head->_next))return Dictionary();
|
||||
|
||||
Dictionary *cursor = _head;
|
||||
Dictionary *cursor = _head->_next;
|
||||
|
||||
while(!isListEmpty(cursor))
|
||||
{
|
||||
@ -75,13 +75,10 @@ Dictionary Dictionary::operator()(const char *parameter)
|
||||
|
||||
unsigned int Dictionary::count()
|
||||
{
|
||||
unsigned int counter = 0;
|
||||
if(isListEmpty(_head))return counter;
|
||||
|
||||
if(_head->_parameter == NULL && _head->_value == NULL) return 0;
|
||||
|
||||
Dictionary *cursor = _head;
|
||||
unsigned int counter(0);
|
||||
if(isListEmpty(_head->_next))return counter;
|
||||
|
||||
Dictionary *cursor = _head->_next;
|
||||
while(!isListEmpty(cursor))
|
||||
{
|
||||
counter++;
|
||||
@ -95,56 +92,14 @@ boolean Dictionary::addNewNodeAtTheEnd(Dictionary *node)
|
||||
if(node == NULL) return false;
|
||||
|
||||
node->_head = _head; //Every node should point to the first node
|
||||
|
||||
if(_parameter == NULL && _value == NULL)
|
||||
{
|
||||
_parameter = (char *) malloc((strlen(node->_parameter) * sizeof(char)) + 1); //+1 for the string terminating character
|
||||
if(_parameter == NULL)return false;
|
||||
|
||||
_value = (char *) malloc((strlen(node->_value) * sizeof(char)) + 1);
|
||||
if(_value == NULL)return false;
|
||||
|
||||
strcpy(_parameter, node->_parameter);
|
||||
strcpy(_value, node->_value);
|
||||
|
||||
delete node;
|
||||
_head = this;
|
||||
|
||||
if(_next == NULL) //This is our first node then
|
||||
{
|
||||
_next = node;
|
||||
return true;
|
||||
}
|
||||
|
||||
if(_next == NULL)
|
||||
{
|
||||
if(strcmp(_parameter, node->_parameter) == 0)//If the same, we replace it
|
||||
{
|
||||
free(_value);
|
||||
_value = (char *) malloc((strlen(node->_value) * sizeof(char)) + 1);
|
||||
if(_value == NULL)return false;
|
||||
strcpy(_value, node->_value);
|
||||
delete node;
|
||||
|
||||
return true;
|
||||
}
|
||||
else //If not the same, we add the new node to the end of the list
|
||||
{
|
||||
_next = node;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(strcmp(_parameter, node->_parameter) == 0)//If the same, we replace it
|
||||
{
|
||||
free(_value);
|
||||
_value = (char *) malloc((strlen(node->_value) * sizeof(char)) + 1);
|
||||
if(_value == NULL)return false;
|
||||
strcpy(_value, node->_value);
|
||||
delete node;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// /!\ We have to work with the _next reference in the loop, if we don't it won't work as expected
|
||||
Dictionary *cursor = _head;
|
||||
while(!isListEmpty(cursor->_next))
|
||||
{
|
||||
@ -165,35 +120,41 @@ boolean Dictionary::addNewNodeAtTheEnd(Dictionary *node)
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean Dictionary::removeNode(Dictionary *node)
|
||||
boolean Dictionary::deleteParameter(const char *parameter)
|
||||
{
|
||||
if(node == NULL) return false;
|
||||
return true;
|
||||
if(_head->_next == NULL) return false;
|
||||
|
||||
Dictionary *cursor = _head, *toDelete(NULL);
|
||||
|
||||
while(!isListEmpty(cursor->_next))
|
||||
{
|
||||
if(strcmp(cursor->_next->_parameter, parameter) == 0)
|
||||
{
|
||||
toDelete = cursor->_next;
|
||||
cursor->_next = cursor->_next->_next;
|
||||
delete toDelete;
|
||||
return true;
|
||||
}
|
||||
cursor = cursor->_next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Dictionary::dispose()
|
||||
{
|
||||
if(isListEmpty(_head))return;
|
||||
if(isListEmpty(_head->_next))return;
|
||||
|
||||
Dictionary *cursor = _head, *toDelete(NULL);
|
||||
Dictionary *cursor = _head->_next, *toDelete(NULL);
|
||||
|
||||
while(!isListEmpty(cursor))
|
||||
{
|
||||
toDelete = cursor;
|
||||
cursor = cursor->_next;
|
||||
if(toDelete != this)
|
||||
{
|
||||
delete toDelete;
|
||||
}
|
||||
else //If its the statically allocated object, just free the strings
|
||||
{
|
||||
free(_parameter);
|
||||
free(_value);
|
||||
|
||||
_parameter = NULL;
|
||||
_value = NULL;
|
||||
}
|
||||
|
||||
delete toDelete;
|
||||
}
|
||||
_head = NULL;
|
||||
_head = this;
|
||||
_next = NULL;
|
||||
}
|
||||
|
||||
void Dictionary::clear() {this->dispose();}
|
||||
|
@ -3,18 +3,36 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
class Dictionary
|
||||
{
|
||||
public:
|
||||
~Dictionary();
|
||||
|
||||
|
||||
boolean addParameter(const char *parameter, const char *value);
|
||||
boolean addParameter(const char *parameter, const int value)
|
||||
{
|
||||
char buffer[12] = "";
|
||||
sprintf(buffer,"%d",value);
|
||||
return addParameter(parameter, buffer);
|
||||
};
|
||||
boolean addParameter(const char *parameter, const float value)
|
||||
{
|
||||
return addParameter(parameter, (double) value);
|
||||
};
|
||||
boolean addParameter(const char *parameter, const double value)
|
||||
{
|
||||
char buffer[50] = "";
|
||||
sprintf(buffer,"%f",value);
|
||||
return addParameter(parameter, buffer);
|
||||
};
|
||||
boolean deleteParameter(const char *parameter);
|
||||
Dictionary get(const char *parameter);
|
||||
Dictionary operator()(const char *parameter);
|
||||
unsigned int count();
|
||||
void clear();
|
||||
void dispose();
|
||||
|
||||
long longValue() const {return _value == NULL ? 0 : strtol(_value, NULL, 10);}
|
||||
@ -27,7 +45,7 @@ public:
|
||||
{
|
||||
if(_value == NULL)
|
||||
return false;
|
||||
return strcmp(_value,"true") == false ? true : false;
|
||||
return strcmp(_value,"true") == 0 || strcmp(_value,"TRUE") == 0 ? true : false;
|
||||
}
|
||||
protected:
|
||||
Dictionary();
|
||||
@ -35,7 +53,6 @@ protected:
|
||||
Dictionary(Dictionary const& dictionaryToCopy); //Copy constructor
|
||||
|
||||
boolean addNewNodeAtTheEnd(Dictionary *node);
|
||||
boolean removeNode(Dictionary *node);
|
||||
boolean isListEmpty(Dictionary *node) {return node == NULL;}
|
||||
|
||||
char *_parameter;
|
||||
|
Loading…
Reference in New Issue
Block a user