libopenraw
memstream.cpp
1 /*
2  * libopenraw - memstream.cpp
3  *
4  * Copyright (C) 2007-2008 Hubert Figuière
5  *
6  * This library is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
21 
22 #include <string.h>
23 
24 #include <libopenraw/libopenraw.h>
25 
26 #include "memstream.h"
27 #include "trace.h"
28 
29 using namespace Debug;
30 
31 namespace OpenRaw {
32  namespace IO {
33 
34  MemStream::MemStream(void *ptr, size_t s)
35  : Stream(""),
36  m_ptr(ptr),
37  m_size(s),
38  m_current(NULL)
39  {
40  }
41 
42  or_error MemStream::open()
43  {
44  m_current = (unsigned char *)m_ptr;
45  return OR_ERROR_NONE;
46  }
47 
48 
49  int MemStream::close()
50  {
51  m_current = NULL;
52  return 0;
53  }
54 
55  int MemStream::seek(off_t offset, int whence)
56  {
57  int newpos = 0;
58 // Trace(DEBUG1) << "MemStream::seek " << offset
59 // << " bytes - whence = "
60 // << whence << "\n";
61  // TODO check bounds
62  if (m_current == NULL) {
63  // TODO set error code
64  return -1;
65  }
66  switch(whence)
67  {
68  case SEEK_SET:
69  m_current = (unsigned char*)m_ptr + offset;
70  newpos = offset;
71  break;
72  case SEEK_END:
73  m_current = (unsigned char*)m_ptr + m_size + offset;
74  newpos = m_size + offset;
75  break;
76  case SEEK_CUR:
77  m_current += offset;
78  newpos = (m_current - (unsigned char*)m_ptr);
79  break;
80  default:
81  return -1;
82  break;
83  }
84  return newpos;
85  }
86 
87 
88  int MemStream::read(void *buf, size_t count)
89  {
90  if((m_current == NULL) || (m_ptr == NULL)) {
91  Trace(DEBUG1) << "MemStream::failed\n";
92  return -1;
93  }
94 
95  unsigned char * end = (unsigned char*)m_ptr + m_size;
96  if((off_t)count > (end - m_current)) {
97  count = end - m_current;
98  // TODO set EOF
99  }
100  memcpy(buf, m_current, count);
101  m_current += count;
102  return count;
103  }
104 
105 
106  off_t MemStream::filesize()
107  {
108  return m_size;
109  }
110 
111  }
112 }
CIFF is the container for CRW files. It is an attempt from Canon to make this a standard. I guess it failed.
Definition: arwfile.cpp:33
Definition: trace.cpp:28