Added the Dictionary utility class to handle key value pairs
This commit is contained in:
parent
625dd0ef4c
commit
de462a4de3
16
src/app/CFGDictionary.cpp
Normal file
16
src/app/CFGDictionary.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
#include "CFGDictionary.h"
|
||||
|
||||
CFGDictionary::CFGDictionary()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
CFGDictionary::CFGDictionary(const char *parameter, const char *value) : Dictionary(parameter, value)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
CFGDictionary::~CFGDictionary()
|
||||
{
|
||||
|
||||
}
|
16
src/app/CFGDictionary.h
Normal file
16
src/app/CFGDictionary.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef CFGDICTIONARY_H
|
||||
#define CFGDICTIONARY_H
|
||||
|
||||
#include "Dictionary.h"
|
||||
|
||||
class CFGDictionary : public Dictionary
|
||||
{
|
||||
public:
|
||||
CFGDictionary();
|
||||
~CFGDictionary();
|
||||
protected:
|
||||
private:
|
||||
CFGDictionary(const char *parameter, const char *value);
|
||||
};
|
||||
|
||||
#endif //CFGDICTIONARY_H
|
199
src/app/Dictionary.cpp
Normal file
199
src/app/Dictionary.cpp
Normal file
@ -0,0 +1,199 @@
|
||||
#include "Dictionary.h"
|
||||
|
||||
Dictionary::Dictionary() :_parameter(NULL), _value(NULL), _next(NULL), _head(NULL)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Dictionary::Dictionary(const char *parameter, const char *value) : Dictionary()
|
||||
{
|
||||
//We copy the parameter and the value
|
||||
_parameter = (char *) malloc((strlen(parameter) * sizeof(char)) + 1); //+1 for the string terminating character
|
||||
_value = (char *) malloc((strlen(value) * sizeof(char)) + 1);
|
||||
|
||||
strcpy(_parameter, parameter);
|
||||
strcpy(_value, value);
|
||||
}
|
||||
|
||||
Dictionary::Dictionary(Dictionary const& dictionaryToCopy) //Copy constructor needed because of pointers
|
||||
{
|
||||
_head = NULL;
|
||||
_next = NULL;
|
||||
|
||||
_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);
|
||||
}
|
||||
|
||||
Dictionary::~Dictionary()
|
||||
{
|
||||
free(_parameter);
|
||||
_parameter = NULL;
|
||||
free(_value);
|
||||
_value = NULL;
|
||||
}
|
||||
|
||||
boolean Dictionary::addParameter(const char *parameter, const char *value)
|
||||
{
|
||||
Dictionary *dictionaryNode = new Dictionary(parameter, value);
|
||||
return addNewNodeAtTheEnd(dictionaryNode);
|
||||
}
|
||||
|
||||
Dictionary Dictionary::get(const char *parameter)
|
||||
{
|
||||
if(isListEmpty(_head))return Dictionary();
|
||||
|
||||
Dictionary *cursor = _head;
|
||||
|
||||
while(!isListEmpty(cursor))
|
||||
{
|
||||
if(strcmp(cursor->_parameter,parameter) == 0)
|
||||
return *cursor;
|
||||
cursor = cursor->_next;
|
||||
}
|
||||
|
||||
return Dictionary();
|
||||
}
|
||||
|
||||
Dictionary Dictionary::operator()(const char *parameter)
|
||||
{
|
||||
if(isListEmpty(_head))return Dictionary();
|
||||
|
||||
Dictionary *cursor = _head;
|
||||
|
||||
while(!isListEmpty(cursor))
|
||||
{
|
||||
if(strcmp(cursor->_parameter,parameter) == 0)
|
||||
return *cursor;
|
||||
cursor = cursor->_next;
|
||||
}
|
||||
|
||||
return Dictionary();
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
while(!isListEmpty(cursor))
|
||||
{
|
||||
counter++;
|
||||
cursor = cursor->_next;
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Dictionary *cursor = _head;
|
||||
while(!isListEmpty(cursor->_next))
|
||||
{
|
||||
if(strcmp(cursor->_next->_parameter, node->_parameter) == 0)//If we find the same parameter name, we replace it
|
||||
{
|
||||
free(cursor->_next->_value);
|
||||
cursor->_next->_value = (char *) malloc((strlen(node->_value) * sizeof(char)) + 1);
|
||||
if(cursor->_next->_value == NULL)return false;
|
||||
strcpy(cursor->_next->_value, node->_value);
|
||||
delete node;
|
||||
|
||||
return true;
|
||||
}
|
||||
cursor = cursor->_next;
|
||||
}
|
||||
cursor->_next = node;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean Dictionary::removeNode(Dictionary *node)
|
||||
{
|
||||
if(node == NULL) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void Dictionary::dispose()
|
||||
{
|
||||
if(isListEmpty(_head))return;
|
||||
|
||||
Dictionary *cursor = _head, *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;
|
||||
}
|
||||
}
|
||||
_head = NULL;
|
||||
_next = NULL;
|
||||
}
|
48
src/app/Dictionary.h
Normal file
48
src/app/Dictionary.h
Normal file
@ -0,0 +1,48 @@
|
||||
#ifndef DICTIONARY_H
|
||||
#define DICTIONARY_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
class Dictionary
|
||||
{
|
||||
public:
|
||||
~Dictionary();
|
||||
|
||||
boolean addParameter(const char *parameter, const char *value);
|
||||
boolean deleteParameter(const char *parameter);
|
||||
Dictionary get(const char *parameter);
|
||||
Dictionary operator()(const char *parameter);
|
||||
unsigned int count();
|
||||
void dispose();
|
||||
|
||||
long longValue() const {return _value == NULL ? 0 : strtol(_value, NULL, 10);}
|
||||
int intValue() const {return (int) (_value == NULL ? 0 : strtol(_value, NULL, 10));}
|
||||
unsigned long uintValue() const {return _value == NULL ? 0 : strtoul(_value, NULL, 10);}
|
||||
const char *stringValue() const {return _value == NULL ? "" : _value;}
|
||||
double doubleValue() const {return _value == NULL ? 0 : strtod(_value, NULL);}
|
||||
float floatValue() const {return _value == NULL ? 0 : strtof(_value, NULL);}
|
||||
boolean booleanValue() const
|
||||
{
|
||||
if(_value == NULL)
|
||||
return false;
|
||||
return strcmp(_value,"true") == false ? true : false;
|
||||
}
|
||||
protected:
|
||||
Dictionary();
|
||||
Dictionary(const char *parameter, const char *value);
|
||||
Dictionary(Dictionary const& dictionaryToCopy); //Copy constructor
|
||||
|
||||
boolean addNewNodeAtTheEnd(Dictionary *node);
|
||||
boolean removeNode(Dictionary *node);
|
||||
boolean isListEmpty(Dictionary *node) {return node == NULL;}
|
||||
|
||||
char *_parameter;
|
||||
char *_value;
|
||||
Dictionary *_next;
|
||||
Dictionary *_head;
|
||||
private:
|
||||
};
|
||||
|
||||
#endif //DICTIONARY_H
|
@ -2,14 +2,15 @@
|
||||
#include "PinMapping.h"
|
||||
#include "SAB.h"
|
||||
#include "views.h"
|
||||
#include "CFGDictionary.h"
|
||||
|
||||
SAB sab;
|
||||
|
||||
unsigned long currentMs = 0, batteryMs = 0, buttonMs = 0;
|
||||
int mySize = 0;
|
||||
unsigned char curView = 0;
|
||||
int mySize = 3;
|
||||
View1Packet v1p = {sab.getRtcManager().getDateTime(), sab.getSdCardManager().getSize(GBYTE), sab.getPowerInfo(),0};
|
||||
ViewAPPacket vap = {sab.getConnectivityManager().softAPmacAddress(), sab.getConnectivityManager().softAPSSID(), sab.getConnectivityManager().softAPIP(), sab.getConnectivityManager().softAPgetStationNum()};
|
||||
CFGDictionary dico;
|
||||
|
||||
void setup()
|
||||
{
|
||||
@ -19,7 +20,7 @@ void setup()
|
||||
sab.getScreenManager().addView(&(view_1), &v1p, 0);
|
||||
sab.getScreenManager().addView(&(view_2), &vap, 1);
|
||||
sab.getScreenManager().addView(&(view_3), NULL, 2);
|
||||
sab.getScreenManager().displayView(curView);
|
||||
sab.getScreenManager().displayView(0);
|
||||
if(sab.getRtcManager().hasLostPower())
|
||||
{
|
||||
Serial.println("Clock lost power");
|
||||
@ -42,12 +43,22 @@ void loop()
|
||||
vap.nbOfCon = sab.getConnectivityManager().softAPgetStationNum();
|
||||
|
||||
sab.getScreenManager().displayView();
|
||||
Serial.print(F("View count : "));Serial.println(sab.getScreenManager().getViewCount());
|
||||
|
||||
//Dico test
|
||||
dico.addParameter("SSID", "ESP8266SwissArmyBoard");
|
||||
dico.addParameter("PASSWORD", "APassWord");
|
||||
Serial.print(F("SSID "));Serial.println(dico("SSID").stringValue());
|
||||
Serial.print(F("PASSWORD "));Serial.println(dico("PASSWORD").stringValue());
|
||||
Serial.print(F("Dico count : "));Serial.println(dico.count());
|
||||
}
|
||||
|
||||
if(millis() - batteryMs > 10000)
|
||||
{
|
||||
batteryMs = millis();
|
||||
v1p.powerInfo = sab.getPowerInfo();
|
||||
//Dico test
|
||||
dico.dispose();
|
||||
}
|
||||
|
||||
|
||||
@ -56,9 +67,7 @@ void loop()
|
||||
buttonMs = millis();
|
||||
Serial.println("Changing view");
|
||||
Serial.print("Selected view is : ");Serial.println(sab.getScreenManager().getCurrentViewUID());
|
||||
/*curView++;
|
||||
curView %= 3;*/
|
||||
|
||||
sab.getScreenManager().displayNextView();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user