/ source / libsmackerdec / src / FileStream.cpp
FileStream.cpp
  1  /*
  2   * libsmackerdec - Smacker video decoder
  3   * Copyright (C) 2011 Barry Duncan
  4   *
  5   * This library is free software; you can redistribute it and/or
  6   * modify it under the terms of the GNU Lesser General Public
  7   * License as published by the Free Software Foundation; either
  8   * version 2.1 of the License, or (at your option) any later version.
  9   *
 10   * This library is distributed in the hope that it will be useful,
 11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 13   * Lesser General Public License for more details.
 14   *
 15   * You should have received a copy of the GNU Lesser General Public
 16   * License along with this library; if not, write to the Free Software
 17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 18   */
 19  
 20  #include "FileStream.h"
 21  #include <stdlib.h>
 22  
 23  namespace SmackerCommon {
 24  
 25  bool FileStream::Open(const char *fileName)
 26  {
 27      file = kopen4loadfrommod(fileName, 0);
 28  	if (file == -1)
 29  	{
 30  		// log error
 31  		return false;
 32  	}
 33  
 34  	return true;
 35  }
 36  
 37  bool FileStream::Is_Open()
 38  {
 39  	return file != -1;
 40  }
 41  
 42  void FileStream::Close()
 43  {
 44      kclose(file);
 45      file = -1;
 46  }
 47  
 48  int32_t FileStream::ReadBytes(uint8_t *data, uint32_t nBytes)
 49  {
 50  	uint32_t nCount = (uint32_t)kread(file, data, static_cast<int32_t>(nBytes));
 51  
 52  	if (nCount != nBytes)
 53  	{
 54  		return 0;
 55  	}
 56  
 57  	return (int32_t)nCount;
 58  }
 59  
 60  uint32_t FileStream::ReadUint32LE()
 61  {
 62  	uint32_t value;
 63  	kread(file, &value, 4);
 64  	return B_LITTLE32(value);
 65  }
 66  
 67  uint32_t FileStream::ReadUint32BE()
 68  {
 69  	uint32_t value;
 70      kread(file, &value, 4);
 71  	return B_BIG32(value);
 72  }
 73  
 74  uint16_t FileStream::ReadUint16LE()
 75  {
 76  	uint16_t value;
 77      kread(file, &value, 2);
 78  	return B_LITTLE16(value);
 79  }
 80  
 81  uint16_t FileStream::ReadUint16BE()
 82  {
 83  	uint16_t value;
 84      kread(file, &value, 2);
 85  	return B_BIG16(value);
 86  }
 87  
 88  uint8_t FileStream::ReadByte()
 89  {
 90  	uint8_t value;
 91      kread(file, &value, 1);
 92  	return value;
 93  }
 94  
 95  int32_t FileStream::Seek(int32_t offset, SeekDirection direction)
 96  {
 97      int32_t nStatus = -1;
 98  	if (kSeekStart == direction) {
 99          nStatus = klseek(file, offset, SEEK_SET);
100  	}
101  	else if (kSeekCurrent == direction) {
102          nStatus = klseek(file, offset, SEEK_CUR);
103  	}
104      else if (kSeekEnd == direction) {
105          nStatus = klseek(file, offset, SEEK_END);
106      }
107  
108      return nStatus;
109  }
110  
111  int32_t FileStream::Skip(int32_t offset)
112  {
113  	return Seek(offset, kSeekCurrent);
114  }
115  
116  int32_t FileStream::GetPosition()
117  {
118      return ktell(file);
119  }
120  
121  } // close namespace SmackerCommon