MPD  0.20.6
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  gcc_pure
90  FileInfo GetFileInfo() const;
91 
92  gcc_pure
93  uint64_t GetSize() const {
94 #ifdef WIN32
95  LARGE_INTEGER size;
96  return GetFileSizeEx(handle, &size)
97  ? size.QuadPart
98  : 0;
99 #else
100  return fd.GetSize();
101 #endif
102  }
103 
104  gcc_pure
105  uint64_t GetPosition() const {
106 #ifdef WIN32
107  LARGE_INTEGER zero;
108  zero.QuadPart = 0;
109  LARGE_INTEGER position;
110  return SetFilePointerEx(handle, zero, &position, FILE_CURRENT)
111  ? position.QuadPart
112  : 0;
113 #else
114  return fd.Tell();
115 #endif
116  }
117 
118  void Rewind() {
119  Seek(0);
120  }
121 
122  void Seek(off_t offset);
123  void Skip(off_t offset);
124 
125  /* virtual methods from class Reader */
126  size_t Read(void *data, size_t size) override;
127 };
128 
129 #endif
An interface that can read bytes from a stream until the stream ends.
Definition: Reader.hxx:35
void Rewind()
Definition: FileReader.hxx:118
constexpr bool IsDefined() const
void Skip(off_t offset)
A path name in the native file system character set.
gcc_pure off_t GetSize() const
Returns the size of the file in bytes, or -1 on error.
FileReader(Path _path)
gcc_pure off_t Tell() 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.
gcc_pure FileInfo GetFileInfo() const
void Seek(off_t offset)
FileDescriptor GetFD() const
Definition: FileReader.hxx:82
void Close()
FileReader(FileReader &&other)
Definition: FileReader.hxx:58
An OO wrapper for a UNIX file descriptor.
#define gcc_pure
Definition: Compiler.h:116
gcc_pure uint64_t GetSize() const
Definition: FileReader.hxx:93
bool IsDefined() const
Definition: FileReader.hxx:72
gcc_pure uint64_t GetPosition() const
Definition: FileReader.hxx:105