MASA-Core
FileCellsReader.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 "FileCellsReader.hpp"
00023 
00024 
00025 #include <stdlib.h>
00026 
00027 FileCellsReader::FileCellsReader(FILE* file) {
00028         this->file = file;
00029 }
00030 
00031 FileCellsReader::FileCellsReader(const string path) {
00032         FILE* file = fopen(path.c_str(), "rb");
00033         if (file == NULL) {
00034                 fprintf(stderr, "Could not open file (%s).\n", path.c_str());
00035                 exit(1);
00036         }
00037         this->file = file;
00038 }
00039 
00040 FileCellsReader::~FileCellsReader() {
00041         close();
00042 }
00043 
00044 void FileCellsReader::close() {
00045         if (file != NULL) {
00046                 fclose(file);
00047                 file = NULL;
00048         }
00049 }
00050 
00051 int FileCellsReader::getType() {
00052         return INIT_WITH_CUSTOM_DATA;
00053 }
00054 
00055 int FileCellsReader::read(cell_t* buffer, int len) {
00056         int p = 0;
00057         if (buffer == NULL) {
00058                 fseek(file, len, SEEK_CUR);
00059         } else {
00060                 while (p < len) {
00061                         int ret = fread(buffer, sizeof(cell_t), len-p, file);
00062                         if (ret <= 0) {
00063                                 if (feof(file)) {
00064                                         fprintf(stderr, "FileCellsReader::read(): End-of-file.\n");
00065                                         return p;
00066                                 } else {
00067                                         fprintf(stderr, "FileCellsReader::read() - failed to read completely [%d/%d]\n", p, len-p);
00068                                         fprintf(stderr, "Could not load busH file (%d).\n", ret);
00069                                         exit(1);
00070                                 }
00071                         }
00072                         p += ret;
00073                 }
00074         }
00075         return len;
00076 }
00077 
00078 void FileCellsReader::seek(int position) {
00079         fseek(file, position*sizeof(cell_t), SEEK_SET);
00080 }
00081 
00082 int FileCellsReader::getOffset() {
00083         return ftell(file)/sizeof(cell_t);
00084 }