MPD: fs/io/FileReader.hxx Source File

MPD 0.20.18

FileReader.hxx
Go to the documentation of this file.
1 /*
2 * Copyright 2003-2017 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef MPD_FILE_READER_HXX
21 #define MPD_FILE_READER_HXX
22
23 #include "check.h"
24 #include "Reader.hxx"
25 #include "fs/AllocatedPath.hxx"
26 #include "Compiler.h"
27
28 #ifndef _WIN32
29 #include "system/FileDescriptor.hxx"
30 #endif
31
32 #ifdef _WIN32
33 #include <windows.h>
34 #endif
35
36 class Path;
37 class FileInfo;
38
39 class FileReader final : public Reader {
40 AllocatedPath path;
41
42 #ifdef _WIN32
43 HANDLE handle;
44 #else
45 FileDescriptor fd;
46 #endif
47
48 public:
49 explicit FileReader(Path _path);
50
51 #ifdef _WIN32
52 FileReader(FileReader &&other)
53 :path(std::move(other.path)),
54 handle(other.handle) {
55 other.handle = INVALID_HANDLE_VALUE;
56 }
57 #else
58 FileReader(FileReader &&other)
59 :path(std::move(other.path)),
60 fd(other.fd) {
61 other.fd.SetUndefined();
62 }
63 #endif
64
65 ~FileReader() {
66 if (IsDefined())
67 Close();
68 }
69
70
71 protected:
72 bool IsDefined() const {
73 #ifdef _WIN32
74 return handle != INVALID_HANDLE_VALUE;
75 #else
76 return fd.IsDefined();
77 #endif
78 }
79
80 public:
81 #ifndef _WIN32
82 FileDescriptor GetFD() const {
83 return fd;
84 }
85 #endif
86
87 void Close();
88
89 FileInfo GetFileInfo() const;
90
91 gcc_pure
92 uint64_t GetSize() const noexcept {
93 #ifdef _WIN32
94 LARGE_INTEGER size;
95 return GetFileSizeEx(handle, &size)
96 ? size.QuadPart
97 : 0;
98 #else
99 return fd.GetSize();
100 #endif
101 }
102
103 gcc_pure
104 uint64_t GetPosition() const noexcept {
105 #ifdef _WIN32
106 LARGE_INTEGER zero;
107 zero.QuadPart = 0;
108 LARGE_INTEGER position;
109 return SetFilePointerEx(handle, zero, &position, FILE_CURRENT)
110 ? position.QuadPart
111 : 0;
112 #else
113 return fd.Tell();
114 #endif
115 }
116
117 void Rewind() {
118 Seek(0);
119 }
120
121 void Seek(off_t offset);
122 void Skip(off_t offset);
123
124 /* virtual methods from class Reader */
125 size_t Read(void *data, size_t size) override;
126 };
127
128 #endif
Reader
An interface that can read bytes from a stream until the stream ends.
Definition: Reader.hxx:35
FileDescriptor::Tell
gcc_pure off_t Tell() const noexcept
Definition: FileDescriptor.hxx:154
check.h
FileReader::Rewind
void Rewind()
Definition: FileReader.hxx:117
FileDescriptor::GetSize
gcc_pure off_t GetSize() const noexcept
Returns the size of the file in bytes, or -1 on error.
FileReader::Skip
void Skip(off_t offset)
FileReader::GetFD
FileDescriptor GetFD() const
Definition: FileReader.hxx:82
AllocatedPath
A path name in the native file system character set.
Definition: AllocatedPath.hxx:38
Reader.hxx
FileReader::IsDefined
bool IsDefined() const
Definition: FileReader.hxx:72
FileReader::GetSize
gcc_pure uint64_t GetSize() const noexcept
Definition: FileReader.hxx:92
FileReader::FileReader
FileReader(Path _path)
FileDescriptor::fd
int fd
Definition: FileDescriptor.hxx:51
FileReader::GetFileInfo
FileInfo GetFileInfo() const
Path
A path name in the native file system character set.
Definition: Path.hxx:39
FileReader::Read
size_t Read(void *data, size_t size) override
Read data from the stream.
Compiler.h
FileReader::~FileReader
~FileReader()
Definition: FileReader.hxx:65
AllocatedPath.hxx
FileDescriptor::IsDefined
constexpr bool IsDefined() const
Definition: FileDescriptor.hxx:61
FileReader::GetPosition
gcc_pure uint64_t GetPosition() const noexcept
Definition: FileReader.hxx:104
FileReader::Seek
void Seek(off_t offset)
FileReader
Definition: FileReader.hxx:39
FileReader::Close
void Close()
FileReader::FileReader
FileReader(FileReader &&other)
Definition: FileReader.hxx:58
FileDescriptor
An OO wrapper for a UNIX file descriptor.
Definition: FileDescriptor.hxx:49
gcc_pure
#define gcc_pure
Definition: Compiler.h:116
FileDescriptor.hxx
FileInfo
Definition: FileInfo.hxx:52