GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/fs/virtfs/rwops.cpp Lines: 13 18 72.2 %
Date: 2017-11-29 Branches: 4 16 25.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2013-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
/*
22
 * This code provides a glue layer between PhysicsFS and Simple Directmedia
23
 *  Layer's (SDL) RWops i/o abstraction.
24
 *
25
 * License: this code is public domain. I make no warranty that it is useful,
26
 *  correct, harmless, or environmentally safe.
27
 *
28
 * This particular file may be used however you like, including copying it
29
 *  verbatim into a closed-source project, exploiting it commercially, and
30
 *  removing any trace of my name from the source (although I hope you won't
31
 *  do that). I welcome enhancements and corrections to this file, but I do
32
 *  not require you to send me patches if you make changes. This code has
33
 *  NO WARRANTY.
34
 *
35
 * Unless otherwise stated, the rest of PhysicsFS falls under the zlib license.
36
 *  Please see LICENSE.txt in the root of the source tree.
37
 *
38
 * SDL falls under the LGPL license. You can get SDL at http://www.libsdl.org/
39
 *
40
 *  This file was written by Ryan C. Gordon. ([email protected]).
41
 *
42
 *  Copyright (C) 2012-2017  The ManaPlus Developers
43
 */
44
45
#include "fs/virtfs/rwops.h"
46
47
#include "logger.h"
48
49
#include "fs/virtfs/file.h"
50
#include "fs/virtfs/fs.h"
51
#include "fs/virtfs/fsfuncs.h"
52
53
#include "utils/fuzzer.h"
54
55
PRAGMA48(GCC diagnostic push)
56
PRAGMA48(GCC diagnostic ignored "-Wshadow")
57
#include <SDL_rwops.h>
58
PRAGMA48(GCC diagnostic pop)
59
60
#include "debug.h"
61
62
namespace VirtFs
63
{
64
65
2984
SDL_RWops *create_rwops(File *const file)
66
{
67
2984
    SDL_RWops *retval = nullptr;
68
69
2984
    if (file == nullptr)
70
    {
71
        logger->assertLog("VirtFs::rwops_seek: create rwops error.");
72
    }
73
    else
74
    {
75
2984
        retval = SDL_AllocRW();
76
2984
        if (retval != nullptr)
77
        {
78
#ifdef USE_SDL2
79
            retval->size  = file->funcs->rwops_size;
80
#endif  // USE_SDL2
81
82
2984
            retval->seek  = file->funcs->rwops_seek;
83
2984
            retval->read  = file->funcs->rwops_read;
84
2984
            retval->write = file->funcs->rwops_write;
85
2984
            retval->close = file->funcs->rwops_close;
86
2984
            retval->hidden.unknown.data1 = file;
87
        } /* if */
88
    } /* else */
89
90
2984
    return retval;
91
} /* VirtFs::create_rwops */
92
93
#ifdef __APPLE__
94
static bool checkFilePath(const std::string &restrict fname)
95
{
96
    if (fname.empty())
97
        return false;
98
    if (!exists(fname) || isDirectory(fname))
99
        return false;
100
    return true;
101
}
102
#endif  // __APPLE__
103
104
2984
SDL_RWops *rwopsOpenRead(const std::string &restrict fname)
105
{
106
    BLOCK_START("RWopsopenRead")
107
#ifdef __APPLE__
108
    if (!checkFilePath(fname))
109
        return nullptr;
110
#endif  // __APPLE__
111
#ifdef USE_FUZZER
112
    if (Fuzzer::conditionTerminate(fname))
113
        return nullptr;
114
#endif  // USE_FUZZER
115
#ifdef USE_PROFILER
116
117
    SDL_RWops *const ret = create_rwops(openRead(fname));
118
119
    BLOCK_END("RWopsopenRead")
120
    return ret;
121
#else  // USE_PROFILER
122
123

5968
    return create_rwops(openRead(fname));
124
#endif  // USE_PROFILER
125
} /* RWopsopenRead */
126
127
SDL_RWops *rwopsOpenWrite(const std::string &restrict fname)
128
{
129
#ifdef __APPLE__
130
    if (!checkFilePath(fname))
131
        return nullptr;
132
#endif  // __APPLE__
133
134
    return create_rwops(openWrite(fname));
135
} /* RWopsopenWrite */
136
137
SDL_RWops *rwopsOpenAppend(const std::string &restrict fname)
138
{
139
#ifdef __APPLE__
140
    if (!checkFilePath(fname))
141
        return nullptr;
142
#endif  // __APPLE__
143
144
    return create_rwops(openAppend(fname));
145
} /* RWopsopenAppend */
146
147
}  // namespace VirtFs
148
149
/* end of virtfsrwops.c ... */