rawcontainer.cpp

00001 /*
00002  * libopenraw - rawcontainer.cpp
00003  *
00004  * Copyright (C) 2006 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
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of 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, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 
00055         RawContainer::readInt16(IO::Stream *f, int16_t & v)
00056         {
00057             if (m_endian == ENDIAN_NULL) {
00058 
00059                 Trace(ERROR) << "null endian\n";
00060 
00061                 return false;
00062             }
00063             unsigned char buf[2];
00064             int s = f->read(buf, 2);
00065             if (s != 2) {
00066                 return false;
00067             }
00068             std::cerr.setf(std::ios_base::hex, std::ios_base::basefield);
00069             Trace(DEBUG1) << "read16 " << (int)buf[0] << " " << (int)buf [1] 
00070                                 << "\n";
00071             if (m_endian == ENDIAN_LITTLE) {
00072                 v = EL16(buf);
00073             }
00074             else {
00075                 v = BE16(buf);
00076             }
00077             std::cerr.setf((std::ios_base::fmtflags)0, std::ios_base::basefield);
00078             Trace(DEBUG1) << "value = " << v << "\n";
00079             return true;
00080         }
00081 
00082 
00083         bool 
00084         RawContainer::readInt32(IO::Stream *f, int32_t & v)
00085         {
00086             if (m_endian == ENDIAN_NULL) {
00087 
00088                 Trace(ERROR) << "null endian\n";
00089 
00090                 return false;
00091             }
00092             unsigned char buf[4];
00093             int s = f->read(buf, 4);
00094             if (s != 4) {
00095                 Trace(ERROR) << "read " << s << " bytes\n";
00096                 return false;
00097             }
00098 
00099             std::cerr.setf(std::ios_base::hex, std::ios_base::basefield);
00100             Trace(DEBUG1) << "read32 " << (int)buf[0] << " " << (int)buf [1] 
00101                                 << " " << (int)buf [2] << " " << (int)buf[3] 
00102                                 << "\n";
00103 
00104             if (m_endian == ENDIAN_LITTLE) {
00105                 v = EL32(buf);
00106             }
00107             else {
00108                 v = BE32(buf);
00109             }
00110 
00111             std::cerr.setf((std::ios_base::fmtflags)0, std::ios_base::basefield);
00112             Trace(DEBUG1) << "value = " << v << "\n";
00113 
00114             return true;
00115         }
00116 
00117 
00118         bool 
00119         RawContainer::readUInt16(IO::Stream *f, uint16_t & v)
00120         {
00121             if (m_endian == ENDIAN_NULL) {
00122 
00123                 Trace(ERROR) << "null endian\n";
00124 
00125                 return false;
00126             }
00127             unsigned char buf[2];
00128             int s = f->read(buf, 2);
00129             if (s != 2) {
00130                 return false;
00131             }
00132             std::cerr.setf(std::ios_base::hex, std::ios_base::basefield);
00133             Trace(DEBUG1) << "readu16 " << (int)buf[0] << " " << (int)buf [1] 
00134                                 << "\n";
00135             if (m_endian == ENDIAN_LITTLE) {
00136                 v = EL16(buf);
00137             }
00138             else {
00139                 v = BE16(buf);
00140             }
00141             std::cerr.setf((std::ios_base::fmtflags)0, std::ios_base::basefield);
00142             Trace(DEBUG1) << "value = " << v << "\n";
00143             return true;
00144         }
00145 
00146 
00147         bool 
00148         RawContainer::readUInt32(IO::Stream *f, uint32_t & v)
00149         {
00150             if (m_endian == ENDIAN_NULL) {
00151 
00152                 Trace(ERROR) << "null endian\n";
00153 
00154                 return false;
00155             }
00156             unsigned char buf[4];
00157             int s = f->read(buf, 4);
00158             if (s != 4) {
00159                 return false;
00160             }
00161 
00162             std::cerr.setf(std::ios_base::hex, std::ios_base::basefield);
00163             Trace(DEBUG1) << "readu32 " << (int)buf[0] << " " << (int)buf [1] 
00164                                 << " " << (int)buf [2] << " " << (int)buf[3] 
00165                                 << "\n";
00166 
00167             if (m_endian == ENDIAN_LITTLE) {
00168                 v = EL32(buf);
00169             }
00170             else {
00171                 v = BE32(buf);
00172             }
00173 
00174             std::cerr.setf((std::ios_base::fmtflags)0, std::ios_base::basefield);
00175             Trace(DEBUG1) << "value = " << v << "\n";
00176 
00177             return true;
00178         }
00179 
00180 
00181         size_t 
00182         RawContainer::fetchData(void *buf, const off_t offset,
00183                                                         const size_t buf_size)
00184         {
00185             size_t s;
00186             m_file->seek(offset, SEEK_SET);
00187             s = m_file->read(buf, buf_size);
00188             return s;
00189         }
00190 
00191 
00192     }
00193 }

Generated on Sat Jan 27 11:36:13 2007 for libopenraw by  doxygen 1.4.7