Hi everyone!
I'm making a string List library, to use in other libraries.
My list have the items and then, sub items. The sub items are divided by the DEMILITER_CHAR.
What I'm trying to do is append a sub item to a specific item index. So I decided to create a class inside my stringList class to handle the sub items functions, but I don't know how to do so, to acess it like this (in the .ino file):
stringList.item(int Index).append(char* SubItem)
The sub items class could be named class item
My files (without sub item management):
.h
# ifndef stringList_h
# define stringList_h
#include <Arduino.h>
#include <string.h>
//------------------------------------------------------------------------
#define makeList(Array) ((char*)Array)
#define element(Index) (&array[(Index)*itemsMaxLength])
#define DELIMITER_CHAR ';'
class stringList
{
public:
stringList() {}
void setup(char* Items, int MaxItems, int ItemsMaxLength);
void assign(char* Items);
void assign(char* Items, int MaxItems, int ItemsMaxLength);
int size();
void clear();
void sort();
void sortReverse();
void reverse();
void shuffle();
void lower();
void upper();
int append(char* NewItem);
bool modify(int Index, char* NewItem);
inline bool modify(char* Item, char* NewItem, int From=0) { return modify(getIndex(Item,From),NewItem); }
bool remove(int Index);
inline bool remove(char* Item, int From=0) { return remove(getIndex(Item,From)); }
inline char* getItem(int Index) { return (Index>=0 && Index<nItems ? element(Index) : NULL); }
bool getItem(int Index, char* Buffer, int Length);
int getIndex(char* Item, int From=0);
private:
char* array;
int nItems, itemsMaxLength, maxItems;
};
#endif
.cpp
#include "stringList.h"
//--------------------------------------------------------------------------------
void stringList::setup(char *Items, int MaxItems, int ItemsMaxLength){
assign(Items,MaxItems,ItemsMaxLength);
}
//--------------------------------------------------------------------------------
void stringList::assign(char* Items){
if(Items!=NULL){
array = Items;
nItems = 0;
for(int n=maxItems-1; n>=0; n--){
if(strlen(element(n))>0){
if(n+1>nItems)
nItems = n+1;
break;
}
}
}
}
//--------------------------------------------------------------------------------
void stringList::assign(char* Items, int MaxItems, int ItemsMaxLength){
itemsMaxLength = ItemsMaxLength;
maxItems = MaxItems;
assign(Items);
}
//--------------------------------------------------------------------------------
void stringList::clear(){
memset(array,0,maxItems*itemsMaxLength);
nItems = 0;
}
//--------------------------------------------------------------------------------
void stringList::sort(){
char buffer[itemsMaxLength];
memset(buffer,0,sizeof(buffer));
for(int n=0;n<nItems-1;n++){
for(int m=n+1;m<nItems;m++){
if(strcmp(element(m),element(n))<0){
strcpy(buffer,element(n));
strcpy(element(n),element(m));
strcpy(element(m),buffer);
}
}
}
}
//--------------------------------------------------------------------------------
void stringList::sortReverse(){
char buffer[itemsMaxLength];
memset(buffer,0,sizeof(buffer));
for(int n=0;n<nItems-1;n++){
for(int m=n+1;m<nItems;m++){
if(strcmp(element(m),element(n))>0){
strcpy(buffer,element(n));
strcpy(element(n),element(m));
strcpy(element(m),buffer);
}
}
}
}
//--------------------------------------------------------------------------------
void stringList::reverse(){
char buffer[itemsMaxLength];
memset(buffer,0,sizeof(buffer));
for(int n=0;n<nItems;n++){
int m = (nItems-1-n);
if(n>=m)
break;
strcpy(buffer,element(n));
strcpy(element(n),element(m));
strcpy(element(m),buffer);
}
}
//--------------------------------------------------------------------------------
void stringList::shuffle(){
char buffer[itemsMaxLength];
memset(buffer,0,sizeof(buffer));
int m;
randomSeed(analogRead(0));
for(int n=0;n<nItems;n++){
m = random(n,nItems);
strcpy(buffer,element(n));
strcpy(element(n),element(m));
strcpy(element(m),buffer);
}
}
//--------------------------------------------------------------------------------
void stringList::lower(){
for(int n=0;n<nItems;n++)
strlwr(element(n));
}
//--------------------------------------------------------------------------------
void stringList::upper(){
for(int n=0;n<nItems;n++)
strupr(element(n));
}
//--------------------------------------------------------------------------------
int stringList::size(){
return nItems;
}
//--------------------------------------------------------------------------------
int stringList::append(char* NewItem){
if(nItems >= maxItems || strlen(NewItem)>=itemsMaxLength)
return -1;
strcpy(element(nItems-1),NewItem);
return nItems-1;
}
//--------------------------------------------------------------------------------
bool stringList::modify(int Index, char* NewItem){
if(Index<0 || Index>=maxItems || strlen(NewItem)>=itemsMaxLength)
return false;
strcpy(element(Index),NewItem);
if(Index+1>nItems)
nItems = Index+1;
return true;
}
//--------------------------------------------------------------------------------
bool stringList::remove(int Index){
if(Index<0 || Index>=nItems || nItems<=0)
return false;
for(int n=Index;n<nItems;n++){
strcpy(element(n),element(n+1));
}
memset(element(nItems-1),0,itemsMaxLength);
if(Index<nItems)
nItems--;
return true;
}
//--------------------------------------------------------------------------------
bool stringList::getItem(int Index, char* Buffer, int Length){
if(Index<0 || Index>=maxItems)
return false;
strncpy(Buffer,element(Index),Length);
return true;
}
//--------------------------------------------------------------------------------
int stringList::getIndex(char* Item, int From){
if(From<0)
From = 0;
if(strlen(Item)>=itemsMaxLength || From<0 || From>=maxItems)
return -1;
for(int n=From;n<nItems;n++)
if(!strcmp(element(n),Item))
return n;
return -1;
}
.ino (simple test)
#include <stringList.h>
stringList myList;
char myListArray[10][15] = {{"FirstItem"},{"SecondItem"},{"ThirdItem"}};
void setup() {
Serial.begin(115200);
Serial.println(" -- BEGIN -- ");
myList.setup(makeList(myListArray),10,15);
myList.append("FourthItem");
myList.modify(3,"AnotherItem");
myList.remove(2);
Serial.println(myList.getItem(2));
}
void loop() {
/* NOTHING */
}
Is there a way to do this?