-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.cpp
89 lines (75 loc) · 2.16 KB
/
options.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
University of Parma - Italy
This file is part of ArduinoCoAP
ArduinoCoAP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Author:
Daniele Sportillo (daniele.sportillo@gmail.com)
*/
#include "options.h"
Options::Options() {
this->opts = (Option**)malloc(sizeof(Option*));
this->index = 0;
}
Options::~Options(){
for(byte i=0; i<this->index; i++) {
if(this->opts[i] != NULL){
delete this->opts[i];
this->opts[i] = NULL;
}
}
if(this->opts!=NULL){
free(this->opts);
this->opts = NULL;
}
}
boolean Options::addOption(Option* opt, boolean over) {
if (false==over) {
this->opts[this->index++] = opt;
this->opts = (Option**)realloc(this->opts, (this->index+1)*sizeof(Option*));
if(opts==NULL) Serial.println("MEMORY OPTION ERROR");
return (this->opts==NULL);
} else {
int j = this->getOptionIndexByNumber(opt->getNumber());
if (index>0) {
this->opts[index] = opt;
return true;
} else {
return this->addOption(opt, false);
}
}
}
Option* Options::getOptionByNumber(byte number) {
for (byte i=0; i<this->index; i++) {
Option* opt = this->opts[i];
if (opt->getNumber()==number) {
return opt;
}else{
delete opt;
opt = NULL;
}
}
}
byte Options::getOptionIndexByNumber(byte number) {
for (byte i=0; i<this->index; i++) {
Option* o = this->opts[i];
if (o->getNumber()==number) {
return i;
}
}
return -1;
}
Option* Options::getOptionAt(byte index) {
return this->opts[index];
}
byte Options::getSize() {
return this->index;
}