forked from GLDsuh-a/qt-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmmgmt.cpp
263 lines (232 loc) · 5.65 KB
/
mmgmt.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// File mmgmt.cpp
// Memory management for 2Pnotes data
#include <stdio.h>
#include "mmgmt.h"
mmgmt::mmgmt()
{
//separator for strings in the list
sep = '\01';
//Create an empty Stringlist
slist = new QStringList();
}
mmgmt::~mmgmt()
{
delete slist;
}
bool mmgmt::listIsEmpty(void)
{
//return true is list is empty
return slist->isEmpty();
}
int mmgmt::countOfItems(void)
{
//return the number of items in the list
return slist->size();
}
void mmgmt::delAllContent(void)
{
//delete the content of the strlist, not the strlist itself
slist->clear();
}
bool mmgmt::deleteRecord(int index)
{
//return true, if record successful deleted
if(!slist->isEmpty())
{
if(index>=0 && index<slist->size())
{
slist->removeAt(index);
{
return true;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
QString mmgmt::getBookmark(int index)
{
//return the bookmark or a null string
if(!slist->isEmpty())
{
if(index>=0 && index<slist->size())
{
QStringList list;
list = slist->at(index).split(sep);
return list.at(0);
}
else
{
return QString();
}
}
else
{
return QString();
}
}
QString mmgmt::getPassword(int index)
{
//return the password or a null string
if(!slist->isEmpty())
{
if(index>=0 && index<slist->size())
{
QStringList list;
list = slist->at(index).split(sep);
return list.at(1);
}
else
{
return QString();
}
}
else
{
return QString();
}
}
QString mmgmt::getRemarks(int index)
{
//return the remark or a null string
if(!slist->isEmpty())
{
if(index>=0 && index<slist->size())
{
QStringList list;
list = slist->at(index).split(sep);
return list.at(2);
}
else
{
return QString();
}
}
else
{
return QString();
}
}
QString mmgmt::getListItem(int index)
{
//return the full string at position item or a null string.
//The string includes the parts bookmark, password, remarks and the separator char.
//This function is usefull to copy the data into another Stringlist.
if(!slist->isEmpty())
{
if(index >= 0 && index < slist->size())
{
return slist->at(index);
}
else
{
return QString();
}
}
else
{
return QString();
}
}
void mmgmt::appendListItem(QString item)
{
//append the QString item at the end of the list.
//The string must include the parts bookmark, password, remarks and the separator char.
//This function is usefull to copy the data into another Stringlist.
slist->append(item);
}
void mmgmt::insertString(int index, int pos, QString c)
{
//insert QString c into QStringlist(index)at position pos.
//pos=0 - bookmark, pos=1 - password, pos=2 - remarks
if(!slist->isEmpty())
{
if(index < 0 || index > slist->size())
{
qWarning("Module mmgmt.cpp, func. insertString: index out of range.");
}
else
{
if(pos < 0 || pos > 2)
{
qWarning("Module mmgmt.cpp, func. insertString: pos out of range.");
}
else
{
QString i = slist->at(index);
QStringList l = i.split(sep);
l.removeAt(pos);
l.insert(pos, c);
i.clear();
i = l.join(sep);
slist->removeAt(index);
slist->insert(index, i);
}
}
}
else
{
qWarning("Module mmgmt.cpp, func. insertString: QStringList slist is empty");
}
}
int mmgmt::insertRecord(QString* b, QString* p, QString* r)
{
/*insert a record into the list l (in alphbetical order),
using the string b. Insert at first position if the list is empty
if the content of *b exists into the list, a number is added to *b.
return the index of the inserted string, */
QStringList list;
QString element, cp, ex;
int i = 0, c, z = 2;
cp = *b;
if(!slist->isEmpty())
{
do
{
element = slist->at(i);
list = element.split(sep);
c = QString::localeAwareCompare(list.at(0), cp);
if(c>0) //exit loop, insert record
{
break;
}
if(c == 0) //strings identical, append an extension to the new string
{
cp = *b;
ex.clear();
ex.setNum(z);
ex.prepend(" - ");
switch(z)
{
case 2:
ex += "nd";
break;
case 3:
ex += "rd";
break;
default:
ex += "th";
}
cp += ex;
z++;
}
i++;
}while(i<slist->size());
}
//insert, prepend or append the new record
element.clear();
element+=*b+ex+sep+*p+sep+*r;
slist->insert(i, element);
return i;
}
int mmgmt::insertEditedRecord(int oldindex, QString* b, QString* p, QString* r)
{
slist->removeAt(oldindex); // del old record
return insertRecord(b, p, r); // insert edited string like a new record
}