DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
src/mt32/FileStream.cpp
00001 /* Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009 Dean Beeler, Jerome Fisher
00002  * Copyright (C) 2011, 2012, 2013 Dean Beeler, Jerome Fisher, Sergey V. Mikayev
00003  *
00004  *  This program is free software: you can redistribute it and/or modify
00005  *  it under the terms of the GNU Lesser General Public License as published by
00006  *  the Free Software Foundation, either version 2.1 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU Lesser General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Lesser General Public License
00015  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include "mt32emu.h"
00019 #include "FileStream.h"
00020 
00021 namespace MT32Emu {
00022 
00023 using std::ifstream;
00024 using std::ios_base;
00025 
00026 FileStream::FileStream() {
00027         ifsp = new ifstream();
00028 }
00029 
00030 FileStream::~FileStream() {
00031         if (ifsp != NULL) {
00032                 delete ifsp; // destructor closes the file itself
00033         }
00034         if (data) {
00035                 delete[] data;
00036         }
00037 }
00038 
00039 size_t FileStream::getSize() {
00040         if (fileSize != 0) {
00041                 return fileSize;
00042         }
00043         if (ifsp == NULL) {
00044                 return 0;
00045         }
00046         if (!ifsp->is_open()) {
00047                 return 0;
00048         }
00049         ifsp->seekg(0, ios_base::end);
00050         fileSize = (size_t)ifsp->tellg();
00051         return fileSize;
00052 }
00053 
00054 const unsigned char* FileStream::getData() {
00055         if (data != NULL) {
00056                 return data;
00057         }
00058         if (ifsp == NULL) {
00059                 return NULL;
00060         }
00061         if (!ifsp->is_open()) {
00062                 return NULL;
00063         }
00064         if (getSize() == 0) {
00065                 return NULL;
00066         }
00067         data = new unsigned char[fileSize];
00068         if (data == NULL) {
00069                 return NULL;
00070         }
00071         ifsp->seekg(0);
00072         ifsp->read((char *)data, fileSize);
00073         if ((size_t)ifsp->tellg() != fileSize) {
00074                 delete[] data;
00075                 data = NULL;
00076                 return NULL;
00077         }
00078         return data;
00079 }
00080 
00081 bool FileStream::open(const char *filename) {
00082         if (ifsp) {
00083                 ifsp->open(filename, ios_base::in | ios_base::binary);
00084         }
00085         return (ifsp->good());
00086 }
00087 
00088 void FileStream::close() {
00089         ifsp->close();
00090 }
00091 
00092 }