Snorktracker
 All Data Structures Files Functions Variables Macros Pages
StringList.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2018 SFini
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
25 #define MAX_LOG_INFOS_SIZE 1500
26 
27 
35 {
36 public:
37  String infos;
38  int infosCount;
40 
41 public:
42  StringList();
43 
44  bool isEmpty();
45  int count();
46  int rolledOut();
47 
48  void removeAll();
49 
50  String getAt(int idx);
51  void addTail(String newInfo);
52 
53  String removeHead();
54  String removeTail();
55 };
56 
57 /* ******************************************** */
58 
59 StringList::StringList()
60  : infosCount(0)
61  , infosRolledOut(0)
62 {
63 }
64 
67 {
68  return infosCount == 0;
69 }
70 
73 {
74  return infosCount;
75 }
76 
79 {
80  return infosRolledOut;
81 }
82 
85 {
86  infos = "";
87  infosCount = 0;
88  infosRolledOut = 0;
89 }
90 
92 String StringList::getAt(int idx)
93 {
94  int currIdx = 0;
95  int lastPos = 0;
96 
97  for(int i = 0; i < infos.length(); i++) {
98  if (infos[i] == '\1') {
99  if (currIdx == idx) {
100  return infos.substring(lastPos, i);
101  }
102  lastPos = i + 1;
103  currIdx++;
104  }
105  }
106  return "";
107 }
108 
112 void StringList::addTail(String newInfo)
113 {
114  while (infos.length() + newInfo.length() + 1 > MAX_LOG_INFOS_SIZE) {
115  removeHead();
116  }
117  infos += newInfo;
118  infos += '\1';
119  infosCount++;
120 }
121 
124 {
125  String ret;
126  int idx = infos.indexOf('\1');
127 
128  if (idx != -1) {
129  ret = infos.substring(0, idx);
130  infos = infos.substring(idx + 1);
131  infosCount--;
132  infosRolledOut++;
133  }
134  return ret;
135 }
136 
139 {
140  String ret;
141 
142  for (int i = infos.length() - 1; i >= 0; i--) {
143  if (infos[i] == '\1') {
144  ret = infos.substring(i + 1);
145  infos = infos.substring(0, i);
146  infosCount--;
147  break;
148  }
149  }
150  return ret;
151 }
int rolledOut()
Definition: StringList.h:78
bool isEmpty()
Definition: StringList.h:66
int infosRolledOut
Number of items rolled out.
Definition: StringList.h:39
String removeHead()
Definition: StringList.h:123
void removeAll()
Definition: StringList.h:84
int count()
Definition: StringList.h:72
#define MAX_LOG_INFOS_SIZE
Maximum bytes of the complete list items + separators.
Definition: StringList.h:25
String getAt(int idx)
Definition: StringList.h:92
String removeTail()
Definition: StringList.h:138
void addTail(String newInfo)
Definition: StringList.h:112
int infosCount
Number of items in the list.
Definition: StringList.h:38
String infos
All the items in one string.
Definition: StringList.h:37