• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SFML / SFML / 9373018345

04 Jun 2024 07:24PM CUT coverage: 55.821% (+0.07%) from 55.748%
9373018345

Pull #2964

github

web-flow
Merge b54abf46c into 1e1c13b51
Pull Request #2964: Use `std::optional` rather than sentinel values

6125 of 12050 branches covered (50.83%)

Branch coverage included in aggregate %.

77 of 80 new or added lines in 12 files covered. (96.25%)

1 existing line in 1 file now uncovered.

11577 of 19662 relevant lines covered (58.88%)

50900.03 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.1
/src/SFML/System/FileInputStream.cpp
1
////////////////////////////////////////////////////////////
2
//
3
// SFML - Simple and Fast Multimedia Library
4
// Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.org)
5
//
6
// This software is provided 'as-is', without any express or implied warranty.
7
// In no event will the authors be held liable for any damages arising from the use of this software.
8
//
9
// Permission is granted to anyone to use this software for any purpose,
10
// including commercial applications, and to alter it and redistribute it freely,
11
// subject to the following restrictions:
12
//
13
// 1. The origin of this software must not be misrepresented;
14
//    you must not claim that you wrote the original software.
15
//    If you use this software in a product, an acknowledgment
16
//    in the product documentation would be appreciated but is not required.
17
//
18
// 2. Altered source versions must be plainly marked as such,
19
//    and must not be misrepresented as being the original software.
20
//
21
// 3. This notice may not be removed or altered from any source distribution.
22
//
23
////////////////////////////////////////////////////////////
24

25
////////////////////////////////////////////////////////////
26
// Headers
27
////////////////////////////////////////////////////////////
28
#include <SFML/System/FileInputStream.hpp>
29
#ifdef SFML_SYSTEM_ANDROID
30
#include <SFML/System/Android/Activity.hpp>
31
#include <SFML/System/Android/ResourceStream.hpp>
32
#endif
33
#include <memory>
34

35
#include <cstddef>
36

37
namespace sf
38
{
39
////////////////////////////////////////////////////////////
40
void FileInputStream::FileCloser::operator()(std::FILE* file)
1,528✔
41
{
16✔
42
    std::fclose(file);
1,544✔
43
}
1,544✔
44

45

46
////////////////////////////////////////////////////////////
47
FileInputStream::FileInputStream() = default;
2,384✔
48

49

50
////////////////////////////////////////////////////////////
51
FileInputStream::~FileInputStream() = default;
4,150✔
52

53

54
////////////////////////////////////////////////////////////
55
FileInputStream::FileInputStream(FileInputStream&&) noexcept = default;
38✔
56

57

58
////////////////////////////////////////////////////////////
59
FileInputStream& FileInputStream::operator=(FileInputStream&&) noexcept = default;
32✔
60

61

62
////////////////////////////////////////////////////////////
63
bool FileInputStream::open(const std::filesystem::path& filename)
1,588✔
64
{
16✔
65
#ifdef SFML_SYSTEM_ANDROID
66
    if (priv::getActivityStatesPtr() != nullptr)
67
    {
68
        m_androidFile = std::make_unique<priv::ResourceStream>(filename);
69
        return m_androidFile->tell() != -1;
70
    }
71
#endif
72
#ifdef SFML_SYSTEM_WINDOWS
73
    m_file.reset(_wfopen(filename.c_str(), L"rb"));
230✔
74
#else
75
    m_file.reset(std::fopen(filename.c_str(), "rb"));
1,374✔
76
#endif
77
    return m_file != nullptr;
1,604✔
78
}
12✔
79

80

81
////////////////////////////////////////////////////////////
82
std::optional<std::size_t> FileInputStream::read(void* data, std::size_t size)
26,923✔
83
{
16✔
84
#ifdef SFML_SYSTEM_ANDROID
85
    if (priv::getActivityStatesPtr() != nullptr)
86
    {
87
        if (!m_androidFile)
88
            return std::nullopt;
89
        return m_androidFile->read(data, size);
90
    }
91
#endif
92
    if (!m_file)
26,939✔
93
        return std::nullopt;
32✔
94
    return std::fread(data, 1, size, m_file.get());
26,923✔
95
}
13,245✔
96

97

98
////////////////////////////////////////////////////////////
99
std::optional<std::size_t> FileInputStream::seek(std::size_t position)
13,431✔
100
{
16✔
101
#ifdef SFML_SYSTEM_ANDROID
102
    if (priv::getActivityStatesPtr() != nullptr)
103
    {
104
        if (!m_androidFile)
105
            return std::nullopt;
106
        return m_androidFile->seek(position);
107
    }
108
#endif
109
    if (!m_file)
13,447✔
110
        return std::nullopt;
120✔
111
    if (std::fseek(m_file.get(), static_cast<long>(position), SEEK_SET))
13,343✔
NEW
112
        return std::nullopt;
×
113

114
    return tell();
13,343✔
115
}
7,197✔
116

117

118
////////////////////////////////////////////////////////////
119
std::optional<std::size_t> FileInputStream::tell()
22,303✔
120
{
16✔
121
#ifdef SFML_SYSTEM_ANDROID
122
    if (priv::getActivityStatesPtr() != nullptr)
123
    {
124
        if (!m_androidFile)
125
            return std::nullopt;
126
        return m_androidFile->tell();
127
    }
128
#endif
129
    if (!m_file)
22,319✔
130
        return std::nullopt;
32✔
131
    const auto position = std::ftell(m_file.get());
22,303✔
132
    return position < 0 ? std::nullopt : std::optional<std::size_t>(position);
22,303✔
133
}
11,265✔
134

135

136
////////////////////////////////////////////////////////////
137
std::optional<std::size_t> FileInputStream::getSize()
2,560✔
138
{
16✔
139
#ifdef SFML_SYSTEM_ANDROID
140
    if (priv::getActivityStatesPtr() != nullptr)
141
    {
142
        if (!m_androidFile)
143
            return std::nullopt;
144
        return m_androidFile->getSize();
145
    }
146
#endif
147
    if (!m_file)
2,576✔
148
        return std::nullopt;
104✔
149
    const auto position = tell().value();
2,488✔
150
    std::fseek(m_file.get(), 0, SEEK_END);
2,488✔
151
    const auto size = tell();
2,488✔
152

153
    if (!seek(position).has_value())
2,488✔
NEW
154
        return std::nullopt;
×
155

156
    return size;
2,488✔
157
}
1,208✔
158

159
} // namespace sf
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc