Reworked the dictionary algorithm and Added the deleteParameter method
This commit is contained in:
parent
de462a4de3
commit
933b055ec3
@ -1,6 +1,6 @@
|
|||||||
#include "Dictionary.h"
|
#include "Dictionary.h"
|
||||||
|
|
||||||
Dictionary::Dictionary() :_parameter(NULL), _value(NULL), _next(NULL), _head(NULL)
|
Dictionary::Dictionary() :_parameter(NULL), _value(NULL), _next(NULL), _head(this)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -30,9 +30,9 @@ Dictionary::Dictionary(Dictionary const& dictionaryToCopy) //Copy constructor ne
|
|||||||
Dictionary::~Dictionary()
|
Dictionary::~Dictionary()
|
||||||
{
|
{
|
||||||
free(_parameter);
|
free(_parameter);
|
||||||
_parameter = NULL;
|
//_parameter = NULL; Useless, just my c habits
|
||||||
free(_value);
|
free(_value);
|
||||||
_value = NULL;
|
//_value = NULL; Useless, just my c habits
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean Dictionary::addParameter(const char *parameter, const char *value)
|
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)
|
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))
|
while(!isListEmpty(cursor))
|
||||||
{
|
{
|
||||||
@ -59,9 +59,9 @@ Dictionary Dictionary::get(const char *parameter)
|
|||||||
|
|
||||||
Dictionary Dictionary::operator()(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))
|
while(!isListEmpty(cursor))
|
||||||
{
|
{
|
||||||
@ -75,13 +75,10 @@ Dictionary Dictionary::operator()(const char *parameter)
|
|||||||
|
|
||||||
unsigned int Dictionary::count()
|
unsigned int Dictionary::count()
|
||||||
{
|
{
|
||||||
unsigned int counter = 0;
|
unsigned int counter(0);
|
||||||
if(isListEmpty(_head))return counter;
|
if(isListEmpty(_head->_next))return counter;
|
||||||
|
|
||||||
if(_head->_parameter == NULL && _head->_value == NULL) return 0;
|
|
||||||
|
|
||||||
Dictionary *cursor = _head;
|
|
||||||
|
|
||||||
|
Dictionary *cursor = _head->_next;
|
||||||
while(!isListEmpty(cursor))
|
while(!isListEmpty(cursor))
|
||||||
{
|
{
|
||||||
counter++;
|
counter++;
|
||||||
@ -96,55 +93,13 @@ boolean Dictionary::addNewNodeAtTheEnd(Dictionary *node)
|
|||||||
|
|
||||||
node->_head = _head; //Every node should point to the first node
|
node->_head = _head; //Every node should point to the first node
|
||||||
|
|
||||||
if(_parameter == NULL && _value == NULL)
|
if(_next == NULL) //This is our first node then
|
||||||
{
|
{
|
||||||
_parameter = (char *) malloc((strlen(node->_parameter) * sizeof(char)) + 1); //+1 for the string terminating character
|
_next = node;
|
||||||
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;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(_next == NULL)
|
// /!\ We have to work with the _next reference in the loop, if we don't it won't work as expected
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Dictionary *cursor = _head;
|
Dictionary *cursor = _head;
|
||||||
while(!isListEmpty(cursor->_next))
|
while(!isListEmpty(cursor->_next))
|
||||||
{
|
{
|
||||||
@ -165,35 +120,41 @@ boolean Dictionary::addNewNodeAtTheEnd(Dictionary *node)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean Dictionary::removeNode(Dictionary *node)
|
boolean Dictionary::deleteParameter(const char *parameter)
|
||||||
{
|
{
|
||||||
if(node == NULL) return false;
|
if(_head->_next == NULL) return false;
|
||||||
return true;
|
|
||||||
|
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()
|
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))
|
while(!isListEmpty(cursor))
|
||||||
{
|
{
|
||||||
toDelete = cursor;
|
toDelete = cursor;
|
||||||
cursor = cursor->_next;
|
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;
|
delete toDelete;
|
||||||
_value = NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_head = NULL;
|
_head = this;
|
||||||
_next = NULL;
|
_next = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Dictionary::clear() {this->dispose();}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
class Dictionary
|
class Dictionary
|
||||||
@ -11,10 +12,27 @@ public:
|
|||||||
~Dictionary();
|
~Dictionary();
|
||||||
|
|
||||||
boolean addParameter(const char *parameter, const char *value);
|
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);
|
boolean deleteParameter(const char *parameter);
|
||||||
Dictionary get(const char *parameter);
|
Dictionary get(const char *parameter);
|
||||||
Dictionary operator()(const char *parameter);
|
Dictionary operator()(const char *parameter);
|
||||||
unsigned int count();
|
unsigned int count();
|
||||||
|
void clear();
|
||||||
void dispose();
|
void dispose();
|
||||||
|
|
||||||
long longValue() const {return _value == NULL ? 0 : strtol(_value, NULL, 10);}
|
long longValue() const {return _value == NULL ? 0 : strtol(_value, NULL, 10);}
|
||||||
@ -27,7 +45,7 @@ public:
|
|||||||
{
|
{
|
||||||
if(_value == NULL)
|
if(_value == NULL)
|
||||||
return false;
|
return false;
|
||||||
return strcmp(_value,"true") == false ? true : false;
|
return strcmp(_value,"true") == 0 || strcmp(_value,"TRUE") == 0 ? true : false;
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
Dictionary();
|
Dictionary();
|
||||||
@ -35,7 +53,6 @@ protected:
|
|||||||
Dictionary(Dictionary const& dictionaryToCopy); //Copy constructor
|
Dictionary(Dictionary const& dictionaryToCopy); //Copy constructor
|
||||||
|
|
||||||
boolean addNewNodeAtTheEnd(Dictionary *node);
|
boolean addNewNodeAtTheEnd(Dictionary *node);
|
||||||
boolean removeNode(Dictionary *node);
|
|
||||||
boolean isListEmpty(Dictionary *node) {return node == NULL;}
|
boolean isListEmpty(Dictionary *node) {return node == NULL;}
|
||||||
|
|
||||||
char *_parameter;
|
char *_parameter;
|
||||||
|
Loading…
Reference in New Issue
Block a user