Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

FileInputStream.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * ファイル入力ストリーム実装
00022  * @author Junpee
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 } // End of namespace Lamp
00109 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:30 2005 for Lamp by doxygen 1.3.2