MASA-Core
BufferedCellsReader.cpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2010-2015   Edans Sandes
00004  *
00005  * This file is part of MASA-Core.
00006  * 
00007  * MASA-Core is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * MASA-Core is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with MASA-Core.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  ******************************************************************************/
00021 
00022 #include "BufferedCellsReader.hpp"
00023 
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 
00027 #define DEBUG (0)
00028 
00029 BufferedCellsReader::BufferedCellsReader(CellsReader* reader, int bufferLimit) {
00030     if (reader == NULL){
00031         printf("BufferedCellsReader::ERROR; null reader\n");
00032         exit(-1);
00033     }
00034         this->reader = reader;
00035         this->offset = 0;
00036 
00037         initBuffer(bufferLimit);
00038 }
00039 
00040 BufferedCellsReader::~BufferedCellsReader() {
00041         close();
00042 }
00043 
00044 void BufferedCellsReader::close() {
00045         if (DEBUG) fprintf(stderr, "BufferedCellsReader::close().\n");
00046         if (reader != NULL) {
00047                 reader->close();
00048         }
00049         destroyBuffer();
00050         if (reader != NULL) {
00051                 reader = NULL;
00052         }
00053         if (DEBUG) fprintf(stderr, "BufferedCellsReader::close() DONE.\n");
00054 }
00055 
00056 int BufferedCellsReader::getType() {
00057         return reader->getType();
00058 }
00059 
00060 int BufferedCellsReader::read(cell_t* buf, int len) {
00061         int ret = readBuffer(buf, len);
00062         offset += ret;
00063         return ret;
00064 }
00065 
00066 void BufferedCellsReader::seek(int position) {
00067         fprintf(stderr, "BufferedCellsReader: Cannot seek.\n");
00068         exit(1);
00069 }
00070 
00071 int BufferedCellsReader::getOffset() {
00072         return offset;
00073 }
00074 
00075 void BufferedCellsReader::bufferLoop() {
00076     while (!isBufferDestroyed()) {
00077         cell_t cell;
00078         int len = reader->read(&cell, 1);
00079         if (len <= 0) break;
00080         len = writeBuffer(&cell, 1);
00081         if (len <= 0) break;
00082     }
00083         if (DEBUG) printf("BufferedCellsReader::bufferLoop() - DONE\n");
00084 }
00085 
00086