rawcontainer.cpp

00001 /*
00002  * libopenraw - rawcontainer.cpp
00003  *
00004  * Copyright (C) 2006-2007 Hubert Figuiere
00005  *
00006  * This library is free software: you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public License
00008  * as published by the Free Software Foundation, either version 3 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library.  If not, see
00018  * <http://www.gnu.org/licenses/>.
00019  */
00020 
00021 #include <iostream>
00022 
00023 #include <libopenraw/types.h>
00024 
00025 #include "debug.h"
00026 #include "endianutils.h"
00027 #include "io/file.h"
00028 #include "rawcontainer.h"
00029 
00030 
00031 
00032 using namespace Debug;
00033 
00034 namespace OpenRaw {
00035     namespace Internals {
00036     
00037     
00038         RawContainer::RawContainer(IO::Stream *_file, off_t offset)
00039             : m_file(_file),
00040                 m_offset(offset),
00041                 m_endian(ENDIAN_NULL)
00042         {
00043             m_file->open();
00044             m_file->seek(offset, SEEK_SET);
00045         }
00046 
00047     
00048         RawContainer::~RawContainer()
00049         {
00050             m_file->close();
00051         }
00052 
00053 
00054         bool RawContainer::readInt8(IO::Stream *f, int8_t & v)
00055         {
00056             unsigned char buf;
00057             int s = f->read(&buf, 1);
00058             if (s != 1) {
00059                 return false;
00060             }
00061             v = buf;
00062             return true;
00063         }
00064 
00065         bool RawContainer::readUInt8(IO::Stream *f, uint8_t & v)
00066         {
00067             unsigned char buf;
00068             int s = f->read(&buf, 1);
00069             if (s != 1) {
00070                 return false;
00071             }
00072             v = buf;
00073             return true;
00074         }
00075 
00076         bool 
00077         RawContainer::readInt16(IO::Stream *f, int16_t & v)
00078         {
00079             if (m_endian == ENDIAN_NULL) {
00080 
00081                 Trace(ERROR) << "null endian\n";
00082 
00083                 return false;
00084             }
00085             unsigned char buf[2];
00086             int s = f->read(buf, 2);
00087             if (s != 2) {
00088                 return false;
00089             }
00090             if (m_endian == ENDIAN_LITTLE) {
00091                 v = EL16(buf);
00092             }
00093             else {
00094                 v = BE16(buf);
00095             }
00096             return true;
00097         }
00098 
00099 
00100         bool 
00101         RawContainer::readInt32(IO::Stream *f, int32_t & v)
00102         {
00103             if (m_endian == ENDIAN_NULL) {
00104 
00105                 Trace(ERROR) << "null endian\n";
00106 
00107                 return false;
00108             }
00109             unsigned char buf[4];
00110             int s = f->read(buf, 4);
00111             if (s != 4) {
00112                 Trace(ERROR) << "read " << s << " bytes\n";
00113                 return false;
00114             }
00115 
00116             if (m_endian == ENDIAN_LITTLE) {
00117                 v = EL32(buf);
00118             }
00119             else {
00120                 v = BE32(buf);
00121             }
00122 
00123             return true;
00124         }
00125 
00126 
00127         bool 
00128         RawContainer::readUInt16(IO::Stream *f, uint16_t & v)
00129         {
00130             if (m_endian == ENDIAN_NULL) {
00131 
00132                 Trace(ERROR) << "null endian\n";
00133 
00134                 return false;
00135             }
00136             unsigned char buf[2];
00137             int s = f->read(buf, 2);
00138             if (s != 2) {
00139                 return false;
00140             }
00141             if (m_endian == ENDIAN_LITTLE) {
00142                 v = EL16(buf);
00143             }
00144             else {
00145                 v = BE16(buf);
00146             }
00147             return true;
00148         }
00149 
00150 
00151         bool 
00152         RawContainer::readUInt32(IO::Stream *f, uint32_t & v)
00153         {
00154             if (m_endian == ENDIAN_NULL) {
00155 
00156                 Trace(ERROR) << "null endian\n";
00157 
00158                 return false;
00159             }
00160             unsigned char buf[4];
00161             int s = f->read(buf, 4);
00162             if (s != 4) {
00163                 return false;
00164             }
00165 
00166             if (m_endian == ENDIAN_LITTLE) {
00167                 v = EL32(buf);
00168             }
00169             else {
00170                 v = BE32(buf);
00171             }
00172 
00173             return true;
00174         }
00175 
00176 
00177         size_t 
00178         RawContainer::fetchData(void *buf, const off_t offset,
00179                                                         const size_t buf_size)
00180         {
00181             size_t s;
00182             m_file->seek(offset, SEEK_SET);
00183             s = m_file->read(buf, buf_size);
00184             return s;
00185         }
00186 
00187 
00188     }
00189 }

Generated on Sat Aug 15 17:27:04 2009 for libopenraw by  doxygen 1.5.9