sectionBuffer.cpp
3.74 KB
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
/* Modificado por: @Felipe Lacet
* Ultima Atualização: 08/10/2008
*
* SectionBuffer.cpp
*
*/
//#include "queue.h"
#include <string.h>
#include <stdio.h>
//#include "dprintf.h"
#include "section_hal.h"
#include "sectionBuffer.h"
SectionBuffer::SectionBuffer() {
initSectionsBuffer();
}
SectionBuffer::~SectionBuffer() {
}
void SectionBuffer::copyData(unsigned char *data, int *position, int size, int fromBuffer) {
unsigned char *source, *target;
int rightSpace;
rightSpace = SECTIONS_BUFFER_SIZE - *position;
if (fromBuffer) {
source = sectionsBufferData + *position;
target = data;
} else {
source = data;
target = sectionsBufferData + *position;
}
if (size > rightSpace) {
memcpy(target, source, rightSpace);
if (fromBuffer) {
target = data + rightSpace;
source = sectionsBufferData;
} else {
target = sectionsBufferData;
source = data + rightSpace;
}
memcpy(target, source, size - rightSpace);
} else {
memcpy(target, source, size);
}
*position = (*position + size) % SECTIONS_BUFFER_SIZE;
}
void SectionBuffer::initSectionsBuffer() {
sectionsBufferBegin = 0;
sectionsBufferEnd = 0;
bufferSectionsCount = 0;
memset(sectionsBufferData, 0, SECTIONS_BUFFER_SIZE);
}
//int getSectionsCount(demux_section_t *demux_section)
int SectionBuffer::getSectionsCount() {
return bufferSectionsCount;
}
void SectionBuffer::addSection(unsigned char *section) {
copyData(section, §ionsBufferEnd, getSectionSize(section), 0);
bufferSectionsCount++;
}
void SectionBuffer::removeSection(unsigned char *section) {
// copy section header
copyData(section, §ionsBufferBegin, SECTION_HEADER_SIZE, 1);
// copy section body
copyData(section + SECTION_HEADER_SIZE, §ionsBufferBegin, getSectionSize(section) - SECTION_HEADER_SIZE, 1);
// update sections counter
bufferSectionsCount--;
}
/*
// tests
int main() {
int i, j, k;
unsigned char section[5][8] = {
{ 99, 0, 2, 99, 99, 99, 99, 99 },
{ 88, 0, 5, 88, 88, 88, 88, 88 },
{ 77, 0, 1, 77, 77, 77, 77, 77 },
{ 66, 0, 3, 66, 66, 66, 66, 66 },
{ 55, 0, 1, 55, 55, 55, 55, 55 }
};
initSectionsBuffer();
DPRINTF("---------------\n");
for (i = 0, k = 0; i < 10; i++) {
DDPRINTF("i = %d\n", i);
if (i % 2 == 1) {
unsigned char removed[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
DDPRINTF("will REMOVE section:");
removeSection(removed);
for (j = 0; j < getSectionSize(removed); j++) {
printf("%3d", removed[j]);
}
printf("\n");
} else {
DDPRINTF("will ADD section %d with %d bytes\n", k, getSectionSize(section[k]));
addSection(section[k++]);
}
DPRINTF("data:");
for (j = 0; j < SECTIONS_BUFFER_SIZE; j++) {
printf("%3d", demux_section->sectionsBufferData[j]);
}
printf("\n");
DPRINTF(" ");
for (j = 0; j < SECTIONS_BUFFER_SIZE; j++) {
printf(demux_section->sectionsBufferBegin == j ? (demux_section->sectionsBufferEnd == j ? " be" : " b") : (demux_section->sectionsBufferEnd == j ? " e" : " "));
}
printf("\n");
DPRINTF("sections count = %d\n", demux_section->bufferSectionsCount);
DPRINTF("---------------\n");
}
}*/