libopenraw
streamclone.cpp
1 /*
2  * libopenraw - streamclone.cpp
3  *
4  * Copyright (C) 2006 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 
23 
24 #include "streamclone.h"
25 
26 
27 namespace OpenRaw {
28  namespace IO {
29 
30  StreamClone::StreamClone(Stream *clone, off_t offset)
31  : Stream(clone->get_path().c_str()),
32  m_cloned(clone), m_offset(offset)
33  {
34 
35  }
36 
37  StreamClone::~StreamClone()
38  {
39  }
40 
41 
42  Stream::Error StreamClone::open()
43  {
44  if (m_cloned == NULL) {
45  set_error(OR_ERROR_CLOSED_STREAM);
46  return OR_ERROR_CLOSED_STREAM;
47  }
48  m_cloned->seek(m_offset, SEEK_SET);
49  //no-op
50  //FIXME determine what is the policy for opening clone
51  //streams
52  return OR_ERROR_NONE;
53  }
54 
56  {
57  m_cloned = NULL;
58  return 0;
59  }
60 
61 
62  int StreamClone::seek(off_t offset, int whence)
63  {
64  if (m_cloned == NULL) {
65  set_error(OR_ERROR_CLOSED_STREAM);
66  return -1;
67  }
68  if (whence == SEEK_SET) {
69  offset += m_offset;
70  }
71  return m_cloned->seek(offset, whence);
72  }
73 
74 
75  int StreamClone::read(void *buf, size_t count)
76  {
77  if (m_cloned == NULL) {
78  set_error(OR_ERROR_CLOSED_STREAM);
79  return -1;
80  }
81  return m_cloned->read(buf, count);
82  }
83 
84 
85  off_t StreamClone::filesize()
86  {
87  if (m_cloned == NULL) {
88  set_error(OR_ERROR_CLOSED_STREAM);
89  return -1;
90  }
91  return m_cloned->filesize();
92  }
93 
94  }
95 }
virtual int seek(off_t offset, int whence)
Definition: streamclone.cpp:62
virtual int seek(off_t offset, int whence)=0
virtual int read(void *buf, size_t count)=0
virtual int read(void *buf, size_t count)
Definition: streamclone.cpp:75
virtual Error open()
Definition: streamclone.cpp:42