TuttleOFX  1
InStream.cpp
Go to the documentation of this file.
00001 // -*- mode: C++; tab-width: 4 -*-
00002 // vi: ts=4
00003 
00004 /*
00005  * Copyright (c) 2009, Patrick A. Palmer.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without 
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  *   - Redistributions of source code must retain the above copyright notice,
00012  *     this list of conditions and the following disclaimer.
00013  *
00014  *   - Redistributions in binary form must reproduce the above copyright
00015  *     notice, this list of conditions and the following disclaimer in the
00016  *     documentation and/or other materials provided with the distribution.
00017  *
00018  *   - Neither the name of Patrick A. Palmer nor the names of its
00019  *     contributors may be used to endorse or promote products derived from
00020  *     this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
00023  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00024  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
00025  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
00026  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
00027  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
00028  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00029  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00030  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
00031  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
00032  * POSSIBILITY OF SUCH DAMAGE.
00033  */
00034  
00035 
00036 #include <cstdio>
00037 
00038 
00039 #include "DPXStream.h"
00040 
00041 
00042 InStream::InStream() : fp(0)
00043 {
00044 }
00045 
00046 
00047 InStream::~InStream()
00048 {
00049 }
00050 
00051 
00052 bool InStream::Open(const char *f)
00053 {
00054         if (this->fp)
00055                 this->Close();
00056         if ((this->fp = ::fopen(f, "rb")) == 0)
00057                 return false;
00058                 
00059         return true;
00060 }
00061 
00062 
00063 void InStream::Close()
00064 {
00065         if (this->fp)
00066         {
00067                 ::fclose(this->fp);
00068                 this->fp = 0;
00069         }
00070 }
00071 
00072 
00073 void InStream::Rewind()
00074 {
00075         if (this->fp)
00076                 ::rewind(fp);
00077 }
00078 
00079 
00080 bool InStream::Seek(long offset, Origin origin)
00081 {
00082         int o = 0;
00083         switch (origin)
00084         {
00085         case kCurrent:
00086                 o = SEEK_CUR;
00087                 break;
00088         case kEnd:
00089                 o = SEEK_END;
00090                 break;
00091         case kStart:
00092                 o = SEEK_SET;
00093                 break;
00094         }
00095         
00096         if (this->fp == 0)
00097                 return -1;
00098         return (::fseek(this->fp, offset, o) == 0);
00099 }
00100 
00101 
00102 
00103 size_t InStream::Read(void *buf, const size_t size)
00104 {
00105         if (this->fp == 0)
00106                 return 0;
00107         return ::fread(buf, 1, size, this->fp);
00108 }
00109 
00110 
00111 size_t InStream::ReadDirect(void *buf, const size_t size)
00112 {
00113         return this->Read(buf, size);
00114 }
00115 
00116 
00117 bool InStream::EndOfFile() const 
00118 {
00119         if (this->fp == 0)
00120                 return true;
00121         return ::feof(this->fp);
00122 }
00123 
00124 
00125 
00126 
00127