00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <stdio.h>
00026 #include "LampBasic.h"
00027 #include "Core/InputOutput/FileInputStream.h"
00028
00029 namespace Lamp{
00030
00031
00032
00033 FileInputStream::FileInputStream(String fileName) :
00034 fileName_(fileName), position_(0){
00035 file_ = fopen(fileName_.getBytes(), "rb");
00036 Assert(file_ != NULL);
00037
00038 int result;
00039 result = fseek(file_, 0, SEEK_END);
00040 Assert(result == 0);
00041 size_ = ftell(file_);
00042 Assert(size_ != -1);
00043 fseek(file_, 0, SEEK_SET);
00044 Assert(result == 0);
00045 }
00046
00047
00048 FileInputStream::~FileInputStream(){
00049 int closeResult = fclose(file_);
00050 Assert(closeResult == 0);
00051 }
00052
00053
00054 FileInputStream* FileInputStream::cloneFileInputStream(){
00055 FileInputStream* fileInputStream = new FileInputStream(fileName_);
00056 fileInputStream->size_ = size_;
00057 fileInputStream->setPosition(getPosition());
00058 return fileInputStream;
00059 }
00060
00061
00062 bool FileInputStream::isEnd(){
00063 return (position_ == size_);
00064 }
00065
00066
00067 void FileInputStream::readBytes(void* data, int size){
00068 int result = (int)fread(data, 1, size, file_);
00069 Assert(result == size);
00070 position_ += size;
00071 }
00072
00073
00074 int FileInputStream::getSize(){
00075 return size_;
00076 }
00077
00078
00079 void FileInputStream::skip(int size){
00080 position_ += size;
00081 int result = fseek(file_, position_, SEEK_SET);
00082 Assert(result == 0);
00083 }
00084
00085
00086 int FileInputStream::align(int alignSize){
00087 int size = position_ % alignSize;
00088 if(size == 0){ return size; }
00089 size = alignSize - size;
00090 position_ += size;
00091 int result = fseek(file_, position_, SEEK_SET);
00092 Assert(result == 0);
00093 return size;
00094 }
00095
00096
00097 int FileInputStream::getPosition(){
00098 return position_;
00099 }
00100
00101
00102 void FileInputStream::setPosition(int position){
00103 int result = fseek(file_, position, SEEK_SET);
00104 Assert(result == 0);
00105 position_ = position;
00106 }
00107
00108 }
00109