MPD  0.20.15
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
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
59  :path(std::move(other.path)),
60  fd(other.fd) {
61  other.fd.SetUndefined();
62  }
63 #endif
64 
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
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
An interface that can read bytes from a stream until the stream ends.
Definition: Reader.hxx:35
gcc_pure off_t Tell() const noexcept
void Rewind()
Definition: FileReader.hxx:117
gcc_pure off_t GetSize() const noexcept
Returns the size of the file in bytes, or -1 on error.
void Skip(off_t offset)
FileDescriptor GetFD() const
Definition: FileReader.hxx:82
A path name in the native file system character set.
bool IsDefined() const
Definition: FileReader.hxx:72
gcc_pure uint64_t GetSize() const noexcept
Definition: FileReader.hxx:92
FileReader(Path _path)
FileInfo GetFileInfo() const
A path name in the native file system character set.
Definition: Path.hxx:39
size_t Read(void *data, size_t size) override
Read data from the stream.
constexpr bool IsDefined() const
gcc_pure uint64_t GetPosition() const noexcept
Definition: FileReader.hxx:104
void Seek(off_t offset)
void Close()
FileReader(FileReader &&other)
Definition: FileReader.hxx:58
An OO wrapper for a UNIX file descriptor.
#define gcc_pure
Definition: Compiler.h:116