libopenraw  0.3.7
file.cpp
1 /* -*- mode:c++; tab-width:4; c-basic-offset:4; indent-tabs-mode:t; -*- */
2 /*
3  * libopenraw - file.cpp
4  *
5  * Copyright (C) 2006-2018 Hubert Figuière
6  *
7  * This library is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation, either version 3 of
10  * the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library. If not, see
19  * <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <fcntl.h>
23 #include <string>
24 
25 #include "libopenraw/consts.h"
26 #include "libopenraw/io.h"
27 
28 #include "trace.hpp"
29 #include "io/stream.hpp"
30 #include "file.hpp"
31 
32 namespace OpenRaw {
33  namespace IO {
34 
35  File::File(const char *filename)
36  : OpenRaw::IO::Stream(filename),
37  m_methods(::get_default_io_methods()),
38  m_ioRef(NULL)
39  {
40  }
41 
42  File::~File()
43  {
44  if (m_ioRef) {
45  close();
46  }
47  }
48 
50  {
51  if (m_ioRef) {
52  LOGDBG1("Already open\n");
53  return OR_ERROR_ALREADY_OPEN;
54  }
55  m_ioRef = ::raw_open(m_methods, get_path().c_str(), O_RDONLY);
56  if (m_ioRef == NULL) {
57  return OR_ERROR_CANT_OPEN;
58  }
59  return OR_ERROR_NONE;
60  }
61 
63  {
64  int result = ::raw_close(m_ioRef);
65  m_ioRef = NULL;
66  return result;
67  }
68 
69  int File::seek(off_t offset, int whence)
70  {
71  return ::raw_seek(m_ioRef, offset, whence);
72  }
73 
74  int File::read(void *buf, size_t count)
75  {
76  return ::raw_read(m_ioRef, buf, count);
77  }
78 
80  {
81  return ::raw_filesize(m_ioRef);
82  }
83 
84  }
85 }
virtual int read(void *buf, size_t count) override
read in the file. Semantics are similar to POSIX read()
Definition: file.cpp:74
virtual Error open() override
Open the file.
Definition: file.cpp:49
File(const char *filename)
Definition: file.cpp:35
virtual int close() override
close the file
Definition: file.cpp:62
virtual int seek(off_t offset, int whence) override
seek in the file. Semantics are similar to POSIX lseek()
Definition: file.cpp:69
virtual off_t filesize() override
Return the filesize.
Definition: file.cpp:79
base virtual class for IO
Definition: stream.hpp:44
const std::string & get_path() const
Get the uri path of the file.
Definition: stream.hpp:82
IOFileRef raw_open(struct io_methods *methods, const char *path, int mode)
Raw open function.
Definition: io.c:49
int raw_close(IOFileRef f)
Definition: io.c:64
struct io_methods * get_default_io_methods(void)
Get the default IO methods.
Definition: io.c:39
int raw_seek(IOFileRef f, off_t offset, int whence)
Definition: io.c:81
int raw_read(IOFileRef f, void *buf, size_t count)
Definition: io.c:95
or_error
Error codes returned by libopenraw.
Definition: consts.h:42
@ OR_ERROR_ALREADY_OPEN
Definition: consts.h:53
@ OR_ERROR_CANT_OPEN
Definition: consts.h:46
@ OR_ERROR_NONE
Definition: consts.h:43
Global namespace for libopenraw.
Definition: arwfile.cpp:29