libopenraw  0.3.7
streamclone.cpp
1 /*
2  * libopenraw - streamclone.cpp
3  *
4  * Copyright (C) 2006-2016 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 <stdio.h>
23 #include <memory>
24 #include <string>
25 
26 #include "io/stream.hpp"
27 #include "libopenraw/consts.h"
28 
29 #include "streamclone.hpp"
30 
31 
32 namespace OpenRaw {
33 namespace IO {
34 
36  off_t offset)
37  : Stream(clone->get_path().c_str()),
38  m_cloned(clone), m_offset(offset)
39 {
40 
41 }
42 
43 StreamClone::~StreamClone()
44 {
45 }
46 
47 
49 {
50  if (m_cloned == NULL) {
53  }
54  m_cloned->seek(m_offset, SEEK_SET);
55  //no-op
56  //FIXME determine what is the policy for opening clone
57  //streams
58  return OR_ERROR_NONE;
59 }
60 
62 {
63  m_cloned = NULL;
64  return 0;
65 }
66 
67 
68 int StreamClone::seek(off_t offset, int whence)
69 {
70  if (m_cloned == NULL) {
72  return -1;
73  }
74  if (whence == SEEK_SET) {
75  offset += m_offset;
76  }
77  int new_pos = m_cloned->seek(offset, whence);
78  new_pos -= m_offset;
79  return new_pos;
80 }
81 
82 
83 int StreamClone::read(void *buf, size_t count)
84 {
85  if (m_cloned == NULL) {
87  return -1;
88  }
89  return m_cloned->read(buf, count);
90 }
91 
92 
94 {
95  if (m_cloned == NULL) {
97  return -1;
98  }
99  return m_cloned->filesize() - m_offset;
100 }
101 
102 }
103 }
104 /*
105  Local Variables:
106  mode:c++
107  c-file-style:"stroustrup"
108  c-file-offsets:((innamespace . 0))
109  tab-width:2
110  c-basic-offset:2
111  indent-tabs-mode:nil
112  fill-column:80
113  End:
114 */
virtual Error open() override
open the file
Definition: streamclone.cpp:48
virtual int read(void *buf, size_t count) override
read in the file. Semantics are similar to POSIX read()
Definition: streamclone.cpp:83
virtual int close() override
close the file
Definition: streamclone.cpp:61
virtual int seek(off_t offset, int whence) override
seek in the file. Semantics are similar to POSIX lseek()
Definition: streamclone.cpp:68
StreamClone(const Stream::Ptr &clone, off_t offset)
Definition: streamclone.cpp:35
virtual off_t filesize() override
Return the filesize.
Definition: streamclone.cpp:93
base virtual class for IO
Definition: stream.hpp:44
std::shared_ptr< Stream > Ptr
Definition: stream.hpp:47
void set_error(Error error)
Set the error. This is calld by implementations.
Definition: stream.hpp:91
or_error
Error codes returned by libopenraw.
Definition: consts.h:42
@ OR_ERROR_CLOSED_STREAM
Definition: consts.h:47
@ OR_ERROR_NONE
Definition: consts.h:43
Global namespace for libopenraw.
Definition: arwfile.cpp:29