-
Notifications
You must be signed in to change notification settings - Fork 16
/
pagelist.cpp
481 lines (427 loc) · 15 KB
/
pagelist.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/** PageList
*/
#include "pagelist.h"
using namespace ttx;
PageList::PageList(Configure *configure, vbit::Debug *debug) :
_configure(configure),
_debug(debug),
_iterMag(0),
_iterSubpage(nullptr)
{
for (int i=0;i<8;i++)
{
_mag[i]=nullptr;
}
if (_configure==nullptr)
{
_debug->Log(vbit::Debug::LogLevels::logERROR,"NULL configuration object");
return;
}
LoadPageList(_configure->GetPageDirectory());
}
PageList::~PageList()
{
}
int PageList::LoadPageList(std::string filepath)
{
// Create PacketMags before loading
for (int i=0;i<8;i++)
{
_mag[i]=new vbit::PacketMag(i, &_pageList[i], _configure, _debug, 9); // this creates the eight PacketMags that Service will use. Priority will be set in Service later
}
// Load files
if (ReadDirectory(filepath))
return errno;
PopulatePageTypeLists(); // add pages to the appropriate lists for their type
return 0;
}
int PageList::ReadDirectory(std::string filepath)
{
DIR *dp;
TTXPageStream* q;
struct dirent *dirp;
struct stat attrib;
// Open the directory
if ((dp = opendir(filepath.c_str())) == NULL)
{
_debug->Log(vbit::Debug::LogLevels::logERROR,"Error(" + std::to_string(errno) + ") opening " + filepath);
return errno;
}
// Load the filenames into a list
while ((dirp = readdir(dp)) != NULL)
{
std::string name;
name=filepath;
name+="/";
name+=dirp->d_name;
if (stat(name.c_str(), &attrib) == -1) // get the attributes of the file
continue; // skip file on failure
if (attrib.st_mode & S_IFDIR)
{
// directory entry is another directory
if (dirp->d_name[0] != '.') // ignore anything beginning with .
{
_debug->Log(vbit::Debug::LogLevels::logINFO,"[PageList::LoadPageList] recursing into " + name);
if (ReadDirectory(name)) // recurse into directory
{
_debug->Log(vbit::Debug::LogLevels::logERROR,"Error(" + std::to_string(errno) + ") recursing into " + filepath);
}
}
continue;
}
//p=new TTXPageStream(filepath+"/"+dirp->d_name);
if (std::string(dirp->d_name).find(".tti") != std::string::npos) // Is the file type .tti or ttix?
{
q=new TTXPageStream(filepath+"/"+dirp->d_name);
// If the page loaded, then push it into the appropriate magazine
if (q->Loaded())
{
q->RenumberSubpages();
CheckForPacket29OrCustomHeader(q);
}
int mag=(q->GetPageNumber() >> 16) & 0x7;
_pageList[mag].push_back(*q); // This copies. But we can't copy a mutex
}
}
closedir(dp);
for (int mag=0;mag<8;mag++)
{
_debug->SetMagazineSize(mag, _pageList[mag].size());
}
return 0;
}
void PageList::AddPage(TTXPageStream* page)
{
int mag=(page->GetPageNumber() >> 16) & 0x7;
_pageList[mag].push_back(*page);
_debug->SetMagazineSize(mag, _pageList[mag].size());
}
void PageList::CheckForPacket29OrCustomHeader(TTXPageStream* page)
{
if (page->IsCarousel()) // page mFF should never be a carousel and this code leads to a crash if it is so bail out now
return;
int mag=(page->GetPageNumber() >> 16) & 0x7;
if (((page->GetPageNumber() >> 8) & 0xFF) == 0xFF) // Only read from page mFF
{
/* attempt to load custom header from the page */
if ((!_mag[mag]->GetCustomHeaderFlag()) || page->GetCustomHeaderFlag()) // Only allow one file to set header per magazine
{
if (page->GetTxRow(0)){
_mag[mag]->SetCustomHeader(page->GetTxRow(0)->GetLine().substr(8,32)); // set custom headers
if (!page->GetCustomHeaderFlag())
_debug->Log(vbit::Debug::LogLevels::logINFO,"[PageList::CheckForPacket29OrCustomHeader] Added custom header for magazine " + std::to_string((mag == 0)?8:mag));
page->SetCustomHeaderFlag(true); // mark the page
}
else if (page->GetCustomHeaderFlag()) // page previously had custom header
{
_mag[mag]->DeleteCustomHeader();
page->SetCustomHeaderFlag(false);
}
}
/* attempt to load packet M/29 data from the page */
if ((!_mag[mag]->GetPacket29Flag()) || page->GetPacket29Flag()) // Only allow one file to set packet29 per magazine
{
bool Packet29Flag = false;
if (page->GetPacket29Flag())
_mag[mag]->DeletePacket29(); // clear previous packet 29
TTXLine* tempLine = page->GetTxRow(29);
while (tempLine != nullptr)
{
switch (tempLine->GetCharAt(0))
{
case '@':
{
Packet29Flag = true;
_mag[mag]->SetPacket29(0, new TTXLine(tempLine->GetLine(), true));
break;
}
case 'A':
{
Packet29Flag = true;
_mag[mag]->SetPacket29(1, new TTXLine(tempLine->GetLine(), true));
break;
}
case 'D':
{
Packet29Flag = true;
_mag[mag]->SetPacket29(2, new TTXLine(tempLine->GetLine(), true));
break;
}
}
tempLine = tempLine->GetNextLine();
// loop until every row 29 is copied
}
if (Packet29Flag && !page->GetPacket29Flag())
_debug->Log(vbit::Debug::LogLevels::logINFO,"[PageList::CheckForPacket29OrCustomHeader] Added packet 29 for magazine "+ std::to_string((mag == 0)?8:mag));
page->SetPacket29Flag(Packet29Flag); // mark the page
}
}
}
// Find a page by filename
TTXPageStream* PageList::Locate(std::string filename)
{
// This is called from the FileMonitor thread
for (int mag=0;mag<8;mag++)
{
//for (auto p : _pageList[mag])
for (std::list<TTXPageStream>::iterator p=_pageList[mag].begin();p!=_pageList[mag].end();++p)
{
TTXPageStream* ptr;
ptr=&(*p);
//_debug->Log(vbit::Debug::LogLevels::logDEBUG,"[PageList::Locate]scan:" + ptr->GetSourcePage());
if (filename==ptr->GetSourcePage())
return ptr;
}
}
return NULL; // @todo placeholder What should we do here?
}
int PageList::Match(char* pageNumber)
{
int matchCount=0;
_debug->Log(vbit::Debug::LogLevels::logDEBUG,"[PageList::Match] Selecting " + std::string(pageNumber));
int begin=0;
int end=7;
for (int mag=begin;mag<end+1;mag++)
{
// For each page
for (std::list<TTXPageStream>::iterator p=_pageList[mag].begin();p!=_pageList[mag].end();++p)
{
TTXPageStream* ptr;
char s[6];
char* ps=s;
bool match=true;
for (ptr=&(*p);ptr!=nullptr;ptr=(TTXPageStream*)ptr->Getm_SubPage()) // For all the subpages in a carousel
{
// Convert the page number into a string so we can compare it
std::stringstream ss;
ss << std::hex << std::uppercase << std::setw(5) << ptr->GetPageNumber();
strcpy(ps,ss.str().c_str());
//_debug->Log(vbit::Debug::LogLevels::logDEBUG,"[PageList::Match] matching " + std::string(ps));
for (int i=0;i<5;i++)
{
if (pageNumber[i]!='*') // wildcard
{
if (pageNumber[i]!=ps[i])
{
match=false;
}
}
}
if (match)
{
matchCount++;
}
ptr->SetSelected(match);
}
}
}
// Set up the iterator for commands that use pages selected by the Page Identity
_iterMag=0;
_iter=_pageList[_iterMag].begin();
return matchCount; // final count
}
TTXPageStream* PageList::NextPage()
{
_debug->Log(vbit::Debug::LogLevels::logDEBUG,"[PageList::NextPage] looking for a selected page, mag=" + std::to_string((int)_iterMag));
bool more=true;
if (_iterSubpage!=nullptr)
{
_debug->Log(vbit::Debug::LogLevels::logDEBUG,"A");
_iterSubpage=(TTXPageStream*) _iterSubpage->Getm_SubPage();
_debug->Log(vbit::Debug::LogLevels::logDEBUG,"B");
}
if (_iterSubpage!=nullptr)
{
_debug->Log(vbit::Debug::LogLevels::logDEBUG,"C");
return _iterSubpage;
}
_debug->Log(vbit::Debug::LogLevels::logDEBUG,"[PageList::NextPage] _iterSubpage is null, so checking next page");
if (_iter!=_pageList[_iterMag].end())
{
++_iter; // Next page
}
if (_iter==_pageList[_iterMag].end()) // end of mag?
{
if (_iterMag<7)
{
_iterMag++; // next mag
_iter=_pageList[_iterMag].begin();
}
else
{
more=false; // End of last mag
}
}
if (more)
{
_iterSubpage=&(*_iter);
return _iterSubpage;
}
_iterSubpage=nullptr;
return nullptr; // Returned after the last page is iterated
}
TTXPageStream* PageList::PrevPage()
{
//_debug->Log(vbit::Debug::LogLevels::logDEBUG,"[PageList::NextPage] looking for a selected page, mag=" + std::to_string((int)_iterMag));
bool more=true;
if (_iter!=_pageList[_iterMag].begin())
{
--_iter; // Previous page
}
if (_iter==_pageList[_iterMag].begin()) // beginning of mag?
{
if (_iterMag>0)
{
_iterMag--; // previous mag
_iter=_pageList[_iterMag].end();
}
else
{
more=false;
}
}
if (more)
{
return &(*_iter);
}
return nullptr; // Returned after the first page is iterated
}
TTXPageStream* PageList::FirstPage()
{
// Reset the iterators
_iterMag=0;
_iter=_pageList[_iterMag].begin();
_iterSubpage=&(*_iter);
// Iterate through all the pages
_debug->Log(vbit::Debug::LogLevels::logDEBUG,"[PageList::FirstPage] about to find if there is a selected page");
for (TTXPageStream* p=_iterSubpage; p!=nullptr; p=NextPage())
{
if (p->Selected()) // If the page is selected, return a pointer to it
{
_debug->Log(vbit::Debug::LogLevels::logDEBUG,"[PageList::FirstPage] selected page found");
return p;
}
}
_debug->Log(vbit::Debug::LogLevels::logDEBUG,"[PageList::FirstPage] no selected page");
return nullptr; // No selected page
}
TTXPageStream* PageList::LastPage()
{
// Reset the iterators
_iterMag=7;
_iter=_pageList[_iterMag].end();
// Iterate through all the pages
for (TTXPageStream* p=&(*_iter); p!=nullptr; p=PrevPage())
{
if (p->Selected()) // If the page is selected, return a pointer to it
{
return p;
}
}
return nullptr; // No selected page
}
TTXPageStream* PageList::NextSelectedPage()
{
TTXPageStream* page;
for(;;)
{
page=NextPage();
if (page==nullptr || page->Selected())
{
return page;
}
}
}
// Detect pages that have been deleted from the drive
// Do this by first clearing all the "exists" flags
// As we scan through the list, set the "exists" flag as we match up the drive to the loaded page
void PageList::ClearFlags()
{
for (int mag=0;mag<8;mag++)
{
for (std::list<TTXPageStream>::iterator p=_pageList[mag].begin();p!=_pageList[mag].end();++p)
{
TTXPageStream* ptr;
ptr=&(*p);
// Don't unmark a file that was MARKED. Once condemned it won't be pardoned
if (ptr->GetStatusFlag()==TTXPageStream::FOUND || ptr->GetStatusFlag()==TTXPageStream::NEW)
{
ptr->SetState(TTXPageStream::NOTFOUND);
}
}
}
}
void PageList::DeleteOldPages()
{
// This is called from the FileMonitor thread
for (int mag=0;mag<8;mag++)
{
for (std::list<TTXPageStream>::iterator p=_pageList[mag].begin();p!=_pageList[mag].end();++p)
{
TTXPageStream* ptr;
ptr=&(*p);
if (ptr->GetStatusFlag()==TTXPageStream::GONE)
{
if (ptr->GetPacket29Flag())
{
// Packet 29 was loaded from this page, so remove it.
_mag[mag]->DeletePacket29();
_debug->Log(vbit::Debug::LogLevels::logINFO,"[PageList::DeleteOldPages] Removing packet 29 from magazine " + std::to_string((mag == 0)?8:mag));
}
if (ptr->GetCustomHeaderFlag())
{
// Custom header was loaded from this page, so remove it.
_mag[mag]->DeleteCustomHeader();
}
// page has been removed from lists
_pageList[mag].remove(*p--);
_debug->SetMagazineSize(mag, _pageList[mag].size());
if (_iterMag == mag)
{
// _iter is iterating _pageList[mag]
_iter=_pageList[_iterMag].begin(); // reset it?
}
}
else if (ptr->GetStatusFlag()==TTXPageStream::NOTFOUND)
{
// Pages marked here get deleted in the Service thread
ptr->SetState(TTXPageStream::MARKED);
}
}
}
}
void PageList::PopulatePageTypeLists()
{
for (int mag=0;mag<8;mag++)
{
for (std::list<TTXPageStream>::iterator p=_pageList[mag].begin();p!=_pageList[mag].end();++p)
{
TTXPageStream* ptr;
ptr=&(*p);
if (ptr->Special())
{
// Page is 'special'
ptr->SetSpecialFlag(true);
ptr->SetNormalFlag(false);
ptr->SetCarouselFlag(false);
_mag[mag]->GetSpecialPages()->addPage(ptr);
}
else
{
// Page is 'normal'
ptr->SetSpecialFlag(false);
ptr->SetNormalFlag(true);
_mag[mag]->GetNormalPages()->addPage(ptr);
if (ptr->IsCarousel())
{
// Page is also 'carousel'
ptr->SetCarouselFlag(true);
_mag[mag]->GetCarousel()->addPage(ptr);
ptr->StepNextSubpage();
}
else
ptr->SetCarouselFlag(false);
}
}
}
}