sectionBuffer.cpp 3.74 KB
/* 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, &sectionsBufferEnd, getSectionSize(section), 0);
    bufferSectionsCount++;
}

void SectionBuffer::removeSection(unsigned char *section) {
    // copy section header
    copyData(section, &sectionsBufferBegin, SECTION_HEADER_SIZE, 1);
    // copy section body
    copyData(section + SECTION_HEADER_SIZE, &sectionsBufferBegin, 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");
        }
}*/