Merge pull request #90 from gizmo98/patch-2
[libretro-ppsspp.git] / Core / FileLoaders / LocalFileLoader.cpp
blobbcf3c699014bf16650e103344d5c9d7fbd98d6b0
1 // Copyright (c) 2012- PPSSPP Project.
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 2.0 for more details.
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
18 #include <cstdio>
19 #include "file/file_util.h"
20 #include "Common/FileUtil.h"
21 #include "Core/FileLoaders/LocalFileLoader.h"
23 LocalFileLoader::LocalFileLoader(const std::string &filename)
24 : fd_(0), f_(nullptr), filesize_(0), filename_(filename) {
25 f_ = File::OpenCFile(filename, "rb");
26 if (!f_) {
27 return;
30 #ifdef ANDROID
31 // Android NDK does not support 64-bit file I/O using C streams
32 // so we fall back onto syscalls
33 fd_ = fileno(f_);
35 off64_t off = lseek64(fd_, 0, SEEK_END);
36 filesize_ = off;
37 lseek64(fd_, 0, SEEK_SET);
38 #else
39 fseek(f_, 0, SEEK_END);
40 filesize_ = ftello(f_);
41 fseek(f_, 0, SEEK_SET);
42 #endif
45 LocalFileLoader::~LocalFileLoader() {
46 if (f_) {
47 fclose(f_);
51 bool LocalFileLoader::Exists() {
52 // If we couldn't open it for reading, we say it does not exist.
53 if (f_ || IsDirectory()) {
54 FileInfo info;
55 return getFileInfo(filename_.c_str(), &info);
57 return false;
60 bool LocalFileLoader::IsDirectory() {
61 FileInfo info;
62 if (getFileInfo(filename_.c_str(), &info)) {
63 return info.isDirectory;
65 return false;
68 s64 LocalFileLoader::FileSize() {
69 return filesize_;
72 std::string LocalFileLoader::Path() const {
73 return filename_;
76 void LocalFileLoader::Seek(s64 absolutePos) {
77 #ifdef ANDROID
78 lseek64(fd_, absolutePos, SEEK_SET);
79 #else
80 fseeko(f_, absolutePos, SEEK_SET);
81 #endif
84 size_t LocalFileLoader::Read(size_t bytes, size_t count, void *data) {
85 #ifdef ANDROID
86 return read(fd_, data, bytes * count) / bytes;
87 #else
88 return fread(data, bytes, count, f_);
89 #endif
92 size_t LocalFileLoader::ReadAt(s64 absolutePos, size_t bytes, size_t count, void *data) {
93 Seek(absolutePos);
94 return Read(bytes, count, data);