FileReader.hxx 2.99 KB
Newer Older
1
/*
2
 * Copyright 2014-2019 Max Kellermann <max.kellermann@gmail.com>
3
 *
4 5 6
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
7
 *
8 9
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
10
 *
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 29
 */

30 31
#ifndef FILE_READER_HXX
#define FILE_READER_HXX
32 33 34

#include "Reader.hxx"
#include "fs/AllocatedPath.hxx"
35
#include "util/Compiler.h"
36

37
#ifdef _WIN32
38
#include <windows.h>
39
#else
40
#include "io/UniqueFileDescriptor.hxx"
41 42 43
#endif

class Path;
44
class FileInfo;
45 46 47 48

class FileReader final : public Reader {
	AllocatedPath path;

49
#ifdef _WIN32
50 51
	HANDLE handle;
#else
52
	UniqueFileDescriptor fd;
53 54 55
#endif

public:
56
	explicit FileReader(Path _path);
57

58
#ifdef _WIN32
59
	FileReader(FileReader &&other) noexcept
60
		:path(std::move(other.path)),
61
		 handle(std::exchange(other.handle, INVALID_HANDLE_VALUE)) {}
62

63
	~FileReader() noexcept {
64 65 66
		if (IsDefined())
			Close();
	}
67 68 69 70 71
#else
	FileReader(FileReader &&other) noexcept
		:path(std::move(other.path)),
		 fd(std::move(other.fd)) {}
#endif
72 73


74
protected:
75
	bool IsDefined() const noexcept {
76
#ifdef _WIN32
77 78
		return handle != INVALID_HANDLE_VALUE;
#else
79
		return fd.IsDefined();
80 81 82
#endif
	}

83
public:
84
#ifndef _WIN32
85
	FileDescriptor GetFD() const noexcept {
86 87 88 89
		return fd;
	}
#endif

90
	void Close() noexcept;
91

92
	FileInfo GetFileInfo() const;
93

94
	gcc_pure
95
	uint64_t GetSize() const noexcept {
96
#ifdef _WIN32
97 98 99 100 101 102 103 104 105 106
		LARGE_INTEGER size;
		return GetFileSizeEx(handle, &size)
			? size.QuadPart
			: 0;
#else
		return fd.GetSize();
#endif
	}

	gcc_pure
107
	uint64_t GetPosition() const noexcept {
108
#ifdef _WIN32
109 110 111 112 113 114 115 116 117 118 119
		LARGE_INTEGER zero;
		zero.QuadPart = 0;
		LARGE_INTEGER position;
		return SetFilePointerEx(handle, zero, &position, FILE_CURRENT)
			? position.QuadPart
			: 0;
#else
		return fd.Tell();
#endif
	}

120 121 122 123
	void Rewind() {
		Seek(0);
	}

124
	void Seek(off_t offset);
125
	void Skip(off_t offset);
126

127
	/* virtual methods from class Reader */
128
	size_t Read(void *data, size_t size) override;
129 130 131
};

#endif