MASA-Core
TeeCellsReader.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 "TeeCellsReader.hpp"
00023 
00024 #include <cstdio>
00025 #include <cstdlib>
00026 
00027 #define DEBUG (0)
00028 
00029 TeeCellsReader::TeeCellsReader(CellsReader* reader, string filename) {
00030         this->reader = reader;
00031         this->filename = filename;
00032 
00033         this->fileWriter = new FileCellsWriter(filename);
00034         this->fileReader = NULL;
00035 }
00036 
00037 TeeCellsReader::~TeeCellsReader() {
00038         close();
00039 }
00040 
00041 void TeeCellsReader::close() {
00042         if (reader != NULL) {
00043                 reader->close();
00044                 reader = NULL;
00045         }
00046         if (fileWriter != NULL) {
00047                 fileWriter->close();
00048                 delete fileWriter;
00049                 fileWriter = NULL;
00050         }
00051         if (fileReader != NULL) {
00052                 fileReader->close();
00053                 delete fileReader;
00054                 fileReader = NULL;
00055         }
00056 }
00057 
00058 int TeeCellsReader::getType() {
00059         return INIT_WITH_CUSTOM_DATA;
00060 }
00061 
00062 int TeeCellsReader::read(cell_t* buf, int len) {
00063         if (reader != NULL) {
00064                 int ret = reader->read(buf, len);
00065                 fileWriter->write(buf, ret);
00066                 return ret;
00067         } else if (fileReader != NULL){
00068                 return fileReader->read(buf, len);
00069         } else {
00070                 return -1;
00071         }
00072 }
00073 
00074 void TeeCellsReader::seek(int position) {
00075         if (DEBUG) printf("TeeCellsReader::seek(%d)\n", position);
00076         if (reader != NULL) {
00077                 reader->close();
00078                 reader = NULL;
00079         }
00080         if (fileWriter != NULL) {
00081                 fileWriter->close();
00082                 fileWriter = NULL;
00083             fileReader = new FileCellsReader(filename);
00084         } else if (fileReader == NULL) {
00085                 fprintf(stderr, "TeeCellsReader: Cannot seek.\n");
00086                 exit(1);
00087         }
00088         this->fileReader->seek(position);
00089         if (DEBUG) printf("TeeCellsReader::seek() DONE\n");
00090 }
00091 
00092 int TeeCellsReader::getOffset() {
00093         return this->fileReader->getOffset();
00094 }