memstream.cpp

00001 /*
00002  * libopenraw - memstream.cpp
00003  *
00004  * Copyright (C) 2007-2008 Hubert Figuière
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
00019  */
00020 
00021 
00022 #include <string.h>
00023 
00024 #include <libopenraw/libopenraw.h>
00025 
00026 #include "memstream.h"
00027 #include "debug.h"
00028 
00029 using namespace Debug;
00030 
00031 namespace OpenRaw {
00032     namespace IO {
00033         
00034         MemStream::MemStream(void *ptr, size_t s)
00035             : Stream(""),
00036                 m_ptr(ptr),
00037                 m_size(s),
00038                 m_current(NULL)
00039         {
00040         }
00041 
00042         or_error MemStream::open()
00043         {
00044             m_current = (unsigned char *)m_ptr;
00045             return OR_ERROR_NONE;
00046         }
00047 
00048 
00049         int MemStream::close()
00050         {
00051             m_current = NULL;
00052             return 0;
00053         }
00054 
00055         int MemStream::seek(off_t offset, int whence)
00056         {
00057             int newpos = 0;
00058 //          Trace(DEBUG1) << "MemStream::seek " << offset 
00059 //                                      << " bytes - whence = " 
00060 //                                      << whence <<  "\n";
00061             // TODO check bounds
00062             if (m_current == NULL) {
00063                 // TODO set error code
00064                 return -1;
00065             }
00066             switch(whence)
00067             {
00068             case SEEK_SET:
00069                 m_current = (unsigned char*)m_ptr + offset;
00070                 newpos = offset;
00071                 break;
00072             case SEEK_END:
00073                 m_current = (unsigned char*)m_ptr + m_size + offset;
00074                 newpos = m_size + offset;
00075                 break;
00076             case SEEK_CUR:
00077                 m_current += offset;
00078                 newpos = (m_current - (unsigned char*)m_ptr);
00079                 break;
00080             default:
00081                 return -1;
00082                 break;
00083             }
00084             return newpos;
00085         }
00086 
00087 
00088         int MemStream::read(void *buf, size_t count)
00089         {
00090             if((m_current == NULL) || (m_ptr == NULL)) {
00091                 Trace(DEBUG1) << "MemStream::failed\n";
00092                 return -1;
00093             }
00094 
00095             unsigned char * end = (unsigned char*)m_ptr + m_size;
00096             if((off_t)count > (end - m_current)) {
00097                 count = end - m_current;
00098                 // TODO set EOF
00099             }
00100             memcpy(buf, m_current, count);
00101             m_current += count;
00102             return count;
00103         }
00104 
00105 
00106         off_t MemStream::filesize()
00107         {
00108             return m_size;
00109         }
00110 
00111     }
00112 }

Generated on Tue Jun 17 11:47:19 2008 for libopenraw by  doxygen 1.5.6